'How to Use Multiple Prisma Databases in SvelteKit

I followed this great tutorial and successfully have Prisma running in a SvelteKit app. I need to query against a second database, and I'm using local SQLite files found in my SvelteKit project.

I reviewed this GitHub issue/comment about multiple database support in Prisma. I have successfully done a prisma db pull on both my databases and have generated the the two schema files.

First DB schema:

//---------------------
// prisma/schema.prisma
//---------------------

generator client {
  provider = "prisma-client-js"
  output = "./generated/client"
}

datasource db {
  provider = "sqlite"
  url = "file: One.sqlite?schema=schema"
}

// models...

Second DB schema:

//------------------------
// prisma/schemaTwo.prisma
//------------------------

generator client {
  provider = "prisma-client-js"
  output = "./generated/clientTwo"
}

datasource db {
  provider = "sqlite"
  url = "file: Two.sqlite?schema=schemaTwo"
}

// models...

I can import and query the first schema just fine in one of my endpoints:

//----------------------
// routes/first/index.ts
//----------------------
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()

let stuff = await prisma.myTable.findMany()

But I can't figure out how to use the second database:

//-----------------------
// routes/second/index.ts
//-----------------------
import { PrismaClient } from '@prisma/clientTwo'
const prisma = new PrismaClient()

let stuff = await prisma.anotherTable.findMany()

This yields the following error:

Failed to resolve import "@prisma/clientTwo" from "src/routes/second/index.ts". Does the file exist?
1  |  import { PrismaClient } from "@prisma/clientTwo";
   |                                ^

I tried referencing the generated client file directly like this:

import { PrismaClient } from '../prisma/generated/clientTwo'

Then I get this error:

TypeError: __vite_ssr_import_0__.PrismaClient is not a constructor

Has anyone been able to get two databases working in SvelteKit? Any ideas on what I am doing wrong?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source