@vrplatform/kysely 1.3.20 → 1.3.28

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.
Files changed (88) hide show
  1. package/build/.data.tar +0 -0
  2. package/build/main/camel-case/index.js.map +1 -1
  3. package/build/main/error.js.map +1 -1
  4. package/build/main/index.d.ts +3 -3
  5. package/build/main/index.js +21 -2
  6. package/build/main/index.js.map +1 -1
  7. package/build/main/isomorphic.d.ts +1 -1
  8. package/build/main/isomorphic.js +2 -2
  9. package/build/main/isomorphic.js.map +1 -1
  10. package/build/main/local/digest.js.map +1 -1
  11. package/build/main/local/generate.js +3 -3
  12. package/build/main/local/generate.js.map +1 -1
  13. package/build/main/local/index.d.ts +2 -1
  14. package/build/main/local/index.js +53 -1
  15. package/build/main/local/index.js.map +1 -1
  16. package/build/main/pganalyze-comment-plugin/comment-transformer.js +1 -0
  17. package/build/main/pganalyze-comment-plugin/comment-transformer.js.map +1 -1
  18. package/build/main/pganalyze-comment-plugin/index.js +3 -16
  19. package/build/main/pganalyze-comment-plugin/index.js.map +1 -1
  20. package/build/main/plugins.js.map +1 -1
  21. package/build/main/query/listing/index.d.ts +1 -1
  22. package/build/main/query/listing/index.js +1 -1
  23. package/build/main/query/listing/index.js.map +1 -1
  24. package/build/main/query/listing/status.d.ts +26 -0
  25. package/build/main/query/listing/status.js +48 -0
  26. package/build/main/query/listing/status.js.map +1 -0
  27. package/build/main/test/index.d.ts +3 -0
  28. package/build/main/test/index.js +9 -0
  29. package/build/main/test/index.js.map +1 -1
  30. package/build/main/test.d.ts +4 -0
  31. package/build/main/test.js +19 -0
  32. package/build/main/test.js.map +1 -0
  33. package/build/main/tsconfig.main.tsbuildinfo +1 -1
  34. package/build/main/v1.generated.d.ts +33 -3
  35. package/build/main/v1.generated.js.map +1 -1
  36. package/build/module/camel-case/index.js.map +1 -1
  37. package/build/module/error.js.map +1 -1
  38. package/build/module/index.d.ts +3 -3
  39. package/build/module/index.js +21 -2
  40. package/build/module/index.js.map +1 -1
  41. package/build/module/isomorphic.d.ts +1 -1
  42. package/build/module/isomorphic.js.map +1 -1
  43. package/build/module/local/digest.js.map +1 -1
  44. package/build/module/local/generate.js +3 -3
  45. package/build/module/local/generate.js.map +1 -1
  46. package/build/module/local/index.d.ts +2 -1
  47. package/build/module/local/index.js +20 -1
  48. package/build/module/local/index.js.map +1 -1
  49. package/build/module/pganalyze-comment-plugin/comment-transformer.js.map +1 -1
  50. package/build/module/pganalyze-comment-plugin/index.js.map +1 -1
  51. package/build/module/plugins.js.map +1 -1
  52. package/build/module/query/listing/index.d.ts +1 -1
  53. package/build/module/query/listing/index.js +1 -1
  54. package/build/module/query/listing/index.js.map +1 -1
  55. package/build/module/query/listing/status.d.ts +26 -0
  56. package/build/module/query/listing/status.js +41 -0
  57. package/build/module/query/listing/status.js.map +1 -0
  58. package/build/module/test/index.d.ts +3 -0
  59. package/build/module/test/index.js +9 -0
  60. package/build/module/test/index.js.map +1 -1
  61. package/build/module/test.d.ts +4 -0
  62. package/build/module/test.js +16 -0
  63. package/build/module/test.js.map +1 -0
  64. package/build/module/tsconfig.esm.tsbuildinfo +1 -1
  65. package/build/module/v1.generated.d.ts +33 -3
  66. package/build/module/v1.generated.js.map +1 -1
  67. package/package.json +13 -30
  68. package/build/main/query/listing/is-active-or-at-least-one-ownership-period-active.d.ts +0 -6
  69. package/build/main/query/listing/is-active-or-at-least-one-ownership-period-active.js +0 -24
  70. package/build/main/query/listing/is-active-or-at-least-one-ownership-period-active.js.map +0 -1
  71. package/build/module/query/listing/is-active-or-at-least-one-ownership-period-active.d.ts +0 -6
  72. package/build/module/query/listing/is-active-or-at-least-one-ownership-period-active.js +0 -21
  73. package/build/module/query/listing/is-active-or-at-least-one-ownership-period-active.js.map +0 -1
  74. package/src/camel-case/index.ts +0 -68
  75. package/src/error.ts +0 -6
  76. package/src/index.ts +0 -145
  77. package/src/isomorphic.ts +0 -33
  78. package/src/local/digest.ts +0 -44
  79. package/src/local/generate.ts +0 -137
  80. package/src/local/index.ts +0 -48
  81. package/src/pganalyze-comment-plugin/comment-transformer.ts +0 -117
  82. package/src/pganalyze-comment-plugin/index.ts +0 -27
  83. package/src/plugins.ts +0 -30
  84. package/src/query/index.ts +0 -1
  85. package/src/query/listing/index.ts +0 -1
  86. package/src/query/listing/is-active-or-at-least-one-ownership-period-active.ts +0 -29
  87. package/src/test/index.ts +0 -64
  88. package/src/v1.generated.ts +0 -1917
package/src/index.ts DELETED
@@ -1,145 +0,0 @@
1
- import type { Kysely as K, LogConfig } from 'kysely';
2
- import { PostgresJSDialect } from 'kysely-postgres-js';
3
- import postgres, { type Sql } from 'postgres';
4
- import { getKysely } from './plugins';
5
-
6
- export type { LogEvent } from 'kysely';
7
- export * from './error';
8
- export * from './isomorphic';
9
- export * from './query';
10
-
11
- import type { DB } from './v1.generated';
12
-
13
- type KyselyOpts = {
14
- repositoryName?: string;
15
- log?: true | LogConfig;
16
- };
17
-
18
- type PgOpt = {
19
- forceDisableSSL?: boolean;
20
- retries?: false | number;
21
- };
22
-
23
- export type Kysely = K<DB>;
24
- export type Postgres = Sql;
25
- /**
26
- * @deprecated This method is deprecated in favor of the useAsyncKysely method.
27
- */
28
- export function useKysely(postgres: Sql, options?: KyselyOpts) {
29
- console.debug('Creating Kysely instance');
30
- const kysely = getKysely(
31
- new PostgresJSDialect({
32
- postgres,
33
- }),
34
- {
35
- repositoryName: options?.repositoryName,
36
- log: options?.log,
37
- }
38
- );
39
- kysely.destroy = async () => {
40
- console.debug('Closing Kysely connection');
41
- await Promise.race([
42
- postgres.end({ timeout: 5000 }).catch(() => undefined),
43
- new Promise((res) => setTimeout(res, 6000)),
44
- ]);
45
- const index = _connections.indexOf(postgres);
46
- if (index !== -1) _connections.splice(index, 1);
47
- };
48
-
49
- return kysely;
50
- }
51
-
52
- let _connections: postgres.Sql[] = [];
53
- export async function useAsyncPostgres(connectionString: string, opt?: PgOpt) {
54
- const delayMs = 2500;
55
- const retries = opt?.retries === false ? 0 : opt?.retries || 3;
56
- const ssl = opt?.forceDisableSSL !== true;
57
- const url = new URL(connectionString);
58
-
59
- console.debug(
60
- `Creating Postgres connection with ${retries} retries and ssl=${ssl} and port=${url.port}`
61
- );
62
- if (_connections.length > 0)
63
- console.warn(`We already have ${_connections.length} Postgres connections`);
64
-
65
- for (let attempt = 1; attempt <= retries; attempt++) {
66
- try {
67
- const sql = postgres(connectionString, {
68
- ssl: ssl ? 'require' : false,
69
- // connect_timeout: 10
70
- // idle_timeout: 10
71
- // enable prepare statements, pgbouncer on crunchy allows for prepared statements
72
- prepare: true,
73
- connect_timeout: 10,
74
- idle_timeout: 60,
75
- });
76
-
77
- await sql`select 1`;
78
- try {
79
- // connect 30s, idle 60s, max lifetime 45min, statement 60s, lock 10s, log_min_duration 20s, log_lock_waits off
80
- await sql`set statement_timeout to '30s';`;
81
- await sql`set lock_timeout to '10s';`;
82
- } catch (e) {
83
- console.warn('Failed to set statement timeout, continuing', e);
84
- }
85
-
86
- _connections.push(sql);
87
- return sql;
88
- } catch (err) {
89
- if (attempt === retries) throw err;
90
- console.warn(
91
- `Postgres connection failed (attempt ${attempt}), retrying...`
92
- );
93
- console.error('error', err);
94
- await new Promise((res) => setTimeout(res, delayMs));
95
- }
96
- }
97
- throw new Error('Unable to connect to Postgres');
98
- }
99
-
100
- export async function useAsyncKysely(
101
- connectionString: string,
102
- opt?: PgOpt & KyselyOpts
103
- ) {
104
- const sql = await useAsyncPostgres(connectionString, opt);
105
- return useKysely(sql, opt);
106
- }
107
-
108
- /**
109
- * @deprecated This method is deprecated in favor of the useAsyncKysely method.
110
- */
111
- export function usePostgres(
112
- connectionString?: string,
113
- opt?: Pick<PgOpt, 'forceDisableSSL'>
114
- ) {
115
- if (!connectionString) throw new Error('No connection string');
116
- const ssl = opt?.forceDisableSSL !== true;
117
- const url = new URL(connectionString);
118
- console.debug(
119
- `Creating sync Postgres connection with ssl=${ssl} and port=${url.port}`
120
- );
121
- const pg = postgres(connectionString, {
122
- ssl: ssl ? 'require' : false,
123
- });
124
- _connections.push(pg);
125
- return pg;
126
- }
127
-
128
- export async function forceCloseAllPostgres(timeoutMs = 5000) {
129
- await Promise.race([
130
- Promise.all(
131
- _connections.map((pg) =>
132
- pg.end({ timeout: timeoutMs }).catch(() => undefined)
133
- )
134
- ),
135
- new Promise((res) => setTimeout(res, timeoutMs + 1000)),
136
- ]);
137
- _connections = [];
138
- }
139
-
140
- export class DatabaseError extends Error {
141
- constructor(message: string, cause: any) {
142
- super(message);
143
- this.cause = cause;
144
- }
145
- }
package/src/isomorphic.ts DELETED
@@ -1,33 +0,0 @@
1
- import type { Kysely as K, Transaction as T } from 'kysely';
2
-
3
- export type {
4
- ColumnType,
5
- Expression,
6
- ExpressionBuilder,
7
- ExpressionOrFactory,
8
- Generated,
9
- Insertable,
10
- JSONColumnType,
11
- Nullable,
12
- OperandExpression,
13
- Selectable,
14
- SelectQueryBuilder,
15
- SqlBool,
16
- Updateable,
17
- } from 'kysely';
18
- export { jsonArrayFrom } from 'kysely/helpers/postgres';
19
- export * from './v1.generated';
20
-
21
- import type { DB } from './v1.generated';
22
-
23
- export type Transaction = T<DB>;
24
- export { sql } from 'kysely';
25
-
26
- export type Kysely = K<DB>;
27
-
28
- export class DatabaseError extends Error {
29
- constructor(message: string, cause: any) {
30
- super(message);
31
- this.cause = cause;
32
- }
33
- }
@@ -1,44 +0,0 @@
1
- import { type Kysely, sql } from '../isomorphic';
2
-
3
- export async function ensureDigestFunction(kysely: Kysely) {
4
- try {
5
- await sql`select digest('', 'sha256')`.execute(kysely);
6
- } catch {
7
- await sql`
8
- create or replace function digest(input text, algo text)
9
- returns bytea
10
- language sql
11
- immutable
12
- strict
13
- as $$
14
- select '\\x0000000000000000000000000000000000000000000000000000000000000000'::bytea;
15
- $$;
16
- `.execute(kysely);
17
- }
18
- }
19
-
20
- export async function ensureGenRandomBytesFunction(kysely: Kysely) {
21
- try {
22
- await sql`select gen_random_bytes(1)`.execute(kysely);
23
- } catch {
24
- await sql`
25
- create or replace function gen_random_bytes(p_len integer)
26
- returns bytea
27
- language sql
28
- stable
29
- strict
30
- as $$
31
- select substring(
32
- decode(
33
- repeat(
34
- md5(random()::text || now()::text),
35
- ((p_len + 15) / 16)
36
- ),
37
- 'hex'
38
- )
39
- from 1 for p_len
40
- );
41
- $$;
42
- `.execute(kysely);
43
- }
44
- }
@@ -1,137 +0,0 @@
1
- import { promises as fs } from 'node:fs';
2
- import { exists } from 'node:fs/promises';
3
- import * as path from 'node:path';
4
- import { join } from 'node:path';
5
- import { PGlite } from '@electric-sql/pglite';
6
- import { FileMigrationProvider, type Kysely, Migrator } from 'kysely';
7
- import codegen from 'kysely-codegen';
8
- import { KyselyPGlite } from 'kysely-pglite';
9
- import { getKysely } from '../plugins';
10
- import type { DB } from '../v1.generated';
11
-
12
- async function migrate(root: string, client: PGlite) {
13
- console.log('migrating pglite ....');
14
- const glob = new Bun.Glob('initial/*.sql');
15
- for await (const migration of glob.scan(root)) {
16
- const raw = await Bun.file(join(root, migration)).text();
17
- // `pg_dump` (v17+) prepends `\restrict`/`\unrestrict` psql meta commands
18
- // which PostgreSQL-compatible parsers (like PGlite) cannot execute.
19
- const sanitized = raw.replace(/^\s*\\(?:un)?restrict.*$/gim, '');
20
- await client.exec(sanitized);
21
- }
22
- }
23
-
24
- async function migrateKysely(root: string, kysely: Kysely<DB>) {
25
- console.log('init migrating kysely ....');
26
- const migrator = new Migrator({
27
- db: kysely,
28
- migrationTableSchema: 'core',
29
- migrationTableName: 'kysely_migration',
30
- migrationLockTableName: 'kysely_migration_lock',
31
- provider: new FileMigrationProvider({
32
- fs,
33
- path,
34
- // This needs to be an absolute path.
35
- migrationFolder: path.join(root, 'migrations'),
36
- }),
37
- });
38
-
39
- console.log('migrating kysely ....');
40
- const { error, results } = await migrator.migrateToLatest().catch(() => {
41
- console.error('failed to migrate');
42
- process.exit(1);
43
- });
44
-
45
- for (const it of results ?? []) {
46
- if (it.status === 'Success') {
47
- console.log(`migration "${it.migrationName}" was executed successfully`);
48
- } else if (it.status === 'Error') {
49
- console.error(`failed to execute migration "${it.migrationName}"`);
50
- }
51
- }
52
-
53
- if (error) {
54
- console.error('failed to migrate');
55
- console.error(error);
56
- process.exit(1);
57
- }
58
- }
59
-
60
- async function generateDump(root: string, dumpPath: string) {
61
- const dataExists = await exists(dumpPath).catch(() => false);
62
- if (!dataExists) {
63
- console.log('Creating initial dump');
64
- const main = new PGlite();
65
- await migrate(root, main).catch(async (err) => {
66
- console.error('failed to migrate initial dump');
67
- await Bun.write(
68
- '../logs/migrate-initial-dump.json',
69
- JSON.stringify(err as any, null, 2)
70
- );
71
- process.exit(1);
72
- });
73
- const { dialect } = new KyselyPGlite(main);
74
- const kysely = getKysely(dialect, {
75
- repositoryName: 'localTesting',
76
- log: undefined,
77
- });
78
- await migrateKysely(root, kysely).catch(() => {
79
- console.error('failed to migrate kysely');
80
- process.exit(1);
81
- });
82
- await codegen
83
- .generate({
84
- db: kysely,
85
- outFile: path.join(root, 'src/v1.generated.ts'),
86
- defaultSchemas: ['xxx'],
87
- camelCase: true,
88
- dialect: codegen.getDialect('postgres'),
89
- })
90
- .catch(() => {
91
- console.error('failed to generate kysely');
92
- process.exit(1);
93
- });
94
- const content = await main.dumpDataDir('none').catch(() => {
95
- console.error('failed to dump data');
96
- process.exit(1);
97
- });
98
- await Bun.write(dumpPath, content).catch(() => {
99
- console.error('failed to write dump');
100
- process.exit(1);
101
- });
102
- return content;
103
- }
104
- return Bun.file(dumpPath);
105
- }
106
-
107
- export async function createLocalKysely(root: string, dumpPath: string) {
108
- let isDestroyed = false;
109
- //const now = performance.now();
110
-
111
- const dump = await generateDump(root, dumpPath);
112
-
113
- const pglite = new PGlite({ loadDataDir: dump });
114
- // warm up
115
- await pglite.sql`SELECT 1`;
116
- const { dialect } = new KyselyPGlite(pglite);
117
- const kysely = getKysely(dialect, {
118
- repositoryName: 'localTesting',
119
- log: undefined,
120
- });
121
-
122
- const originalDestroy = kysely.destroy.bind(kysely);
123
- kysely.destroy = async () => {
124
- if (isDestroyed) return;
125
- isDestroyed = true;
126
- // const destroyTime = performance.now();
127
- await originalDestroy().catch(() => undefined);
128
- await pglite.close().catch(() => undefined);
129
- //console.log(
130
- // `Database destroyed: ${Math.round(performance.now() - destroyTime)}ms`
131
- //);
132
- };
133
-
134
- // console.log(`Database initialized: ${Math.round(performance.now() - now)}ms`);
135
-
136
- return kysely;
137
- }
@@ -1,48 +0,0 @@
1
- import { join } from 'node:path';
2
- import { PGlite } from '@electric-sql/pglite';
3
- import type { LogConfig } from 'kysely';
4
- import { PGliteDialect } from 'kysely-pglite-dialect';
5
- import { getKysely } from '../plugins';
6
- import { ensureDigestFunction, ensureGenRandomBytesFunction } from './digest';
7
-
8
- export const root = join(__dirname, '../..');
9
- export const dumpPath = join(root, '.data.tar');
10
-
11
- export type UseLocalKyselyOptions = {
12
- repositoryName?: string;
13
- log?: true | LogConfig;
14
- };
15
- export async function useLocalKysely(args: UseLocalKyselyOptions = {}) {
16
- let isDestroyed = false;
17
- // const now = performance.now();
18
-
19
- const dump = Bun.file(dumpPath);
20
-
21
- const pglite = new PGlite({
22
- loadDataDir: dump,
23
- });
24
- // warm up
25
- const dialect = new PGliteDialect(pglite);
26
- const kysely = getKysely(dialect, {
27
- repositoryName: args?.repositoryName ?? 'localTesting',
28
- log: args?.log,
29
- });
30
- await ensureDigestFunction(kysely);
31
- await ensureGenRandomBytesFunction(kysely);
32
-
33
- const originalDestroy = kysely.destroy.bind(kysely);
34
- kysely.destroy = async () => {
35
- if (isDestroyed) return;
36
- isDestroyed = true;
37
- //const destroyTime = performance.now();
38
- await originalDestroy().catch(() => undefined);
39
- await pglite.close().catch(() => undefined);
40
- //console.log(
41
- // `Database destroyed: ${Math.round(performance.now() - destroyTime)}ms`
42
- //);
43
- };
44
-
45
- // console.log(`Database initialized: ${Math.round(performance.now() - now)}ms`);
46
-
47
- return kysely;
48
- }
@@ -1,117 +0,0 @@
1
- import {
2
- type DeleteQueryNode,
3
- type InsertQueryNode,
4
- OperationNodeTransformer,
5
- type RootOperationNode,
6
- SelectModifierNode,
7
- type SelectQueryNode,
8
- sql,
9
- type UpdateQueryNode,
10
- } from 'kysely';
11
-
12
- function createCommentNode(repositoryName: string): SelectModifierNode {
13
- return SelectModifierNode.createWithExpression(
14
- sql.raw(`/* repository: ${repositoryName} */`).toOperationNode()
15
- );
16
- }
17
- /**
18
- * Transformer that adds a SQL comment to queries for pganalyze tracking.
19
- * The comment format is: repository: REPOSITORY_NAME
20
- *
21
- * This works by appending the comment to queries using the endModifiers property,
22
- * similar to how modifyEnd works.
23
- */
24
- export class CommentTransformer extends OperationNodeTransformer {
25
- constructor(readonly _repositoryName: string) {
26
- super();
27
- }
28
-
29
- transformRootNode(node: RootOperationNode): RootOperationNode {
30
- if (node.kind === 'SelectQueryNode') {
31
- return this.transformSelectQuery(node);
32
- }
33
- if (node.kind === 'InsertQueryNode') {
34
- return this.transformInsertQuery(node);
35
- }
36
- if (node.kind === 'UpdateQueryNode') {
37
- return this.transformUpdateQuery(node);
38
- }
39
- if (node.kind === 'DeleteQueryNode') {
40
- return this.transformDeleteQuery(node);
41
- }
42
- return node;
43
- }
44
-
45
- protected override transformSelectQuery(
46
- node: SelectQueryNode
47
- ): SelectQueryNode {
48
- const transformed = super.transformSelectQuery(node);
49
- return this.addCommentToSelectQuery(transformed);
50
- }
51
-
52
- protected override transformInsertQuery(
53
- node: InsertQueryNode
54
- ): InsertQueryNode {
55
- const transformed = super.transformInsertQuery(node);
56
- return this.addCommentToInsertQuery(transformed);
57
- }
58
-
59
- protected override transformUpdateQuery(
60
- node: UpdateQueryNode
61
- ): UpdateQueryNode {
62
- const transformed = super.transformUpdateQuery(node);
63
- return this.addCommentToUpdateQuery(transformed);
64
- }
65
-
66
- protected override transformDeleteQuery(
67
- node: DeleteQueryNode
68
- ): DeleteQueryNode {
69
- const transformed = super.transformDeleteQuery(node);
70
- return this.addCommentToDeleteQuery(transformed);
71
- }
72
-
73
- private addCommentToSelectQuery(node: SelectQueryNode): SelectQueryNode {
74
- // Append comment using endModifiers property, similar to modifyEnd
75
- const commentNode = createCommentNode(this._repositoryName);
76
-
77
- return {
78
- ...node,
79
- endModifiers: node.endModifiers
80
- ? [...node.endModifiers, commentNode]
81
- : [commentNode],
82
- };
83
- }
84
-
85
- private addCommentToInsertQuery(node: InsertQueryNode): InsertQueryNode {
86
- const commentNode = createCommentNode(this._repositoryName);
87
-
88
- return {
89
- ...node,
90
- endModifiers: node.endModifiers
91
- ? [...node.endModifiers, commentNode]
92
- : [commentNode],
93
- };
94
- }
95
-
96
- private addCommentToUpdateQuery(node: UpdateQueryNode): UpdateQueryNode {
97
- const commentNode = createCommentNode(this._repositoryName);
98
-
99
- return {
100
- ...node,
101
- endModifiers: node.endModifiers
102
- ? [...node.endModifiers, commentNode]
103
- : [commentNode],
104
- };
105
- }
106
-
107
- private addCommentToDeleteQuery(node: DeleteQueryNode): DeleteQueryNode {
108
- const commentNode = createCommentNode(this._repositoryName);
109
-
110
- return {
111
- ...node,
112
- endModifiers: node.endModifiers
113
- ? [...node.endModifiers, commentNode]
114
- : [commentNode],
115
- };
116
- }
117
- }
@@ -1,27 +0,0 @@
1
- import type {
2
- KyselyPlugin,
3
- PluginTransformQueryArgs,
4
- PluginTransformResultArgs,
5
- QueryResult,
6
- RootOperationNode,
7
- UnknownRow,
8
- } from 'kysely';
9
- import { CommentTransformer } from './comment-transformer';
10
-
11
- export class KyselyPganalyzeCommentPlugin implements KyselyPlugin {
12
- readonly #transformer: CommentTransformer;
13
-
14
- constructor(options: { repository: string }) {
15
- this.#transformer = new CommentTransformer(options.repository);
16
- }
17
-
18
- transformQuery(args: PluginTransformQueryArgs): RootOperationNode {
19
- return this.#transformer.transformRootNode(args.node);
20
- }
21
-
22
- async transformResult(
23
- args: PluginTransformResultArgs
24
- ): Promise<QueryResult<UnknownRow>> {
25
- return args.result;
26
- }
27
- }
package/src/plugins.ts DELETED
@@ -1,30 +0,0 @@
1
- import {
2
- type Dialect,
3
- Kysely as K,
4
- type KyselyPlugin,
5
- type LogConfig,
6
- } from 'kysely';
7
- import { CustomCasePlugin } from './camel-case';
8
- import { KyselyPganalyzeCommentPlugin } from './pganalyze-comment-plugin';
9
- import type { DB } from './v1.generated';
10
-
11
- export function getKysely(
12
- dialect: Dialect,
13
- options: { repositoryName?: string; log?: true | LogConfig }
14
- ) {
15
- const plugins: KyselyPlugin[] = [new CustomCasePlugin()];
16
-
17
- if (options?.repositoryName) {
18
- plugins.push(
19
- new KyselyPganalyzeCommentPlugin({
20
- repository: options.repositoryName,
21
- })
22
- );
23
- }
24
- const kysely = new K<DB>({
25
- dialect,
26
- plugins,
27
- log: options?.log === true ? ['query', 'error'] : options?.log,
28
- });
29
- return kysely;
30
- }
@@ -1 +0,0 @@
1
- export * from './listing';
@@ -1 +0,0 @@
1
- export * from './is-active-or-at-least-one-ownership-period-active';
@@ -1,29 +0,0 @@
1
- import type { DB, ExpressionBuilder, PublicListing } from '../../isomorphic';
2
-
3
- export function isActiveOrAtLeastOneOwnershipPeriodActive(
4
- eb: ExpressionBuilder<DB & { listing: PublicListing }, 'listing'>,
5
- today: string
6
- ) {
7
- return eb.or([
8
- eb('listing.status', '=', 'active'),
9
- eb.exists(
10
- eb
11
- .selectFrom('public.listingOwnershipPeriod as period')
12
- .select(eb.lit(1).as('one'))
13
- .whereRef('period.listingId', '=', 'listing.id')
14
- .where('period.setListingInactive', '=', false)
15
- .where((eb2) =>
16
- eb2.or([
17
- eb2.and([
18
- eb2('period.startAt', '<=', today),
19
- eb2('period.endAt', '>=', today),
20
- ]),
21
- eb2.and([
22
- eb2('period.startAt', '<=', today),
23
- eb2('period.endAt', 'is', null),
24
- ]),
25
- ])
26
- )
27
- ),
28
- ]);
29
- }
package/src/test/index.ts DELETED
@@ -1,64 +0,0 @@
1
- import { afterAll } from 'bun:test';
2
- import type { Kysely } from 'kysely';
3
- import { type UseLocalKyselyOptions, useLocalKysely } from '../local';
4
- import type { DB } from '../v1.generated';
5
-
6
- /**
7
- * @deprecated Use `useLocalKyselyResource` instead.
8
- */
9
- export async function useTestKysely(
10
- cleanupStrategy: 'afterAll',
11
- args?: UseLocalKyselyOptions
12
- ) {
13
- const kysely = await useLocalKysely(args);
14
-
15
- if (cleanupStrategy === 'afterAll')
16
- afterAll(async () => {
17
- await kysely.destroy();
18
- });
19
-
20
- return {
21
- kysely,
22
- };
23
- }
24
-
25
- export async function useTestAutoCleanupKysely(args?: UseLocalKyselyOptions) {
26
- const kysely = await useLocalKysely(args);
27
-
28
- afterAll(async () => {
29
- await kysely.destroy();
30
- });
31
-
32
- return kysely;
33
- }
34
-
35
- type ReturnType = Kysely<DB> & {
36
- [Symbol.dispose]: () => Promise<void>;
37
- };
38
-
39
- export async function useLocalKyselyResource(
40
- args?: UseLocalKyselyOptions
41
- ): Promise<ReturnType> {
42
- const kysely = await useLocalKysely(args);
43
-
44
- return new Proxy(
45
- {},
46
- {
47
- get(_target, prop) {
48
- // somehow not working if using Symbol.dispose
49
- if (prop === Symbol.asyncDispose) {
50
- return async () => {
51
- await kysely.destroy();
52
- };
53
- }
54
- if (prop === 'kysely') {
55
- return kysely;
56
- }
57
- if (typeof kysely[prop] === 'function') {
58
- return kysely[prop].bind(kysely);
59
- }
60
- return kysely[prop];
61
- },
62
- }
63
- ) as ReturnType;
64
- }