Dan😏
Dan😏
PPrisma
Created by Christophe on 2/10/2025 in #help-and-questions
Prisma postgres and replicas
if you're using a prisma-managed database on console.prisma.io, you won't have direct access to modify postgresql settings like wal_level or create a replication user manually you know, prisma cloud manages the database for you, so low-level configuration changes aren’t exposed
19 replies
PPrisma
Created by nhan.nguyenvan on 2/10/2025 in #help-and-questions
Find the way to write unit-test by postgres in memory db
it spins up a real postgresql instance in the background, runs your tests, and automatically cleans
55 replies
PPrisma
Created by nhan.nguyenvan on 2/10/2025 in #help-and-questions
Find the way to write unit-test by postgres in memory db
pg_tmp lets you create a temporary postgresql database without needing docker
55 replies
PPrisma
Created by nhan.nguyenvan on 2/10/2025 in #help-and-questions
Find the way to write unit-test by postgres in memory db
yeah, that's right
55 replies
PPrisma
Created by nhan.nguyenvan on 2/10/2025 in #help-and-questions
Find the way to write unit-test by postgres in memory db
so, while pg-mem work today, it's smart to keep an eye on whether it meets your needs as your app grows
55 replies
PPrisma
Created by nhan.nguyenvan on 2/10/2025 in #help-and-questions
Find the way to write unit-test by postgres in memory db
anyway, if you wanna long-term reliability, you might wanna look at altervatives like testcontainers for node.js, pg_tmp, using a ram-backed postgresql instance
55 replies
PPrisma
Created by nhan.nguyenvan on 2/10/2025 in #help-and-questions
Find the way to write unit-test by postgres in memory db
that said, pg-mem is still useful for lightweight unit tests where you dont need full postgresql behavior
55 replies
PPrisma
Created by nhan.nguyenvan on 2/10/2025 in #help-and-questions
Find the way to write unit-test by postgres in memory db
since it mimics postgresql using javascript, it's not guaranteed to stay 100% in sync with newer postgresql features, optimizaions, or quirks
55 replies
PPrisma
Created by nhan.nguyenvan on 2/10/2025 in #help-and-questions
Find the way to write unit-test by postgres in memory db
pg-mem is great for now, but yeah, there's always a risk that it could become outdated or inaccurate over time
55 replies
PPrisma
Created by nhan.nguyenvan on 2/10/2025 in #help-and-questions
Find the way to write unit-test by postgres in memory db
good question~~
55 replies
PPrisma
Created by nhan.nguyenvan on 2/10/2025 in #help-and-questions
Find the way to write unit-test by postgres in memory db
so the best approach depends on what you’re testing - simple unit tests → pg-mem is great - more realistic database behavior → pg_tmp or a real sostgres instance
55 replies
PPrisma
Created by nhan.nguyenvan on 2/10/2025 in #help-and-questions
Find the way to write unit-test by postgres in memory db
so if your queries are simple like basic crud, joins etc, pg-mem is a solid choice but if you need full postgresql behavior like complex transactions, foreign key constraints, or postgis, then you’d need a real postgres instance like running one on a ram disk or using pg_tmp for a temporary database
55 replies
PPrisma
Created by nhan.nguyenvan on 2/10/2025 in #help-and-questions
Find the way to write unit-test by postgres in memory db
so, pg-mem isnt postgresql itself, but a library that mimics its behavior using javascript it’s super fast and great for unit tests where you dont need full postgresql features like advanced indexing, triggers, or extensions
55 replies
PPrisma
Created by nhan.nguyenvan on 2/10/2025 in #help-and-questions
Find the way to write unit-test by postgres in memory db
yeah, i get the confusion you know, postgresql itself doesn’t have a true "in-memory" mode like sqlite, but pg-mem works differently, it simulates a postgresql database entirely in memory, without actually running a real postgres instance
55 replies
PPrisma
Created by nhan.nguyenvan on 2/10/2025 in #help-and-questions
Find the way to write unit-test by postgres in memory db
so if you need real postgres behavior, pg-mem or using a RAM-disk-backed postgresql instance is better
55 replies
PPrisma
Created by nhan.nguyenvan on 2/10/2025 in #help-and-questions
Find the way to write unit-test by postgres in memory db
in my exp, pg-mem is great for unit tests but doesnt fully replicate postgresql behavior, so it might not work well for complex queries
55 replies
PPrisma
Created by nhan.nguyenvan on 2/10/2025 in #help-and-questions
Find the way to write unit-test by postgres in memory db
if you are testing api routes using jest + supertest, you can do something like this
import request from 'supertest';
import { newDb } from 'pg-mem';
import { app } from '../app'; // Your Express app

let db: any;
beforeAll(async () => {
db = newDb();
db.public.none(`CREATE TABLE users (id SERIAL PRIMARY KEY, name TEXT)`);
});

test('GET /users should return an empty array', async () => {
const res = await request(app).get('/users');
expect(res.status).toBe(200);
expect(res.body).toEqual([]);
});
import request from 'supertest';
import { newDb } from 'pg-mem';
import { app } from '../app'; // Your Express app

let db: any;
beforeAll(async () => {
db = newDb();
db.public.none(`CREATE TABLE users (id SERIAL PRIMARY KEY, name TEXT)`);
});

test('GET /users should return an empty array', async () => {
const res = await request(app).get('/users');
expect(res.status).toBe(200);
expect(res.body).toEqual([]);
});
55 replies
PPrisma
Created by nhan.nguyenvan on 2/10/2025 in #help-and-questions
Find the way to write unit-test by postgres in memory db
and next, you have to use pg-mem in jest tests
55 replies
PPrisma
Created by nhan.nguyenvan on 2/10/2025 in #help-and-questions
Find the way to write unit-test by postgres in memory db
then, set up an in-memory database
import { newDb } from 'pg-mem';
import { Pool } from 'pg';

// Create an in-memory database
const db = newDb();
db.public.none(`CREATE TABLE users (id SERIAL PRIMARY KEY, name TEXT);`);

// Mock the pg Pool to use this in-memory DB
db.adapters.createPg();
const pool = new Pool({ connectionString: db.adapters.createConnection() });

// Example query
async function testQuery() {
await pool.query(`INSERT INTO users (name) VALUES ('Alice')`);
const res = await pool.query(`SELECT * FROM users`);
console.log(res.rows); // Should log [{ id: 1, name: 'Alice' }]
}

testQuery();
import { newDb } from 'pg-mem';
import { Pool } from 'pg';

// Create an in-memory database
const db = newDb();
db.public.none(`CREATE TABLE users (id SERIAL PRIMARY KEY, name TEXT);`);

// Mock the pg Pool to use this in-memory DB
db.adapters.createPg();
const pool = new Pool({ connectionString: db.adapters.createConnection() });

// Example query
async function testQuery() {
await pool.query(`INSERT INTO users (name) VALUES ('Alice')`);
const res = await pool.query(`SELECT * FROM users`);
console.log(res.rows); // Should log [{ id: 1, name: 'Alice' }]
}

testQuery();
55 replies
PPrisma
Created by nhan.nguyenvan on 2/10/2025 in #help-and-questions
Find the way to write unit-test by postgres in memory db
npm install pg-mem - install
55 replies