Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add postgres-js adapter #398

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
61 changes: 59 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"typeorm",
"pg-promise",
"pg-native",
"postgres-js",
"unit test",
"ut",
"sql",
Expand Down Expand Up @@ -87,6 +88,7 @@
"nyc": "^15.1.0",
"pg": "^8.5.1",
"pg-promise": "^10.8.7",
"postgres": "^3.4.4",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-hot-loader": "^4.12.21",
Expand Down Expand Up @@ -153,4 +155,4 @@
"instrument": false,
"sourceMap": false
}
}
}
27 changes: 27 additions & 0 deletions src/adapters/adapters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,33 @@ export class Adapters implements LibAdapters {
return db;
}

createPostgresJs(queryLatency = 0) {
const that = this;

const queryDb = (query: string, queryLatency: number) => {
return {
async then(callback: any) {
await delay(queryLatency);
const result = that.db.public.query(query).rows;
callback(result);
}
};
}

const sql = function sql(strings: TemplateStringsArray, ...values: any[]) {
const sql = strings.reduce((prev, curr, i) => {
return prev + curr + (values[i] ?? '');
}, '');
return queryDb(sql, queryLatency);
}

sql.unsafe = function unsafe(query: string, args = [], options = {}) {
return queryDb(query, queryLatency); // discard args and options for now
}

return sql;
}

createPgNative(queryLatency?: number) {
queryLatency = queryLatency ?? 0;
const prepared = new lru<string, string>({
Expand Down
3 changes: 3 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ export interface LibAdapters {
/** Create a slonik pool bound to this db */
createSlonik(queryLatency?: number): any;

/** Create a postgres-js mimic instance bound to this db */
createPostgresJs(queryLatency?: number): any;

/** Create a pg-native instance bound to this db */
createPgNative(queryLatency?: number): any;

Expand Down
62 changes: 62 additions & 0 deletions src/tests/postgres-js-mimic.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import 'mocha';
import 'chai';
import { expect } from 'chai';
import { newDb } from '../db';
import postgres from 'postgres';

// Spin up a real postgres instance with e.g., docker,
// to compare the results of the fake postgres instance
// with the real one.
const realSQL = postgres({
host: 'localhost',
port: 5432,
database: 'postgres',
username: 'postgres',
password: 'password',
})

describe.only("PostgresJS smoketests", () => {
it("should use template correctly", async () => {
const mem = newDb();
const pg = mem.adapters.createPostgresJs();
const fake = await doSQLStuff(pg);

await realSQL`DROP TABLE IF EXISTS users`; // Clear table from any previous tests
const real = await doSQLStuff(realSQL);

expect(fake).to.deep.equal(real);
})


it("should use unsafe correctly", async () => {
const mem = newDb();
const pg = mem.adapters.createPostgresJs();
const fake = await doUnsafeStuff(pg);

await realSQL`DROP TABLE IF EXISTS users`;
const real = await doUnsafeStuff(realSQL);

expect(fake).to.deep.equal(real); // [ { id: 1, name: 'Alice' }]
});
});

async function doSQLStuff(sql: any) {
// Use the postgres-js tagged template literal to run SQL queries
await sql`
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL
)`;
await sql`INSERT INTO users (name) VALUES ('Alice')`;
return await sql`SELECT * FROM users`;
}

async function doUnsafeStuff(sql: any) {
await sql.unsafe(`
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL
)`)
await sql.unsafe(`INSERT INTO users (name) VALUES ('Alice')`);
return await sql.unsafe(`SELECT * FROM users`);
}