So, I am building a real estate application in Express for college.
I want to have a PostgreSQL database as the main database and the SQLite database for unit testing. For example, I made a service for the User model and I want to use it both for the web app and testing. I need a TypeScript ORM that supports multiple datasources (PostgreSQL and SQLite (in-memory if possible)).
services/UserService.ts:
import { compareSync, hashSync } from "bcrypt";
import logger from "../global/logger";
import logMessages from "../global/logMessages";
import { userMapper as mapper } from "../mappers";
import { User } from "../models";
import { IRegister, IUserChangePassword, IUserUpdate } from "../interfaces";
import prisma from "../prisma/main/client";
export default class UserService {
constructor(private prisma: any) {}
// Main
async create(data: IRegister) {
delete data.password2;
data.email = data.email.toLowerCase();
data.password = hashSync(data.password, 10);
// 1 Parametrized queries
const user = await prisma.user.create({ data });
// 5 Security logging
logger.info(logMessages.USER_SERV.create, { userId: user.id });
return mapper.toClass(user);
}
// ... other methods
}
I used to use Prisma, but setting it to support both PostgreSQL and SQLite was so convoluted. I had to build a schema, a config and a client for each datasource. That also implied having to duplicate the entire services just for unit testing.
Configurations `prisma/main/prisma,config.ts` and `prisma/test/prisma.config.ts`:
```TS
import "dotenv/config";
import { defineConfig, env } from "prisma/config";
export default defineConfig({
schema: "./schema.prisma",
migrations: { path: "./migrations" },
datasource: { url: env("MAIN_DATABASE_URL") }
});
// same as above
export default defineConfig({
// same as above
datasource: { url: env("TEST_DATABASE_URL") }
});
prisma/main/client.ts and prisma/test/client.ts:
import { PrismaPg } from "@prisma/adapter-pg";
import { PrismaClient } from "./generated/client";
import { MAIN_DATABASE_URL } from "../../global/env";
const connectionString = MAIN_DATABASE_URL;
const adapter = new PrismaPg({ connectionString });
const client = new PrismaClient({ adapter });
export default client;
import { PrismaBetterSqlite3 } from "@prisma/adapter-better-sqlite3";
import { PrismaClient } from "./generated/client";
import { TEST_DATABASE_URL } from "../../global/env";
const connectionString = TEST_DATABASE_URL;
const adapter = new PrismaBetterSqlite3({ url: connectionString });
const client = new PrismaClient({ adapter });
export default client;
So, I turned the service into a class that will accept any client in the constructor.
export default class UserService {
constructor(private prisma: any) {}
And I instantiated it in controllers/auth.controller.ts and tests/userService.test.ts:
// ... other imports
import { mainClient } from "../prisma";
import { UserService } from "../services";
const service = new UserService(mainClient);
import { testClient } from "../prisma";
import { UserService } from "../services";
const service = new UserService(client);
I tried Drizzle ORM with PGLite, but I did not know how to set it up for both main db and testing.
I need something simple like its in Django and Laravel. Please don't recommend raw SQL and query builders (Kysely (I hate that name) and others) at the moment.