drizzle-databend 0.1.11 → 0.1.13
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/dist/columns.d.ts +25 -37
- package/dist/databend-core/alias.d.ts +1 -0
- package/dist/databend-core/columns/all.d.ts +35 -0
- package/dist/databend-core/columns/array.d.ts +16 -0
- package/dist/databend-core/columns/bigint.d.ts +24 -0
- package/dist/databend-core/columns/binary.d.ts +13 -0
- package/dist/databend-core/columns/bitmap.d.ts +13 -0
- package/dist/databend-core/columns/boolean.d.ts +13 -0
- package/dist/databend-core/columns/common.d.ts +45 -0
- package/dist/databend-core/columns/custom.d.ts +33 -0
- package/dist/databend-core/columns/date.d.ts +16 -0
- package/dist/databend-core/columns/decimal.d.ts +19 -0
- package/dist/databend-core/columns/double.d.ts +14 -0
- package/dist/databend-core/columns/float.d.ts +14 -0
- package/dist/databend-core/columns/index.d.ts +21 -0
- package/dist/databend-core/columns/integer.d.ts +13 -0
- package/dist/databend-core/columns/map.d.ts +17 -0
- package/dist/databend-core/columns/smallint.d.ts +13 -0
- package/dist/databend-core/columns/text.d.ts +13 -0
- package/dist/databend-core/columns/timestamp.d.ts +16 -0
- package/dist/databend-core/columns/tinyint.d.ts +13 -0
- package/dist/databend-core/columns/tuple.d.ts +16 -0
- package/dist/databend-core/columns/varchar.d.ts +17 -0
- package/dist/databend-core/columns/variant.d.ts +15 -0
- package/dist/databend-core/db.d.ts +31 -0
- package/dist/databend-core/dialect.d.ts +29 -0
- package/dist/databend-core/index.d.ts +18 -0
- package/dist/databend-core/indexes.d.ts +24 -0
- package/dist/databend-core/primary-keys.d.ts +20 -0
- package/dist/databend-core/query-builders/count.d.ts +18 -0
- package/dist/databend-core/query-builders/delete.d.ts +18 -0
- package/dist/databend-core/query-builders/index.d.ts +5 -0
- package/dist/databend-core/query-builders/insert.d.ts +27 -0
- package/dist/databend-core/query-builders/query-builder.d.ts +19 -0
- package/dist/databend-core/query-builders/query.d.ts +37 -0
- package/dist/databend-core/query-builders/raw.d.ts +17 -0
- package/dist/databend-core/query-builders/select.d.ts +66 -0
- package/dist/databend-core/query-builders/update.d.ts +27 -0
- package/dist/databend-core/schema.d.ts +12 -0
- package/dist/databend-core/session.d.ts +28 -0
- package/dist/databend-core/subquery.d.ts +1 -0
- package/dist/databend-core/table.d.ts +12 -0
- package/dist/databend-core/utils.d.ts +7 -0
- package/dist/databend-core/view-base.d.ts +5 -0
- package/dist/databend-core/view-common.d.ts +1 -0
- package/dist/databend-core/view.d.ts +30 -0
- package/dist/dialect.d.ts +1 -10
- package/dist/driver.d.ts +4 -4
- package/dist/index.d.ts +10 -0
- package/dist/index.mjs +2611 -205
- package/dist/session.d.ts +22 -19
- package/dist/sql/result-mapper.d.ts +2 -3
- package/dist/sql/selection.d.ts +2 -1
- package/package.json +4 -4
- package/src/columns.ts +8 -7
- package/src/databend-core/alias.ts +5 -0
- package/src/databend-core/columns/all.ts +38 -0
- package/src/databend-core/columns/array.ts +46 -0
- package/src/databend-core/columns/bigint.ts +52 -0
- package/src/databend-core/columns/binary.ts +27 -0
- package/src/databend-core/columns/bitmap.ts +27 -0
- package/src/databend-core/columns/boolean.ts +27 -0
- package/src/databend-core/columns/common.ts +97 -0
- package/src/databend-core/columns/custom.ts +86 -0
- package/src/databend-core/columns/date.ts +49 -0
- package/src/databend-core/columns/decimal.ts +44 -0
- package/src/databend-core/columns/double.ts +34 -0
- package/src/databend-core/columns/float.ts +31 -0
- package/src/databend-core/columns/index.ts +21 -0
- package/src/databend-core/columns/integer.ts +27 -0
- package/src/databend-core/columns/map.ts +49 -0
- package/src/databend-core/columns/smallint.ts +27 -0
- package/src/databend-core/columns/text.ts +27 -0
- package/src/databend-core/columns/timestamp.ts +51 -0
- package/src/databend-core/columns/tinyint.ts +27 -0
- package/src/databend-core/columns/tuple.ts +46 -0
- package/src/databend-core/columns/varchar.ts +35 -0
- package/src/databend-core/columns/variant.ts +45 -0
- package/src/databend-core/db.ts +153 -0
- package/src/databend-core/dialect.ts +725 -0
- package/src/databend-core/index.ts +18 -0
- package/src/databend-core/indexes.ts +67 -0
- package/src/databend-core/primary-keys.ts +48 -0
- package/src/databend-core/query-builders/count.ts +47 -0
- package/src/databend-core/query-builders/delete.ts +56 -0
- package/src/databend-core/query-builders/index.ts +5 -0
- package/src/databend-core/query-builders/insert.ts +105 -0
- package/src/databend-core/query-builders/query-builder.ts +77 -0
- package/src/databend-core/query-builders/query.ts +124 -0
- package/src/databend-core/query-builders/raw.ts +37 -0
- package/src/databend-core/query-builders/select.ts +412 -0
- package/src/databend-core/query-builders/update.ts +82 -0
- package/src/databend-core/schema.ts +29 -0
- package/src/databend-core/session.ts +85 -0
- package/src/databend-core/subquery.ts +1 -0
- package/src/databend-core/table.ts +67 -0
- package/src/databend-core/utils.ts +34 -0
- package/src/databend-core/view-base.ts +6 -0
- package/src/databend-core/view-common.ts +1 -0
- package/src/databend-core/view.ts +127 -0
- package/src/dialect.ts +3 -119
- package/src/driver.ts +6 -7
- package/src/index.ts +27 -0
- package/src/migrator.ts +1 -2
- package/src/session.ts +42 -57
- package/src/sql/result-mapper.ts +12 -54
- package/src/sql/selection.ts +2 -1
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { is } from 'drizzle-orm/entity';
|
|
2
|
+
import { Table } from 'drizzle-orm/table';
|
|
3
|
+
import { IndexBuilder } from './indexes.ts';
|
|
4
|
+
import { PrimaryKeyBuilder } from './primary-keys.ts';
|
|
5
|
+
import { DatabendTable } from './table.ts';
|
|
6
|
+
|
|
7
|
+
export function getTableConfig(table: any) {
|
|
8
|
+
const columns = Object.values(table[(Table as any).Symbol.Columns]);
|
|
9
|
+
const indexes: any[] = [];
|
|
10
|
+
const primaryKeys: any[] = [];
|
|
11
|
+
const name = table[(Table as any).Symbol.Name];
|
|
12
|
+
const schema = table[(Table as any).Symbol.Schema];
|
|
13
|
+
|
|
14
|
+
const extraConfigBuilder = table[(DatabendTable as any).Symbol.ExtraConfigBuilder];
|
|
15
|
+
if (extraConfigBuilder !== undefined) {
|
|
16
|
+
const extraConfig = extraConfigBuilder(table[(Table as any).Symbol.ExtraConfigColumns]);
|
|
17
|
+
const extraValues = Array.isArray(extraConfig) ? extraConfig.flat(1) : Object.values(extraConfig);
|
|
18
|
+
for (const builder of extraValues) {
|
|
19
|
+
if (is(builder, IndexBuilder)) {
|
|
20
|
+
indexes.push(builder.build(table));
|
|
21
|
+
} else if (is(builder, PrimaryKeyBuilder)) {
|
|
22
|
+
primaryKeys.push(builder.build(table));
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return {
|
|
28
|
+
columns,
|
|
29
|
+
indexes,
|
|
30
|
+
primaryKeys,
|
|
31
|
+
name,
|
|
32
|
+
schema,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const DatabendViewConfig = Symbol.for('drizzle:DatabendViewConfig');
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { entityKind, is } from 'drizzle-orm/entity';
|
|
2
|
+
import { SelectionProxyHandler } from 'drizzle-orm/selection-proxy';
|
|
3
|
+
import { getTableColumns } from 'drizzle-orm/utils';
|
|
4
|
+
import { QueryBuilder } from './query-builders/query-builder.ts';
|
|
5
|
+
import { databendTable } from './table.ts';
|
|
6
|
+
import { DatabendViewBase } from './view-base.ts';
|
|
7
|
+
import { DatabendViewConfig } from './view-common.ts';
|
|
8
|
+
|
|
9
|
+
class DefaultViewBuilderCore {
|
|
10
|
+
static readonly [entityKind]: string = 'DatabendDefaultViewBuilderCore';
|
|
11
|
+
config: any = {};
|
|
12
|
+
|
|
13
|
+
constructor(protected name: string, protected schema?: string) {}
|
|
14
|
+
|
|
15
|
+
with(config: any) {
|
|
16
|
+
this.config.with = config;
|
|
17
|
+
return this;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export class ViewBuilder extends DefaultViewBuilderCore {
|
|
22
|
+
static override readonly [entityKind]: string = 'DatabendViewBuilder';
|
|
23
|
+
|
|
24
|
+
as(qb: any) {
|
|
25
|
+
if (typeof qb === 'function') {
|
|
26
|
+
qb = qb(new QueryBuilder());
|
|
27
|
+
}
|
|
28
|
+
const selectionProxy = new SelectionProxyHandler({
|
|
29
|
+
alias: this.name,
|
|
30
|
+
sqlBehavior: 'error',
|
|
31
|
+
sqlAliasedBehavior: 'alias',
|
|
32
|
+
replaceOriginalName: true,
|
|
33
|
+
});
|
|
34
|
+
const aliasedSelection = new Proxy(qb.getSelectedFields(), selectionProxy);
|
|
35
|
+
return new Proxy(
|
|
36
|
+
new DatabendView({
|
|
37
|
+
databendConfig: this.config,
|
|
38
|
+
config: {
|
|
39
|
+
name: this.name,
|
|
40
|
+
schema: this.schema,
|
|
41
|
+
selectedFields: aliasedSelection,
|
|
42
|
+
query: qb.getSQL().inlineParams(),
|
|
43
|
+
},
|
|
44
|
+
}),
|
|
45
|
+
selectionProxy
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export class ManualViewBuilder extends DefaultViewBuilderCore {
|
|
51
|
+
static override readonly [entityKind]: string = 'DatabendManualViewBuilder';
|
|
52
|
+
columns: any;
|
|
53
|
+
|
|
54
|
+
constructor(name: string, columns: any, schema?: string) {
|
|
55
|
+
super(name, schema);
|
|
56
|
+
this.columns = getTableColumns(databendTable(name, columns));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
existing() {
|
|
60
|
+
return new Proxy(
|
|
61
|
+
new DatabendView({
|
|
62
|
+
databendConfig: undefined,
|
|
63
|
+
config: {
|
|
64
|
+
name: this.name,
|
|
65
|
+
schema: this.schema,
|
|
66
|
+
selectedFields: this.columns,
|
|
67
|
+
query: undefined,
|
|
68
|
+
},
|
|
69
|
+
}),
|
|
70
|
+
new SelectionProxyHandler({
|
|
71
|
+
alias: this.name,
|
|
72
|
+
sqlBehavior: 'error',
|
|
73
|
+
sqlAliasedBehavior: 'alias',
|
|
74
|
+
replaceOriginalName: true,
|
|
75
|
+
})
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
as(query: any) {
|
|
80
|
+
return new Proxy(
|
|
81
|
+
new DatabendView({
|
|
82
|
+
databendConfig: this.config,
|
|
83
|
+
config: {
|
|
84
|
+
name: this.name,
|
|
85
|
+
schema: this.schema,
|
|
86
|
+
selectedFields: this.columns,
|
|
87
|
+
query: query.inlineParams(),
|
|
88
|
+
},
|
|
89
|
+
}),
|
|
90
|
+
new SelectionProxyHandler({
|
|
91
|
+
alias: this.name,
|
|
92
|
+
sqlBehavior: 'error',
|
|
93
|
+
sqlAliasedBehavior: 'alias',
|
|
94
|
+
replaceOriginalName: true,
|
|
95
|
+
})
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export class DatabendView extends DatabendViewBase {
|
|
101
|
+
static override readonly [entityKind]: string = 'DatabendView';
|
|
102
|
+
[DatabendViewConfig]: any;
|
|
103
|
+
|
|
104
|
+
constructor({ databendConfig, config }: any) {
|
|
105
|
+
super(config);
|
|
106
|
+
if (databendConfig) {
|
|
107
|
+
this[DatabendViewConfig] = {
|
|
108
|
+
with: databendConfig.with,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function databendViewWithSchema(name: string, selection?: any, schema?: string) {
|
|
115
|
+
if (selection) {
|
|
116
|
+
return new ManualViewBuilder(name, selection, schema);
|
|
117
|
+
}
|
|
118
|
+
return new ViewBuilder(name, schema);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export function databendView(name: string, columns?: any) {
|
|
122
|
+
return databendViewWithSchema(name, columns, undefined);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export function isDatabendView(obj: unknown): obj is DatabendView {
|
|
126
|
+
return is(obj, DatabendView);
|
|
127
|
+
}
|
package/src/dialect.ts
CHANGED
|
@@ -1,119 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
sql,
|
|
5
|
-
} from 'drizzle-orm';
|
|
6
|
-
import { entityKind, is } from 'drizzle-orm/entity';
|
|
7
|
-
import type { MigrationConfig, MigrationMeta } from 'drizzle-orm/migrator';
|
|
8
|
-
import {
|
|
9
|
-
PgBigInt53,
|
|
10
|
-
PgBigInt64,
|
|
11
|
-
PgDate,
|
|
12
|
-
PgDateString,
|
|
13
|
-
PgDialect,
|
|
14
|
-
PgDoublePrecision,
|
|
15
|
-
PgInteger,
|
|
16
|
-
PgNumeric,
|
|
17
|
-
PgReal,
|
|
18
|
-
type PgSession,
|
|
19
|
-
PgSmallInt,
|
|
20
|
-
PgTime,
|
|
21
|
-
PgTimestamp,
|
|
22
|
-
PgTimestampString,
|
|
23
|
-
PgUUID,
|
|
24
|
-
} from 'drizzle-orm/pg-core';
|
|
25
|
-
|
|
26
|
-
export class DatabendDialect extends PgDialect {
|
|
27
|
-
static readonly [entityKind]: string = 'DatabendPgDialect';
|
|
28
|
-
|
|
29
|
-
// Databend does not support savepoints
|
|
30
|
-
areSavepointsUnsupported(): boolean {
|
|
31
|
-
return true;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
override async migrate(
|
|
35
|
-
migrations: MigrationMeta[],
|
|
36
|
-
session: PgSession,
|
|
37
|
-
config: MigrationConfig | string
|
|
38
|
-
): Promise<void> {
|
|
39
|
-
const migrationConfig: MigrationConfig =
|
|
40
|
-
typeof config === 'string' ? { migrationsFolder: config } : config;
|
|
41
|
-
|
|
42
|
-
const migrationsSchema = migrationConfig.migrationsSchema ?? 'default';
|
|
43
|
-
const migrationsTable =
|
|
44
|
-
migrationConfig.migrationsTable ?? '__drizzle_migrations';
|
|
45
|
-
|
|
46
|
-
// Databend has no sequences. Use explicit ID via COALESCE(MAX(id), 0) + 1.
|
|
47
|
-
const migrationTableCreate = sql`
|
|
48
|
-
CREATE TABLE IF NOT EXISTS ${sql.identifier(migrationsSchema)}.${sql.identifier(
|
|
49
|
-
migrationsTable
|
|
50
|
-
)} (
|
|
51
|
-
id INT NOT NULL,
|
|
52
|
-
hash VARCHAR NOT NULL,
|
|
53
|
-
created_at BIGINT
|
|
54
|
-
)
|
|
55
|
-
`;
|
|
56
|
-
|
|
57
|
-
await session.execute(migrationTableCreate);
|
|
58
|
-
|
|
59
|
-
const dbMigrations = await session.all<{
|
|
60
|
-
id: number;
|
|
61
|
-
hash: string;
|
|
62
|
-
created_at: string;
|
|
63
|
-
}>(
|
|
64
|
-
sql`SELECT id, hash, created_at FROM ${sql.identifier(
|
|
65
|
-
migrationsSchema
|
|
66
|
-
)}.${sql.identifier(migrationsTable)} ORDER BY created_at DESC LIMIT 1`
|
|
67
|
-
);
|
|
68
|
-
|
|
69
|
-
const lastDbMigration = dbMigrations[0];
|
|
70
|
-
|
|
71
|
-
await session.transaction(async (tx) => {
|
|
72
|
-
for await (const migration of migrations) {
|
|
73
|
-
if (
|
|
74
|
-
!lastDbMigration ||
|
|
75
|
-
Number(lastDbMigration.created_at) < migration.folderMillis
|
|
76
|
-
) {
|
|
77
|
-
for (const stmt of migration.sql) {
|
|
78
|
-
await tx.execute(sql.raw(stmt));
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
await tx.execute(
|
|
82
|
-
sql`INSERT INTO ${sql.identifier(
|
|
83
|
-
migrationsSchema
|
|
84
|
-
)}.${sql.identifier(migrationsTable)} (id, hash, created_at)
|
|
85
|
-
VALUES (
|
|
86
|
-
(SELECT COALESCE(MAX(id), 0) + 1 FROM ${sql.identifier(
|
|
87
|
-
migrationsSchema
|
|
88
|
-
)}.${sql.identifier(migrationsTable)}),
|
|
89
|
-
${migration.hash},
|
|
90
|
-
${migration.folderMillis}
|
|
91
|
-
)`
|
|
92
|
-
);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
override prepareTyping(
|
|
99
|
-
encoder: DriverValueEncoder<unknown, unknown>
|
|
100
|
-
): QueryTypingsValue {
|
|
101
|
-
if (
|
|
102
|
-
is(encoder, PgNumeric) || is(encoder, PgInteger) || is(encoder, PgSmallInt)
|
|
103
|
-
|| is(encoder, PgReal) || is(encoder, PgDoublePrecision)
|
|
104
|
-
|| is(encoder, PgBigInt53) || is(encoder, PgBigInt64)
|
|
105
|
-
) {
|
|
106
|
-
return 'decimal';
|
|
107
|
-
} else if (is(encoder, PgTime)) {
|
|
108
|
-
return 'time';
|
|
109
|
-
} else if (is(encoder, PgTimestamp) || is(encoder, PgTimestampString)) {
|
|
110
|
-
return 'timestamp';
|
|
111
|
-
} else if (is(encoder, PgDate) || is(encoder, PgDateString)) {
|
|
112
|
-
return 'date';
|
|
113
|
-
} else if (is(encoder, PgUUID)) {
|
|
114
|
-
return 'uuid';
|
|
115
|
-
} else {
|
|
116
|
-
return 'none';
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
}
|
|
1
|
+
// Re-export DatabendDialect from the core module.
|
|
2
|
+
// All dialect logic now lives in databend-core/dialect.ts.
|
|
3
|
+
export { DatabendDialect } from './databend-core/dialect.ts';
|
package/src/driver.ts
CHANGED
|
@@ -3,7 +3,6 @@ import { Client } from 'databend-driver';
|
|
|
3
3
|
import { entityKind } from 'drizzle-orm/entity';
|
|
4
4
|
import type { Logger } from 'drizzle-orm/logger';
|
|
5
5
|
import { DefaultLogger } from 'drizzle-orm/logger';
|
|
6
|
-
import { PgDatabase } from 'drizzle-orm/pg-core/db';
|
|
7
6
|
import {
|
|
8
7
|
createTableRelationsHelpers,
|
|
9
8
|
type ExtractTablesWithRelations,
|
|
@@ -13,14 +12,14 @@ import {
|
|
|
13
12
|
} from 'drizzle-orm/relations';
|
|
14
13
|
import type { DrizzleConfig } from 'drizzle-orm/utils';
|
|
15
14
|
import { closeClientConnection, isPool } from './client.ts';
|
|
16
|
-
import {
|
|
15
|
+
import { DatabendDatabase as DatabendDatabaseBase } from './databend-core/db.ts';
|
|
16
|
+
import { DatabendDialect } from './databend-core/dialect.ts';
|
|
17
17
|
import {
|
|
18
18
|
createDatabendConnectionPool,
|
|
19
19
|
type DatabendPoolConfig,
|
|
20
20
|
} from './pool.ts';
|
|
21
21
|
import type {
|
|
22
22
|
DatabendClientLike,
|
|
23
|
-
DatabendQueryResultHKT,
|
|
24
23
|
DatabendTransaction,
|
|
25
24
|
} from './session.ts';
|
|
26
25
|
import { DatabendSession } from './session.ts';
|
|
@@ -230,8 +229,8 @@ export class DatabendDatabase<
|
|
|
230
229
|
TFullSchema extends Record<string, unknown> = Record<string, never>,
|
|
231
230
|
TSchema extends TablesRelationalConfig =
|
|
232
231
|
ExtractTablesWithRelations<TFullSchema>,
|
|
233
|
-
> extends
|
|
234
|
-
static readonly [entityKind]: string = 'DatabendDatabase';
|
|
232
|
+
> extends DatabendDatabaseBase {
|
|
233
|
+
static override readonly [entityKind]: string = 'DatabendDatabase';
|
|
235
234
|
|
|
236
235
|
/** The underlying connection or pool */
|
|
237
236
|
readonly $client: DatabendClientLike;
|
|
@@ -240,8 +239,8 @@ export class DatabendDatabase<
|
|
|
240
239
|
readonly $databendClient?: Client;
|
|
241
240
|
|
|
242
241
|
constructor(
|
|
243
|
-
readonly dialect: DatabendDialect,
|
|
244
|
-
readonly session: DatabendSession<TFullSchema, TSchema>,
|
|
242
|
+
override readonly dialect: DatabendDialect,
|
|
243
|
+
override readonly session: DatabendSession<TFullSchema, TSchema>,
|
|
245
244
|
schema: RelationalSchemaConfig<TSchema> | undefined,
|
|
246
245
|
client: DatabendClientLike,
|
|
247
246
|
databendClient?: Client
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,32 @@
|
|
|
1
1
|
export * from './client.ts';
|
|
2
2
|
export * from './columns.ts';
|
|
3
|
+
export { alias } from './databend-core/alias.ts';
|
|
4
|
+
export { DatabendColumn, DatabendColumnBuilder } from './databend-core/columns/common.ts';
|
|
5
|
+
export {
|
|
6
|
+
bigint,
|
|
7
|
+
binary,
|
|
8
|
+
bitmap,
|
|
9
|
+
boolean,
|
|
10
|
+
date,
|
|
11
|
+
decimal,
|
|
12
|
+
doublePrecision,
|
|
13
|
+
float,
|
|
14
|
+
integer,
|
|
15
|
+
real,
|
|
16
|
+
smallint,
|
|
17
|
+
text,
|
|
18
|
+
timestamp,
|
|
19
|
+
tinyint,
|
|
20
|
+
varchar,
|
|
21
|
+
variant,
|
|
22
|
+
} from './databend-core/columns/index.ts';
|
|
23
|
+
export { DatabendDialect } from './databend-core/dialect.ts';
|
|
24
|
+
export { index, uniqueIndex } from './databend-core/indexes.ts';
|
|
25
|
+
export { primaryKey } from './databend-core/primary-keys.ts';
|
|
26
|
+
export { databendSchema } from './databend-core/schema.ts';
|
|
27
|
+
export { DatabendTable, databendTable, databendTableCreator } from './databend-core/table.ts';
|
|
28
|
+
export { getTableConfig } from './databend-core/utils.ts';
|
|
29
|
+
export { databendView } from './databend-core/view.ts';
|
|
3
30
|
export * from './driver.ts';
|
|
4
31
|
export * from './migrator.ts';
|
|
5
32
|
export * from './pool.ts';
|
package/src/migrator.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import type { MigrationConfig } from 'drizzle-orm/migrator';
|
|
2
2
|
import { readMigrationFiles } from 'drizzle-orm/migrator';
|
|
3
|
-
import type { PgSession } from 'drizzle-orm/pg-core/session';
|
|
4
3
|
import type { DatabendDatabase } from './driver.ts';
|
|
5
4
|
|
|
6
5
|
export type DatabendMigrationConfig = MigrationConfig | string;
|
|
@@ -16,7 +15,7 @@ export async function migrate<TSchema extends Record<string, unknown>>(
|
|
|
16
15
|
|
|
17
16
|
await db.dialect.migrate(
|
|
18
17
|
migrations,
|
|
19
|
-
db.session
|
|
18
|
+
db.session,
|
|
20
19
|
migrationConfig
|
|
21
20
|
);
|
|
22
21
|
}
|
package/src/session.ts
CHANGED
|
@@ -3,14 +3,6 @@ import { entityKind } from 'drizzle-orm/entity';
|
|
|
3
3
|
import { TransactionRollbackError } from 'drizzle-orm/errors';
|
|
4
4
|
import type { Logger } from 'drizzle-orm/logger';
|
|
5
5
|
import { NoopLogger } from 'drizzle-orm/logger';
|
|
6
|
-
import { PgTransaction } from 'drizzle-orm/pg-core';
|
|
7
|
-
import type { SelectedFieldsOrdered } from 'drizzle-orm/pg-core/query-builders/select.types';
|
|
8
|
-
import type {
|
|
9
|
-
PgQueryResultHKT,
|
|
10
|
-
PgTransactionConfig,
|
|
11
|
-
PreparedQueryConfig,
|
|
12
|
-
} from 'drizzle-orm/pg-core/session';
|
|
13
|
-
import { PgPreparedQuery, PgSession } from 'drizzle-orm/pg-core/session';
|
|
14
6
|
import type {
|
|
15
7
|
RelationalSchemaConfig,
|
|
16
8
|
TablesRelationalConfig,
|
|
@@ -27,25 +19,33 @@ import {
|
|
|
27
19
|
executeOnClient,
|
|
28
20
|
isPool,
|
|
29
21
|
} from './client.ts';
|
|
30
|
-
import type { DatabendDialect } from './dialect.ts';
|
|
22
|
+
import type { DatabendDialect } from './databend-core/dialect.ts';
|
|
23
|
+
import {
|
|
24
|
+
DatabendPreparedQuery as DatabendPreparedQueryBase,
|
|
25
|
+
DatabendSession as DatabendSessionBase,
|
|
26
|
+
DatabendTransaction as DatabendTransactionBase,
|
|
27
|
+
} from './databend-core/session.ts';
|
|
31
28
|
import { mapResultRow } from './sql/result-mapper.ts';
|
|
32
29
|
|
|
33
30
|
export type { DatabendClientLike, RowData } from './client.ts';
|
|
34
31
|
|
|
35
|
-
export
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
32
|
+
export interface DatabendQueryResultHKT {
|
|
33
|
+
readonly row: RowData;
|
|
34
|
+
type: GenericTableData<Assume<this['row'], RowData>>;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export class DatabendPreparedQuery extends DatabendPreparedQueryBase {
|
|
38
|
+
static override readonly [entityKind]: string = 'DatabendPreparedQuery';
|
|
39
39
|
|
|
40
40
|
constructor(
|
|
41
41
|
private client: DatabendClientLike,
|
|
42
42
|
private queryString: string,
|
|
43
43
|
private params: unknown[],
|
|
44
44
|
private logger: Logger,
|
|
45
|
-
private fields:
|
|
45
|
+
private fields: any[] | undefined,
|
|
46
46
|
private _isResponseInArrayMode: boolean,
|
|
47
47
|
private customResultMapper:
|
|
48
|
-
| ((rows: unknown[][]) =>
|
|
48
|
+
| ((rows: unknown[][]) => any)
|
|
49
49
|
| undefined,
|
|
50
50
|
private typings?: QueryTypingsValue[]
|
|
51
51
|
) {
|
|
@@ -54,7 +54,7 @@ export class DatabendPreparedQuery<
|
|
|
54
54
|
|
|
55
55
|
async execute(
|
|
56
56
|
placeholderValues: Record<string, unknown> | undefined = {}
|
|
57
|
-
): Promise<
|
|
57
|
+
): Promise<any> {
|
|
58
58
|
const params = fillPlaceholders(this.params, placeholderValues);
|
|
59
59
|
this.logger.logQuery(this.queryString, params);
|
|
60
60
|
|
|
@@ -70,24 +70,24 @@ export class DatabendPreparedQuery<
|
|
|
70
70
|
);
|
|
71
71
|
|
|
72
72
|
if (rows.length === 0) {
|
|
73
|
-
return []
|
|
73
|
+
return [];
|
|
74
74
|
}
|
|
75
75
|
|
|
76
76
|
return customResultMapper
|
|
77
77
|
? customResultMapper(rows)
|
|
78
78
|
: rows.map((row) =>
|
|
79
|
-
mapResultRow
|
|
79
|
+
mapResultRow(fields, row, joinsNotNullableMap)
|
|
80
80
|
);
|
|
81
81
|
}
|
|
82
82
|
|
|
83
83
|
const rows = await executeOnClient(this.client, this.queryString, params, typings);
|
|
84
84
|
|
|
85
|
-
return rows
|
|
85
|
+
return rows;
|
|
86
86
|
}
|
|
87
87
|
|
|
88
88
|
all(
|
|
89
89
|
placeholderValues: Record<string, unknown> | undefined = {}
|
|
90
|
-
): Promise<
|
|
90
|
+
): Promise<any> {
|
|
91
91
|
return this.execute(placeholderValues);
|
|
92
92
|
}
|
|
93
93
|
|
|
@@ -103,10 +103,10 @@ export interface DatabendSessionOptions {
|
|
|
103
103
|
export class DatabendSession<
|
|
104
104
|
TFullSchema extends Record<string, unknown> = Record<string, never>,
|
|
105
105
|
TSchema extends TablesRelationalConfig = Record<string, never>,
|
|
106
|
-
> extends
|
|
107
|
-
static readonly [entityKind]: string = 'DatabendSession';
|
|
106
|
+
> extends DatabendSessionBase {
|
|
107
|
+
static override readonly [entityKind]: string = 'DatabendSession';
|
|
108
108
|
|
|
109
|
-
|
|
109
|
+
override dialect: DatabendDialect;
|
|
110
110
|
private logger: Logger;
|
|
111
111
|
private rollbackOnly = false;
|
|
112
112
|
|
|
@@ -121,13 +121,13 @@ export class DatabendSession<
|
|
|
121
121
|
this.logger = options.logger ?? new NoopLogger();
|
|
122
122
|
}
|
|
123
123
|
|
|
124
|
-
prepareQuery
|
|
124
|
+
override prepareQuery(
|
|
125
125
|
query: Query,
|
|
126
|
-
fields:
|
|
126
|
+
fields: any[] | undefined,
|
|
127
127
|
name: string | undefined,
|
|
128
128
|
isResponseInArrayMode: boolean,
|
|
129
|
-
customResultMapper?: (rows: unknown[][]) =>
|
|
130
|
-
):
|
|
129
|
+
customResultMapper?: (rows: unknown[][]) => any
|
|
130
|
+
): DatabendPreparedQuery {
|
|
131
131
|
void name;
|
|
132
132
|
return new DatabendPreparedQuery(
|
|
133
133
|
this.client,
|
|
@@ -143,7 +143,7 @@ export class DatabendSession<
|
|
|
143
143
|
|
|
144
144
|
override async transaction<T>(
|
|
145
145
|
transaction: (tx: DatabendTransaction<TFullSchema, TSchema>) => Promise<T>,
|
|
146
|
-
config?:
|
|
146
|
+
config?: DatabendTransactionConfig
|
|
147
147
|
): Promise<T> {
|
|
148
148
|
let pinnedConnection: Connection | undefined;
|
|
149
149
|
let pool: DatabendConnectionPool | undefined;
|
|
@@ -203,19 +203,10 @@ export class DatabendSession<
|
|
|
203
203
|
}
|
|
204
204
|
}
|
|
205
205
|
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
dialect: DatabendDialect;
|
|
211
|
-
session: DatabendSession<TFullSchema, TSchema>;
|
|
212
|
-
};
|
|
213
|
-
|
|
214
|
-
type DatabendTransactionWithInternals<
|
|
215
|
-
TFullSchema extends Record<string, unknown> = Record<string, never>,
|
|
216
|
-
TSchema extends TablesRelationalConfig = Record<string, never>,
|
|
217
|
-
> = DatabendTransactionInternals<TFullSchema, TSchema> &
|
|
218
|
-
DatabendTransaction<TFullSchema, TSchema>;
|
|
206
|
+
export interface DatabendTransactionConfig {
|
|
207
|
+
isolationLevel?: string;
|
|
208
|
+
accessMode?: string;
|
|
209
|
+
}
|
|
219
210
|
|
|
220
211
|
const VALID_TRANSACTION_ISOLATION_LEVELS = new Set<string>([
|
|
221
212
|
'read uncommitted',
|
|
@@ -232,14 +223,16 @@ const VALID_TRANSACTION_ACCESS_MODES = new Set<string>([
|
|
|
232
223
|
export class DatabendTransaction<
|
|
233
224
|
TFullSchema extends Record<string, unknown>,
|
|
234
225
|
TSchema extends TablesRelationalConfig,
|
|
235
|
-
> extends
|
|
236
|
-
static readonly [entityKind]: string = 'DatabendTransaction';
|
|
226
|
+
> extends DatabendTransactionBase {
|
|
227
|
+
static override readonly [entityKind]: string = 'DatabendTransaction';
|
|
228
|
+
|
|
229
|
+
declare schema: any;
|
|
237
230
|
|
|
238
|
-
rollback(): never {
|
|
231
|
+
override rollback(): never {
|
|
239
232
|
throw new TransactionRollbackError();
|
|
240
233
|
}
|
|
241
234
|
|
|
242
|
-
getTransactionConfigSQL(config:
|
|
235
|
+
getTransactionConfigSQL(config: DatabendTransactionConfig): SQL {
|
|
243
236
|
if (
|
|
244
237
|
config.isolationLevel &&
|
|
245
238
|
!VALID_TRANSACTION_ISOLATION_LEVELS.has(config.isolationLevel)
|
|
@@ -272,9 +265,8 @@ export class DatabendTransaction<
|
|
|
272
265
|
return sql.raw(chunks.join(' '));
|
|
273
266
|
}
|
|
274
267
|
|
|
275
|
-
setTransaction(config:
|
|
276
|
-
|
|
277
|
-
return (this as unknown as Tx).session.execute(
|
|
268
|
+
setTransaction(config: DatabendTransactionConfig): Promise<void> {
|
|
269
|
+
return (this as any).session.execute(
|
|
278
270
|
sql`SET TRANSACTION ${this.getTransactionConfigSQL(config)}`
|
|
279
271
|
);
|
|
280
272
|
}
|
|
@@ -283,8 +275,7 @@ export class DatabendTransaction<
|
|
|
283
275
|
transaction: (tx: DatabendTransaction<TFullSchema, TSchema>) => Promise<T>
|
|
284
276
|
): Promise<T> {
|
|
285
277
|
// Databend does not support savepoints. Use rollback-only fallback.
|
|
286
|
-
|
|
287
|
-
const internals = this as unknown as Tx;
|
|
278
|
+
const internals = this as any;
|
|
288
279
|
|
|
289
280
|
const nestedTx = new DatabendTransaction<TFullSchema, TSchema>(
|
|
290
281
|
internals.dialect,
|
|
@@ -294,9 +285,7 @@ export class DatabendTransaction<
|
|
|
294
285
|
);
|
|
295
286
|
|
|
296
287
|
return transaction(nestedTx).catch((error) => {
|
|
297
|
-
(
|
|
298
|
-
internals.session as DatabendSession<TFullSchema, TSchema>
|
|
299
|
-
).markRollbackOnly();
|
|
288
|
+
(internals.session as DatabendSession<TFullSchema, TSchema>).markRollbackOnly();
|
|
300
289
|
throw error;
|
|
301
290
|
});
|
|
302
291
|
}
|
|
@@ -305,7 +294,3 @@ export class DatabendTransaction<
|
|
|
305
294
|
export type GenericRowData<T extends RowData = RowData> = T;
|
|
306
295
|
|
|
307
296
|
export type GenericTableData<T = RowData> = T[];
|
|
308
|
-
|
|
309
|
-
export interface DatabendQueryResultHKT extends PgQueryResultHKT {
|
|
310
|
-
type: GenericTableData<Assume<this['row'], RowData>>;
|
|
311
|
-
}
|