@vrplatform/kysely 1.2.24 → 1.2.25

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,6 +7,8 @@ export { sql } from 'kysely';
7
7
  export type Kysely = K<DB>;
8
8
  export type Postgres = Sql;
9
9
  export declare function useKysely(postgres: Sql): K<DB>;
10
+ export declare function useAsyncPostgres(connectionString: string, ssl?: boolean, retries?: number, delayMs?: number): Promise<postgres.Sql<{}>>;
11
+ export declare function useAsyncKysely(connectionString: string, ssl?: boolean): Promise<K<DB>>;
10
12
  export declare function usePostgres(connectionString: string, ssl?: boolean): postgres.Sql<{}>;
11
13
  export declare function forceCloseAllPostgres(): Promise<void>;
12
14
  export declare class DatabaseError extends Error {
@@ -19,6 +19,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
19
19
  Object.defineProperty(exports, "__esModule", { value: true });
20
20
  exports.DatabaseError = exports.sql = void 0;
21
21
  exports.useKysely = useKysely;
22
+ exports.useAsyncPostgres = useAsyncPostgres;
23
+ exports.useAsyncKysely = useAsyncKysely;
22
24
  exports.usePostgres = usePostgres;
23
25
  exports.forceCloseAllPostgres = forceCloseAllPostgres;
24
26
  exports.withStack = withStack;
@@ -38,6 +40,29 @@ function useKysely(postgres) {
38
40
  plugins: [new kysely_1.CamelCasePlugin()],
39
41
  });
40
42
  }
43
+ async function useAsyncPostgres(connectionString, ssl, retries = 5, delayMs = 2500) {
44
+ for (let attempt = 1; attempt <= retries; attempt++) {
45
+ try {
46
+ const sql = (0, postgres_1.default)(connectionString, {
47
+ ssl: ssl ? 'require' : false,
48
+ });
49
+ await sql `select 1`;
50
+ connections.push(sql);
51
+ return sql;
52
+ }
53
+ catch (err) {
54
+ if (attempt === retries)
55
+ throw err;
56
+ console.warn(`Postgres connection failed (attempt ${attempt}), retrying...`);
57
+ await new Promise((res) => setTimeout(res, delayMs));
58
+ }
59
+ }
60
+ throw new Error('Unable to connect to Postgres');
61
+ }
62
+ async function useAsyncKysely(connectionString, ssl) {
63
+ const sql = await useAsyncPostgres(connectionString, ssl);
64
+ return useKysely(sql);
65
+ }
41
66
  const connections = [];
42
67
  function usePostgres(connectionString, ssl) {
43
68
  const pg = (0, postgres_1.default)(connectionString, {
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"src/","sources":["index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;AA2BA,8BAOC;AAGD,kCAMC;AAED,sDAEC;AAYD,8BA+BC;AA1FD,4CAAkD;AAClD,mCAAsD;AACtD,2DAAuD;AACvD,wDAA8C;AAe9C,iDAA+B;AAE/B,yCAA2C;AAG3C,iCAA6B;AAApB,6FAAA,GAAG,OAAA;AAIZ,SAAgB,SAAS,CAAC,QAAa;IACrC,OAAO,IAAI,eAAC,CAAK;QACf,OAAO,EAAE,IAAI,sCAAiB,CAAC;YAC7B,QAAQ;SACT,CAAC;QACF,OAAO,EAAE,CAAC,IAAI,wBAAe,EAAE,CAAC;KACjC,CAAC,CAAC;AACL,CAAC;AAED,MAAM,WAAW,GAAwB,EAAE,CAAC;AAC5C,SAAgB,WAAW,CAAC,gBAAwB,EAAE,GAAa;IACjE,MAAM,EAAE,GAAG,IAAA,kBAAQ,EAAC,gBAAgB,EAAE;QACpC,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,KAAK;KAC7B,CAAC,CAAC;IACH,WAAW,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IACrB,OAAO,EAAE,CAAC;AACZ,CAAC;AAEM,KAAK,UAAU,qBAAqB;IACzC,MAAM,OAAO,CAAC,GAAG,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;AAC9E,CAAC;AAED,MAAa,aAAc,SAAQ,KAAK;IACtC,YAAY,OAAe,EAAE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K,GAAG,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,CAAC;;YAC5C,KAAK,CAAC,KAAK,GAAG,CAAC,CAAC,KAAK,CAAC;QAC3B,mBAAmB;QACnB,KAAK,CAAC,OAAO,GAAG,CAAC,CAAC,OAAO,CAAC;QAC1B,MAAM,UAAU,GAAG,IAAA,sBAAc,EAAC,CAAC,CAAC,CAAC;QACrC,OAAO,UAAU,CAAC,KAAK,CAAC;QACxB,KAAK,CAAC,KAAK,GAAG,UAAU,CAAC;QACzB,OAAO,CAAC,KAAK,CAAC,qBAAqB,SAAS,EAAE,EAAE,IAAA,sBAAc,EAAC,KAAK,CAAC,CAAC,CAAC;QACvE,OAAO,CAAC,GAAG,CACT,IAAI,SAAS,qBAAqB,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,GAAG,EAAE,GAAG,GAAG,CAAC,IAAI,CAC1E,CAAC;QACF,MAAM,KAAK,CAAC;IACd,CAAC;AACH,CAAC","sourcesContent":["import { serializeError } from '@finalytic/utils';\nimport { CamelCasePlugin, Kysely as K } from 'kysely';\nimport { PostgresJSDialect } from 'kysely-postgres-js';\nimport postgres, { type Sql } from 'postgres';\n\nexport type {\n ColumnType,\n Expression,\n ExpressionBuilder,\n ExpressionOrFactory,\n Generated,\n Insertable,\n JSONColumnType,\n OperandExpression,\n Selectable,\n SqlBool,\n Updateable,\n} from 'kysely';\nexport * from './v1.generated';\n\nimport { join, relative } from 'node:path';\nimport type { DB } from './v1.generated';\n\nexport { sql } from 'kysely';\n\nexport type Kysely = K<DB>;\nexport type Postgres = Sql;\nexport function useKysely(postgres: Sql) {\n return new K<DB>({\n dialect: new PostgresJSDialect({\n postgres,\n }),\n plugins: [new CamelCasePlugin()],\n });\n}\n\nconst connections: postgres.Sql<any>[] = [];\nexport function usePostgres(connectionString: string, ssl?: boolean) {\n const pg = postgres(connectionString, {\n ssl: ssl ? 'require' : false,\n });\n connections.push(pg);\n return pg;\n}\n\nexport async function forceCloseAllPostgres() {\n await Promise.all(connections.map((pg) => pg.end().catch(() => undefined)));\n}\n\nexport class DatabaseError extends Error {\n constructor(message: string, cause: any) {\n super(message);\n this.cause = cause;\n }\n}\n\nconst dirname =\n typeof __dirname === 'string' ? join(__dirname, '../../..') : '';\n\nexport async function withStack<T>(a: Promise<T>) {\n // skip if we can't get the stack, probably in a worker\n if (!dirname) return await a;\n\n const now = performance.now();\n const error = new DatabaseError('Error in execute', undefined);\n const stack = error.stack?.split('\\n').slice(3) || undefined;\n const firstLine =\n stack?.length && stack[0]?.includes('(') && dirname\n ? relative(dirname, stack[0].split('(')[1].split(')')[0])\n : undefined;\n try {\n const result = await a;\n console.log(\n `[${firstLine}] Execution time: ${Math.round(performance.now() - now)}ms`\n );\n return result;\n } catch (e: any) {\n if (stack) error.stack = stack.join('\\n').trim();\n else error.stack = e.stack;\n // error.cause = e;\n error.message = e.message;\n const serialized = serializeError(e);\n delete serialized.stack;\n error.cause = serialized;\n console.error(`Error in execute: ${firstLine}`, serializeError(error));\n console.log(\n `[${firstLine}] Execution time: ${Math.round(performance.now() - now)}ms`\n );\n throw error;\n }\n}\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"src/","sources":["index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;AA2BA,8BAOC;AAED,4CA0BC;AAED,wCAGC;AAGD,kCAMC;AAED,sDAEC;AAYD,8BA+BC;AA3HD,4CAAkD;AAClD,mCAAsD;AACtD,2DAAuD;AACvD,wDAA8C;AAe9C,iDAA+B;AAE/B,yCAA2C;AAG3C,iCAA6B;AAApB,6FAAA,GAAG,OAAA;AAIZ,SAAgB,SAAS,CAAC,QAAa;IACrC,OAAO,IAAI,eAAC,CAAK;QACf,OAAO,EAAE,IAAI,sCAAiB,CAAC;YAC7B,QAAQ;SACT,CAAC;QACF,OAAO,EAAE,CAAC,IAAI,wBAAe,EAAE,CAAC;KACjC,CAAC,CAAC;AACL,CAAC;AAEM,KAAK,UAAU,gBAAgB,C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AAC;QACzD,CAAC,CAAC,SAAS,CAAC;IAChB,IAAI,CAAC;QACH,MAAM,MAAM,GAAG,MAAM,CAAC,CAAC;QACvB,OAAO,CAAC,GAAG,CACT,IAAI,SAAS,qBAAqB,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,GAAG,EAAE,GAAG,GAAG,CAAC,IAAI,CAC1E,CAAC;QACF,OAAO,MAAM,CAAC;IAChB,CAAC;IAAC,OAAO,CAAM,EAAE,CAAC;QAChB,IAAI,KAAK;YAAE,KAAK,CAAC,KAAK,GAAG,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,CAAC;;YAC5C,KAAK,CAAC,KAAK,GAAG,CAAC,CAAC,KAAK,CAAC;QAC3B,mBAAmB;QACnB,KAAK,CAAC,OAAO,GAAG,CAAC,CAAC,OAAO,CAAC;QAC1B,MAAM,UAAU,GAAG,IAAA,sBAAc,EAAC,CAAC,CAAC,CAAC;QACrC,OAAO,UAAU,CAAC,KAAK,CAAC;QACxB,KAAK,CAAC,KAAK,GAAG,UAAU,CAAC;QACzB,OAAO,CAAC,KAAK,CAAC,qBAAqB,SAAS,EAAE,EAAE,IAAA,sBAAc,EAAC,KAAK,CAAC,CAAC,CAAC;QACvE,OAAO,CAAC,GAAG,CACT,IAAI,SAAS,qBAAqB,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,GAAG,EAAE,GAAG,GAAG,CAAC,IAAI,CAC1E,CAAC;QACF,MAAM,KAAK,CAAC;IACd,CAAC;AACH,CAAC","sourcesContent":["import { serializeError } from '@finalytic/utils';\nimport { CamelCasePlugin, Kysely as K } from 'kysely';\nimport { PostgresJSDialect } from 'kysely-postgres-js';\nimport postgres, { type Sql } from 'postgres';\n\nexport type {\n ColumnType,\n Expression,\n ExpressionBuilder,\n ExpressionOrFactory,\n Generated,\n Insertable,\n JSONColumnType,\n OperandExpression,\n Selectable,\n SqlBool,\n Updateable,\n} from 'kysely';\nexport * from './v1.generated';\n\nimport { join, relative } from 'node:path';\nimport type { DB } from './v1.generated';\n\nexport { sql } from 'kysely';\n\nexport type Kysely = K<DB>;\nexport type Postgres = Sql;\nexport function useKysely(postgres: Sql) {\n return new K<DB>({\n dialect: new PostgresJSDialect({\n postgres,\n }),\n plugins: [new CamelCasePlugin()],\n });\n}\n\nexport async function useAsyncPostgres(\n connectionString: string,\n ssl?: boolean,\n retries = 5,\n delayMs = 2500\n) {\n for (let attempt = 1; attempt <= retries; attempt++) {\n try {\n const sql = postgres(connectionString, {\n ssl: ssl ? 'require' : false,\n });\n\n await sql`select 1`;\n\n connections.push(sql);\n\n return sql;\n } catch (err) {\n if (attempt === retries) throw err;\n console.warn(\n `Postgres connection failed (attempt ${attempt}), retrying...`\n );\n await new Promise((res) => setTimeout(res, delayMs));\n }\n }\n throw new Error('Unable to connect to Postgres');\n}\n\nexport async function useAsyncKysely(connectionString: string, ssl?: boolean) {\n const sql = await useAsyncPostgres(connectionString, ssl);\n return useKysely(sql);\n}\n\nconst connections: postgres.Sql<any>[] = [];\nexport function usePostgres(connectionString: string, ssl?: boolean) {\n const pg = postgres(connectionString, {\n ssl: ssl ? 'require' : false,\n });\n connections.push(pg);\n return pg;\n}\n\nexport async function forceCloseAllPostgres() {\n await Promise.all(connections.map((pg) => pg.end().catch(() => undefined)));\n}\n\nexport class DatabaseError extends Error {\n constructor(message: string, cause: any) {\n super(message);\n this.cause = cause;\n }\n}\n\nconst dirname =\n typeof __dirname === 'string' ? join(__dirname, '../../..') : '';\n\nexport async function withStack<T>(a: Promise<T>) {\n // skip if we can't get the stack, probably in a worker\n if (!dirname) return await a;\n\n const now = performance.now();\n const error = new DatabaseError('Error in execute', undefined);\n const stack = error.stack?.split('\\n').slice(3) || undefined;\n const firstLine =\n stack?.length && stack[0]?.includes('(') && dirname\n ? relative(dirname, stack[0].split('(')[1].split(')')[0])\n : undefined;\n try {\n const result = await a;\n console.log(\n `[${firstLine}] Execution time: ${Math.round(performance.now() - now)}ms`\n );\n return result;\n } catch (e: any) {\n if (stack) error.stack = stack.join('\\n').trim();\n else error.stack = e.stack;\n // error.cause = e;\n error.message = e.message;\n const serialized = serializeError(e);\n delete serialized.stack;\n error.cause = serialized;\n console.error(`Error in execute: ${firstLine}`, serializeError(error));\n console.log(\n `[${firstLine}] Execution time: ${Math.round(performance.now() - now)}ms`\n );\n throw error;\n }\n}\n"]}
@@ -7,6 +7,8 @@ export { sql } from 'kysely';
7
7
  export type Kysely = K<DB>;
8
8
  export type Postgres = Sql;
9
9
  export declare function useKysely(postgres: Sql): K<DB>;
10
+ export declare function useAsyncPostgres(connectionString: string, ssl?: boolean, retries?: number, delayMs?: number): Promise<postgres.Sql<{}>>;
11
+ export declare function useAsyncKysely(connectionString: string, ssl?: boolean): Promise<K<DB>>;
10
12
  export declare function usePostgres(connectionString: string, ssl?: boolean): postgres.Sql<{}>;
11
13
  export declare function forceCloseAllPostgres(): Promise<void>;
12
14
  export declare class DatabaseError extends Error {
@@ -13,6 +13,29 @@ export function useKysely(postgres) {
13
13
  plugins: [new CamelCasePlugin()],
14
14
  });
15
15
  }
16
+ export async function useAsyncPostgres(connectionString, ssl, retries = 5, delayMs = 2500) {
17
+ for (let attempt = 1; attempt <= retries; attempt++) {
18
+ try {
19
+ const sql = postgres(connectionString, {
20
+ ssl: ssl ? 'require' : false,
21
+ });
22
+ await sql `select 1`;
23
+ connections.push(sql);
24
+ return sql;
25
+ }
26
+ catch (err) {
27
+ if (attempt === retries)
28
+ throw err;
29
+ console.warn(`Postgres connection failed (attempt ${attempt}), retrying...`);
30
+ await new Promise((res) => setTimeout(res, delayMs));
31
+ }
32
+ }
33
+ throw new Error('Unable to connect to Postgres');
34
+ }
35
+ export async function useAsyncKysely(connectionString, ssl) {
36
+ const sql = await useAsyncPostgres(connectionString, ssl);
37
+ return useKysely(sql);
38
+ }
16
39
  const connections = [];
17
40
  export function usePostgres(connectionString, ssl) {
18
41
  const pg = postgres(connectionString, {
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sour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AAC,IAAI,EAAE,CAAC;;YAC5C,KAAK,CAAC,KAAK,GAAG,CAAC,CAAC,KAAK,CAAC;QAC3B,mBAAmB;QACnB,KAAK,CAAC,OAAO,GAAG,CAAC,CAAC,OAAO,CAAC;QAC1B,MAAM,UAAU,GAAG,cAAc,CAAC,CAAC,CAAC,CAAC;QACrC,OAAO,UAAU,CAAC,KAAK,CAAC;QACxB,KAAK,CAAC,KAAK,GAAG,UAAU,CAAC;QACzB,OAAO,CAAC,KAAK,CAAC,qBAAqB,SAAS,EAAE,EAAE,cAAc,CAAC,KAAK,CAAC,CAAC,CAAC;QACvE,OAAO,CAAC,GAAG,CACT,IAAI,SAAS,qBAAqB,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,GAAG,EAAE,GAAG,GAAG,CAAC,IAAI,CAC1E,CAAC;QACF,MAAM,KAAK,CAAC;IACd,CAAC;AACH,CAAC","sourcesContent":["import { serializeError } from '@finalytic/utils';\nimport { CamelCasePlugin, Kysely as K } from 'kysely';\nimport { PostgresJSDialect } from 'kysely-postgres-js';\nimport postgres, { type Sql } from 'postgres';\n\nexport type {\n ColumnType,\n Expression,\n ExpressionBuilder,\n ExpressionOrFactory,\n Generated,\n Insertable,\n JSONColumnType,\n OperandExpression,\n Selectable,\n SqlBool,\n Updateable,\n} from 'kysely';\nexport * from './v1.generated';\n\nimport { join, relative } from 'node:path';\nimport type { DB } from './v1.generated';\n\nexport { sql } from 'kysely';\n\nexport type Kysely = K<DB>;\nexport type Postgres = Sql;\nexport function useKysely(postgres: Sql) {\n return new K<DB>({\n dialect: new PostgresJSDialect({\n postgres,\n }),\n plugins: [new CamelCasePlugin()],\n });\n}\n\nconst connections: postgres.Sql<any>[] = [];\nexport function usePostgres(connectionString: string, ssl?: boolean) {\n const pg = postgres(connectionString, {\n ssl: ssl ? 'require' : false,\n });\n connections.push(pg);\n return pg;\n}\n\nexport async function forceCloseAllPostgres() {\n await Promise.all(connections.map((pg) => pg.end().catch(() => undefined)));\n}\n\nexport class DatabaseError extends Error {\n constructor(message: string, cause: any) {\n super(message);\n this.cause = cause;\n }\n}\n\nconst dirname =\n typeof __dirname === 'string' ? join(__dirname, '../../..') : '';\n\nexport async function withStack<T>(a: Promise<T>) {\n // skip if we can't get the stack, probably in a worker\n if (!dirname) return await a;\n\n const now = performance.now();\n const error = new DatabaseError('Error in execute', undefined);\n const stack = error.stack?.split('\\n').slice(3) || undefined;\n const firstLine =\n stack?.length && stack[0]?.includes('(') && dirname\n ? relative(dirname, stack[0].split('(')[1].split(')')[0])\n : undefined;\n try {\n const result = await a;\n console.log(\n `[${firstLine}] Execution time: ${Math.round(performance.now() - now)}ms`\n );\n return result;\n } catch (e: any) {\n if (stack) error.stack = stack.join('\\n').trim();\n else error.stack = e.stack;\n // error.cause = e;\n error.message = e.message;\n const serialized = serializeError(e);\n delete serialized.stack;\n error.cause = serialized;\n console.error(`Error in execute: ${firstLine}`, serializeError(error));\n console.log(\n `[${firstLine}] Execution time: ${Math.round(performance.now() - now)}ms`\n );\n throw error;\n }\n}\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"src/","sources":["index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,EAAE,eAAe,EAAE,MAAM,IAAI,CAAC,EAAE,MAAM,QAAQ,CAAC;AACtD,OAAO,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AACvD,OAAO,QAAsB,MAAM,UAAU,CAAC;AAe9C,cAAc,gBAAgB,CAAC;AAE/B,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAG3C,OAAO,EAAE,GAAG,EAAE,MAAM,QAAQ,CAAC;AAI7B,MAAM,UAAU,SAAS,CAAC,QAAa;IACrC,OAAO,IAAI,CAAC,CAAK;QACf,OAAO,EAAE,IAAI,iBAAiB,CAAC;YAC7B,QAAQ;SACT,CAAC;QACF,OAAO,EAAE,CAAC,IAAI,eAAe,EAA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GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;QACzD,CAAC,CAAC,SAAS,CAAC;IAChB,IAAI,CAAC;QACH,MAAM,MAAM,GAAG,MAAM,CAAC,CAAC;QACvB,OAAO,CAAC,GAAG,CACT,IAAI,SAAS,qBAAqB,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,GAAG,EAAE,GAAG,GAAG,CAAC,IAAI,CAC1E,CAAC;QACF,OAAO,MAAM,CAAC;IAChB,CAAC;IAAC,OAAO,CAAM,EAAE,CAAC;QAChB,IAAI,KAAK;YAAE,KAAK,CAAC,KAAK,GAAG,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,CAAC;;YAC5C,KAAK,CAAC,KAAK,GAAG,CAAC,CAAC,KAAK,CAAC;QAC3B,mBAAmB;QACnB,KAAK,CAAC,OAAO,GAAG,CAAC,CAAC,OAAO,CAAC;QAC1B,MAAM,UAAU,GAAG,cAAc,CAAC,CAAC,CAAC,CAAC;QACrC,OAAO,UAAU,CAAC,KAAK,CAAC;QACxB,KAAK,CAAC,KAAK,GAAG,UAAU,CAAC;QACzB,OAAO,CAAC,KAAK,CAAC,qBAAqB,SAAS,EAAE,EAAE,cAAc,CAAC,KAAK,CAAC,CAAC,CAAC;QACvE,OAAO,CAAC,GAAG,CACT,IAAI,SAAS,qBAAqB,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,GAAG,EAAE,GAAG,GAAG,CAAC,IAAI,CAC1E,CAAC;QACF,MAAM,KAAK,CAAC;IACd,CAAC;AACH,CAAC","sourcesContent":["import { serializeError } from '@finalytic/utils';\nimport { CamelCasePlugin, Kysely as K } from 'kysely';\nimport { PostgresJSDialect } from 'kysely-postgres-js';\nimport postgres, { type Sql } from 'postgres';\n\nexport type {\n ColumnType,\n Expression,\n ExpressionBuilder,\n ExpressionOrFactory,\n Generated,\n Insertable,\n JSONColumnType,\n OperandExpression,\n Selectable,\n SqlBool,\n Updateable,\n} from 'kysely';\nexport * from './v1.generated';\n\nimport { join, relative } from 'node:path';\nimport type { DB } from './v1.generated';\n\nexport { sql } from 'kysely';\n\nexport type Kysely = K<DB>;\nexport type Postgres = Sql;\nexport function useKysely(postgres: Sql) {\n return new K<DB>({\n dialect: new PostgresJSDialect({\n postgres,\n }),\n plugins: [new CamelCasePlugin()],\n });\n}\n\nexport async function useAsyncPostgres(\n connectionString: string,\n ssl?: boolean,\n retries = 5,\n delayMs = 2500\n) {\n for (let attempt = 1; attempt <= retries; attempt++) {\n try {\n const sql = postgres(connectionString, {\n ssl: ssl ? 'require' : false,\n });\n\n await sql`select 1`;\n\n connections.push(sql);\n\n return sql;\n } catch (err) {\n if (attempt === retries) throw err;\n console.warn(\n `Postgres connection failed (attempt ${attempt}), retrying...`\n );\n await new Promise((res) => setTimeout(res, delayMs));\n }\n }\n throw new Error('Unable to connect to Postgres');\n}\n\nexport async function useAsyncKysely(connectionString: string, ssl?: boolean) {\n const sql = await useAsyncPostgres(connectionString, ssl);\n return useKysely(sql);\n}\n\nconst connections: postgres.Sql<any>[] = [];\nexport function usePostgres(connectionString: string, ssl?: boolean) {\n const pg = postgres(connectionString, {\n ssl: ssl ? 'require' : false,\n });\n connections.push(pg);\n return pg;\n}\n\nexport async function forceCloseAllPostgres() {\n await Promise.all(connections.map((pg) => pg.end().catch(() => undefined)));\n}\n\nexport class DatabaseError extends Error {\n constructor(message: string, cause: any) {\n super(message);\n this.cause = cause;\n }\n}\n\nconst dirname =\n typeof __dirname === 'string' ? join(__dirname, '../../..') : '';\n\nexport async function withStack<T>(a: Promise<T>) {\n // skip if we can't get the stack, probably in a worker\n if (!dirname) return await a;\n\n const now = performance.now();\n const error = new DatabaseError('Error in execute', undefined);\n const stack = error.stack?.split('\\n').slice(3) || undefined;\n const firstLine =\n stack?.length && stack[0]?.includes('(') && dirname\n ? relative(dirname, stack[0].split('(')[1].split(')')[0])\n : undefined;\n try {\n const result = await a;\n console.log(\n `[${firstLine}] Execution time: ${Math.round(performance.now() - now)}ms`\n );\n return result;\n } catch (e: any) {\n if (stack) error.stack = stack.join('\\n').trim();\n else error.stack = e.stack;\n // error.cause = e;\n error.message = e.message;\n const serialized = serializeError(e);\n delete serialized.stack;\n error.cause = serialized;\n console.error(`Error in execute: ${firstLine}`, serializeError(error));\n console.log(\n `[${firstLine}] Execution time: ${Math.round(performance.now() - now)}ms`\n );\n throw error;\n }\n}\n"]}
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.2.24",
6
+ "version": "1.2.25",
7
7
  "description": "",
8
8
  "main": "build/main/index.js",
9
9
  "module": "build/module/index.js",
package/src/index.ts CHANGED
@@ -34,6 +34,39 @@ export function useKysely(postgres: Sql) {
34
34
  });
35
35
  }
36
36
 
37
+ export async function useAsyncPostgres(
38
+ connectionString: string,
39
+ ssl?: boolean,
40
+ retries = 5,
41
+ delayMs = 2500
42
+ ) {
43
+ for (let attempt = 1; attempt <= retries; attempt++) {
44
+ try {
45
+ const sql = postgres(connectionString, {
46
+ ssl: ssl ? 'require' : false,
47
+ });
48
+
49
+ await sql`select 1`;
50
+
51
+ connections.push(sql);
52
+
53
+ return sql;
54
+ } catch (err) {
55
+ if (attempt === retries) throw err;
56
+ console.warn(
57
+ `Postgres connection failed (attempt ${attempt}), retrying...`
58
+ );
59
+ await new Promise((res) => setTimeout(res, delayMs));
60
+ }
61
+ }
62
+ throw new Error('Unable to connect to Postgres');
63
+ }
64
+
65
+ export async function useAsyncKysely(connectionString: string, ssl?: boolean) {
66
+ const sql = await useAsyncPostgres(connectionString, ssl);
67
+ return useKysely(sql);
68
+ }
69
+
37
70
  const connections: postgres.Sql<any>[] = [];
38
71
  export function usePostgres(connectionString: string, ssl?: boolean) {
39
72
  const pg = postgres(connectionString, {