Skip to content

Commit

Permalink
Merge pull request #30 from openaddresses/tsconfig-update
Browse files Browse the repository at this point in the history
TSConfig Update
  • Loading branch information
ingalls authored Aug 30, 2024
2 parents ed4373f + aeea402 commit ab183e6
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 15 deletions.
35 changes: 23 additions & 12 deletions lib/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ import postgres from 'postgres';
import { sql } from 'drizzle-orm';
import { PgDatabase, PgDialect } from 'drizzle-orm/pg-core';
import { migrate } from 'drizzle-orm/postgres-js/migrator'
import { PostgresJsSession, PostgresJsQueryResultHKT } from 'drizzle-orm/postgres-js';
import { PostgresJsSession } from 'drizzle-orm/postgres-js'
import type { PostgresJsQueryResultHKT } from 'drizzle-orm/postgres-js';
import {
createTableRelationsHelpers,
extractTablesRelationalConfig
createTableRelationsHelpers,
extractTablesRelationalConfig
} from "drizzle-orm/relations";
import type {
ExtractTablesWithRelations,
RelationalSchemaConfig,
TablesRelationalConfig
} from "drizzle-orm/relations";

export type PostgresJsDatabase<TSchema extends Record<string, unknown> = Record<string, never>> = PgDatabase<PostgresJsQueryResultHKT, TSchema>;

/**
* @class
Expand All @@ -29,7 +33,7 @@ export default class Pool<TSchema extends Record<string, unknown> = Record<strin
ssl: config.ssl
});

let schema;
let schema: RelationalSchemaConfig<TablesRelationalConfig> | undefined;
if (config.schema) {
const tablesConfig = extractTablesRelationalConfig(
config.schema,
Expand All @@ -44,10 +48,14 @@ export default class Pool<TSchema extends Record<string, unknown> = Record<strin

const dialect = new PgDialect();
const session = new PostgresJsSession(client, dialect, schema)
super(dialect, session, schema);
super(
dialect,
session,
schema as RelationalSchemaConfig<ExtractTablesWithRelations<TSchema>>
);

this.connstr = connstr;
this.schema = schema;
this.schema = config.schema;
}

end() {
Expand All @@ -72,18 +80,21 @@ export default class Pool<TSchema extends Record<string, unknown> = Record<strin
} = {}): Promise<Pool<TSchema>> {
if (!opts.retry) opts.retry = 5;

let pool;
let pool: Pool<TSchema> | undefined = undefined;
let retry = opts.retry;
do {
try {
pool = new Pool(connstr, {
ssl: opts.ssl,
schema
});
await pool.select(sql`NOW()`);

await pool.select({
now: sql`NOW()`
});
} catch (err) {
console.error(err);
pool = false;
pool = undefined;

if (retry === 0) {
console.error('not ok - terminating due to lack of postgres connection');
Expand All @@ -95,7 +106,7 @@ export default class Pool<TSchema extends Record<string, unknown> = Record<strin
console.error(`ok - retrying... (${5 - retry}/5)`);
await sleep(5000);
}
} while (!pool);
} while (pool === undefined);

if (opts.migrationsFolder) {
await migrate(pool, { migrationsFolder: opts.migrationsFolder });
Expand Down
22 changes: 22 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"devDependencies": {
"@types/express": "^4.17.21",
"@types/geojson": "^7946.0.13",
"@types/tape": "^5.6.4",
"eslint": "^9.0.0",
"tape": "^5.4.0",
"typedoc": "^0.26.0",
Expand Down
17 changes: 16 additions & 1 deletion test/generic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as pgschema from './schema.base.js';

const connstr = init();

test('Generic Generate', async () => {
test('Generic Generate', async (t) => {
const pool = await Pool.connect(connstr, pgschema);

const ProfileModel = new Modeler(pool, pgschema.Profile);
Expand All @@ -15,6 +15,7 @@ test('Generic Generate', async () => {
});

pool.end();
t.end();
});

test('Generic From - 404', async (t) => {
Expand All @@ -34,4 +35,18 @@ test('Generic From - 404', async (t) => {
}

pool.end();
t.end();
});

test('Generic From', async (t) => {
const pool = await Pool.connect(connstr, pgschema);

const ProfileModel = new Modeler(pool, pgschema.Profile);

const user = await ProfileModel.from('test-user');

t.equals(user.username, 'test-user')

pool.end();
t.end();
});
5 changes: 3 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
},
"compilerOptions": {
"strict": true,
"strictNullChecks": true,
"esModuleInterop": true,
"skipLibCheck": true,
"module": "es2022",
"target": "es2022",
"esModuleInterop": true,
"noImplicitAny": false,
"moduleResolution": "node",
"sourceMap": true,
"outDir": "dist",
Expand All @@ -19,6 +19,7 @@
]
}
},
"exclude": [],
"include": [
"generic.ts",
"lib/*.ts"
Expand Down

0 comments on commit ab183e6

Please sign in to comment.