uql-orm 0.108.1 → 0.109.1
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/README.md +2 -1
- package/dist/browser/uql-browser.min.js +2 -2
- package/dist/browser/uql-browser.min.js.map +5 -5
- package/dist/dialect/abstractSqlDialect.d.ts +11 -14
- package/dist/dialect/abstractSqlDialect.js +22 -24
- package/dist/dialect/mergeSqlDialect.d.ts +2 -2
- package/dist/dialect/mergeSqlDialect.js +2 -2
- package/dist/dialect/mysqlLikeSqlDialect.d.ts +2 -2
- package/dist/dialect/mysqlLikeSqlDialect.js +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/mongodb/mongodbQuerier.d.ts +1 -1
- package/dist/mongodb/mongodbQuerier.js +8 -8
- package/dist/mssql/mssqlDialect.d.ts +2 -2
- package/dist/mssql/mssqlDialect.js +2 -2
- package/dist/querier/abstractQuerier.d.ts +32 -15
- package/dist/querier/abstractQuerier.js +48 -30
- package/dist/querier/abstractSqlQuerier.d.ts +1 -1
- package/dist/querier/abstractSqlQuerier.js +12 -12
- package/dist/sqlite/sqliteDialect.d.ts +2 -0
- package/dist/sqlite/sqliteDialect.js +4 -0
- package/dist/type/dialect.d.ts +2 -3
- package/dist/type/querier.d.ts +28 -15
- package/dist/type/query.d.ts +18 -13
- package/dist/type/querySql.d.ts +4 -2
- package/dist/type/querySql.js +6 -2
- package/dist/type/universalQuerier.d.ts +12 -12
- package/dist/util/dialect.util.d.ts +3 -3
- package/dist/util/dialect.util.js +4 -3
- package/dist/util/index.d.ts +1 -1
- package/dist/util/object.util.d.ts +2 -0
- package/dist/util/object.util.js +4 -0
- package/dist/util/sql.d.ts +12 -4
- package/dist/util/sql.js +71 -23
- package/dist/util/triggerWrite.d.ts +10 -5
- package/dist/util/triggerWrite.js +5 -5
- package/package.json +1 -1
- package/skills/uql-orm/SKILL.md +6 -6
|
@@ -49,7 +49,7 @@ export declare class MongodbQuerier extends AbstractQuerier implements MongoQuer
|
|
|
49
49
|
* Updating a found document `apart`, it writes only an inserted one.
|
|
50
50
|
*/
|
|
51
51
|
private upsertUpdate;
|
|
52
|
-
protected internalUpsertMany<E extends Document>(entity: Type<E>, q: QueryUpsert<E>, payload: E[]
|
|
52
|
+
protected internalUpsertMany<E extends Document>(entity: Type<E>, q: QueryUpsert<E>, payload: E[]): Promise<(UpsertedId<E> | undefined)[]>;
|
|
53
53
|
protected internalDeleteMany<E extends Document>(entity: Type<E>, qm: QuerySearch<E>, opts?: QueryOptions): Promise<number>;
|
|
54
54
|
get hasOpenTransaction(): boolean;
|
|
55
55
|
/** Every read and write goes through here, which makes it where a released querier is caught. */
|
|
@@ -183,7 +183,7 @@ export class MongodbQuerier extends AbstractQuerier {
|
|
|
183
183
|
}
|
|
184
184
|
return update;
|
|
185
185
|
}
|
|
186
|
-
async internalUpsertMany(entity, q, payload
|
|
186
|
+
async internalUpsertMany(entity, q, payload) {
|
|
187
187
|
refuseTriggers(entity);
|
|
188
188
|
if (!payload.length) {
|
|
189
189
|
return [];
|
|
@@ -191,18 +191,18 @@ export class MongodbQuerier extends AbstractQuerier {
|
|
|
191
191
|
// A found document is updated apart from the insert when the upsert has an update of its own, as `$setOnInsert`
|
|
192
192
|
// and `$inc` on one field conflict in one operation, or a `$where`. The two batches are not atomic on their own:
|
|
193
193
|
// a transaction the caller opens makes them so.
|
|
194
|
-
const apart = update !== undefined || q.$where !== undefined;
|
|
194
|
+
const apart = q.$update !== undefined || q.$where !== undefined;
|
|
195
195
|
const meta = getMeta(entity);
|
|
196
|
-
const
|
|
197
|
-
const kept = payload.map((row) => upsertKeepsOwnKey(meta,
|
|
196
|
+
const { $conflict } = q;
|
|
197
|
+
const kept = payload.map((row) => upsertKeepsOwnKey(meta, $conflict, row));
|
|
198
198
|
// A row naming no conflict key conflicts with none, so it inserts, as `ON CONFLICT` inserts it; a soft-deleted
|
|
199
199
|
// document conflicts as any other does, its unique key holding across both.
|
|
200
|
-
const keys = getKeys(
|
|
200
|
+
const keys = getKeys($conflict);
|
|
201
201
|
const upserts = payload.map((row) => !namesEvery(row, keys)
|
|
202
202
|
? { insertOne: { document: this.dialect.getPersistable(meta, clone(row), 'onInsert') } }
|
|
203
203
|
: {
|
|
204
204
|
updateOne: {
|
|
205
|
-
filter: this.dialect.where(entity, foundRowWhere({ $conflict
|
|
205
|
+
filter: this.dialect.where(entity, foundRowWhere({ $conflict }, row), withDeleted()),
|
|
206
206
|
update: this.upsertUpdate(meta, row, apart),
|
|
207
207
|
upsert: true,
|
|
208
208
|
},
|
|
@@ -216,7 +216,7 @@ export class MongodbQuerier extends AbstractQuerier {
|
|
|
216
216
|
});
|
|
217
217
|
const updates = apart
|
|
218
218
|
? payload.flatMap((row, index) => {
|
|
219
|
-
const assigned = reported[index] === undefined && foundRowAssigned(meta,
|
|
219
|
+
const assigned = reported[index] === undefined && foundRowAssigned(meta, q, row);
|
|
220
220
|
return assigned
|
|
221
221
|
? [{ updateOne: this.updateOperation(entity, foundRowWhere(q, row), assigned, withDeleted()) }]
|
|
222
222
|
: [];
|
|
@@ -225,7 +225,7 @@ export class MongodbQuerier extends AbstractQuerier {
|
|
|
225
225
|
if (updates.length) {
|
|
226
226
|
await this.execute('upsertMany', (session) => this.collection(entity).bulkWrite(updates, { session }));
|
|
227
227
|
}
|
|
228
|
-
return this.withUnreportedIds(entity,
|
|
228
|
+
return this.withUnreportedIds(entity, $conflict, payload, reported, kept);
|
|
229
229
|
}
|
|
230
230
|
async internalDeleteMany(entity, qm, opts = {}) {
|
|
231
231
|
refuseTriggers(entity);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { type RelationRows, type SelectTerm } from '../dialect/abstractSqlDialect.js';
|
|
2
2
|
import { type JsonAccessMode, type JsonContainer, type JsonKeyWrite, type JsonSlot } from '../dialect/jsonSql.js';
|
|
3
3
|
import { MergeSqlDialect } from '../dialect/mergeSqlDialect.js';
|
|
4
|
-
import type { FieldKey, ReportedWrite, EntityMeta, SavepointCommand, FieldOptions, Query, QueryUpsert, QueryContext, QueryPager, SqlDialectFeatures, SqlValues, TriggerRows, AbstractType,
|
|
4
|
+
import type { FieldKey, ReportedWrite, EntityMeta, SavepointCommand, FieldOptions, Query, QueryUpsert, QueryContext, QueryPager, SqlDialectFeatures, SqlValues, TriggerRows, AbstractType, VectorDistance, VectorMetric, TransactionOptions } 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;
|
|
@@ -76,7 +76,7 @@ export declare class MsSqlDialect extends MergeSqlDialect {
|
|
|
76
76
|
* carrying a trigger, and through a table it works whether one is there or not.
|
|
77
77
|
*/
|
|
78
78
|
returningId<E>(meta: EntityMeta<E>): string;
|
|
79
|
-
upsert<E>(ctx: QueryContext, entity: AbstractType<E>, q: QueryUpsert<E>, payload: E | E[],
|
|
79
|
+
upsert<E>(ctx: QueryContext, entity: AbstractType<E>, q: QueryUpsert<E>, payload: E | E[], returning?: readonly FieldKey<E>[]): readonly SelectTerm[];
|
|
80
80
|
/**
|
|
81
81
|
* `OUTPUT` the columns `returning` reads `INTO` a table variable typed as they are, every stored one where it names an
|
|
82
82
|
* inlined field, then the fields read off it as off the table. Through a table, as `OUTPUT` straight back to the
|
|
@@ -206,8 +206,8 @@ export class MsSqlDialect extends MergeSqlDialect {
|
|
|
206
206
|
const expression = this.returningIdExpression(meta);
|
|
207
207
|
return expression ? `OUTPUT ${expression} INTO ${OUTPUT_TABLE}` : '';
|
|
208
208
|
}
|
|
209
|
-
upsert(ctx, entity, q, payload,
|
|
210
|
-
return this.identityInserted(ctx, getMeta(entity), payload, () => super.upsert(ctx, entity, q, payload,
|
|
209
|
+
upsert(ctx, entity, q, payload, returning) {
|
|
210
|
+
return this.identityInserted(ctx, getMeta(entity), payload, () => super.upsert(ctx, entity, q, payload, returning));
|
|
211
211
|
}
|
|
212
212
|
/**
|
|
213
213
|
* `OUTPUT` the columns `returning` reads `INTO` a table variable typed as they are, every stored one where it names an
|
|
@@ -11,7 +11,7 @@ export type ReportingWrites = {
|
|
|
11
11
|
/** Inserts `rows`, reporting each in their order and placing its key on it, as `internalInsertMany` does. */
|
|
12
12
|
insert<E extends object>(entity: Type<E>, rows: EntityData<E>[], keys: readonly FieldKey<E>[]): Promise<E[]>;
|
|
13
13
|
/** The rows of `payload` the upsert writes, in its order, `undefined` for one it kept. */
|
|
14
|
-
upsert<E extends object>(entity: Type<E>, q: QueryUpsert<E>, payload: E[],
|
|
14
|
+
upsert<E extends object>(entity: Type<E>, q: QueryUpsert<E>, payload: E[], keys: readonly FieldKey<E>[]): Promise<(E | undefined)[]>;
|
|
15
15
|
};
|
|
16
16
|
/** Base class for all database queriers. */
|
|
17
17
|
export declare abstract class AbstractQuerier implements Querier {
|
|
@@ -108,12 +108,17 @@ export declare abstract class AbstractQuerier implements Querier {
|
|
|
108
108
|
/**
|
|
109
109
|
* Run an aggregate query.
|
|
110
110
|
*/
|
|
111
|
+
aggregate<E extends object, const Q extends QueryAggregate<E> & {
|
|
112
|
+
$entity: Type<E>;
|
|
113
|
+
}>(q: Captured<QueryAggregate<E, Q> & {
|
|
114
|
+
$entity: Type<E>;
|
|
115
|
+
}, Q>, opts?: QueryOptions): Promise<QueryAggregateResult<E, Q>[]>;
|
|
111
116
|
aggregate<E extends object, const Q extends QueryAggregate<E>>(entity: Type<E>, q: Captured<QueryAggregate<E, Q>, Q>, opts?: QueryOptions): Promise<QueryAggregateResult<E, Q>[]>;
|
|
112
117
|
protected abstract internalAggregate<E extends object>(entity: Type<E>, q: QueryAggregate<E>, opts?: QueryOptions): Promise<RawRow[]>;
|
|
113
118
|
/** Abstract outright: nothing is shared to do around it. See {@link UniversalQuerier.estimatedCount}. */
|
|
114
119
|
abstract estimatedCount<E extends object>(entity: Type<E>): Promise<number>;
|
|
115
|
-
insertOne<E extends object, const S extends FieldKey<E> = never, const V extends BooleanLike = true>(entity: Type<E>, payload: EntityWrite<E>, opts?: WriteOptions<E, S, V>): Promise<WriteResults<E, S, V>['inserted']>;
|
|
116
|
-
insertMany<E extends object, const S extends FieldKey<E> = never, const V extends BooleanLike = true>(entity: Type<E>, payload: readonly EntityWrite<E>[], opts?: WriteOptions<E, S, V>): Promise<WriteResults<E, S, V>['inserted'][]>;
|
|
120
|
+
insertOne<E extends object, const S extends FieldKey<E> = never, const V extends BooleanLike = true, const W extends boolean = false>(entity: Type<E>, payload: EntityWrite<E>, opts?: WriteOptions<E, S, V, W>): Promise<WriteResults<E, S, V, W>['inserted']>;
|
|
121
|
+
insertMany<E extends object, const S extends FieldKey<E> = never, const V extends BooleanLike = true, const W extends boolean = false>(entity: Type<E>, payload: readonly EntityWrite<E>[], opts?: WriteOptions<E, S, V, W>): Promise<WriteResults<E, S, V, W>['inserted'][]>;
|
|
117
122
|
/**
|
|
118
123
|
* What an insert reports: the ids, or the rows `returning` asks for, off the insert's own statement where
|
|
119
124
|
* {@link reportingWrites} can, else read back.
|
|
@@ -146,8 +151,11 @@ export declare abstract class AbstractQuerier implements Querier {
|
|
|
146
151
|
private insertRows;
|
|
147
152
|
/** Writes `rows`, its columns only, and onto each one the key the database generated for it, where it can tell. */
|
|
148
153
|
protected abstract internalInsertMany<E extends object>(entity: Type<E>, rows: EntityData<E>[]): Promise<void>;
|
|
149
|
-
updateOneById<E extends object, const S extends FieldKey<E> = never, const V extends BooleanLike = true>(entity: Type<E>, id: EntityId<E>, payload: UpdateWrite<E>, opts?: QueryOptions & WriteOptions<E, S, V>): Promise<WriteResults<E, S, V>['affected']>;
|
|
150
|
-
updateMany<E extends object, const S extends FieldKey<E> = never, const V extends BooleanLike = true
|
|
154
|
+
updateOneById<E extends object, const S extends FieldKey<E> = never, const V extends BooleanLike = true, const W extends boolean = false>(entity: Type<E>, id: EntityId<E>, payload: UpdateWrite<E>, opts?: QueryOptions & WriteOptions<E, S, V, W>): Promise<WriteResults<E, S, V, W>['affected']>;
|
|
155
|
+
updateMany<E extends object, const S extends FieldKey<E> = never, const V extends BooleanLike = true, const W extends boolean = false>(q: QuerySearch<E> & {
|
|
156
|
+
$entity: Type<E>;
|
|
157
|
+
}, payload: UpdateWrite<E>, opts?: QueryOptions & WriteOptions<E, S, V, W>): Promise<WriteResults<E, S, V, W>['affectedMany']>;
|
|
158
|
+
updateMany<E extends object, const S extends FieldKey<E> = never, const V extends BooleanLike = true, const W extends boolean = false>(entity: Type<E>, q: QuerySearch<E>, payload: UpdateWrite<E>, opts?: QueryOptions & WriteOptions<E, S, V, W>): Promise<WriteResults<E, S, V, W>['affectedMany']>;
|
|
151
159
|
/**
|
|
152
160
|
* The number of rows the update changed, or the rows `returning` reads back: settled first and held, so the
|
|
153
161
|
* update writes those rows and no other, whatever a payload or another writer does to what `$where` reads.
|
|
@@ -207,14 +215,23 @@ export declare abstract class AbstractQuerier implements Querier {
|
|
|
207
215
|
/** Runs one UPDATE over `q`, which names its rows by id wherever {@link updateMany} settled them. */
|
|
208
216
|
protected abstract internalUpdateMany<E extends object>(entity: Type<E>, q: QuerySearch<E>, payload: UpdatePayload<E>, opts?: QueryOptions): Promise<number>;
|
|
209
217
|
restoreOneById<E extends object>(entity: Type<E>, id: EntityId<E>): Promise<number>;
|
|
218
|
+
restoreMany<E extends object>(q: QuerySearch<E> & {
|
|
219
|
+
$entity: Type<E>;
|
|
220
|
+
}): Promise<number>;
|
|
210
221
|
restoreMany<E extends object>(entity: Type<E>, q: QuerySearch<E>): Promise<number>;
|
|
211
|
-
upsertOne<E extends object, const S extends FieldKey<E> = never, const V extends BooleanLike = true
|
|
212
|
-
|
|
222
|
+
upsertOne<E extends object, const S extends FieldKey<E> = never, const V extends BooleanLike = true, const W extends boolean = false>(q: QueryUpsert<E> & {
|
|
223
|
+
$entity: Type<E>;
|
|
224
|
+
}, payload: EntityWrite<E>, opts?: WriteOptions<E, S, V, W>): Promise<WriteResults<E, S, V, W>['inserted']>;
|
|
225
|
+
upsertOne<E extends object, const S extends FieldKey<E> = never, const V extends BooleanLike = true, const W extends boolean = false>(entity: Type<E>, q: QueryUpsert<E>, payload: EntityWrite<E>, opts?: WriteOptions<E, S, V, W>): Promise<WriteResults<E, S, V, W>['inserted']>;
|
|
226
|
+
upsertMany<E extends object, const S extends FieldKey<E> = never, const V extends BooleanLike = true, const W extends boolean = false>(q: QueryUpsert<E> & {
|
|
227
|
+
$entity: Type<E>;
|
|
228
|
+
}, payload: readonly EntityWrite<E>[], opts?: WriteOptions<E, S, V, W>): Promise<WriteResults<E, S, V, W>['inserted'][]>;
|
|
229
|
+
upsertMany<E extends object, const S extends FieldKey<E> = never, const V extends BooleanLike = true, const W extends boolean = false>(entity: Type<E>, q: QueryUpsert<E>, payload: readonly EntityWrite<E>[], opts?: WriteOptions<E, S, V, W>): Promise<WriteResults<E, S, V, W>['inserted'][]>;
|
|
213
230
|
/** Upserts `rows`, reading first where {@link upsertsByReading} says so, and answers the id each row got. */
|
|
214
231
|
private upsertIds;
|
|
215
232
|
/**
|
|
216
233
|
* An upsert of one statement reporting each row as it stands after it: off its `RETURNING` where it wrote the row,
|
|
217
|
-
* else, where its `$where` or an empty
|
|
234
|
+
* else, where its `$where` or an empty `$update` kept it, read by the conflict keys.
|
|
218
235
|
*/
|
|
219
236
|
private upsertReturning;
|
|
220
237
|
/**
|
|
@@ -233,13 +250,13 @@ export declare abstract class AbstractQuerier implements Querier {
|
|
|
233
250
|
* Upserts `payload` as `q` names its rows, answering each row's key in payload order, `undefined` for one the
|
|
234
251
|
* engine did not report.
|
|
235
252
|
*/
|
|
236
|
-
protected abstract internalUpsertMany<E extends object>(entity: Type<E>, q: QueryUpsert<E>, payload: E[]
|
|
237
|
-
deleteOneById<E extends object, const S extends FieldKey<E> = never, const V extends BooleanLike = true>(entity: Type<E>, id: EntityId<E>, opts?: QueryOptions & WriteOptions<E, S, V>): Promise<WriteResults<E, S, V>['affected']>;
|
|
253
|
+
protected abstract internalUpsertMany<E extends object>(entity: Type<E>, q: QueryUpsert<E>, payload: E[]): Promise<(UpsertedId<E> | undefined)[]>;
|
|
254
|
+
deleteOneById<E extends object, const S extends FieldKey<E> = never, const V extends BooleanLike = true, const W extends boolean = false>(entity: Type<E>, id: EntityId<E>, opts?: QueryOptions & WriteOptions<E, S, V, W>): Promise<WriteResults<E, S, V, W>['affected']>;
|
|
238
255
|
/** Delete records matching the query, the entity passed first or as `$entity`; soft-deletes unless `opts.hardDelete`. */
|
|
239
|
-
deleteMany<E extends object>(q: QuerySearch<E> & {
|
|
256
|
+
deleteMany<E extends object, const S extends FieldKey<E> = never, const V extends BooleanLike = true, const W extends boolean = false>(q: QuerySearch<E> & {
|
|
240
257
|
$entity: Type<E>;
|
|
241
|
-
}, opts?: QueryOptions): Promise<
|
|
242
|
-
deleteMany<E extends object, const S extends FieldKey<E> = never, const V extends BooleanLike = true>(entity: Type<E>, q: QuerySearch<E>, opts?: QueryOptions & WriteOptions<E, S, V>): Promise<WriteResults<E, S, V>['affectedMany']>;
|
|
258
|
+
}, opts?: QueryOptions & WriteOptions<E, S, V, W>): Promise<WriteResults<E, S, V, W>['affectedMany']>;
|
|
259
|
+
deleteMany<E extends object, const S extends FieldKey<E> = never, const V extends BooleanLike = true, const W extends boolean = false>(entity: Type<E>, q: QuerySearch<E>, opts?: QueryOptions & WriteOptions<E, S, V, W>): Promise<WriteResults<E, S, V, W>['affectedMany']>;
|
|
243
260
|
/** The number of rows deleted, or the rows `returning` reads as they were: held before the delete, which deletes those. */
|
|
244
261
|
private deleteWhere;
|
|
245
262
|
/** Whether an update is its one statement over `q`: no version to lock, relation to write or page to settle first. */
|
|
@@ -252,8 +269,8 @@ export declare abstract class AbstractQuerier implements Querier {
|
|
|
252
269
|
private deleteRows;
|
|
253
270
|
/** Runs one DELETE (or soft-delete stamp) over `q`, which names its rows by id wherever {@link deleteMany} settled them. */
|
|
254
271
|
protected abstract internalDeleteMany<E extends object>(entity: Type<E>, q: QuerySearch<E>, opts?: QueryOptions): Promise<number>;
|
|
255
|
-
saveOne<E extends object, const S extends FieldKey<E> = never, const V extends BooleanLike = true>(entity: Type<E>, payload: EntityWrite<E>, opts?: WriteOptions<E, S, V>): Promise<WriteResults<E, S, V>['inserted']>;
|
|
256
|
-
saveMany<E extends object, const S extends FieldKey<E> = never, const V extends BooleanLike = true>(entity: Type<E>, payload: readonly EntityWrite<E>[], opts?: WriteOptions<E, S, V>): Promise<WriteResults<E, S, V>['inserted'][]>;
|
|
272
|
+
saveOne<E extends object, const S extends FieldKey<E> = never, const V extends BooleanLike = true, const W extends boolean = false>(entity: Type<E>, payload: EntityWrite<E>, opts?: WriteOptions<E, S, V, W>): Promise<WriteResults<E, S, V, W>['inserted']>;
|
|
273
|
+
saveMany<E extends object, const S extends FieldKey<E> = never, const V extends BooleanLike = true, const W extends boolean = false>(entity: Type<E>, payload: readonly EntityWrite<E>[], opts?: WriteOptions<E, S, V, W>): Promise<WriteResults<E, S, V, W>['inserted'][]>;
|
|
257
274
|
/**
|
|
258
275
|
* Whether a row names its key decides its statement, never whether the row exists: a named row
|
|
259
276
|
* upserts on that key, so a stale id is written rather than silently missed, and an unnamed one
|
|
@@ -2,7 +2,7 @@ import { getMeta, idOf, namesEvery, namesKey, relationOf } from '../entity/index
|
|
|
2
2
|
import { SAVEPOINT_PREFIX } from '../dialect/aliases.js';
|
|
3
3
|
import { namesRows } from '../dialect/operators.js';
|
|
4
4
|
import { parseQueryLock } from '../type/index.js';
|
|
5
|
-
import { childrenExcept, childrenOf, chunk, clone, entityName, fillOnFields, filterFieldKeys, getKeys, deletionOf, hasKeys, getRelationRequestSummary, guardWrite, idOnlyQuery, keyJoins, keySet, isPagedQuery, isScalarId, LoggerWrapper, normalizeScalarFieldSelection, parentJoins, pointAt, queryLoggerFor, parseRelationAtKey, rowKey, runHooks, securityConditions, someKey, targetJoins, foundRowAssigned, whereAnd, whereIds, whereKeysIn, whereWith, withDeleted, cascadingRelations, holdsForeignKey, } from '../util/index.js';
|
|
5
|
+
import { childrenExcept, childrenOf, chunk, clone, entityName, fillOnFields, filterFieldKeys, getKeys, deletionOf, hasKeys, getRelationRequestSummary, guardWrite, idOnlyQuery, keyJoins, keySet, isPagedQuery, isScalarId, LoggerWrapper, normalizeScalarFieldSelection, parentJoins, pointAt, queryLoggerFor, parseRelationAtKey, rowKey, runHooks, securityConditions, someKey, targetJoins, foundRowAssigned, whereAnd, whereIds, whereKeysIn, whereWith, withDeleted, cascadingRelations, isClass, defaultReadKeys, holdsForeignKey, } from '../util/index.js';
|
|
6
6
|
import { UqlOptimisticLockError, UqlUsageError } from '../util/uqlError.js';
|
|
7
7
|
import { keysetRead } from './keyset.js';
|
|
8
8
|
import { enrichError } from './queryError.js';
|
|
@@ -79,10 +79,10 @@ function writtenIds(meta, rows) {
|
|
|
79
79
|
* Whether an upsert has to read before it writes: `ON CONFLICT` updates a guarded row whatever tenant it belongs
|
|
80
80
|
* to, writes no relation, and cannot tell a found row it skipped for a stale version.
|
|
81
81
|
*/
|
|
82
|
-
function upsertsByReading(meta,
|
|
82
|
+
function upsertsByReading(meta, q, rows) {
|
|
83
83
|
return (meta.version !== undefined ||
|
|
84
84
|
securityConditions(meta).length > 0 ||
|
|
85
|
-
writesRelations(meta, update ? [...rows, update] : rows));
|
|
85
|
+
writesRelations(meta, q.$update ? [...rows, q.$update] : rows));
|
|
86
86
|
}
|
|
87
87
|
/**
|
|
88
88
|
* Writes the key an upsert reported onto each row, over the one it named: a found row keeps its own, and a row
|
|
@@ -110,19 +110,34 @@ function entityArgs(args) {
|
|
|
110
110
|
const [entity, q, opts] = args;
|
|
111
111
|
return [entity, q ?? {}, opts];
|
|
112
112
|
}
|
|
113
|
-
const [
|
|
113
|
+
const [q, opts] = args;
|
|
114
|
+
return [...withoutEntity(q), opts];
|
|
115
|
+
}
|
|
116
|
+
/** Normalizes either write form to `[entity, query, payload, opts]`. */
|
|
117
|
+
function payloadArgs(args) {
|
|
118
|
+
if (isEntityFirst(args)) {
|
|
119
|
+
return args;
|
|
120
|
+
}
|
|
121
|
+
const [q, payload, opts] = args;
|
|
122
|
+
return [...withoutEntity(q), payload, opts];
|
|
123
|
+
}
|
|
124
|
+
/** A query of the `$entity` form as its entity and the query without it, throwing when it names none. */
|
|
125
|
+
function withoutEntity(query) {
|
|
126
|
+
const { $entity, ...q } = query;
|
|
114
127
|
if (!$entity) {
|
|
115
128
|
throw new UqlUsageError('$entity is required when using query-object syntax');
|
|
116
129
|
}
|
|
117
|
-
return [$entity, q
|
|
130
|
+
return [$entity, q];
|
|
118
131
|
}
|
|
119
|
-
/** Whether the entity is passed first,
|
|
132
|
+
/** Whether the entity is passed first, the first argument a class rather than a query object. */
|
|
120
133
|
function isEntityFirst(args) {
|
|
121
|
-
|
|
122
|
-
return typeof first === 'function' && first.prototype !== undefined;
|
|
134
|
+
return isClass(args[0]);
|
|
123
135
|
}
|
|
124
|
-
/** The fields `returning` projects, read as `$select` is, or `undefined` where it names none
|
|
136
|
+
/** The fields `returning` projects, read as `$select` is, every field a read returns for `true`, or `undefined` where it names none. */
|
|
125
137
|
function returnedKeys(meta, returning) {
|
|
138
|
+
if (typeof returning === 'boolean') {
|
|
139
|
+
return returning ? defaultReadKeys(meta) : undefined;
|
|
140
|
+
}
|
|
126
141
|
return hasKeys(returning) ? normalizeScalarFieldSelection(meta, returning) : undefined;
|
|
127
142
|
}
|
|
128
143
|
/** Whether an update of `row` writes a column, its `onUpdate` fills included: one that writes none sends nothing. */
|
|
@@ -312,8 +327,8 @@ export class AbstractQuerier {
|
|
|
312
327
|
const [entity, q, opts] = entityArgs(args);
|
|
313
328
|
return (await this.internalCount(entity, { $where: q.$where, $limit: 1 }, opts)) > 0;
|
|
314
329
|
}
|
|
315
|
-
aggregate(
|
|
316
|
-
return this.internalAggregate(
|
|
330
|
+
aggregate(...args) {
|
|
331
|
+
return this.internalAggregate(...entityArgs(args));
|
|
317
332
|
}
|
|
318
333
|
async insertOne(entity, payload, opts) {
|
|
319
334
|
const [written] = await this.insertWritten(entity, [payload], opts);
|
|
@@ -409,8 +424,8 @@ export class AbstractQuerier {
|
|
|
409
424
|
const updated = await this.updateWhere(entity, { $where: whereIds(getMeta(entity), id) }, payload, opts);
|
|
410
425
|
return typeof updated === 'number' ? updated : updated[0];
|
|
411
426
|
}
|
|
412
|
-
async updateMany(
|
|
413
|
-
return this.updateWhere(
|
|
427
|
+
async updateMany(...args) {
|
|
428
|
+
return this.updateWhere(...payloadArgs(args));
|
|
414
429
|
}
|
|
415
430
|
/**
|
|
416
431
|
* The number of rows the update changed, or the rows `returning` reads back: settled first and held, so the
|
|
@@ -571,7 +586,8 @@ export class AbstractQuerier {
|
|
|
571
586
|
assertIdValue(entity, id);
|
|
572
587
|
return this.restoreMany(entity, { $where: whereIds(getMeta(entity), id) });
|
|
573
588
|
}
|
|
574
|
-
async restoreMany(
|
|
589
|
+
async restoreMany(...args) {
|
|
590
|
+
const [entity, q] = entityArgs(args);
|
|
575
591
|
const meta = getMeta(entity);
|
|
576
592
|
if (!meta.softDelete) {
|
|
577
593
|
throw new UqlUsageError(`'${entity.name}' has not enabled 'softDelete'`);
|
|
@@ -581,40 +597,42 @@ export class AbstractQuerier {
|
|
|
581
597
|
// them racing agree on the result anyway. A lock is for content, and a restore writes none.
|
|
582
598
|
return this.hooked(entity, 'Update', [{ [meta.softDelete]: null }], ([row]) => this.updateRows(entity, { ...q, $where }, row, withDeleted(), undefined));
|
|
583
599
|
}
|
|
584
|
-
async upsertOne(
|
|
585
|
-
const [
|
|
600
|
+
async upsertOne(...args) {
|
|
601
|
+
const [entity, q, payload, opts] = payloadArgs(args);
|
|
602
|
+
const [written] = await this.upsertMany(entity, q, [payload], opts);
|
|
586
603
|
return written;
|
|
587
604
|
}
|
|
588
|
-
async upsertMany(
|
|
605
|
+
async upsertMany(...args) {
|
|
606
|
+
const [entity, q, payload, opts] = payloadArgs(args);
|
|
589
607
|
const meta = getMeta(entity);
|
|
590
608
|
const keys = returnedKeys(meta, opts?.returning);
|
|
591
609
|
const reportingWrites = keys && this.reportingWrites(meta, 'upsert');
|
|
592
610
|
// Fires `beforeUpsert`/`afterUpsert`: which branch a row takes is the database's to decide, so neither
|
|
593
611
|
// the insert's nor the update's pair fits. How to write is decided on the rows the hooks hand over.
|
|
594
612
|
if (!keys || !reportingWrites) {
|
|
595
|
-
return this.writtenOrReadBack(entity, () => this.hooked(entity, 'Upsert', payload, (rows) => this.upsertIds(entity, q, rows
|
|
613
|
+
return this.writtenOrReadBack(entity, () => this.hooked(entity, 'Upsert', payload, (rows) => this.upsertIds(entity, q, rows)), opts);
|
|
596
614
|
}
|
|
597
|
-
return this.atomically(() => this.hooked(entity, 'Upsert', payload, async (rows) => upsertsByReading(meta,
|
|
598
|
-
? this.readBack(entity, await this.upsertIds(entity, q, rows
|
|
599
|
-
: this.upsertReturning(reportingWrites, entity, q, rows,
|
|
615
|
+
return this.atomically(() => this.hooked(entity, 'Upsert', payload, async (rows) => upsertsByReading(meta, q, rows)
|
|
616
|
+
? this.readBack(entity, await this.upsertIds(entity, q, rows), keys)
|
|
617
|
+
: this.upsertReturning(reportingWrites, entity, q, rows, keys)));
|
|
600
618
|
}
|
|
601
619
|
/** Upserts `rows`, reading first where {@link upsertsByReading} says so, and answers the id each row got. */
|
|
602
|
-
async upsertIds(entity, q, rows
|
|
620
|
+
async upsertIds(entity, q, rows) {
|
|
603
621
|
const meta = getMeta(entity);
|
|
604
|
-
const ids = upsertsByReading(meta,
|
|
605
|
-
? await this.readThenUpsert(entity, q, rows
|
|
606
|
-
: await this.internalUpsertMany(entity, q, rows
|
|
622
|
+
const ids = upsertsByReading(meta, q, rows)
|
|
623
|
+
? await this.readThenUpsert(entity, q, rows)
|
|
624
|
+
: await this.internalUpsertMany(entity, q, rows);
|
|
607
625
|
adoptReportedIds(meta, rows, ids);
|
|
608
626
|
return writtenIds(meta, rows);
|
|
609
627
|
}
|
|
610
628
|
/**
|
|
611
629
|
* An upsert of one statement reporting each row as it stands after it: off its `RETURNING` where it wrote the row,
|
|
612
|
-
* else, where its `$where` or an empty
|
|
630
|
+
* else, where its `$where` or an empty `$update` kept it, read by the conflict keys.
|
|
613
631
|
*/
|
|
614
|
-
async upsertReturning(writes, entity, q, rows,
|
|
632
|
+
async upsertReturning(writes, entity, q, rows, keys) {
|
|
615
633
|
const meta = getMeta(entity);
|
|
616
634
|
const read = [...new Set([...keys, ...meta.ids, ...getKeys(q.$conflict)])];
|
|
617
|
-
const written = await writes.upsert(entity, q, rows,
|
|
635
|
+
const written = await writes.upsert(entity, q, rows, read);
|
|
618
636
|
const keptAt = rows.flatMap((_, index) => (written[index] === undefined ? [index] : []));
|
|
619
637
|
const kept = await this.rowsByConflict(entity, q.$conflict, keptAt.map((index) => rows[index]), read);
|
|
620
638
|
const keptBy = new Map(keptAt.map((index, position) => [index, kept[position]]));
|
|
@@ -638,7 +656,7 @@ export class AbstractQuerier {
|
|
|
638
656
|
* relations and a guarded row stays its tenant's. Inside a transaction a found row is held until the write, and
|
|
639
657
|
* a row inserted concurrently fails on its key. See architecture/security-filter-writes.md.
|
|
640
658
|
*/
|
|
641
|
-
async readThenUpsert(entity, q, rows
|
|
659
|
+
async readThenUpsert(entity, q, rows) {
|
|
642
660
|
const meta = getMeta(entity);
|
|
643
661
|
guardWrite(meta, rows, 'insert');
|
|
644
662
|
if (!rows.length) {
|
|
@@ -651,7 +669,7 @@ export class AbstractQuerier {
|
|
|
651
669
|
if (id === undefined) {
|
|
652
670
|
continue;
|
|
653
671
|
}
|
|
654
|
-
const assigned = foundRowAssigned(meta, q
|
|
672
|
+
const assigned = foundRowAssigned(meta, q, row);
|
|
655
673
|
if (assigned) {
|
|
656
674
|
const $where = q.$where ? whereAnd(whereIds(meta, id), [q.$where]) : whereIds(meta, id);
|
|
657
675
|
await this.updateRows(entity, { $where }, assigned, withDeleted(), meta.version).catch((error) => {
|
|
@@ -120,7 +120,7 @@ export declare abstract class AbstractSqlQuerier extends AbstractQuerier impleme
|
|
|
120
120
|
* skipped by `DO NOTHING` or its `$where`, is read back by its conflict keys, unless it keeps its own key (`kept`,
|
|
121
121
|
* asked before the statement fills `onInsert` keys in).
|
|
122
122
|
*/
|
|
123
|
-
protected internalUpsertMany<E extends object>(entity: Type<E>, q: QueryUpsert<E>, payload: E[]
|
|
123
|
+
protected internalUpsertMany<E extends object>(entity: Type<E>, q: QueryUpsert<E>, payload: E[]): Promise<(UpsertedId<E> | undefined)[]>;
|
|
124
124
|
/** The ids an upsert's header reports, by index: a statement of one row assigning a found row names its key. */
|
|
125
125
|
private headerIds;
|
|
126
126
|
/**
|
|
@@ -362,12 +362,12 @@ export class AbstractSqlQuerier extends AbstractQuerier {
|
|
|
362
362
|
#reportingWrites = {
|
|
363
363
|
update: (entity, q, payload, opts, keys) => this.readRows((ctx) => this.dialect.update(ctx, entity, q, payload, opts, keys)),
|
|
364
364
|
delete: (entity, q, opts, keys) => this.readRows((ctx) => this.dialect.delete(ctx, entity, q, opts, keys)),
|
|
365
|
-
upsert: (entity, q, payload,
|
|
365
|
+
upsert: (entity, q, payload, keys) => {
|
|
366
366
|
const nullKey = getKeys(q.$conflict).find((key) => payload.some((row) => row[key] === null));
|
|
367
367
|
if (nullKey !== undefined) {
|
|
368
368
|
throw new UqlUsageError(`a row's '${nullKey}' is null: no row reads back by it, so its upsert cannot report it`);
|
|
369
369
|
}
|
|
370
|
-
return this.upsertedRows(entity, q, clone(payload),
|
|
370
|
+
return this.upsertedRows(entity, q, clone(payload), keys);
|
|
371
371
|
},
|
|
372
372
|
insert: (entity, rows, keys) => this.insertedRows(entity, rows, keys),
|
|
373
373
|
};
|
|
@@ -376,7 +376,7 @@ export class AbstractSqlQuerier extends AbstractQuerier {
|
|
|
376
376
|
* skipped by `DO NOTHING` or its `$where`, is read back by its conflict keys, unless it keeps its own key (`kept`,
|
|
377
377
|
* asked before the statement fills `onInsert` keys in).
|
|
378
378
|
*/
|
|
379
|
-
async internalUpsertMany(entity, q, payload
|
|
379
|
+
async internalUpsertMany(entity, q, payload) {
|
|
380
380
|
if (!payload.length) {
|
|
381
381
|
return [];
|
|
382
382
|
}
|
|
@@ -384,14 +384,14 @@ export class AbstractSqlQuerier extends AbstractQuerier {
|
|
|
384
384
|
const meta = getMeta(entity);
|
|
385
385
|
const kept = payload.map((row) => upsertKeepsOwnKey(meta, q.$conflict, row));
|
|
386
386
|
const ids = this.dialect.features.reportsWrites.includes('upsert')
|
|
387
|
-
? (await this.upsertedRows(entity, q, payload,
|
|
388
|
-
: await this.headerIds(entity, q, payload
|
|
387
|
+
? (await this.upsertedRows(entity, q, payload, [...meta.ids, ...getKeys(q.$conflict)])).map((row) => row && idOf(meta, row))
|
|
388
|
+
: await this.headerIds(entity, q, payload);
|
|
389
389
|
return this.withUnreportedIds(entity, q.$conflict, payload, ids, kept);
|
|
390
390
|
}
|
|
391
391
|
/** The ids an upsert's header reports, by index: a statement of one row assigning a found row names its key. */
|
|
392
|
-
async headerIds(entity, q, payload
|
|
393
|
-
const groups = this.upsertGroups(entity, q, payload
|
|
394
|
-
const results = await this.runStatements(groups.map(({ indexes }) => this.bound((ctx) => this.dialect.upsert(ctx, entity, q, indexes.map((index) => payload[index])
|
|
392
|
+
async headerIds(entity, q, payload) {
|
|
393
|
+
const groups = this.upsertGroups(entity, q, payload);
|
|
394
|
+
const results = await this.runStatements(groups.map(({ indexes }) => this.bound((ctx) => this.dialect.upsert(ctx, entity, q, indexes.map((index) => payload[index])))), groups.length > 1);
|
|
395
395
|
const ids = new Array(payload.length);
|
|
396
396
|
groups.forEach(({ indexes, reports }, group) => {
|
|
397
397
|
if (reports && results[group].id) {
|
|
@@ -405,9 +405,9 @@ export class AbstractSqlQuerier extends AbstractQuerier {
|
|
|
405
405
|
* the conflict keys `keys` includes, as no engine promises that order. `undefined` for a row it kept, or with no
|
|
406
406
|
* conflict key. The statement fills `payload`'s `onInsert` fields.
|
|
407
407
|
*/
|
|
408
|
-
async upsertedRows(entity, q, payload,
|
|
408
|
+
async upsertedRows(entity, q, payload, keys) {
|
|
409
409
|
const conflictKeys = getKeys(q.$conflict);
|
|
410
|
-
const read = await this.readEach(this.upsertGroups(entity, q, payload
|
|
410
|
+
const read = await this.readEach(this.upsertGroups(entity, q, payload).map(({ indexes }) => (ctx) => this.dialect.upsert(ctx, entity, q, indexes.map((index) => payload[index]), keys)));
|
|
411
411
|
const written = new Map(read.flat().map((row) => [rowKey(row, conflictKeys), row]));
|
|
412
412
|
return payload.map((row) => namesEvery(row, conflictKeys) ? written.get(rowKey(row, conflictKeys)) : undefined);
|
|
413
413
|
}
|
|
@@ -417,10 +417,10 @@ export class AbstractSqlQuerier extends AbstractQuerier {
|
|
|
417
417
|
* the one engine with one, SQL Server, upserts through a MERGE, whose source takes any number. `reports`: the
|
|
418
418
|
* statement's header names its row's id, which spares the read back.
|
|
419
419
|
*/
|
|
420
|
-
upsertGroups(entity, q, payload
|
|
420
|
+
upsertGroups(entity, q, payload) {
|
|
421
421
|
const meta = getMeta(entity);
|
|
422
422
|
return groupByInsertShape(meta, payload).flatMap((group) => {
|
|
423
|
-
const { binds, assigns } = this.dialect.upsertAssignments(entity, q, payload[group[0]]
|
|
423
|
+
const { binds, assigns } = this.dialect.upsertAssignments(entity, q, payload[group[0]]);
|
|
424
424
|
const chunks = chunkWithinLimits(meta, payload, group, this.dialect.maxBindValues - binds);
|
|
425
425
|
return chunks.map((indexes) => ({
|
|
426
426
|
indexes,
|
|
@@ -85,6 +85,8 @@ export declare class SqliteDialect extends AbstractSqlDialect {
|
|
|
85
85
|
protected jsonPathReading(escapedColumn: string, path: string, mode: 'json' | 'text'): string;
|
|
86
86
|
protected numericCast(expr: string): string;
|
|
87
87
|
protected jsonCast(operand: string): string;
|
|
88
|
+
/** Read through {@link jsonCast}: a scalar subquery's value loses its JSON subtype at its edge on SQLite 3.54+. */
|
|
89
|
+
protected jsonSubquery(select: string): string;
|
|
88
90
|
/** `JSON` keeps each element's JSON type in the array, which answers `[]` for no rows. */
|
|
89
91
|
protected jsonArrayOf(elem: string): string;
|
|
90
92
|
protected jsonAppended(ctx: QueryContext, array: string, value: unknown, unless?: string): string;
|
|
@@ -222,6 +222,10 @@ export class SqliteDialect extends AbstractSqlDialect {
|
|
|
222
222
|
jsonCast(operand) {
|
|
223
223
|
return `JSON(${operand})`;
|
|
224
224
|
}
|
|
225
|
+
/** Read through {@link jsonCast}: a scalar subquery's value loses its JSON subtype at its edge on SQLite 3.54+. */
|
|
226
|
+
jsonSubquery(select) {
|
|
227
|
+
return this.jsonCast(`(${select})`);
|
|
228
|
+
}
|
|
225
229
|
/** `JSON` keeps each element's JSON type in the array, which answers `[]` for no rows. */
|
|
226
230
|
jsonArrayOf(elem) {
|
|
227
231
|
return `JSON_GROUP_ARRAY(JSON(${elem}))`;
|
package/dist/type/dialect.d.ts
CHANGED
|
@@ -283,7 +283,6 @@ export type TriggerWrite = {
|
|
|
283
283
|
readonly entity: AbstractType<object>;
|
|
284
284
|
readonly q: QueryUpsert<object>;
|
|
285
285
|
readonly row: Readonly<Record<string, unknown>>;
|
|
286
|
-
readonly update?: Readonly<Record<string, unknown>>;
|
|
287
286
|
} | {
|
|
288
287
|
readonly kind: 'update';
|
|
289
288
|
readonly entity: AbstractType<object>;
|
|
@@ -337,8 +336,8 @@ export interface SqlQueryDialect {
|
|
|
337
336
|
insert<E>(ctx: QueryContext, entity: AbstractType<E>, payload: E | E[], returning?: readonly FieldKey<E>[]): readonly SelectTerm[];
|
|
338
337
|
/** An update of the records the query matches. */
|
|
339
338
|
update<E>(ctx: QueryContext, entity: AbstractType<E>, q: QuerySearch<E>, payload: UpdatePayload<E>, opts?: QueryRenderOptions, returning?: readonly FieldKey<E>[]): readonly SelectTerm[];
|
|
340
|
-
/** An upsert of one record or many as `q` names its rows, a conflicting row taking `update` where given. */
|
|
341
|
-
upsert<E>(ctx: QueryContext, entity: AbstractType<E>, q: QueryUpsert<E>, payload: E | E[],
|
|
339
|
+
/** An upsert of one record or many as `q` names its rows, a conflicting row taking `q.$update` where given. */
|
|
340
|
+
upsert<E>(ctx: QueryContext, entity: AbstractType<E>, q: QueryUpsert<E>, payload: E | E[], returning?: readonly FieldKey<E>[]): readonly SelectTerm[];
|
|
342
341
|
/** A write in a trigger's body; `rows` are what a set-based engine's body reads, narrowed to the ones it fires for. */
|
|
343
342
|
triggerWrite(ctx: QueryContext, write: TriggerWrite, rows?: TriggerRows): void;
|
|
344
343
|
/** A delete of the records the query matches, a soft delete where the entity has one. */
|
package/dist/type/querier.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import type { AbstractSqlDialect } from '../dialect/index.js';
|
|
2
2
|
import type { SqlDialectName } from './dialect.js';
|
|
3
|
-
import type { FieldKey, HookEvent } from './entity.js';
|
|
3
|
+
import type { EntityWrite, FieldKey, HookEvent, UpdateWrite } from './entity.js';
|
|
4
4
|
import type { LoggingOptions } from './logger.js';
|
|
5
5
|
import type { NamingStrategy } from './namingStrategy.js';
|
|
6
|
-
import type { Captured, QueryFilter, QueryFindResult, QueryOptions, QueryPage, QuerySearch, QueryUpdateResult, WriteResults, WriteOptions } from './query.js';
|
|
6
|
+
import type { Captured, QueryFilter, QueryFindResult, QueryOptions, QueryPage, QuerySearch, QueryUpdateResult, QueryUpsert, WriteResults, WriteOptions } from './query.js';
|
|
7
|
+
import type { QueryAggregate, QueryAggregateResult } from './queryAggregate.js';
|
|
7
8
|
import type { QuerySql, SqlStatement } from './querySql.js';
|
|
8
9
|
import type { EntityRead, ReadResults, ReadQueries, UniversalQuerier } from './universalQuerier.js';
|
|
9
10
|
import type { BooleanLike, Type, RawRow, AbstractType } from './utility.js';
|
|
@@ -50,7 +51,7 @@ type DualRead<K extends keyof ReadQueries<object, never>, R extends keyof ReadRe
|
|
|
50
51
|
}>(q: Captured<ReadQueries<E, QuerySql>[K] & {
|
|
51
52
|
$entity: Type<E>;
|
|
52
53
|
}, Q>, opts?: QueryOptions) => ReadResults<'server', QueryFindResult<E, Q>>[R]) & EntityRead<K, R, 'server', QueryOptions>;
|
|
53
|
-
/**
|
|
54
|
+
/** Each method below taking a query takes the entity first or as the query's `$entity`, as {@link DualRead} does. */
|
|
54
55
|
export interface Querier extends UniversalQuerier {
|
|
55
56
|
findOne: DualRead<'one', 'one'>;
|
|
56
57
|
findMany: DualRead<'many', 'many'>;
|
|
@@ -60,21 +61,33 @@ export interface Querier extends UniversalQuerier {
|
|
|
60
61
|
findManyAndCount: DualRead<'many', 'counted'>;
|
|
61
62
|
/** Read a page of records from a cursor. */
|
|
62
63
|
findManyPage: DualRead<'page', 'page'>;
|
|
63
|
-
|
|
64
|
-
count<E extends object>(q: QueryPage<E> & {
|
|
64
|
+
count: (<E extends object>(q: QueryPage<E> & {
|
|
65
65
|
$entity: Type<E>;
|
|
66
|
-
}, opts?: QueryOptions)
|
|
67
|
-
|
|
68
|
-
/** Whether anything matches, the entity passed first or as the query's `$entity`. */
|
|
69
|
-
exists<E extends object>(q: QueryFilter<E> & {
|
|
66
|
+
}, opts?: QueryOptions) => Promise<number>) & UniversalQuerier['count'];
|
|
67
|
+
exists: (<E extends object>(q: QueryFilter<E> & {
|
|
70
68
|
$entity: Type<E>;
|
|
71
|
-
}, opts?: QueryOptions)
|
|
72
|
-
|
|
73
|
-
/** Delete many records, the entity passed first or as `$entity`; soft-deletes where the entity has a soft-delete field. */
|
|
74
|
-
deleteMany<E extends object>(q: QuerySearch<E> & {
|
|
69
|
+
}, opts?: QueryOptions) => Promise<boolean>) & UniversalQuerier['exists'];
|
|
70
|
+
updateMany: (<E extends object, const S extends FieldKey<E> = never, const V extends BooleanLike = true, const W extends boolean = false>(q: QuerySearch<E> & {
|
|
75
71
|
$entity: Type<E>;
|
|
76
|
-
}, opts?: QueryOptions)
|
|
77
|
-
|
|
72
|
+
}, payload: UpdateWrite<E>, opts?: QueryOptions & WriteOptions<E, S, V, W>) => Promise<WriteResults<E, S, V, W>['affectedMany']>) & UniversalQuerier['updateMany'];
|
|
73
|
+
restoreMany: (<E extends object>(q: QuerySearch<E> & {
|
|
74
|
+
$entity: Type<E>;
|
|
75
|
+
}) => Promise<number>) & UniversalQuerier['restoreMany'];
|
|
76
|
+
aggregate: (<E extends object, const Q extends QueryAggregate<E> & {
|
|
77
|
+
$entity: Type<E>;
|
|
78
|
+
}>(q: Captured<QueryAggregate<E, Q> & {
|
|
79
|
+
$entity: Type<E>;
|
|
80
|
+
}, Q>, opts?: QueryOptions) => Promise<QueryAggregateResult<E, Q>[]>) & UniversalQuerier['aggregate'];
|
|
81
|
+
/** Soft-deletes where the entity has a soft-delete field. */
|
|
82
|
+
deleteMany: (<E extends object, const S extends FieldKey<E> = never, const V extends BooleanLike = true, const W extends boolean = false>(q: QuerySearch<E> & {
|
|
83
|
+
$entity: Type<E>;
|
|
84
|
+
}, opts?: QueryOptions & WriteOptions<E, S, V, W>) => Promise<WriteResults<E, S, V, W>['affectedMany']>) & UniversalQuerier['deleteMany'];
|
|
85
|
+
upsertOne: (<E extends object, const S extends FieldKey<E> = never, const V extends BooleanLike = true, const W extends boolean = false>(q: QueryUpsert<E> & {
|
|
86
|
+
$entity: Type<E>;
|
|
87
|
+
}, payload: EntityWrite<E>, opts?: WriteOptions<E, S, V, W>) => Promise<WriteResults<E, S, V, W>['inserted']>) & UniversalQuerier['upsertOne'];
|
|
88
|
+
upsertMany: (<E extends object, const S extends FieldKey<E> = never, const V extends BooleanLike = true, const W extends boolean = false>(q: QueryUpsert<E> & {
|
|
89
|
+
$entity: Type<E>;
|
|
90
|
+
}, payload: readonly EntityWrite<E>[], opts?: WriteOptions<E, S, V, W>) => Promise<WriteResults<E, S, V, W>['inserted'][]>) & UniversalQuerier['upsertMany'];
|
|
78
91
|
/**
|
|
79
92
|
* whether this querier is in a transaction or not.
|
|
80
93
|
*/
|