@vrplatform/kysely 1.3.20 → 1.3.21
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.
- package/build/.data.tar +0 -0
- package/package.json +5 -6
- package/src/camel-case/index.ts +0 -68
- package/src/error.ts +0 -6
- package/src/index.ts +0 -145
- package/src/isomorphic.ts +0 -33
- package/src/local/digest.ts +0 -44
- package/src/local/generate.ts +0 -137
- package/src/local/index.ts +0 -48
- package/src/pganalyze-comment-plugin/comment-transformer.ts +0 -117
- package/src/pganalyze-comment-plugin/index.ts +0 -27
- package/src/plugins.ts +0 -30
- package/src/query/index.ts +0 -1
- package/src/query/listing/index.ts +0 -1
- package/src/query/listing/is-active-or-at-least-one-ownership-period-active.ts +0 -29
- package/src/test/index.ts +0 -64
- package/src/v1.generated.ts +0 -1917
package/build/.data.tar
CHANGED
|
Binary file
|
package/package.json
CHANGED
|
@@ -15,12 +15,12 @@
|
|
|
15
15
|
"require": "./build/main/local/index.js"
|
|
16
16
|
},
|
|
17
17
|
"./test": {
|
|
18
|
-
"import": "./build/module/test.js",
|
|
19
|
-
"require": "./build/main/test.js"
|
|
18
|
+
"import": "./build/module/test/index.js",
|
|
19
|
+
"require": "./build/main/test/index.js"
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
22
|
},
|
|
23
|
-
"version": "1.3.
|
|
23
|
+
"version": "1.3.21",
|
|
24
24
|
"description": "",
|
|
25
25
|
"main": "build/main/index.js",
|
|
26
26
|
"module": "build/module/index.js",
|
|
@@ -34,8 +34,8 @@
|
|
|
34
34
|
"require": "./build/main/local/index.js"
|
|
35
35
|
},
|
|
36
36
|
"./test": {
|
|
37
|
-
"import": "./build/module/test.js",
|
|
38
|
-
"require": "./build/main/test.js"
|
|
37
|
+
"import": "./build/module/test/index.js",
|
|
38
|
+
"require": "./build/main/test/index.js"
|
|
39
39
|
}
|
|
40
40
|
},
|
|
41
41
|
"scripts": {
|
|
@@ -78,7 +78,6 @@
|
|
|
78
78
|
"build/main",
|
|
79
79
|
"build/module",
|
|
80
80
|
"build/.data.tar",
|
|
81
|
-
"src",
|
|
82
81
|
"!**/*.spec.*",
|
|
83
82
|
"LICENSE"
|
|
84
83
|
],
|
package/src/camel-case/index.ts
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
type AliasNode,
|
|
3
|
-
CamelCasePlugin,
|
|
4
|
-
type IdentifierNode,
|
|
5
|
-
type RawNode,
|
|
6
|
-
type SelectQueryNode,
|
|
7
|
-
} from 'kysely';
|
|
8
|
-
import { KyselyError } from '../error';
|
|
9
|
-
|
|
10
|
-
export class CustomCasePlugin extends CamelCasePlugin {
|
|
11
|
-
// _excludedColumns: Record<string, boolean> = {};
|
|
12
|
-
constructor() {
|
|
13
|
-
super({
|
|
14
|
-
maintainNestedObjectKeys: true,
|
|
15
|
-
});
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
transformResult(args) {
|
|
19
|
-
return super.transformResult(args);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
transformQuery(args) {
|
|
23
|
-
this.verifyAlias(args.node);
|
|
24
|
-
return super.transformQuery(args);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
verifyAlias(node: SelectQueryNode) {
|
|
28
|
-
if (!node?.selections) return;
|
|
29
|
-
|
|
30
|
-
for (const x of node.selections ?? []) {
|
|
31
|
-
const selection = x.selection as AliasNode;
|
|
32
|
-
if (!selection.alias) continue;
|
|
33
|
-
const alias = selection.alias as IdentifierNode;
|
|
34
|
-
|
|
35
|
-
// alias can be a raw node (jsonArrayFrom)
|
|
36
|
-
if (selection.node.kind === 'RawNode') {
|
|
37
|
-
const rawNode = selection.node as RawNode;
|
|
38
|
-
for (const x of rawNode.parameters ?? []) {
|
|
39
|
-
if (x.kind === 'SelectQueryNode') {
|
|
40
|
-
const selectQueryNode = x as SelectQueryNode;
|
|
41
|
-
this.verifyAlias(selectQueryNode);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
if (!alias.name.includes('_')) continue;
|
|
47
|
-
|
|
48
|
-
throw new KyselyError(`Alias ${alias.name} contains underscore`);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
protected mapRow(row) {
|
|
53
|
-
if (typeof row !== 'object' || !row) return row;
|
|
54
|
-
|
|
55
|
-
const newRow: Record<string, any> = {};
|
|
56
|
-
// we camel case on first layer, and nested ONLY if in an array (jsonArrayFrom), NOT if it's an object (jsonb)
|
|
57
|
-
for (const key of Object.keys(row)) {
|
|
58
|
-
let value = row[key];
|
|
59
|
-
const newKey = super.camelCase(key);
|
|
60
|
-
// array CAN be non object, so we need to check the first element
|
|
61
|
-
if (Array.isArray(value)) {
|
|
62
|
-
value = value.map((it) => this.mapRow(it));
|
|
63
|
-
}
|
|
64
|
-
newRow[newKey] = value;
|
|
65
|
-
}
|
|
66
|
-
return newRow;
|
|
67
|
-
}
|
|
68
|
-
}
|
package/src/error.ts
DELETED
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
|
-
}
|
package/src/local/digest.ts
DELETED
|
@@ -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
|
-
}
|
package/src/local/generate.ts
DELETED
|
@@ -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
|
-
}
|
package/src/local/index.ts
DELETED
|
@@ -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
|
-
}
|
package/src/query/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './listing';
|