uql-orm 0.98.0 → 0.100.0
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/LICENSE.md +22 -0
- package/README.md +1 -1
- package/dist/browser/uql-browser.min.js +2 -2
- package/dist/browser/uql-browser.min.js.map +5 -5
- package/dist/bunSql/bunSql.util.js +2 -2
- package/dist/bunSql/bunSqlQuerier.js +0 -1
- package/dist/bunSql/bunSqlQuerierPool.js +1 -1
- package/dist/cockroachdb/cockroachDialect.js +1 -0
- package/dist/context/context.browser.d.ts +5 -0
- package/dist/context/context.browser.js +21 -11
- package/dist/context/context.d.ts +5 -0
- package/dist/context/context.js +8 -3
- package/dist/dialect/abstractDialect.js +4 -2
- package/dist/dialect/abstractSqlDialect.d.ts +5 -5
- package/dist/dialect/abstractSqlDialect.js +26 -18
- package/dist/dialect/aliases.d.ts +0 -2
- package/dist/dialect/aliases.js +0 -2
- package/dist/dialect/hydrateColumn.d.ts +3 -3
- package/dist/dialect/hydrateColumn.js +1 -0
- package/dist/dialect/index.js +0 -2
- package/dist/dialect/jsonSql.d.ts +2 -3
- package/dist/dialect/jsonSql.js +4 -6
- package/dist/dialect/mysqlLikeSqlDialect.d.ts +1 -1
- package/dist/dialect/mysqlLikeSqlDialect.js +5 -3
- package/dist/dialect/operators.d.ts +2 -3
- package/dist/dialect/operators.js +3 -4
- package/dist/dialect/pgLikeSqlDialect.js +12 -12
- package/dist/entity/decorator/members.d.ts +4 -12
- package/dist/entity/metadata/definition.js +8 -5
- package/dist/http/handler.d.ts +6 -6
- package/dist/http/handler.js +20 -5
- package/dist/index.d.ts +6 -5
- package/dist/index.js +6 -5
- package/dist/{maria → mariadb}/mariadbQuerier.js +1 -1
- package/dist/migrate/builder/columnBuilder.d.ts +3 -3
- package/dist/migrate/builder/columnBuilder.js +1 -1
- package/dist/migrate/builder/index.d.ts +0 -3
- package/dist/migrate/builder/index.js +0 -3
- package/dist/migrate/builder/migrationBuilder.d.ts +6 -6
- package/dist/migrate/builder/migrationBuilder.js +7 -7
- package/dist/migrate/builder/tableBuilder.d.ts +25 -25
- package/dist/migrate/builder/tableBuilder.js +5 -5
- package/dist/migrate/builder/types.d.ts +37 -37
- package/dist/migrate/cli-config.d.ts +9 -0
- package/dist/migrate/cli-config.js +14 -4
- package/dist/migrate/cli.js +9 -18
- package/dist/migrate/codegen/entityCodeGenerator.js +1 -1
- package/dist/migrate/codegen/migrationFile.d.ts +7 -7
- package/dist/migrate/codegen/migrationFile.js +11 -12
- package/dist/migrate/ddl/indexCapabilities.d.ts +2 -0
- package/dist/migrate/ddl/indexCapabilities.js +6 -0
- package/dist/migrate/ddl/indexDdl.d.ts +8 -0
- package/dist/migrate/ddl/indexDdl.js +22 -6
- package/dist/migrate/generator/definitionToNode.d.ts +1 -1
- package/dist/migrate/generator/definitionToNode.js +1 -1
- package/dist/migrate/index.d.ts +1 -3
- package/dist/migrate/index.js +0 -3
- package/dist/migrate/introspection/abstractSqlSchemaIntrospector.d.ts +11 -15
- package/dist/migrate/introspection/abstractSqlSchemaIntrospector.js +13 -15
- package/dist/migrate/introspection/mssqlIntrospector.d.ts +5 -5
- package/dist/migrate/introspection/mssqlIntrospector.js +24 -28
- package/dist/migrate/introspection/mysqlIntrospector.d.ts +5 -5
- package/dist/migrate/introspection/mysqlIntrospector.js +31 -37
- package/dist/migrate/introspection/postgresIntrospector.d.ts +13 -13
- package/dist/migrate/introspection/postgresIntrospector.js +35 -39
- package/dist/migrate/introspection/registry.js +1 -1
- package/dist/migrate/introspection/sqliteIntrospector.d.ts +6 -4
- package/dist/migrate/introspection/sqliteIntrospector.js +15 -12
- package/dist/migrate/migrationLock.d.ts +38 -0
- package/dist/migrate/migrationLock.js +103 -0
- package/dist/migrate/migrationTarget.d.ts +6 -2
- package/dist/migrate/migrationTarget.js +30 -14
- package/dist/migrate/migrator.d.ts +47 -37
- package/dist/migrate/migrator.js +154 -131
- package/dist/migrate/schemaGenerator.d.ts +3 -1
- package/dist/migrate/schemaGenerator.js +14 -5
- package/dist/migrate/storage/databaseStorage.d.ts +28 -20
- package/dist/migrate/storage/databaseStorage.js +36 -54
- package/dist/migrate/storage/index.d.ts +0 -1
- package/dist/migrate/storage/index.js +0 -1
- package/dist/{mongo → mongodb}/index.d.ts +0 -1
- package/dist/{mongo → mongodb}/index.js +0 -1
- package/dist/{mongo → mongodb}/mongoCommand.d.ts +17 -1
- package/dist/{mongo → mongodb}/mongoCommand.js +15 -2
- package/dist/{mongo → mongodb}/mongoDialect.js +8 -12
- package/dist/{mongo → mongodb}/mongoIntrospector.js +12 -3
- package/dist/{mongo → mongodb}/mongoMigrationStorage.d.ts +5 -5
- package/dist/{mongo → mongodb}/mongoMigrationStorage.js +8 -12
- package/dist/{mongo → mongodb}/mongoQuerier.d.ts +3 -1
- package/dist/{mongo → mongodb}/mongoSchemaGenerator.d.ts +11 -1
- package/dist/{mongo → mongodb}/mongoSchemaGenerator.js +56 -11
- package/dist/{mongo → mongodb}/mongodbQuerier.d.ts +7 -18
- package/dist/{mongo → mongodb}/mongodbQuerier.js +44 -78
- package/dist/mssql/mssqlDialect.d.ts +6 -4
- package/dist/mssql/mssqlDialect.js +20 -10
- package/dist/mssql/mssqlQuerier.d.ts +2 -2
- package/dist/mssql/mssqlQuerier.js +3 -3
- package/dist/mssql/mssqlQuerierPool.d.ts +1 -1
- package/dist/mssql/mssqlQuerierPool.js +2 -2
- package/dist/mysql/mysql2Querier.js +1 -5
- package/dist/namingStrategy/defaultNamingStrategy.d.ts +0 -1
- package/dist/namingStrategy/defaultNamingStrategy.js +0 -3
- package/dist/pglite/pgliteDialect.d.ts +3 -0
- package/dist/pglite/pgliteDialect.js +3 -1
- package/dist/pglite/pgliteQuerier.d.ts +2 -0
- package/dist/pglite/pgliteQuerier.js +4 -0
- package/dist/postgres/postgresDialect.d.ts +3 -4
- package/dist/postgres/postgresDialect.js +5 -6
- package/dist/querier/abstractQuerier.d.ts +119 -36
- package/dist/querier/abstractQuerier.js +465 -161
- package/dist/querier/abstractQuerierPool.d.ts +5 -6
- package/dist/querier/abstractQuerierPool.js +20 -6
- package/dist/querier/abstractSqlQuerier.d.ts +15 -10
- package/dist/querier/abstractSqlQuerier.js +54 -47
- package/dist/querier/abstractSqlQuerierPool.d.ts +5 -3
- package/dist/querier/abstractSqlQuerierPool.js +7 -4
- package/dist/querier/transaction.d.ts +46 -0
- package/dist/querier/transaction.js +56 -0
- package/dist/schema/canonicalType.js +2 -2
- package/dist/schema/schemaASTDiffer.d.ts +11 -3
- package/dist/schema/schemaASTDiffer.js +20 -8
- package/dist/sqlite/nodeSqliteQuerierPool.d.ts +1 -1
- package/dist/sqlite/nodeSqliteQuerierPool.js +1 -1
- package/dist/sqlite/sqliteDialect.d.ts +1 -1
- package/dist/sqlite/sqliteDialect.js +3 -1
- package/dist/sqlite/sqliteQuerier.d.ts +2 -0
- package/dist/sqlite/sqliteQuerier.js +4 -0
- package/dist/sqlite/sqliteQuerierPool.d.ts +4 -4
- package/dist/sqlite/sqliteQuerierPool.js +1 -1
- package/dist/type/dialect.d.ts +16 -2
- package/dist/type/entity.d.ts +32 -32
- package/dist/type/migration.d.ts +10 -27
- package/dist/type/namingStrategy.d.ts +0 -4
- package/dist/type/querier.d.ts +18 -10
- package/dist/type/query.d.ts +7 -22
- package/dist/type/queryRaw.d.ts +11 -6
- package/dist/type/queryRaw.js +2 -3
- package/dist/type/universalQuerier.d.ts +26 -28
- package/dist/type/utility.d.ts +7 -2
- package/dist/util/dialect.util.d.ts +2 -2
- package/dist/util/dialect.util.js +4 -1
- package/dist/util/field.util.d.ts +12 -0
- package/dist/util/field.util.js +24 -0
- package/dist/util/hook.util.d.ts +3 -2
- package/dist/util/query.util.d.ts +5 -0
- package/dist/util/query.util.js +34 -1
- package/dist/util/raw.d.ts +8 -2
- package/dist/util/raw.js +22 -0
- package/dist/util/relationQuery.util.d.ts +2 -0
- package/dist/util/relationQuery.util.js +4 -0
- package/dist/util/sql.util.d.ts +0 -5
- package/dist/util/sql.util.js +3 -9
- package/dist/util/uqlError.d.ts +0 -6
- package/dist/util/uqlError.js +0 -5
- package/package.json +20 -26
- package/skills/uql-orm/SKILL.md +13 -10
- package/dist/migrate/storage/jsonStorage.d.ts +0 -14
- package/dist/migrate/storage/jsonStorage.js +0 -33
- /package/dist/{maria → mariadb}/index.d.ts +0 -0
- /package/dist/{maria → mariadb}/index.js +0 -0
- /package/dist/{maria → mariadb}/mariaDialect.d.ts +0 -0
- /package/dist/{maria → mariadb}/mariaDialect.js +0 -0
- /package/dist/{maria → mariadb}/mariadbQuerier.d.ts +0 -0
- /package/dist/{maria → mariadb}/mariadbQuerierPool.d.ts +0 -0
- /package/dist/{maria → mariadb}/mariadbQuerierPool.js +0 -0
- /package/dist/{mongo → mongodb}/mongoDialect.d.ts +0 -0
- /package/dist/{mongo → mongodb}/mongoIntrospector.d.ts +0 -0
- /package/dist/{mongo → mongodb}/mongoQuerier.js +0 -0
- /package/dist/{mongo → mongodb}/mongodbQuerierPool.d.ts +0 -0
- /package/dist/{mongo → mongodb}/mongodbQuerierPool.js +0 -0
- /package/dist/{mongo → mongodb}/textLanguage.d.ts +0 -0
- /package/dist/{mongo → mongodb}/textLanguage.js +0 -0
- /package/dist/{mongo → mongodb}/vectorDistance.d.ts +0 -0
- /package/dist/{mongo → mongodb}/vectorDistance.js +0 -0
|
@@ -15,7 +15,7 @@ function asksForNoRows(q) {
|
|
|
15
15
|
/**
|
|
16
16
|
* MongoDB runs no trigger within a write (Atlas Database Triggers fire after the commit), so a write to
|
|
17
17
|
* an entity declaring one - a stamp included - would skip it silently. Refused instead, as a query naming
|
|
18
|
-
* SQL is.
|
|
18
|
+
* SQL is.
|
|
19
19
|
*/
|
|
20
20
|
function refuseTriggers(entity) {
|
|
21
21
|
if (hasTriggers(getMeta(entity))) {
|
|
@@ -27,7 +27,11 @@ export class MongodbQuerier extends AbstractQuerier {
|
|
|
27
27
|
dialect;
|
|
28
28
|
conn;
|
|
29
29
|
extra;
|
|
30
|
-
session;
|
|
30
|
+
#session;
|
|
31
|
+
/** The open transaction's session, for a raw call through `db` to run inside it: `{ session: querier.session }`. */
|
|
32
|
+
get session() {
|
|
33
|
+
return this.#session;
|
|
34
|
+
}
|
|
31
35
|
constructor(dialect, conn, extra) {
|
|
32
36
|
super(extra);
|
|
33
37
|
this.dialect = dialect;
|
|
@@ -36,7 +40,7 @@ export class MongodbQuerier extends AbstractQuerier {
|
|
|
36
40
|
}
|
|
37
41
|
async execute(task) {
|
|
38
42
|
return this.serialize(async () => {
|
|
39
|
-
return task(this
|
|
43
|
+
return task(this.#session);
|
|
40
44
|
});
|
|
41
45
|
}
|
|
42
46
|
async internalFindMany(entity, q, opts) {
|
|
@@ -68,7 +72,7 @@ export class MongodbQuerier extends AbstractQuerier {
|
|
|
68
72
|
? this.buildVectorPipeline(entity, q, vectorSort, opts)
|
|
69
73
|
: this.readsThroughPipeline(entity, q) && this.dialect.aggregationPipeline(entity, q, opts);
|
|
70
74
|
return pipeline
|
|
71
|
-
? this.collection(entity).aggregate(pipeline, { session: this
|
|
75
|
+
? this.collection(entity).aggregate(pipeline, { session: this.#session })
|
|
72
76
|
: this.buildFindCursor(entity, q, opts);
|
|
73
77
|
}
|
|
74
78
|
/**
|
|
@@ -91,7 +95,7 @@ export class MongodbQuerier extends AbstractQuerier {
|
|
|
91
95
|
}
|
|
92
96
|
/** Build a MongoDB FindCursor with filter, projection, sort, skip, and limit from the query. */
|
|
93
97
|
buildFindCursor(entity, q, opts) {
|
|
94
|
-
const cursor = this.collection(entity).find({}, { session: this
|
|
98
|
+
const cursor = this.collection(entity).find({}, { session: this.#session });
|
|
95
99
|
const filter = this.dialect.where(entity, q.$where, opts);
|
|
96
100
|
if (hasKeys(filter)) {
|
|
97
101
|
cursor.filter(filter);
|
|
@@ -201,17 +205,19 @@ export class MongodbQuerier extends AbstractQuerier {
|
|
|
201
205
|
});
|
|
202
206
|
}
|
|
203
207
|
/**
|
|
204
|
-
*
|
|
205
|
-
*
|
|
208
|
+
* What an upsert writes, as SQL's does: a found document takes the payload and the `onUpdate` fills, and only
|
|
209
|
+
* an inserted one the `onInsert` fills and the key, which is immutable, so in `$set` it would refuse every match.
|
|
206
210
|
*/
|
|
207
|
-
upsertUpdate(
|
|
208
|
-
const
|
|
211
|
+
upsertUpdate(meta, payload) {
|
|
212
|
+
const updated = this.dialect.getPersistable(meta, clone(payload), 'onUpdate');
|
|
213
|
+
const inserted = this.dialect.getPersistable(meta, clone(payload), 'onInsert');
|
|
214
|
+
const insertOnly = Object.fromEntries(Object.entries(inserted).filter(([column]) => !(column in updated)));
|
|
209
215
|
const update = {};
|
|
210
|
-
if (hasKeys(
|
|
211
|
-
update['$set'] =
|
|
216
|
+
if (hasKeys(updated)) {
|
|
217
|
+
update['$set'] = updated;
|
|
212
218
|
}
|
|
213
|
-
if (
|
|
214
|
-
update['$setOnInsert'] =
|
|
219
|
+
if (hasKeys(insertOnly)) {
|
|
220
|
+
update['$setOnInsert'] = insertOnly;
|
|
215
221
|
}
|
|
216
222
|
return update;
|
|
217
223
|
}
|
|
@@ -226,77 +232,37 @@ export class MongodbQuerier extends AbstractQuerier {
|
|
|
226
232
|
async upsertWithUpdate(entity, conflictPaths, row, update) {
|
|
227
233
|
const persistable = this.dialect.getPersistable(getMeta(entity), clone(row), 'onInsert');
|
|
228
234
|
const res = await this.execute((session) => this.collection(entity).findOneAndUpdate(this.buildConflictFilter(entity, conflictPaths, row), { $setOnInsert: persistable }, { upsert: true, returnDocument: 'after', includeResultMetadata: true, session }));
|
|
229
|
-
|
|
230
|
-
//
|
|
231
|
-
|
|
232
|
-
if (updates) {
|
|
235
|
+
// `updatedExisting` is false where the document was inserted; an empty `update` leaves a found one
|
|
236
|
+
// as it is, as `DO NOTHING` does.
|
|
237
|
+
if (res.lastErrorObject?.['updatedExisting'] && hasKeys(update)) {
|
|
233
238
|
await this.internalUpdateMany(entity, { $where: whereEach(getKeys(conflictPaths), (key) => row[key]) }, update);
|
|
234
239
|
}
|
|
235
|
-
return
|
|
236
|
-
}
|
|
237
|
-
/** The id and whether it was created, read off the document a `findOneAndUpdate` upsert wrote. */
|
|
238
|
-
upserted(res) {
|
|
239
|
-
const id = this.dialect.fromWireId(res.value?._id);
|
|
240
|
-
// `updatedExisting` is false when a new document was inserted (upserted).
|
|
241
|
-
return { id, created: res.lastErrorObject?.['updatedExisting'] === false };
|
|
242
|
-
}
|
|
243
|
-
async internalUpsertOne(entity, conflictPaths, payload, update) {
|
|
244
|
-
refuseTriggers(entity);
|
|
245
|
-
if (update) {
|
|
246
|
-
const { id, created, changes } = await this.upsertWithUpdate(entity, conflictPaths, payload, update);
|
|
247
|
-
return { ids: [id], changes, created };
|
|
248
|
-
}
|
|
249
|
-
return this.timed('upsertOne', undefined, async () => {
|
|
250
|
-
payload = clone(payload);
|
|
251
|
-
const meta = getMeta(entity);
|
|
252
|
-
const persistable = this.dialect.getPersistable(meta, payload, 'onInsert');
|
|
253
|
-
const filter = this.buildConflictFilter(entity, conflictPaths, payload);
|
|
254
|
-
const update = this.upsertUpdate(persistable);
|
|
255
|
-
const res = await this.execute((session) => this.collection(entity).findOneAndUpdate(filter, update, {
|
|
256
|
-
upsert: true,
|
|
257
|
-
returnDocument: 'after',
|
|
258
|
-
includeResultMetadata: true,
|
|
259
|
-
session,
|
|
260
|
-
}));
|
|
261
|
-
// Read off the document as written, which carries its `_id` on either branch.
|
|
262
|
-
const { id, created } = this.upserted(res);
|
|
263
|
-
return { ids: [id], changes: 1, created };
|
|
264
|
-
});
|
|
240
|
+
return this.dialect.fromWireId(res.value?._id);
|
|
265
241
|
}
|
|
266
242
|
async internalUpsertMany(entity, conflictPaths, payload, update) {
|
|
267
243
|
refuseTriggers(entity);
|
|
268
244
|
if (update) {
|
|
269
245
|
const ids = [];
|
|
270
|
-
let changes = 0;
|
|
271
246
|
for (const row of payload) {
|
|
272
|
-
|
|
273
|
-
ids.push(written.id);
|
|
274
|
-
changes += written.changes;
|
|
247
|
+
ids.push(await this.upsertWithUpdate(entity, conflictPaths, row, update));
|
|
275
248
|
}
|
|
276
|
-
return
|
|
249
|
+
return ids;
|
|
277
250
|
}
|
|
278
251
|
return this.timed('upsertMany', undefined, async () => {
|
|
279
|
-
if (!payload
|
|
280
|
-
return
|
|
252
|
+
if (!payload.length) {
|
|
253
|
+
return [];
|
|
281
254
|
}
|
|
282
255
|
const meta = getMeta(entity);
|
|
283
256
|
// Asked before `getPersistable` fills an `onInsert` key into rows it may only update.
|
|
284
257
|
const unnamed = payload.map((row) => !namesKey(meta, row));
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
filter,
|
|
293
|
-
update,
|
|
294
|
-
upsert: true,
|
|
295
|
-
},
|
|
296
|
-
};
|
|
297
|
-
});
|
|
258
|
+
const operations = payload.map((item) => ({
|
|
259
|
+
updateOne: {
|
|
260
|
+
filter: this.buildConflictFilter(entity, conflictPaths, item),
|
|
261
|
+
update: this.upsertUpdate(meta, item),
|
|
262
|
+
upsert: true,
|
|
263
|
+
},
|
|
264
|
+
}));
|
|
298
265
|
const res = await this.execute((session) => this.collection(entity).bulkWrite(operations, { session }));
|
|
299
|
-
const changes = res.upsertedCount + res.modifiedCount;
|
|
300
266
|
// `upsertedIds` names only the documents inserted, keyed by operation index, so each lands on
|
|
301
267
|
// its own row; an updated document's `_id` is read back by the conflict fields instead.
|
|
302
268
|
const reported = payload.map((_, index) => {
|
|
@@ -305,7 +271,7 @@ export class MongodbQuerier extends AbstractQuerier {
|
|
|
305
271
|
});
|
|
306
272
|
const unplaced = reported.some((id, index) => id === undefined && unnamed[index]);
|
|
307
273
|
const found = unplaced ? await this.idsByConflict(entity, conflictPaths, payload) : [];
|
|
308
|
-
return
|
|
274
|
+
return reported.map((id, index) => id ?? found[index]);
|
|
309
275
|
});
|
|
310
276
|
}
|
|
311
277
|
async internalDeleteMany(entity, qm, opts = {}) {
|
|
@@ -332,7 +298,7 @@ export class MongodbQuerier extends AbstractQuerier {
|
|
|
332
298
|
});
|
|
333
299
|
}
|
|
334
300
|
get hasOpenTransaction() {
|
|
335
|
-
return !!this
|
|
301
|
+
return !!this.#session?.inTransaction();
|
|
336
302
|
}
|
|
337
303
|
/** Every read and write goes through here, which makes it where a released querier is caught. */
|
|
338
304
|
collection(entity) {
|
|
@@ -346,24 +312,24 @@ export class MongodbQuerier extends AbstractQuerier {
|
|
|
346
312
|
return this.conn.db();
|
|
347
313
|
}
|
|
348
314
|
async openTransaction(_opts) {
|
|
349
|
-
this.logger.logInfo('
|
|
350
|
-
await this
|
|
351
|
-
this
|
|
352
|
-
this
|
|
315
|
+
this.logger.logInfo('startTransaction');
|
|
316
|
+
await this.#session?.endSession();
|
|
317
|
+
this.#session = this.conn.startSession();
|
|
318
|
+
this.#session.startTransaction();
|
|
353
319
|
}
|
|
354
320
|
/**
|
|
355
321
|
* The driver owns the transaction state here and settles it in its own `finally`, so a failed commit
|
|
356
322
|
* or abort still leaves `inTransaction()` false and the querier releasable.
|
|
357
323
|
*/
|
|
358
324
|
async endTransaction(commit) {
|
|
359
|
-
this.logger.logInfo(commit ? 'commitTransaction' : '
|
|
360
|
-
await (commit ? this
|
|
325
|
+
this.logger.logInfo(commit ? 'commitTransaction' : 'abortTransaction');
|
|
326
|
+
await (commit ? this.#session?.commitTransaction() : this.#session?.abortTransaction());
|
|
361
327
|
}
|
|
362
328
|
async internalRelease() {
|
|
363
|
-
const session = this
|
|
329
|
+
const session = this.#session;
|
|
364
330
|
// Cleared first, so a failing `endSession` cannot leave the querier holding a session it already
|
|
365
331
|
// tried to end.
|
|
366
|
-
this
|
|
332
|
+
this.#session = undefined;
|
|
367
333
|
await session?.endSession();
|
|
368
334
|
}
|
|
369
335
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { type RelationRows } from '../dialect/abstractSqlDialect.js';
|
|
2
2
|
import { type JsonAccessMode, type JsonSlot } from '../dialect/jsonSql.js';
|
|
3
3
|
import { MergeSqlDialect } from '../dialect/mergeSqlDialect.js';
|
|
4
|
-
import type { EntityMeta, FieldOptions, Query, QueryConflictPaths, QueryContext, QueryOptions, QueryPager, SqlDialectFeatures, SqlValues, TriggerRows, Type, UpdatePayload, VectorDistance, VectorMetric } from '../type/index.js';
|
|
4
|
+
import type { EntityMeta, SavepointCommand, FieldOptions, Query, QueryConflictPaths, QueryContext, QueryOptions, QueryPager, SqlDialectFeatures, SqlValues, TriggerRows, Type, UpdatePayload, VectorDistance, VectorMetric } from '../type/index.js';
|
|
5
5
|
/** Microsoft SQL Server 2017 and up. Identifiers are `"`-quoted, the ANSI spelling `tedious` enables. */
|
|
6
6
|
export declare class MsSqlDialect extends MergeSqlDialect {
|
|
7
7
|
#private;
|
|
@@ -11,11 +11,13 @@ export declare class MsSqlDialect extends MergeSqlDialect {
|
|
|
11
11
|
readonly beginTransactionCommand = "BEGIN TRANSACTION";
|
|
12
12
|
readonly commitTransactionCommand = "COMMIT TRANSACTION";
|
|
13
13
|
readonly rollbackTransactionCommand = "ROLLBACK TRANSACTION";
|
|
14
|
+
/** SQL Server saves a transaction under a name and rolls back to it, and releases none: the commit does. */
|
|
15
|
+
savepointStatement(command: SavepointCommand, name: string): string;
|
|
14
16
|
/**
|
|
15
17
|
* T-SQL has no inline form, so the level is set before the `BEGIN`. What a driver that sends these
|
|
16
18
|
* as statements would run; `MsSqlQuerier` opens its transactions through the driver instead.
|
|
17
19
|
*/
|
|
18
|
-
readonly isolationLevelStrategy = "
|
|
20
|
+
readonly isolationLevelStrategy = "setBefore";
|
|
19
21
|
readonly booleanLiteral = "integer";
|
|
20
22
|
/**
|
|
21
23
|
* The clock in UTC, since `CURRENT_TIMESTAMP` is local time in the server's zone. No v7: `NEWSEQUENTIALID()` is
|
|
@@ -28,7 +30,7 @@ export declare class MsSqlDialect extends MergeSqlDialect {
|
|
|
28
30
|
readonly maxBindValues = 2098;
|
|
29
31
|
readonly maxInsertRows = 1000;
|
|
30
32
|
/** `OUTPUT` has no trailing form: it sits between the target and its rows. */
|
|
31
|
-
readonly returningPosition = "
|
|
33
|
+
readonly returningPosition = "afterTarget";
|
|
32
34
|
/** `OUTPUT` reads the written row off the `INSERTED` pseudo-table. */
|
|
33
35
|
protected readonly returnedRowPrefix = "INSERTED.";
|
|
34
36
|
/** Holds the update key lock across the insert; without it two concurrent upserts of one key race. */
|
|
@@ -49,7 +51,7 @@ export declare class MsSqlDialect extends MergeSqlDialect {
|
|
|
49
51
|
insert<E>(ctx: QueryContext, entity: Type<E>, payload: E | E[], opts?: QueryOptions): void;
|
|
50
52
|
/** The table to toggle, or nothing when no record writes a key the engine would have generated. */
|
|
51
53
|
private identityInsertTarget;
|
|
52
|
-
/**
|
|
54
|
+
/** An exact decimal, converted before it crosses the wire, where `tedious` would round it. */
|
|
53
55
|
protected selectFieldExpr(escapedColumn: string, field: FieldOptions): string;
|
|
54
56
|
/**
|
|
55
57
|
* The rows read as they are, `FOR JSON PATH` making the array. It nests a dotted key and leaves a
|
|
@@ -4,11 +4,11 @@ import { BYTES_PREFIX } from '../dialect/hydrateColumn.js';
|
|
|
4
4
|
import { jsonArraySlotArgs, jsonPath, jsonSlotArgs } from '../dialect/jsonSql.js';
|
|
5
5
|
import { MergeSqlDialect } from '../dialect/mergeSqlDialect.js';
|
|
6
6
|
import { getMeta } from '../entity/index.js';
|
|
7
|
-
import { canonicalToSql,
|
|
7
|
+
import { canonicalToSql, resolveColumnCanonicalType } from '../schema/canonicalType.js';
|
|
8
8
|
import { QueryRaw } from '../type/index.js';
|
|
9
9
|
import { parseQueryLock } from '../type/index.js';
|
|
10
10
|
import { bytesToHex } from '../util/bytes.js';
|
|
11
|
-
import { isAutoIncrement } from '../util/field.util.js';
|
|
11
|
+
import { isAutoIncrement, isExactDecimal, jsonPathKeys } from '../util/field.util.js';
|
|
12
12
|
import { assertNonNegativeInteger } from '../util/index.js';
|
|
13
13
|
import { escapeSingleQuotes } from '../util/sqlLiteral.js';
|
|
14
14
|
import { UqlUsageError } from '../util/uqlError.js';
|
|
@@ -26,6 +26,7 @@ const MSSQL_FEATURES = {
|
|
|
26
26
|
supportsTimestamptz: false,
|
|
27
27
|
stringSizing: 'varchar',
|
|
28
28
|
supportsUnsigned: false,
|
|
29
|
+
jsonArrivesDecoded: false,
|
|
29
30
|
serverSideCursors: false,
|
|
30
31
|
correlatedWrites: true,
|
|
31
32
|
rowLocks: { of: true, withWindow: true, placement: 'tableHint' },
|
|
@@ -48,6 +49,7 @@ const MSSQL_FEATURES = {
|
|
|
48
49
|
before: false,
|
|
49
50
|
deferrable: false,
|
|
50
51
|
},
|
|
52
|
+
namedLocks: 'transaction',
|
|
51
53
|
};
|
|
52
54
|
/** The `type` `OPENJSON` reports for the JSON scalar an element is compared with; anything else binds as a string. */
|
|
53
55
|
function openJsonType(value) {
|
|
@@ -78,11 +80,20 @@ export class MsSqlDialect extends MergeSqlDialect {
|
|
|
78
80
|
beginTransactionCommand = 'BEGIN TRANSACTION';
|
|
79
81
|
commitTransactionCommand = 'COMMIT TRANSACTION';
|
|
80
82
|
rollbackTransactionCommand = 'ROLLBACK TRANSACTION';
|
|
83
|
+
/** SQL Server saves a transaction under a name and rolls back to it, and releases none: the commit does. */
|
|
84
|
+
savepointStatement(command, name) {
|
|
85
|
+
const statements = {
|
|
86
|
+
open: `SAVE TRANSACTION ${name}`,
|
|
87
|
+
release: '',
|
|
88
|
+
rollback: `ROLLBACK TRANSACTION ${name}`,
|
|
89
|
+
};
|
|
90
|
+
return statements[command];
|
|
91
|
+
}
|
|
81
92
|
/**
|
|
82
93
|
* T-SQL has no inline form, so the level is set before the `BEGIN`. What a driver that sends these
|
|
83
94
|
* as statements would run; `MsSqlQuerier` opens its transactions through the driver instead.
|
|
84
95
|
*/
|
|
85
|
-
isolationLevelStrategy = '
|
|
96
|
+
isolationLevelStrategy = 'setBefore';
|
|
86
97
|
booleanLiteral = 'integer';
|
|
87
98
|
/**
|
|
88
99
|
* The clock in UTC, since `CURRENT_TIMESTAMP` is local time in the server's zone. No v7: `NEWSEQUENTIALID()` is
|
|
@@ -102,7 +113,7 @@ export class MsSqlDialect extends MergeSqlDialect {
|
|
|
102
113
|
maxBindValues = 2098;
|
|
103
114
|
maxInsertRows = 1000;
|
|
104
115
|
/** `OUTPUT` has no trailing form: it sits between the target and its rows. */
|
|
105
|
-
returningPosition = '
|
|
116
|
+
returningPosition = 'afterTarget';
|
|
106
117
|
/** `OUTPUT` reads the written row off the `INSERTED` pseudo-table. */
|
|
107
118
|
returnedRowPrefix = 'INSERTED.';
|
|
108
119
|
/** Holds the update key lock across the insert; without it two concurrent upserts of one key race. */
|
|
@@ -142,10 +153,9 @@ export class MsSqlDialect extends MergeSqlDialect {
|
|
|
142
153
|
const stated = records.some((record) => record[id.key] !== undefined);
|
|
143
154
|
return stated ? this.escapedTableName(meta) : undefined;
|
|
144
155
|
}
|
|
145
|
-
/**
|
|
156
|
+
/** An exact decimal, converted before it crosses the wire, where `tedious` would round it. */
|
|
146
157
|
selectFieldExpr(escapedColumn, field) {
|
|
147
|
-
|
|
148
|
-
return exactDecimal ? `CONVERT(NVARCHAR(41), ${escapedColumn})` : escapedColumn;
|
|
158
|
+
return isExactDecimal(field) ? `CONVERT(NVARCHAR(41), ${escapedColumn})` : escapedColumn;
|
|
149
159
|
}
|
|
150
160
|
/**
|
|
151
161
|
* The rows read as they are, `FOR JSON PATH` making the array. It nests a dotted key and leaves a
|
|
@@ -300,9 +310,9 @@ export class MsSqlDialect extends MergeSqlDialect {
|
|
|
300
310
|
if (!path) {
|
|
301
311
|
return escapedColumn;
|
|
302
312
|
}
|
|
303
|
-
const
|
|
304
|
-
const
|
|
305
|
-
const
|
|
313
|
+
const keys = jsonPathKeys(path);
|
|
314
|
+
const leaf = keys.pop();
|
|
315
|
+
const parent = ['$', ...keys].join('.');
|
|
306
316
|
return `(SELECT ${this.#elem.value} FROM OPENJSON(${escapedColumn}, '${parent}') WHERE ${this.#elem.key} = N'${leaf}')`;
|
|
307
317
|
}
|
|
308
318
|
/**
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import mssql from 'mssql';
|
|
2
2
|
import { AbstractPoolQuerier } from '../querier/abstractPoolQuerier.js';
|
|
3
3
|
import type { QueryUpdateResult, RawRow, TransactionOptions } from '../type/index.js';
|
|
4
4
|
/** What `tedious` hands back for one statement, whichever shape it took. */
|
|
@@ -18,7 +18,7 @@ type MsSqlRowStream = AsyncIterable<RawRow> & {
|
|
|
18
18
|
/** The part of an `mssql` `Request` a querier drives. */
|
|
19
19
|
type MsSqlRequest = {
|
|
20
20
|
input(name: string, value: unknown): unknown;
|
|
21
|
-
input(name: string, type: typeof DateTime2, value: unknown): unknown;
|
|
21
|
+
input(name: string, type: typeof mssql.DateTime2, value: unknown): unknown;
|
|
22
22
|
query(command: string): Promise<MsSqlResult>;
|
|
23
23
|
toReadableStream(): MsSqlRowStream;
|
|
24
24
|
cancel(): unknown;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import mssql from 'mssql';
|
|
2
2
|
import { AbstractPoolQuerier } from '../querier/abstractPoolQuerier.js';
|
|
3
3
|
import { decodeWireTypes } from './mssqlWireTypes.js';
|
|
4
4
|
/**
|
|
@@ -17,7 +17,7 @@ export class MsSqlQuerier extends AbstractPoolQuerier {
|
|
|
17
17
|
const request = this.#transaction ? this.#transaction.request() : this.getConn().request();
|
|
18
18
|
values?.forEach((value, index) => {
|
|
19
19
|
const name = `p${index + 1}`;
|
|
20
|
-
return value instanceof Date ? request.input(name, DateTime2, value) : request.input(name, value);
|
|
20
|
+
return value instanceof Date ? request.input(name, mssql.DateTime2, value) : request.input(name, value);
|
|
21
21
|
});
|
|
22
22
|
return request;
|
|
23
23
|
}
|
|
@@ -63,7 +63,7 @@ export class MsSqlQuerier extends AbstractPoolQuerier {
|
|
|
63
63
|
*/
|
|
64
64
|
async internalBegin(opts) {
|
|
65
65
|
const transaction = this.getConn().transaction();
|
|
66
|
-
await transaction.begin(opts?.isolationLevel && ISOLATION_LEVEL[ISOLATION[opts.isolationLevel]]);
|
|
66
|
+
await transaction.begin(opts?.isolationLevel && mssql.ISOLATION_LEVEL[ISOLATION[opts.isolationLevel]]);
|
|
67
67
|
this.#transaction = transaction;
|
|
68
68
|
}
|
|
69
69
|
async internalCommit() {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ConnectionPool, type config as MsSqlConfig } from 'mssql';
|
|
1
|
+
import { type ConnectionPool, type config as MsSqlConfig } from 'mssql';
|
|
2
2
|
import { AbstractSqlQuerierPool } from '../querier/index.js';
|
|
3
3
|
import type { ExtraOptions } from '../type/index.js';
|
|
4
4
|
import { MsSqlDialect } from './mssqlDialect.js';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import mssql, {} from 'mssql';
|
|
2
2
|
import { dialectOptionsFrom } from '../dialect/abstractDialect.js';
|
|
3
3
|
import { AbstractSqlQuerierPool } from '../querier/index.js';
|
|
4
4
|
import { attachPoolErrorHandler } from '../util/index.js';
|
|
@@ -11,7 +11,7 @@ export class MsSqlQuerierPool extends AbstractSqlQuerierPool {
|
|
|
11
11
|
#connection;
|
|
12
12
|
constructor(opts, extra) {
|
|
13
13
|
super(new MsSqlDialect(dialectOptionsFrom(extra)), extra);
|
|
14
|
-
this.pool = new ConnectionPool(opts);
|
|
14
|
+
this.pool = new mssql.ConnectionPool(opts);
|
|
15
15
|
attachPoolErrorHandler(this.pool, 'Idle SQL Server pool connection encountered an error', extra?.logger);
|
|
16
16
|
}
|
|
17
17
|
async getQuerier() {
|
|
@@ -10,11 +10,7 @@ export class MySql2Querier extends AbstractPoolQuerier {
|
|
|
10
10
|
}
|
|
11
11
|
async internalRun(query, values) {
|
|
12
12
|
const [res] = await this.getConn().promise().query(query, values);
|
|
13
|
-
return this.buildUpdateResult({
|
|
14
|
-
changes: res.affectedRows,
|
|
15
|
-
id: res.insertId,
|
|
16
|
-
upsertStatus: res.affectedRows,
|
|
17
|
-
});
|
|
13
|
+
return this.buildUpdateResult({ changes: res.affectedRows, id: res.insertId });
|
|
18
14
|
}
|
|
19
15
|
async *internalStream(query, values) {
|
|
20
16
|
yield* this.getConn().query(query, values).stream();
|
|
@@ -5,5 +5,4 @@ import type { NamingStrategy } from '../type/index.js';
|
|
|
5
5
|
export declare class DefaultNamingStrategy implements NamingStrategy {
|
|
6
6
|
tableName(className: string): string;
|
|
7
7
|
columnName(propertyName: string): string;
|
|
8
|
-
joinTableName(source: string, target: string, _propertyName?: string): string;
|
|
9
8
|
}
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { PostgresDialect } from '../postgres/postgresDialect.js';
|
|
2
|
+
import type { SqlDialectFeatures } from '../type/index.js';
|
|
2
3
|
/** Postgres as PGlite runs it, which binds fewer values per statement than a server. */
|
|
3
4
|
export declare class PgliteDialect extends PostgresDialect {
|
|
5
|
+
/** Every querier shares the one session, which an advisory lock it already holds never makes wait. */
|
|
6
|
+
readonly features: SqlDialectFeatures;
|
|
4
7
|
/** Past 32767 PGlite answers no rows, and wrong ones from then on, where a server takes 65535. */
|
|
5
8
|
readonly maxBindValues: number;
|
|
6
9
|
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import { PostgresDialect } from '../postgres/postgresDialect.js';
|
|
1
|
+
import { POSTGRES_FEATURES, PostgresDialect } from '../postgres/postgresDialect.js';
|
|
2
2
|
/** Postgres as PGlite runs it, which binds fewer values per statement than a server. */
|
|
3
3
|
export class PgliteDialect extends PostgresDialect {
|
|
4
|
+
/** Every querier shares the one session, which an advisory lock it already holds never makes wait. */
|
|
5
|
+
features = { ...POSTGRES_FEATURES, namedLocks: false };
|
|
4
6
|
/** Past 32767 PGlite answers no rows, and wrong ones from then on, where a server takes 65535. */
|
|
5
7
|
maxBindValues = 32767;
|
|
6
8
|
}
|
|
@@ -19,6 +19,8 @@ export declare class PgliteQuerier extends AbstractSqlQuerier {
|
|
|
19
19
|
readonly db: PgliteDatabase;
|
|
20
20
|
readonly extra?: ExtraOptions | undefined;
|
|
21
21
|
constructor(db: PgliteDatabase, dialect: PgliteDialect, extra?: ExtraOptions | undefined);
|
|
22
|
+
/** Every querier of the pool is this one handle's session, so they share its transaction. */
|
|
23
|
+
protected get connection(): object;
|
|
22
24
|
internalAll<T>(query: string, values?: unknown[]): Promise<T[]>;
|
|
23
25
|
internalRun(query: string, values?: unknown[]): Promise<import("../type/query.js").QueryUpdateResult>;
|
|
24
26
|
/** The handle belongs to the pool, which hands out one querier per unit of work over it. */
|
|
@@ -13,6 +13,10 @@ export class PgliteQuerier extends AbstractSqlQuerier {
|
|
|
13
13
|
this.db = db;
|
|
14
14
|
this.extra = extra;
|
|
15
15
|
}
|
|
16
|
+
/** Every querier of the pool is this one handle's session, so they share its transaction. */
|
|
17
|
+
get connection() {
|
|
18
|
+
return this.db;
|
|
19
|
+
}
|
|
16
20
|
async internalAll(query, values) {
|
|
17
21
|
const res = await this.db.query(query, values);
|
|
18
22
|
return res.rows;
|
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
import { PgLikeSqlDialect } from '../dialect/pgLikeSqlDialect.js';
|
|
2
2
|
import type { QueryContext, SqlDialectFeatures, SqlDialectName, SqlValues, Type } from '../type/index.js';
|
|
3
|
-
/**
|
|
3
|
+
/** What Postgres has: pgvector, the only engine with `halfvec` and `sparsevec`. */
|
|
4
|
+
export declare const POSTGRES_FEATURES: SqlDialectFeatures;
|
|
5
|
+
/** PostgreSQL, under every driver: `pg`, Neon, PGlite, `bun:sql`. Adds pgvector. */
|
|
4
6
|
export declare class PostgresDialect extends PgLikeSqlDialect {
|
|
5
7
|
readonly dialectName: SqlDialectName;
|
|
6
8
|
readonly vectorExtension: string | undefined;
|
|
7
|
-
/** pgvector is the only engine with `halfvec` and `sparsevec`. */
|
|
8
9
|
readonly features: SqlDialectFeatures;
|
|
9
10
|
/** `uuidv7()` is Postgres 18+; an older server refuses it itself. */
|
|
10
11
|
readonly sqlValues: SqlValues;
|
|
11
|
-
/** The xmax system column is 0 for a newly inserted row and non-zero for an updated one (MVCC). */
|
|
12
|
-
protected readonly upsertCreatedReturning: string;
|
|
13
12
|
/**
|
|
14
13
|
* `to_regclass` rather than a `::regclass` cast: it answers `NULL` for a table that does not exist
|
|
15
14
|
* (mid-migration, say) where the cast throws. `GREATEST` because a table nothing has analyzed yet
|
|
@@ -1,16 +1,15 @@
|
|
|
1
|
-
import { AGGREGATE_VALUE_ALIAS
|
|
1
|
+
import { AGGREGATE_VALUE_ALIAS } from '../dialect/aliases.js';
|
|
2
2
|
import { PG_FEATURES, PG_SQL_VALUES, PgLikeSqlDialect } from '../dialect/pgLikeSqlDialect.js';
|
|
3
3
|
import { getMeta } from '../entity/index.js';
|
|
4
|
-
/**
|
|
4
|
+
/** What Postgres has: pgvector, the only engine with `halfvec` and `sparsevec`. */
|
|
5
|
+
export const POSTGRES_FEATURES = { ...PG_FEATURES, narrowVectorTypes: true };
|
|
6
|
+
/** PostgreSQL, under every driver: `pg`, Neon, PGlite, `bun:sql`. Adds pgvector. */
|
|
5
7
|
export class PostgresDialect extends PgLikeSqlDialect {
|
|
6
8
|
dialectName = 'postgres';
|
|
7
9
|
vectorExtension = 'vector';
|
|
8
|
-
|
|
9
|
-
features = { ...PG_FEATURES, narrowVectorTypes: true };
|
|
10
|
+
features = POSTGRES_FEATURES;
|
|
10
11
|
/** `uuidv7()` is Postgres 18+; an older server refuses it itself. */
|
|
11
12
|
sqlValues = { ...PG_SQL_VALUES, uuidv7: 'uuidv7()' };
|
|
12
|
-
/** The xmax system column is 0 for a newly inserted row and non-zero for an updated one (MVCC). */
|
|
13
|
-
upsertCreatedReturning = `(xmax = 0) AS ${this.escapeId(UPSERT_CREATED_ALIAS)}`;
|
|
14
13
|
/**
|
|
15
14
|
* `to_regclass` rather than a `::regclass` cast: it answers `NULL` for a table that does not exist
|
|
16
15
|
* (mid-migration, say) where the cast throws. `GREATEST` because a table nothing has analyzed yet
|