PrismaP
Prisma8mo ago
10 replies
XenTobias

Using Prisma with Both SQLite and PostgreSQL

Hi community ✌️
is there a way to support both SQLite and PostgreSQL with Prisma at the same time?
In other words, the user would have the option to use either SQLite or PostgreSQL,
with Prisma acting as an abstraction layer.

Specifically, I want to implement the following:

const prisma = (() => {
  switch (process.env.DATABASE_PROVIDER) {
    case 'postgresql':
      return new PrismaClientPostgresql({
        adapter: new PrismaPg({
          connectionString: process.env.DATABASE_URL,
        }),
      });

    case 'sqlite':
      return new PrismaClientSqlite({
        adapter: new PrismaBetterSQLite3({
          url: process.env.DATABASE_URL,
        }),
      });

    default:
      throw new Error(
        `Unsupported DATABASE_PROVIDER: ${process.env.DATABASE_PROVIDER}`
      );
  }
})();

// Works fine
await prisma.user.create({ data: { name: 'test', email: '' } });

// Type error:
/*
This expression is not callable.
  Each member of the union type '(<T extends UserFindUniqueArgs>(args: SelectSubset<T, UserFindUniqueArgs<DefaultArgs>>) => Prisma__UserClient<GetFindResult<$UserPayload<DefaultArgs>, T, { ...; }> | null, null, DefaultArgs, { ...; }>) | (<T extends UserFindUniqueArgs>(args: SelectSubset<...>) => Prisma__UserClient<...>)' has signatures, but none of those signatures are compatible with each other.ts(2349)*/
await prisma.user.findUnique({});


However, the problem here is that prisma.user.findUnique() does not provide type safety for the arguments,
presumably because the types differ between SQLite and PostgreSQL. Is there a workaround for this?

Best regards,
Tobi
Was this page helpful?