imodel 0.19.1 → 0.19.2
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/index.d.mts +32 -8
- package/index.mjs +105 -44
- package/migrate.d.mts +1 -1
- package/migrate.mjs +1 -1
- package/package.json +1 -1
package/index.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* imodel v0.19.
|
|
2
|
+
* imodel v0.19.2
|
|
3
3
|
* (c) 2019-2026 undefined
|
|
4
4
|
* @license undefined
|
|
5
5
|
*/
|
|
@@ -1373,6 +1373,7 @@ interface Options {
|
|
|
1373
1373
|
update?: Record<string, SetValue>;
|
|
1374
1374
|
alias?: string;
|
|
1375
1375
|
group?: string[];
|
|
1376
|
+
lock?: 'S' | 'X' | null;
|
|
1376
1377
|
}
|
|
1377
1378
|
interface Queryable<T extends Fields = Fields> extends TableDefine<T> {
|
|
1378
1379
|
readonly options: Options;
|
|
@@ -1397,6 +1398,7 @@ interface JOIN {
|
|
|
1397
1398
|
alias?: string;
|
|
1398
1399
|
condition?: WhereValue[];
|
|
1399
1400
|
}
|
|
1401
|
+
type LockType = 'S' | 'X';
|
|
1400
1402
|
interface SelectItem {
|
|
1401
1403
|
distinct?: boolean;
|
|
1402
1404
|
select: [string, Select][];
|
|
@@ -1413,6 +1415,7 @@ interface SelectItem {
|
|
|
1413
1415
|
offset?: number;
|
|
1414
1416
|
/** 最大返回数 */
|
|
1415
1417
|
limit?: number;
|
|
1418
|
+
lock?: LockType | null;
|
|
1416
1419
|
}
|
|
1417
1420
|
type GetName = (table?: string) => string;
|
|
1418
1421
|
interface DBColumn {
|
|
@@ -1486,6 +1489,17 @@ interface Skip {
|
|
|
1486
1489
|
interface TransactionFn {
|
|
1487
1490
|
<T>(fn: (t: Connection) => PromiseLike<T> | T): Promise<T>;
|
|
1488
1491
|
}
|
|
1492
|
+
type TransactionHandlerArray = [
|
|
1493
|
+
promise: TransactionHandler['promise'],
|
|
1494
|
+
commit: TransactionHandler['commit'],
|
|
1495
|
+
rollback: TransactionHandler['rollback']
|
|
1496
|
+
];
|
|
1497
|
+
interface TransactionHandler extends TransactionHandlerArray {
|
|
1498
|
+
promise: Promise<void>;
|
|
1499
|
+
commit(): Promise<void>;
|
|
1500
|
+
rollback(e?: any): Promise<void>;
|
|
1501
|
+
[Symbol.dispose](): void;
|
|
1502
|
+
}
|
|
1489
1503
|
|
|
1490
1504
|
declare const Destroy: unique symbol;
|
|
1491
1505
|
interface Destroy {
|
|
@@ -1991,9 +2005,10 @@ declare class Connection<E extends {} = {}> {
|
|
|
1991
2005
|
transaction<T>(fn: (t: Connection) => PromiseLike<T> | T): Promise<T>;
|
|
1992
2006
|
/**
|
|
1993
2007
|
* @overload
|
|
1994
|
-
* @returns {
|
|
2008
|
+
* @returns {TransactionHandler}
|
|
1995
2009
|
*/
|
|
1996
|
-
transaction():
|
|
2010
|
+
transaction(): TransactionHandler;
|
|
2011
|
+
[Symbol.dispose](): void;
|
|
1997
2012
|
#private;
|
|
1998
2013
|
}
|
|
1999
2014
|
|
|
@@ -2014,8 +2029,12 @@ declare function isPseudo(model: Queryable): boolean;
|
|
|
2014
2029
|
|
|
2015
2030
|
/** @import { SelectItem } from './types' */
|
|
2016
2031
|
declare class Subquery {
|
|
2017
|
-
/**
|
|
2018
|
-
|
|
2032
|
+
/**
|
|
2033
|
+
*
|
|
2034
|
+
* @param {boolean?} [lockable]
|
|
2035
|
+
* @returns {SelectItem[]}
|
|
2036
|
+
*/
|
|
2037
|
+
toSubquery(lockable?: boolean | null): SelectItem[];
|
|
2019
2038
|
}
|
|
2020
2039
|
|
|
2021
2040
|
type QueryOptions<T extends Fields, TB extends unknown = any> = {
|
|
@@ -2082,10 +2101,10 @@ declare class Query<T extends Fields, TB extends unknown = any> extends Subquery
|
|
|
2082
2101
|
withDeleted(): this;
|
|
2083
2102
|
/**
|
|
2084
2103
|
* 增加排序项
|
|
2085
|
-
* @param {...string | boolean | [string, boolean?]} sort
|
|
2104
|
+
* @param {...string | null | boolean | [string, boolean?]} sort
|
|
2086
2105
|
* @returns {this}
|
|
2087
2106
|
*/
|
|
2088
|
-
sort(...sort: (string | boolean | [string, boolean?])[]): this;
|
|
2107
|
+
sort(...sort: (string | null | boolean | [string, boolean?])[]): this;
|
|
2089
2108
|
/**
|
|
2090
2109
|
* 设置获取到的最大数量
|
|
2091
2110
|
* @param {number} [limit]
|
|
@@ -2219,6 +2238,11 @@ declare class Query<T extends Fields, TB extends unknown = any> extends Subquery
|
|
|
2219
2238
|
* @param {boolean} [distinct]
|
|
2220
2239
|
*/
|
|
2221
2240
|
distinct(distinct?: boolean): this;
|
|
2241
|
+
/**
|
|
2242
|
+
*
|
|
2243
|
+
* @param {LockType?} [lock]
|
|
2244
|
+
*/
|
|
2245
|
+
lock(lock?: LockType | null): this;
|
|
2222
2246
|
/**
|
|
2223
2247
|
*
|
|
2224
2248
|
* @param {object} a
|
|
@@ -2674,4 +2698,4 @@ declare function toBool(v: any): boolean;
|
|
|
2674
2698
|
declare function setDevelopment(d?: boolean): void;
|
|
2675
2699
|
declare let isDevelopment: boolean;
|
|
2676
2700
|
|
|
2677
|
-
export { Build, type ClassDecorator, type Column, type ColumnOptions, Connection, type Constraint, Create, type DBColumn, type DBIndex, type DBTable, Destroy, type Environment, type FieldDecorator, type FieldDefine, type FieldDefineOption, type FieldDefineType, type FieldSpecific, type FieldSpecificValue, type FieldType, type FieldTypeDefine, type FieldValue, type Fields, type FindArg, type FindRange, type GetName, type Hook, type Hooks, type IConnection, type Index, type IndexInfo, type IndexOptions, type JOIN, type Join, type JoinType, type MainFieldType, type MaybePromise, type MethodDecorator, Model, type Options, PseudoDestroy, Query, type QueryOptions, type Queryable, Save, Scene, Select, type SelectItem, SetValue, type Skip, Submodel, type Support, type TableConnect, type TableConnection, type TableDefine, type TableType, type TableValue, type ToFieldType, type ToType, type TransactionFn, type VirtualTable, Where, type WhereExists, type WhereItem, type WhereLike, type WhereOr, type WhereRaw, type WhereValue, type Wheres, afterCreate, afterCreateMany, afterDelete, afterUpdate, beforeCreate, beforeCreateMany, beforeDelete, beforeUpdate, bindHooks, coefficient, column, creating, decrement, defaultValue, field as define, deleted, deleting, divide, field$1 as field, getPrimaryFields, getPrimaryKeys, hooks, immutable, increment, index, isDevelopment, isPseudo, mergeHooks, model, multiply, now, primary, prop, pseudo, setDevelopment, sort, model as submodel, toBool, toNot, toPrimaries, toPrimary, uncreatable, undeleted, updating, uuid, values, where, withDeleted };
|
|
2701
|
+
export { Build, type ClassDecorator, type Column, type ColumnOptions, Connection, type Constraint, Create, type DBColumn, type DBIndex, type DBTable, Destroy, type Environment, type FieldDecorator, type FieldDefine, type FieldDefineOption, type FieldDefineType, type FieldSpecific, type FieldSpecificValue, type FieldType, type FieldTypeDefine, type FieldValue, type Fields, type FindArg, type FindRange, type GetName, type Hook, type Hooks, type IConnection, type Index, type IndexInfo, type IndexOptions, type JOIN, type Join, type JoinType, type LockType, type MainFieldType, type MaybePromise, type MethodDecorator, Model, type Options, PseudoDestroy, Query, type QueryOptions, type Queryable, Save, Scene, Select, type SelectItem, SetValue, type Skip, Submodel, type Support, type TableConnect, type TableConnection, type TableDefine, type TableType, type TableValue, type ToFieldType, type ToType, type TransactionFn, type TransactionHandler, type TransactionHandlerArray, type VirtualTable, Where, type WhereExists, type WhereItem, type WhereLike, type WhereOr, type WhereRaw, type WhereValue, type Wheres, afterCreate, afterCreateMany, afterDelete, afterUpdate, beforeCreate, beforeCreateMany, beforeDelete, beforeUpdate, bindHooks, coefficient, column, creating, decrement, defaultValue, field as define, deleted, deleting, divide, field$1 as field, getPrimaryFields, getPrimaryKeys, hooks, immutable, increment, index, isDevelopment, isPseudo, mergeHooks, model, multiply, now, primary, prop, pseudo, setDevelopment, sort, model as submodel, toBool, toNot, toPrimaries, toPrimary, uncreatable, undeleted, updating, uuid, values, where, withDeleted };
|
package/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* imodel v0.19.
|
|
2
|
+
* imodel v0.19.2
|
|
3
3
|
* (c) 2019-2026 undefined
|
|
4
4
|
* @license undefined
|
|
5
5
|
*/
|
|
@@ -15,8 +15,12 @@ function setDevelopment(d) {
|
|
|
15
15
|
|
|
16
16
|
/** @import { SelectItem } from './types' */
|
|
17
17
|
class Subquery {
|
|
18
|
-
/**
|
|
19
|
-
|
|
18
|
+
/**
|
|
19
|
+
*
|
|
20
|
+
* @param {boolean?} [lockable]
|
|
21
|
+
* @returns {SelectItem[]}
|
|
22
|
+
*/
|
|
23
|
+
toSubquery(lockable) {
|
|
20
24
|
throw new Error('toSubquery 方法未在子类中实现');
|
|
21
25
|
}
|
|
22
26
|
}
|
|
@@ -803,28 +807,33 @@ var values = /*#__PURE__*/Object.freeze({
|
|
|
803
807
|
/** @import { Options, Queryable } from '../types' */
|
|
804
808
|
/**
|
|
805
809
|
*
|
|
806
|
-
* @param {Queryable}
|
|
810
|
+
* @param {Partial<Queryable>} queryable
|
|
807
811
|
* @returns {Options}
|
|
808
812
|
*/
|
|
809
|
-
function getOptions(
|
|
813
|
+
function getOptions({
|
|
814
|
+
fields,
|
|
815
|
+
pseudo,
|
|
816
|
+
options: modelOptions
|
|
817
|
+
}) {
|
|
810
818
|
const options = {
|
|
811
|
-
|
|
819
|
+
where: [],
|
|
820
|
+
...modelOptions
|
|
812
821
|
};
|
|
813
|
-
if (!
|
|
822
|
+
if (!pseudo || !fields) {
|
|
814
823
|
return options;
|
|
815
824
|
}
|
|
816
|
-
if (!(
|
|
825
|
+
if (!(pseudo in fields)) {
|
|
817
826
|
return options;
|
|
818
827
|
}
|
|
819
828
|
if (options.range === undeleted) {
|
|
820
829
|
options.where = [...options.where, {
|
|
821
|
-
field:
|
|
830
|
+
field: pseudo,
|
|
822
831
|
operator: '<>',
|
|
823
832
|
value: 0
|
|
824
833
|
}];
|
|
825
834
|
} else if (options.range !== withDeleted) {
|
|
826
835
|
options.where = [...options.where, {
|
|
827
|
-
field:
|
|
836
|
+
field: pseudo,
|
|
828
837
|
operator: '=',
|
|
829
838
|
value: 0
|
|
830
839
|
}];
|
|
@@ -2136,6 +2145,7 @@ function toSelectItem(select, fieldColumns) {
|
|
|
2136
2145
|
*/
|
|
2137
2146
|
function select2column(columns, fieldColumns, select) {
|
|
2138
2147
|
/** @type {[string, Select][]} */
|
|
2148
|
+
// eslint-disable-next-line no-nested-ternary
|
|
2139
2149
|
const list = Array.isArray(select) ? select.map(v => typeof v === 'string' ? [v, {
|
|
2140
2150
|
field: v
|
|
2141
2151
|
}] : v) : select ? Object.entries(select) : [];
|
|
@@ -2178,11 +2188,14 @@ function getDefaultSort(columns, noColumn) {
|
|
|
2178
2188
|
|
|
2179
2189
|
/**
|
|
2180
2190
|
*
|
|
2181
|
-
* @param {[field: string, desc: boolean][]} sort
|
|
2182
|
-
* @param {Record<string, string>?} fieldColumns
|
|
2191
|
+
* @param {[field: string, desc: boolean][]?} [sort]
|
|
2192
|
+
* @param {Record<string, string>?} [fieldColumns]
|
|
2183
2193
|
* @returns {[field: string, desc: boolean][]}
|
|
2184
2194
|
*/
|
|
2185
2195
|
function sort2column(sort, fieldColumns) {
|
|
2196
|
+
if (!sort) {
|
|
2197
|
+
return [];
|
|
2198
|
+
}
|
|
2186
2199
|
if (!fieldColumns) {
|
|
2187
2200
|
return sort;
|
|
2188
2201
|
}
|
|
@@ -2190,7 +2203,7 @@ function sort2column(sort, fieldColumns) {
|
|
|
2190
2203
|
}
|
|
2191
2204
|
|
|
2192
2205
|
/** @import Connection from './index.mjs' */
|
|
2193
|
-
/** @import { Environment, FieldDefine, Fields, Skip, TableDefine, FieldType, TableConnection, SelectItem } from '../types' */
|
|
2206
|
+
/** @import { Environment, FieldDefine, Fields, Skip, TableDefine, FieldType, TableConnection, SelectItem, LockType } from '../types' */
|
|
2194
2207
|
/**
|
|
2195
2208
|
* @param {string[] | boolean | null} [items]
|
|
2196
2209
|
*/
|
|
@@ -2248,10 +2261,11 @@ function getSelectFields(select, columns, tableFields, baseFieldSet) {
|
|
|
2248
2261
|
* @param {[string, FieldDefine<TableDefine>][]} fields
|
|
2249
2262
|
* @param {Set<string>} baseParentFieldSet
|
|
2250
2263
|
* @param {Environment} env
|
|
2264
|
+
* @param {LockType?} [lock]
|
|
2251
2265
|
* @param {Record<string, string[] | undefined>?} [returnFields]
|
|
2252
2266
|
* @returns {Promise<any[]>}
|
|
2253
2267
|
*/
|
|
2254
|
-
async function findSub(conn, list, fields, baseParentFieldSet, env, returnFields) {
|
|
2268
|
+
async function findSub(conn, list, fields, baseParentFieldSet, env, lock, returnFields) {
|
|
2255
2269
|
if (!list.length) {
|
|
2256
2270
|
return list;
|
|
2257
2271
|
}
|
|
@@ -2291,13 +2305,14 @@ async function findSub(conn, list, fields, baseParentFieldSet, env, returnFields
|
|
|
2291
2305
|
columns,
|
|
2292
2306
|
select,
|
|
2293
2307
|
where,
|
|
2294
|
-
sort: getDefaultSort(columns, noColumn)
|
|
2308
|
+
sort: getDefaultSort(columns, noColumn),
|
|
2295
2309
|
group: [],
|
|
2296
|
-
having: []
|
|
2310
|
+
having: [],
|
|
2311
|
+
lock
|
|
2297
2312
|
}];
|
|
2298
2313
|
/** @type {PromiseLike<object[]>} */
|
|
2299
2314
|
const result = table && typeof table === 'object' ? table.select(env, conn, selects) : conn.select(env, selects);
|
|
2300
|
-
let promise = result.then(list => findSub(conn, list, tableFields, baseFieldSet, env, fieldMap));
|
|
2315
|
+
let promise = result.then(list => findSub(conn, list, tableFields, baseFieldSet, env, lock, fieldMap));
|
|
2301
2316
|
promises.push(array ? promise.then(subList => {
|
|
2302
2317
|
for (const item of list) {
|
|
2303
2318
|
item[key] = subList.filter(createFilter(item, mapList));
|
|
@@ -2332,7 +2347,8 @@ async function search(conn, that, p, env, returnFields, skip) {
|
|
|
2332
2347
|
const {
|
|
2333
2348
|
sort,
|
|
2334
2349
|
offset,
|
|
2335
|
-
limit
|
|
2350
|
+
limit,
|
|
2351
|
+
lock
|
|
2336
2352
|
} = options;
|
|
2337
2353
|
const [columns, fieldColumns, tableFields] = toColumns(fields);
|
|
2338
2354
|
const where = where2column(options.where, fieldColumns);
|
|
@@ -2344,7 +2360,7 @@ async function search(conn, that, p, env, returnFields, skip) {
|
|
|
2344
2360
|
const baseFieldSet = new Set(Object.keys(fieldColumns || columns));
|
|
2345
2361
|
const selected = fieldMap && getSelectFields(fieldMap[''] || [], columns, tableFields, baseFieldSet) || null;
|
|
2346
2362
|
const select = select2column(columns, fieldColumns, selected);
|
|
2347
|
-
const sorted =
|
|
2363
|
+
const sorted = sort2column(sort, fieldColumns);
|
|
2348
2364
|
/** @type {SelectItem[]} */
|
|
2349
2365
|
const selects = [{
|
|
2350
2366
|
table: typeof table === 'string' ? table : '',
|
|
@@ -2353,16 +2369,17 @@ async function search(conn, that, p, env, returnFields, skip) {
|
|
|
2353
2369
|
where,
|
|
2354
2370
|
offset,
|
|
2355
2371
|
limit,
|
|
2356
|
-
sort: sorted
|
|
2372
|
+
sort: sorted,
|
|
2357
2373
|
group: [],
|
|
2358
|
-
having: []
|
|
2374
|
+
having: [],
|
|
2375
|
+
lock
|
|
2359
2376
|
}];
|
|
2360
2377
|
const list = table && typeof table === 'object' ? await table.select(env, conn, selects) : await conn.select(env, selects);
|
|
2361
2378
|
if (!list.length) {
|
|
2362
2379
|
return [];
|
|
2363
2380
|
}
|
|
2364
2381
|
if (returnFields) {
|
|
2365
|
-
await findSub(conn, list, tableFields, baseFieldSet, env, fieldMap);
|
|
2382
|
+
await findSub(conn, list, tableFields, baseFieldSet, env, lock, fieldMap);
|
|
2366
2383
|
}
|
|
2367
2384
|
return list;
|
|
2368
2385
|
}
|
|
@@ -2384,10 +2401,10 @@ async function first(conn, that, p, env, skip) {
|
|
|
2384
2401
|
table,
|
|
2385
2402
|
fields
|
|
2386
2403
|
} = p;
|
|
2387
|
-
// @ts-ignore
|
|
2388
2404
|
const options = getOptions(p);
|
|
2389
2405
|
const {
|
|
2390
|
-
sort
|
|
2406
|
+
sort,
|
|
2407
|
+
lock
|
|
2391
2408
|
} = options;
|
|
2392
2409
|
const [columns, fieldColumns, tableFields] = toColumns(fields);
|
|
2393
2410
|
const where = where2column(options.where, fieldColumns);
|
|
@@ -2402,14 +2419,15 @@ async function first(conn, that, p, env, skip) {
|
|
|
2402
2419
|
columns,
|
|
2403
2420
|
where,
|
|
2404
2421
|
limit: 1,
|
|
2405
|
-
sort:
|
|
2422
|
+
sort: sort2column(sort, fieldColumns),
|
|
2406
2423
|
select: select2column(columns, fieldColumns),
|
|
2407
2424
|
group: [],
|
|
2408
|
-
having: []
|
|
2425
|
+
having: [],
|
|
2426
|
+
lock
|
|
2409
2427
|
}];
|
|
2410
2428
|
const list = table && typeof table === 'object' ? await table.select(env, conn, selects) : await conn.select(env, selects);
|
|
2411
2429
|
const baseFieldSet = new Set(Object.keys(fieldColumns || columns));
|
|
2412
|
-
const [item] = await findSub(conn, list, tableFields, baseFieldSet, env);
|
|
2430
|
+
const [item] = await findSub(conn, list, tableFields, baseFieldSet, env, lock);
|
|
2413
2431
|
if (!item) {
|
|
2414
2432
|
return null;
|
|
2415
2433
|
}
|
|
@@ -2581,9 +2599,10 @@ function table2db(tables) {
|
|
|
2581
2599
|
/**
|
|
2582
2600
|
* @template {Fields} T
|
|
2583
2601
|
* @param {Queryable<T>} queryable
|
|
2602
|
+
* @param {boolean?} [lockable]
|
|
2584
2603
|
* @returns {SelectItem[]}
|
|
2585
2604
|
*/
|
|
2586
|
-
function toSelect(queryable) {
|
|
2605
|
+
function toSelect(queryable, lockable) {
|
|
2587
2606
|
const [columns, fieldColumns, tableFields] = toColumns(queryable.fields);
|
|
2588
2607
|
const {
|
|
2589
2608
|
table
|
|
@@ -2591,7 +2610,8 @@ function toSelect(queryable) {
|
|
|
2591
2610
|
const options = getOptions(queryable);
|
|
2592
2611
|
const {
|
|
2593
2612
|
offset,
|
|
2594
|
-
limit
|
|
2613
|
+
limit,
|
|
2614
|
+
lock
|
|
2595
2615
|
} = options;
|
|
2596
2616
|
const where = where2column(options.where, fieldColumns);
|
|
2597
2617
|
where.push(...getFixedValueWhere(columns));
|
|
@@ -2607,10 +2627,11 @@ function toSelect(queryable) {
|
|
|
2607
2627
|
limit,
|
|
2608
2628
|
where,
|
|
2609
2629
|
select,
|
|
2610
|
-
sort:
|
|
2630
|
+
sort: sort2column(sort, fieldColumns),
|
|
2611
2631
|
group: options.group || [],
|
|
2612
2632
|
distinct: Boolean(options.distinct),
|
|
2613
|
-
having: []
|
|
2633
|
+
having: [],
|
|
2634
|
+
lock: lockable ? lock : null
|
|
2614
2635
|
}];
|
|
2615
2636
|
return result;
|
|
2616
2637
|
}
|
|
@@ -3467,7 +3488,7 @@ class Connection {
|
|
|
3467
3488
|
table
|
|
3468
3489
|
} = queryable;
|
|
3469
3490
|
const conn = await this.#getTableConnection(queryable.connect);
|
|
3470
|
-
const selects = toSelect(queryable);
|
|
3491
|
+
const selects = toSelect(queryable, true);
|
|
3471
3492
|
const result = table && typeof table === 'object' ? await table.select(this.#env, conn, selects) : await conn.select(this.#env, selects);
|
|
3472
3493
|
return result;
|
|
3473
3494
|
}
|
|
@@ -3636,6 +3657,9 @@ class Connection {
|
|
|
3636
3657
|
}
|
|
3637
3658
|
return false;
|
|
3638
3659
|
}
|
|
3660
|
+
[Symbol.dispose]() {
|
|
3661
|
+
this.abort();
|
|
3662
|
+
}
|
|
3639
3663
|
/**
|
|
3640
3664
|
* @param {string} table
|
|
3641
3665
|
* @returns {Promise<DBTable?>}
|
|
@@ -3676,12 +3700,19 @@ class Connection {
|
|
|
3676
3700
|
*/
|
|
3677
3701
|
/**
|
|
3678
3702
|
* @overload
|
|
3679
|
-
* @returns {
|
|
3703
|
+
* @returns {TransactionHandler}
|
|
3680
3704
|
*/
|
|
3681
3705
|
/**
|
|
3682
3706
|
*
|
|
3683
|
-
* @
|
|
3684
|
-
* @
|
|
3707
|
+
* @template T
|
|
3708
|
+
* @param {(t: Connection, signal: AbortSignal) => PromiseLike<T> | T} [fn]
|
|
3709
|
+
* @returns {Promise<T> | TransactionHandler}
|
|
3710
|
+
*/
|
|
3711
|
+
/**
|
|
3712
|
+
*
|
|
3713
|
+
* @template T
|
|
3714
|
+
* @param {(t: Connection, signal: AbortSignal) => PromiseLike<T> | T} [fn]
|
|
3715
|
+
* @returns {Promise<T> | TransactionHandler}
|
|
3685
3716
|
*/
|
|
3686
3717
|
transaction(fn) {
|
|
3687
3718
|
if (typeof fn === 'function') {
|
|
@@ -3711,13 +3742,24 @@ class Connection {
|
|
|
3711
3742
|
}
|
|
3712
3743
|
return donePromise;
|
|
3713
3744
|
}).then(() => {}, () => {});
|
|
3714
|
-
|
|
3745
|
+
const handler = /** @type {TransactionHandler} */[abortedPromise, () => {
|
|
3715
3746
|
commit();
|
|
3716
3747
|
return dbiPromise;
|
|
3717
|
-
},
|
|
3718
|
-
rollback();
|
|
3748
|
+
}, e => {
|
|
3749
|
+
rollback(e);
|
|
3719
3750
|
return dbiPromise;
|
|
3720
3751
|
}];
|
|
3752
|
+
handler.promise = abortedPromise;
|
|
3753
|
+
handler.commit = () => {
|
|
3754
|
+
commit();
|
|
3755
|
+
return dbiPromise;
|
|
3756
|
+
};
|
|
3757
|
+
handler.rollback = e => {
|
|
3758
|
+
rollback(e);
|
|
3759
|
+
return dbiPromise;
|
|
3760
|
+
};
|
|
3761
|
+
handler[Symbol.dispose] = commit;
|
|
3762
|
+
return handler;
|
|
3721
3763
|
}
|
|
3722
3764
|
}
|
|
3723
3765
|
|
|
@@ -4978,7 +5020,7 @@ function toBool(v) {
|
|
|
4978
5020
|
return !falseValues.has(v.toLowerCase());
|
|
4979
5021
|
}
|
|
4980
5022
|
|
|
4981
|
-
/** @import { FieldDefine, Fields, Options, Queryable, Select, TableConnect, VirtualTable } from './types' */
|
|
5023
|
+
/** @import { FieldDefine, Fields, LockType, Options, Queryable, Select, TableConnect, VirtualTable } from './types' */
|
|
4982
5024
|
/** @import { WhereValue, Wheres } from './Where.mjs' */
|
|
4983
5025
|
/**
|
|
4984
5026
|
*
|
|
@@ -5081,8 +5123,9 @@ class Query extends Subquery {
|
|
|
5081
5123
|
get options() {
|
|
5082
5124
|
return this.#options;
|
|
5083
5125
|
}
|
|
5084
|
-
|
|
5085
|
-
|
|
5126
|
+
/** @param {boolean?} [lockable] */
|
|
5127
|
+
toSubquery(lockable) {
|
|
5128
|
+
return toSelect(this, lockable);
|
|
5086
5129
|
}
|
|
5087
5130
|
/**
|
|
5088
5131
|
* @param {QueryOptions<T, TB> | Query<T, TB>} options
|
|
@@ -5158,7 +5201,7 @@ class Query extends Subquery {
|
|
|
5158
5201
|
}
|
|
5159
5202
|
/**
|
|
5160
5203
|
* 增加排序项
|
|
5161
|
-
* @param {...string | boolean | [string, boolean?]} sort
|
|
5204
|
+
* @param {...string | null | boolean | [string, boolean?]} sort
|
|
5162
5205
|
* @returns {this}
|
|
5163
5206
|
*/
|
|
5164
5207
|
sort(...sort) {
|
|
@@ -5175,10 +5218,19 @@ class Query extends Subquery {
|
|
|
5175
5218
|
desc = value[1] === true;
|
|
5176
5219
|
[value] = value;
|
|
5177
5220
|
}
|
|
5178
|
-
if (typeof value
|
|
5221
|
+
if (typeof value === 'string') {
|
|
5222
|
+
list.push([value, desc]);
|
|
5179
5223
|
continue;
|
|
5180
5224
|
}
|
|
5181
|
-
|
|
5225
|
+
if (value !== null) {
|
|
5226
|
+
continue;
|
|
5227
|
+
}
|
|
5228
|
+
/** @type {[string, boolean, number][]} */
|
|
5229
|
+
const sorted = Object.entries(this.fields).map(([f, d]) => [f, (d.sort || 0) < 0, Math.abs(d.sort || 0)]);
|
|
5230
|
+
const sortFields = sorted.filter(v => v[2]).sort(([,, a], [,, b]) => a - b);
|
|
5231
|
+
for (const [field, desc] of sortFields) {
|
|
5232
|
+
list.push([field, desc]);
|
|
5233
|
+
}
|
|
5182
5234
|
}
|
|
5183
5235
|
if (list.length) {
|
|
5184
5236
|
this.#options.sort = [...(this.#options.sort || []), ...list];
|
|
@@ -5499,6 +5551,15 @@ class Query extends Subquery {
|
|
|
5499
5551
|
this.#options.distinct = Boolean(distinct);
|
|
5500
5552
|
return this;
|
|
5501
5553
|
}
|
|
5554
|
+
/**
|
|
5555
|
+
*
|
|
5556
|
+
* @param {LockType?} [lock]
|
|
5557
|
+
*/
|
|
5558
|
+
lock(lock) {
|
|
5559
|
+
const type = typeof lock === 'string' && lock.toUpperCase() || null;
|
|
5560
|
+
this.#options.lock = type === 'S' || type === 'X' ? type : null;
|
|
5561
|
+
return this;
|
|
5562
|
+
}
|
|
5502
5563
|
}
|
|
5503
5564
|
|
|
5504
5565
|
/** @import { Fields, IndexInfo, FieldDefine } from './types' */
|
package/migrate.d.mts
CHANGED
package/migrate.mjs
CHANGED