imodel 0.4.1 → 0.5.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/index.d.mts +48 -10
- package/index.mjs +300 -138
- package/migrate.d.mts +1 -1
- package/migrate.mjs +1 -1
- package/package.json +1 -1
package/index.d.mts
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* imodel v0.
|
|
2
|
+
* imodel v0.5.1
|
|
3
3
|
* (c) 2019-2025 undefined
|
|
4
4
|
* @license undefined
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
declare const now: unique symbol;
|
|
8
|
-
|
|
9
|
-
declare const
|
|
8
|
+
/** @type {typeof now} now */
|
|
9
|
+
declare const uuid: symbol;
|
|
10
|
+
/** @type {typeof uuid} uuid */
|
|
11
|
+
declare const increment$1: typeof uuid;
|
|
10
12
|
declare const decrement$1: unique symbol;
|
|
11
13
|
declare const multiply$1: unique symbol;
|
|
12
14
|
declare const divide$1: unique symbol;
|
|
@@ -541,9 +543,7 @@ declare function multiply(value: number | bigint): SetValue.Multiply;
|
|
|
541
543
|
declare function divide(value: number | bigint): SetValue.Divide;
|
|
542
544
|
type SetValue = SetValue.Increment | SetValue.Decrement | SetValue.Multiply | SetValue.Divide | string | number | bigint | boolean | null | typeof now | typeof uuid | (string | number | bigint | boolean)[];
|
|
543
545
|
declare namespace SetValue {
|
|
544
|
-
type Increment = {
|
|
545
|
-
[increment$1]: number | bigint;
|
|
546
|
-
};
|
|
546
|
+
type Increment = {};
|
|
547
547
|
type Decrement = {
|
|
548
548
|
[decrement$1]: number | bigint;
|
|
549
549
|
};
|
|
@@ -612,6 +612,7 @@ interface Constraint {
|
|
|
612
612
|
noField?: boolean;
|
|
613
613
|
field?: string;
|
|
614
614
|
value?: any;
|
|
615
|
+
writeOnly?: boolean;
|
|
615
616
|
}
|
|
616
617
|
type MainFieldType = keyof FieldType | TableDefine | null;
|
|
617
618
|
type ToFieldType<T> = T extends keyof FieldType ? FieldType[T] : unknown;
|
|
@@ -638,8 +639,12 @@ interface FieldDefineOption {
|
|
|
638
639
|
updating?: FieldSpecific;
|
|
639
640
|
/** 伪删除时,自定义数据 */
|
|
640
641
|
deleting?: FieldSpecific;
|
|
642
|
+
/** 是否为可创建 */
|
|
643
|
+
uncreatable?: boolean;
|
|
641
644
|
/** 是否为固定值 */
|
|
642
645
|
immutable?: boolean;
|
|
646
|
+
/** 固定值 */
|
|
647
|
+
fixedValue?: any;
|
|
643
648
|
/** 文本查询 */
|
|
644
649
|
text?: boolean;
|
|
645
650
|
/** 排序顺序,绝对值表示在排序列表中的顺序,负数表示逆序排序, 0 表示不排序 */
|
|
@@ -821,6 +826,7 @@ interface IConnection<E extends object = object> {
|
|
|
821
826
|
transaction<R>(environment: Environment<E>, fn: (t: E) => R | PromiseLike<R>): PromiseLike<R>;
|
|
822
827
|
}
|
|
823
828
|
interface Skip {
|
|
829
|
+
uncreatable?: boolean;
|
|
824
830
|
immutable?: boolean;
|
|
825
831
|
specific?: boolean;
|
|
826
832
|
}
|
|
@@ -962,9 +968,10 @@ declare class Connection<E extends {} = {}> {
|
|
|
962
968
|
* @param {TableDefine} model
|
|
963
969
|
* @param {T} data
|
|
964
970
|
* @param {Record<string, any>?} [newData]
|
|
971
|
+
* @param {Skip} [skip]
|
|
965
972
|
* @returns {Promise<(T extends Save ? T : object)?>}
|
|
966
973
|
*/
|
|
967
|
-
save<T>(model: TableDefine, data: T, newData?: Record<string, any> | null): Promise<(T extends Save ? T : object) | null>;
|
|
974
|
+
save<T>(model: TableDefine, data: T, newData?: Record<string, any> | null, skip?: Skip): Promise<(T extends Save ? T : object) | null>;
|
|
968
975
|
/**
|
|
969
976
|
*
|
|
970
977
|
* @template T
|
|
@@ -1466,6 +1473,10 @@ declare function column(column?: string): FieldDecorator<any>;
|
|
|
1466
1473
|
*/
|
|
1467
1474
|
declare function primary(primary?: number): FieldDecorator<any>;
|
|
1468
1475
|
|
|
1476
|
+
/** @import { FieldDecorator } from './index.mjs' */
|
|
1477
|
+
/** @returns {FieldDecorator<any>} */
|
|
1478
|
+
declare function uncreatable(): FieldDecorator<any>;
|
|
1479
|
+
|
|
1469
1480
|
/** @import { FieldDecorator } from './index.mjs' */
|
|
1470
1481
|
/** @returns {FieldDecorator<any>} */
|
|
1471
1482
|
declare function immutable(): FieldDecorator<any>;
|
|
@@ -1510,6 +1521,28 @@ declare function sort(sort: number): FieldDecorator<any>;
|
|
|
1510
1521
|
*/
|
|
1511
1522
|
declare function pseudo(pseudo: string): ClassDecorator;
|
|
1512
1523
|
|
|
1524
|
+
/**
|
|
1525
|
+
* @template T
|
|
1526
|
+
* @overload
|
|
1527
|
+
* @param {now} value
|
|
1528
|
+
* @returns {FieldDecorator<string | Date>}
|
|
1529
|
+
*/
|
|
1530
|
+
declare function defaultValue<T>(value: symbol): FieldDecorator<string | Date>;
|
|
1531
|
+
/**
|
|
1532
|
+
* @template T
|
|
1533
|
+
* @overload
|
|
1534
|
+
* @param {uuid} value
|
|
1535
|
+
* @returns {FieldDecorator<string>}
|
|
1536
|
+
*/
|
|
1537
|
+
declare function defaultValue<T>(value: symbol): FieldDecorator<string>;
|
|
1538
|
+
/**
|
|
1539
|
+
* @template T
|
|
1540
|
+
* @overload
|
|
1541
|
+
* @param {T} value
|
|
1542
|
+
* @returns {FieldDecorator<T>}
|
|
1543
|
+
*/
|
|
1544
|
+
declare function defaultValue<T>(value: T): FieldDecorator<T>;
|
|
1545
|
+
|
|
1513
1546
|
/**
|
|
1514
1547
|
*
|
|
1515
1548
|
* @overload
|
|
@@ -1844,7 +1877,7 @@ declare function toPrimaries(model: any, documents: any): string | string[];
|
|
|
1844
1877
|
/**
|
|
1845
1878
|
* @typedef {object} Scene.Define
|
|
1846
1879
|
* @property {string} table
|
|
1847
|
-
* @property {
|
|
1880
|
+
* @property {{column: string, field?: string, fixedValue?: any, [v: string]: any}[]} fields
|
|
1848
1881
|
*/
|
|
1849
1882
|
/** @import {FieldDefine, Fields, TableDefine,} from './types/table' */
|
|
1850
1883
|
declare class Scene {
|
|
@@ -1864,7 +1897,12 @@ declare class Scene {
|
|
|
1864
1897
|
declare namespace Scene {
|
|
1865
1898
|
type Define = {
|
|
1866
1899
|
table: string;
|
|
1867
|
-
fields:
|
|
1900
|
+
fields: {
|
|
1901
|
+
column: string;
|
|
1902
|
+
field?: string;
|
|
1903
|
+
fixedValue?: any;
|
|
1904
|
+
[v: string]: any;
|
|
1905
|
+
}[];
|
|
1868
1906
|
};
|
|
1869
1907
|
}
|
|
1870
1908
|
|
|
@@ -2233,4 +2271,4 @@ declare function field<T extends MainFieldType>(type: T, options: {
|
|
|
2233
2271
|
declare function setDevelopment(d?: boolean): void;
|
|
2234
2272
|
declare let isDevelopment: boolean;
|
|
2235
2273
|
|
|
2236
|
-
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 IConnection, type Index, type IndexInfo, type IndexOptions, type Join, type JoinType, type MainFieldType, type MaybePromise, Model, type Options, PseudoDestroy, Query, type QueryOptions, type Queryable, Save, Scene, Select, SetValue, type Skip, Submodel, type Support, type TableDefine, type TableType, type ToFieldType, type ToType, type TransactionFn, type VirtualTable, Where, type WhereItem, type WhereLike, type WhereOr, type WhereRaw, type WhereValue, type Wheres, column, creating, decrement, field as define, deleted, deleting, divide, field$1 as field, getPrimaryFields, getPrimaryKeys, immutable, increment, index, isDevelopment, isPseudo, submodel as model, multiply, now, primary, prop, pseudo, setDevelopment, sort, submodel, toNot, toPrimaries, toPrimary, undeleted, updating, uuid, values, withDeleted };
|
|
2274
|
+
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 IConnection, type Index, type IndexInfo, type IndexOptions, type Join, type JoinType, type MainFieldType, type MaybePromise, Model, type Options, PseudoDestroy, Query, type QueryOptions, type Queryable, Save, Scene, Select, SetValue, type Skip, Submodel, type Support, type TableDefine, type TableType, type ToFieldType, type ToType, type TransactionFn, type VirtualTable, Where, type WhereItem, type WhereLike, type WhereOr, type WhereRaw, type WhereValue, type Wheres, column, creating, decrement, defaultValue, field as define, deleted, deleting, divide, field$1 as field, getPrimaryFields, getPrimaryKeys, immutable, increment, index, isDevelopment, isPseudo, submodel as model, multiply, now, primary, prop, pseudo, setDevelopment, sort, submodel, toNot, toPrimaries, toPrimary, uncreatable, undeleted, updating, uuid, values, withDeleted };
|
package/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* imodel v0.
|
|
2
|
+
* imodel v0.5.1
|
|
3
3
|
* (c) 2019-2025 undefined
|
|
4
4
|
* @license undefined
|
|
5
5
|
*/
|
|
@@ -52,7 +52,9 @@ function isPseudo(model) {
|
|
|
52
52
|
}
|
|
53
53
|
|
|
54
54
|
const now = Symbol();
|
|
55
|
+
/** @type {typeof now} now */
|
|
55
56
|
const uuid = Symbol();
|
|
57
|
+
/** @type {typeof uuid} uuid */
|
|
56
58
|
const increment$1 = Symbol();
|
|
57
59
|
const decrement$1 = Symbol();
|
|
58
60
|
const multiply$1 = Symbol();
|
|
@@ -135,33 +137,46 @@ async function getSpecificValue(val) {
|
|
|
135
137
|
return value;
|
|
136
138
|
}
|
|
137
139
|
|
|
138
|
-
/** @import { Fields } from '../../types' */
|
|
140
|
+
/** @import { Fields, Skip } from '../../types' */
|
|
139
141
|
/**
|
|
140
142
|
*
|
|
141
143
|
* @param {Record<string, any>} data
|
|
142
144
|
* @param {Fields} columns
|
|
143
145
|
* @param {Record<string, string> | null} fieldColumns
|
|
146
|
+
* @param {Skip?} [skip]
|
|
147
|
+
* @param {Set<string>?} [ignore]
|
|
144
148
|
* @returns {Promise<Record<string, any>>}
|
|
145
149
|
*/
|
|
146
|
-
async function getInsertValue(data, columns, fieldColumns) {
|
|
150
|
+
async function getInsertValue(data, columns, fieldColumns, skip, ignore) {
|
|
151
|
+
const skipSpecific = skip?.specific;
|
|
152
|
+
const skipCreatable = skip?.uncreatable;
|
|
147
153
|
const insertValue = {};
|
|
148
154
|
for (const [k, col] of fieldColumns ? Object.entries(fieldColumns) : Object.keys(columns).map(v => [v, v])) {
|
|
149
155
|
const column = columns[col];
|
|
150
156
|
if (!column) {
|
|
151
157
|
continue;
|
|
152
158
|
}
|
|
153
|
-
const
|
|
154
|
-
|
|
155
|
-
|
|
159
|
+
const {
|
|
160
|
+
fixedValue
|
|
161
|
+
} = column;
|
|
162
|
+
if (fixedValue !== void 0 && !ignore?.has(k)) {
|
|
163
|
+
insertValue[col] = fixedValue;
|
|
156
164
|
continue;
|
|
157
165
|
}
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
166
|
+
if (!skipSpecific) {
|
|
167
|
+
const creating = await getSpecificValue(column.creating);
|
|
168
|
+
if (creating !== void 0) {
|
|
169
|
+
insertValue[col] = creating;
|
|
170
|
+
continue;
|
|
171
|
+
}
|
|
172
|
+
const updating = await getSpecificValue(column.updating);
|
|
173
|
+
if (updating !== void 0) {
|
|
174
|
+
insertValue[col] = updating;
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
162
177
|
}
|
|
163
178
|
const val = data[k];
|
|
164
|
-
if (val !== undefined) {
|
|
179
|
+
if (!skipCreatable && !column.uncreatable && val !== undefined) {
|
|
165
180
|
insertValue[col] = val;
|
|
166
181
|
continue;
|
|
167
182
|
}
|
|
@@ -309,7 +324,7 @@ function toFieldList(list, fieldColumns) {
|
|
|
309
324
|
return list.map(it => Object.fromEntries(map.map(([k, v]) => [k, it[v]])));
|
|
310
325
|
}
|
|
311
326
|
|
|
312
|
-
/** @import { Environment, FieldDefine, Fields, IConnection, TableDefine } from '../types' */
|
|
327
|
+
/** @import { Environment, FieldDefine, Fields, IConnection, Skip, TableDefine } from '../types' */
|
|
313
328
|
/**
|
|
314
329
|
*
|
|
315
330
|
* @param {any} p
|
|
@@ -345,9 +360,10 @@ function defaultMap(value, others) {
|
|
|
345
360
|
* @param {[string, FieldDefine<TableDefine>][]} fields
|
|
346
361
|
* @param {Record<string, any>[]} newList
|
|
347
362
|
* @param {Record<string, any>[]} list
|
|
363
|
+
* @param {Skip?} [skip]
|
|
348
364
|
* @returns {Promise<Record<string, any>[]>}
|
|
349
365
|
*/
|
|
350
|
-
async function createSub(conn, env, fields, newList, list) {
|
|
366
|
+
async function createSub(conn, env, fields, newList, list, skip) {
|
|
351
367
|
// TODO: 可创建字段过滤
|
|
352
368
|
if (!list.length) {
|
|
353
369
|
return list;
|
|
@@ -419,12 +435,13 @@ async function createSub(conn, env, fields, newList, list) {
|
|
|
419
435
|
}
|
|
420
436
|
const [columns, fieldColumns, tableFields] = toColumns(fields);
|
|
421
437
|
const insertKeys = Object.entries(columns).map(v => v[0]);
|
|
422
|
-
const
|
|
438
|
+
const ignore = new Set(fieldColumns ? constraintEntries.map(([k]) => fieldColumns[k]) : constraintEntries.map(([k]) => k));
|
|
439
|
+
const insertValues = await Promise.all(fieldValues.map(d => getInsertValue(d, columns, fieldColumns, skip, ignore)));
|
|
423
440
|
const {
|
|
424
441
|
table
|
|
425
442
|
} = type;
|
|
426
443
|
const result = (table && typeof table === 'object' ? table.insert(env, conn, columns, insertValues, insertKeys) : conn.insert(env, table || '', columns, insertValues, insertKeys)).then(r => toFieldList(r, fieldColumns));
|
|
427
|
-
promises.push(result.then(r => createSub(conn, env, tableFields, fieldValues, r)));
|
|
444
|
+
promises.push(result.then(r => createSub(conn, env, tableFields, fieldValues, r, skip)));
|
|
428
445
|
promises.push(array ? result.then(values => {
|
|
429
446
|
for (const [value] of allList) {
|
|
430
447
|
value[key] = values.filter(createFilter(value, commonKeys));
|
|
@@ -444,10 +461,11 @@ async function createSub(conn, env, fields, newList, list) {
|
|
|
444
461
|
* @param {Environment} env
|
|
445
462
|
* @param {TableDefine} model
|
|
446
463
|
* @param {any} p
|
|
447
|
-
* @param {
|
|
464
|
+
* @param {Skip?} [skip]
|
|
465
|
+
* @param {(a,b) => object} [mapDo]
|
|
448
466
|
* @returns {Promise<any>}
|
|
449
467
|
*/
|
|
450
|
-
async function create(conn, env, model, p, mapDo = defaultMap) {
|
|
468
|
+
async function create(conn, env, model, p, skip, mapDo = defaultMap) {
|
|
451
469
|
const {
|
|
452
470
|
table,
|
|
453
471
|
fields
|
|
@@ -469,7 +487,7 @@ async function create(conn, env, model, p, mapDo = defaultMap) {
|
|
|
469
487
|
records.push(data);
|
|
470
488
|
}
|
|
471
489
|
const [columns, fieldColumns, tableFields] = toColumns(fields);
|
|
472
|
-
const insertValues = await Promise.all(records.map(d => getInsertValue(d, columns, fieldColumns)));
|
|
490
|
+
const insertValues = await Promise.all(records.map(d => getInsertValue(d, columns, fieldColumns, skip)));
|
|
473
491
|
let insertKeys = [];
|
|
474
492
|
if (!insertKeys.length && !isArray) {
|
|
475
493
|
insertKeys = Object.entries(insertValues[0]).filter(([k, v]) => v !== undefined && k in columns).map(([k]) => k);
|
|
@@ -478,7 +496,7 @@ async function create(conn, env, model, p, mapDo = defaultMap) {
|
|
|
478
496
|
insertKeys = Object.entries(columns).map(([k]) => k);
|
|
479
497
|
}
|
|
480
498
|
const list = toFieldList(table && typeof table === 'object' ? await table.insert(env, conn, columns, insertValues, insertKeys) : await conn.insert(env, table || '', columns, insertValues, insertKeys), fieldColumns);
|
|
481
|
-
await createSub(conn, env, tableFields, records, list);
|
|
499
|
+
await createSub(conn, env, tableFields, records, list, skip);
|
|
482
500
|
const value = list.map((v, i) => mapDo(v, getOtherValues(records[i], fields)));
|
|
483
501
|
return isArray ? value : value[0];
|
|
484
502
|
}
|
|
@@ -502,7 +520,7 @@ function getPrimaryKeys(fieldsOrColumns) {
|
|
|
502
520
|
/**
|
|
503
521
|
* @param {Fields} fields
|
|
504
522
|
* @param {Set<string>} parentFields
|
|
505
|
-
* @param {Record<string,
|
|
523
|
+
* @param {Record<string, Constraint> | undefined} constraints
|
|
506
524
|
* @returns {[string, string][]?}
|
|
507
525
|
*/
|
|
508
526
|
function getMapList(fields, parentFields, constraints) {
|
|
@@ -515,7 +533,12 @@ function getMapList(fields, parentFields, constraints) {
|
|
|
515
533
|
if (!(k in fields)) {
|
|
516
534
|
continue;
|
|
517
535
|
}
|
|
518
|
-
|
|
536
|
+
if (v.writeOnly) {
|
|
537
|
+
continue;
|
|
538
|
+
}
|
|
539
|
+
const {
|
|
540
|
+
field
|
|
541
|
+
} = v;
|
|
519
542
|
if (typeof field !== 'string') {
|
|
520
543
|
continue;
|
|
521
544
|
}
|
|
@@ -531,34 +554,73 @@ function getMapList(fields, parentFields, constraints) {
|
|
|
531
554
|
}
|
|
532
555
|
|
|
533
556
|
/** @import { Constraint, Fields, FieldType } from '../../types' */
|
|
557
|
+
/** @import { WhereValue } from '../../Where.mjs' */
|
|
534
558
|
/**
|
|
535
559
|
*
|
|
536
560
|
* @param {Record<string, Constraint> | undefined} value
|
|
537
561
|
* @param {Fields<keyof FieldType>} columns
|
|
538
|
-
* @
|
|
562
|
+
* @param {Record<string, string>?} fieldColumns
|
|
563
|
+
* @returns {WhereValue[]}
|
|
539
564
|
*/
|
|
540
|
-
function
|
|
565
|
+
function getValueWhere(value, columns, fieldColumns) {
|
|
541
566
|
if (!value) {
|
|
542
567
|
return [];
|
|
543
568
|
}
|
|
569
|
+
const keys = new Set();
|
|
544
570
|
/** @type {[string, unknown][]} */
|
|
545
571
|
const where = [];
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
572
|
+
if (!fieldColumns) {
|
|
573
|
+
for (const [k, v] of Object.entries(value)) {
|
|
574
|
+
if (!(k in columns)) {
|
|
575
|
+
continue;
|
|
576
|
+
}
|
|
577
|
+
keys.add(k);
|
|
578
|
+
if (v.writeOnly) {
|
|
579
|
+
continue;
|
|
580
|
+
}
|
|
581
|
+
const {
|
|
582
|
+
value
|
|
583
|
+
} = v;
|
|
584
|
+
if (value === undefined) {
|
|
585
|
+
continue;
|
|
586
|
+
}
|
|
587
|
+
where.push([k, value]);
|
|
588
|
+
}
|
|
589
|
+
} else {
|
|
590
|
+
for (const [k, v] of Object.entries(value)) {
|
|
591
|
+
if (!(k in fieldColumns)) {
|
|
592
|
+
continue;
|
|
593
|
+
}
|
|
594
|
+
const key = fieldColumns[k];
|
|
595
|
+
keys.add(key);
|
|
596
|
+
if (v.writeOnly) {
|
|
597
|
+
continue;
|
|
598
|
+
}
|
|
599
|
+
const {
|
|
600
|
+
value
|
|
601
|
+
} = v;
|
|
602
|
+
if (value === undefined) {
|
|
603
|
+
continue;
|
|
604
|
+
}
|
|
605
|
+
where.push([key, value]);
|
|
549
606
|
}
|
|
550
|
-
|
|
607
|
+
}
|
|
608
|
+
for (const [k, {
|
|
609
|
+
fixedValue
|
|
610
|
+
}] of Object.entries(columns)) {
|
|
611
|
+
if (keys.has(k)) {
|
|
551
612
|
continue;
|
|
552
613
|
}
|
|
553
|
-
|
|
554
|
-
value
|
|
555
|
-
} = v;
|
|
556
|
-
if (value === undefined) {
|
|
614
|
+
if (fixedValue === void 0) {
|
|
557
615
|
continue;
|
|
558
616
|
}
|
|
559
|
-
where.push([k,
|
|
617
|
+
where.push([k, fixedValue]);
|
|
560
618
|
}
|
|
561
|
-
return where
|
|
619
|
+
return where.map(([k, v]) => ({
|
|
620
|
+
field: k,
|
|
621
|
+
operator: '=',
|
|
622
|
+
value: v
|
|
623
|
+
}));
|
|
562
624
|
}
|
|
563
625
|
|
|
564
626
|
/** @import { WhereValue } from '../../Where.mjs' */
|
|
@@ -566,30 +628,19 @@ function getValueEntries(value, columns) {
|
|
|
566
628
|
*
|
|
567
629
|
* @param {Record<string, string>[]} list
|
|
568
630
|
* @param {[string, string][]} map
|
|
631
|
+
* @param {Record<string, string>?} fieldColumns
|
|
569
632
|
* @returns {WhereValue}
|
|
570
633
|
*/
|
|
571
|
-
function subList2where(list, map) {
|
|
634
|
+
function subList2where(list, map, fieldColumns) {
|
|
572
635
|
const valueKeys = map.map(([, v]) => v);
|
|
636
|
+
const field = fieldColumns ? map.map(([k]) => fieldColumns[k]) : map.map(([k]) => k);
|
|
637
|
+
const value = list.map(d => valueKeys.map(v => d[v]));
|
|
573
638
|
return {
|
|
574
|
-
field
|
|
575
|
-
value
|
|
639
|
+
field,
|
|
640
|
+
value
|
|
576
641
|
};
|
|
577
642
|
}
|
|
578
643
|
|
|
579
|
-
/** @import { WhereValue } from '../../Where.mjs' */
|
|
580
|
-
/**
|
|
581
|
-
*
|
|
582
|
-
* @param {[string, unknown][]} value
|
|
583
|
-
* @returns {WhereValue[]}
|
|
584
|
-
*/
|
|
585
|
-
function valueEntries2where(value) {
|
|
586
|
-
return value.map(([k, v]) => ({
|
|
587
|
-
field: k,
|
|
588
|
-
operator: '=',
|
|
589
|
-
value: v
|
|
590
|
-
}));
|
|
591
|
-
}
|
|
592
|
-
|
|
593
644
|
/** @import { WhereValue } from '../../Where.mjs' */
|
|
594
645
|
/**
|
|
595
646
|
*
|
|
@@ -640,6 +691,30 @@ function where2column(where, fieldColumns) {
|
|
|
640
691
|
}));
|
|
641
692
|
}
|
|
642
693
|
|
|
694
|
+
/** @import { Fields, FieldType } from '../../types' */
|
|
695
|
+
/** @import { WhereValue } from '../../Where.mjs' */
|
|
696
|
+
/**
|
|
697
|
+
*
|
|
698
|
+
* @param {Fields<keyof FieldType>} columns
|
|
699
|
+
* @param {Set<string>?} [ignore]
|
|
700
|
+
* @returns {Iterable<WhereValue>}
|
|
701
|
+
*/
|
|
702
|
+
function* getFixedValueWhere(columns, ignore) {
|
|
703
|
+
for (const [k, {
|
|
704
|
+
fixedValue
|
|
705
|
+
}] of Object.entries(columns)) {
|
|
706
|
+
if (ignore?.has(k)) {
|
|
707
|
+
continue;
|
|
708
|
+
}
|
|
709
|
+
if (fixedValue !== void 0) {
|
|
710
|
+
yield {
|
|
711
|
+
field: k,
|
|
712
|
+
value: fixedValue
|
|
713
|
+
};
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
|
|
643
718
|
/** @import { Environment, FieldDefine, IConnection, Queryable, TableDefine } from '../types' */
|
|
644
719
|
/**
|
|
645
720
|
*
|
|
@@ -671,8 +746,7 @@ async function deleteSub(conn, list, fields, baseFieldSet, env, hasResult) {
|
|
|
671
746
|
if (!mapList) {
|
|
672
747
|
continue;
|
|
673
748
|
}
|
|
674
|
-
const
|
|
675
|
-
const where = where2column([subList2where(list, mapList), ...valueEntries2where(value)], fieldColumns);
|
|
749
|
+
const where = [subList2where(list, mapList, fieldColumns), ...getValueWhere(constraints, columns, fieldColumns)];
|
|
676
750
|
const {
|
|
677
751
|
table
|
|
678
752
|
} = type;
|
|
@@ -714,6 +788,7 @@ async function deleteMany(conn, queryable, env) {
|
|
|
714
788
|
} = queryable;
|
|
715
789
|
const [columns, fieldColumns, tableFields] = toColumns(fields);
|
|
716
790
|
const where = where2column(queryable.options.where, fieldColumns);
|
|
791
|
+
where.push(...getFixedValueWhere(columns));
|
|
717
792
|
if (!tableFields.length) {
|
|
718
793
|
return table && typeof table === 'object' ? table.delete(env, conn, columns, where) : conn.delete(env, table || '', columns, where);
|
|
719
794
|
}
|
|
@@ -787,18 +862,25 @@ async function getSetValue$1(columns, fieldColumns, update, skip) {
|
|
|
787
862
|
if (field.primary) {
|
|
788
863
|
continue;
|
|
789
864
|
}
|
|
865
|
+
const {
|
|
866
|
+
fixedValue
|
|
867
|
+
} = field;
|
|
868
|
+
if (fixedValue !== void 0) {
|
|
869
|
+
data[name] = fixedValue;
|
|
870
|
+
continue;
|
|
871
|
+
}
|
|
790
872
|
const updating = await getSpecificValue(field.updating);
|
|
791
|
-
if (updating
|
|
873
|
+
if (updating !== void 0) {
|
|
874
|
+
data[name] = updating;
|
|
792
875
|
continue;
|
|
793
876
|
}
|
|
794
|
-
data[name] = updating;
|
|
795
877
|
}
|
|
796
878
|
}
|
|
797
879
|
return data;
|
|
798
880
|
}
|
|
799
881
|
|
|
800
882
|
/* eslint-disable no-labels */
|
|
801
|
-
/** @import { Constraint, Environment, FieldDefine, IConnection, TableDefine } from '../types' */
|
|
883
|
+
/** @import { Constraint, Environment, FieldDefine, IConnection, Skip, TableDefine } from '../types' */
|
|
802
884
|
/** @import { WhereValue } from '../Where.mjs' */
|
|
803
885
|
/**
|
|
804
886
|
*
|
|
@@ -833,9 +915,10 @@ function classify(newList, oldList, keys, notFound, found) {
|
|
|
833
915
|
* @param {TableDefine} type
|
|
834
916
|
* @param {boolean} array
|
|
835
917
|
* @param {Record<string, Constraint>?} [constraints]
|
|
918
|
+
* @param {Skip} [skip]
|
|
836
919
|
* @returns
|
|
837
920
|
*/
|
|
838
|
-
async function updateItem(conn, env, updatedOldNewList, key, type, array, constraints) {
|
|
921
|
+
async function updateItem(conn, env, updatedOldNewList, key, type, array, constraints, skip) {
|
|
839
922
|
/** @type {any[]} */
|
|
840
923
|
const promises = [];
|
|
841
924
|
const constraintEntries = Object.entries(constraints || {});
|
|
@@ -844,12 +927,18 @@ async function updateItem(conn, env, updatedOldNewList, key, type, array, constr
|
|
|
844
927
|
const commonValue = {};
|
|
845
928
|
/** @type {[string, string][]} */
|
|
846
929
|
const commonMapKeys = [];
|
|
930
|
+
/** @type {[string, string][]} */
|
|
931
|
+
const commonFilterKeys = [];
|
|
847
932
|
for (const [k, {
|
|
848
933
|
field,
|
|
849
|
-
value: val
|
|
934
|
+
value: val,
|
|
935
|
+
writeOnly
|
|
850
936
|
}] of constraintEntries) {
|
|
851
937
|
if (field) {
|
|
852
938
|
commonMapKeys.push([k, field]);
|
|
939
|
+
if (!writeOnly) {
|
|
940
|
+
commonFilterKeys.push([k, field]);
|
|
941
|
+
}
|
|
853
942
|
} else if (val !== undefined) {
|
|
854
943
|
commonValue[k] = val;
|
|
855
944
|
}
|
|
@@ -903,17 +992,6 @@ async function updateItem(conn, env, updatedOldNewList, key, type, array, constr
|
|
|
903
992
|
return;
|
|
904
993
|
}
|
|
905
994
|
const [columns, fieldColumns, tableFields, fields] = toColumns(type.fields);
|
|
906
|
-
/** @type {Record<string, any>} */
|
|
907
|
-
const commonValues = {};
|
|
908
|
-
for (const [k, v] of constraintEntries) {
|
|
909
|
-
const {
|
|
910
|
-
field,
|
|
911
|
-
value
|
|
912
|
-
} = v;
|
|
913
|
-
if (!field && value !== undefined) {
|
|
914
|
-
commonValues[k] = value;
|
|
915
|
-
}
|
|
916
|
-
}
|
|
917
995
|
const oldList = oldListValues.flat();
|
|
918
996
|
const newList = newListValues.flat();
|
|
919
997
|
const primaryKeys = getPrimaryKeys(fields);
|
|
@@ -930,36 +1008,39 @@ async function updateItem(conn, env, updatedOldNewList, key, type, array, constr
|
|
|
930
1008
|
const needUpdate = [];
|
|
931
1009
|
/** @type {Record<string, any>[]} */
|
|
932
1010
|
const unmatched = [];
|
|
933
|
-
const aKeys = fieldKeys.filter(v => !(v in
|
|
934
|
-
const pKeys = primaryKeys.filter(v => !(v in
|
|
1011
|
+
const aKeys = fieldKeys.filter(v => !(v in commonValue));
|
|
1012
|
+
const pKeys = primaryKeys.filter(v => !(v in commonValue));
|
|
935
1013
|
classify(newList, needDelete, aKeys, unmatched, eq);
|
|
936
1014
|
classify(unmatched, needDelete, pKeys, needCreate, needUpdate);
|
|
937
1015
|
const {
|
|
938
1016
|
table
|
|
939
1017
|
} = type;
|
|
1018
|
+
const ignore = new Set(fieldColumns ? constraintEntries.map(([k]) => fieldColumns[k]) : constraintEntries.map(([k]) => k));
|
|
940
1019
|
if (needDelete.length) {
|
|
941
1020
|
// 批量删除
|
|
942
1021
|
/** @type {WhereValue[]} */
|
|
943
|
-
const
|
|
1022
|
+
const fieldWhere = primaryKeys.filter(v => v in commonValue).map(k => ({
|
|
944
1023
|
field: k,
|
|
945
1024
|
operator: '=',
|
|
946
|
-
value:
|
|
1025
|
+
value: commonValue[k] ?? null
|
|
947
1026
|
}));
|
|
948
1027
|
if (pKeys.length === 1) {
|
|
949
1028
|
const [key] = pKeys;
|
|
950
|
-
|
|
1029
|
+
fieldWhere.push({
|
|
951
1030
|
field: key,
|
|
952
1031
|
operator: 'in',
|
|
953
1032
|
value: needDelete.map(v => v[key] ?? null)
|
|
954
1033
|
});
|
|
955
1034
|
} else if (pKeys.length) {
|
|
956
|
-
|
|
1035
|
+
fieldWhere.push({
|
|
957
1036
|
field: pKeys,
|
|
958
1037
|
operator: 'in',
|
|
959
1038
|
value: needDelete.map(v => pKeys.map(k => v[k] ?? null))
|
|
960
1039
|
});
|
|
961
1040
|
}
|
|
962
|
-
const
|
|
1041
|
+
const where = where2column(fieldWhere, fieldColumns);
|
|
1042
|
+
where.push(...getFixedValueWhere(columns, ignore));
|
|
1043
|
+
const promise = typeof table === 'object' ? table.deleteReturn(env, conn, columnKeys, columns, where) : conn.deleteReturn(env, columnKeys, table || '', columns, where);
|
|
963
1044
|
promises.push(promise.then(list => deleteSub(conn, toFieldList(list, fieldColumns), tableFields, new Set(Object.keys(fieldColumns || columns)), env)));
|
|
964
1045
|
}
|
|
965
1046
|
/** @type {typeof updatedOldNewList} */
|
|
@@ -982,7 +1063,7 @@ async function updateItem(conn, env, updatedOldNewList, key, type, array, constr
|
|
|
982
1063
|
}
|
|
983
1064
|
const setKeysColumn = Object.entries(columns).filter(([, v]) => !v.primary && !v.immutable).map(v => v[0]).filter(v => keysColumn.has(v));
|
|
984
1065
|
/** @type {WhereValue[]} */
|
|
985
|
-
const
|
|
1066
|
+
const fieldWhere = Object.entries(commonValue).filter(([k]) => primaryKeys.includes(k)).map(([k, v]) => ({
|
|
986
1067
|
field: k,
|
|
987
1068
|
operator: '=',
|
|
988
1069
|
value: v
|
|
@@ -995,24 +1076,26 @@ async function updateItem(conn, env, updatedOldNewList, key, type, array, constr
|
|
|
995
1076
|
value[key] = old[key];
|
|
996
1077
|
}
|
|
997
1078
|
}
|
|
998
|
-
const
|
|
1079
|
+
const where = where2column(fieldWhere, fieldColumns);
|
|
1080
|
+
where.push(...getFixedValueWhere(columns, ignore));
|
|
1081
|
+
const list = (typeof table === 'object' ? table.updateManyReturn(env, conn, columns, updateColumn, field2column(pKeys, fieldColumns), setKeysColumn, toColumnList(needUpdate.map(v => v[0]), fieldColumns), where, columnKeys) : conn.updateManyReturn(env, table || '', columns, updateColumn, field2column(pKeys, fieldColumns), setKeysColumn, toColumnList(needUpdate.map(v => v[0]), fieldColumns), where, columnKeys)).then(l => toFieldList(l, fieldColumns));
|
|
999
1082
|
listPromises.push(list);
|
|
1000
1083
|
subPromises.push(list.then(u => u.map((u, i) => [u, ...needUpdate[i]])));
|
|
1001
1084
|
}
|
|
1002
1085
|
// 批量新建
|
|
1003
1086
|
if (needCreate.length) {
|
|
1004
|
-
const subColumnData = await Promise.all(needCreate.map(d => getInsertValue(d, columns, fieldColumns)));
|
|
1087
|
+
const subColumnData = await Promise.all(needCreate.map(d => getInsertValue(d, columns, fieldColumns, skip, ignore)));
|
|
1005
1088
|
const list = (typeof table === 'object' ? table.insert(env, conn, columns, subColumnData, Object.keys(columns)) : conn.insert(env, table || '', columns, subColumnData, Object.keys(columns))).then(l => toFieldList(l, fieldColumns));
|
|
1006
1089
|
listPromises.push(list);
|
|
1007
|
-
promises.push(list.then(list => createSub(conn, env, tableFields, needCreate, list)));
|
|
1090
|
+
promises.push(list.then(list => createSub(conn, env, tableFields, needCreate, list, skip)));
|
|
1008
1091
|
}
|
|
1009
1092
|
promises.push(Promise.all(subPromises).then(
|
|
1010
1093
|
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
1011
|
-
v => updateSub(conn, tableFields, v.flat(), env)));
|
|
1094
|
+
v => updateSub(conn, tableFields, v.flat(), env, skip)));
|
|
1012
1095
|
const allList = Promise.all(listPromises).then(v => v.flat());
|
|
1013
1096
|
promises.push(array ? allList.then(async values => {
|
|
1014
1097
|
for (const [value] of updatedOldNewList) {
|
|
1015
|
-
let data = values.filter(createFilter(value,
|
|
1098
|
+
let data = values.filter(createFilter(value, commonFilterKeys));
|
|
1016
1099
|
if (noField) {
|
|
1017
1100
|
data = data.sort(({
|
|
1018
1101
|
[noField]: a
|
|
@@ -1024,7 +1107,7 @@ async function updateItem(conn, env, updatedOldNewList, key, type, array, constr
|
|
|
1024
1107
|
}
|
|
1025
1108
|
}) : allList.then(async values => {
|
|
1026
1109
|
for (const [value] of updatedOldNewList) {
|
|
1027
|
-
value[key] = values.find(createFilter(value,
|
|
1110
|
+
value[key] = values.find(createFilter(value, commonFilterKeys)) || null;
|
|
1028
1111
|
}
|
|
1029
1112
|
}));
|
|
1030
1113
|
await Promise.all(promises);
|
|
@@ -1035,9 +1118,10 @@ async function updateItem(conn, env, updatedOldNewList, key, type, array, constr
|
|
|
1035
1118
|
* @param {[string, FieldDefine<TableDefine>][]} fields
|
|
1036
1119
|
* @param {[Record<string, any>, Record<string, any>, Record<string, any>][]} list
|
|
1037
1120
|
* @param {Environment} env
|
|
1121
|
+
* @param {Skip} [skip]
|
|
1038
1122
|
* @returns
|
|
1039
1123
|
*/
|
|
1040
|
-
async function updateSub(conn, fields, list, env) {
|
|
1124
|
+
async function updateSub(conn, fields, list, env, skip) {
|
|
1041
1125
|
if (!list.length) {
|
|
1042
1126
|
return;
|
|
1043
1127
|
}
|
|
@@ -1052,7 +1136,7 @@ async function updateSub(conn, fields, list, env) {
|
|
|
1052
1136
|
if (immutable) {
|
|
1053
1137
|
continue;
|
|
1054
1138
|
}
|
|
1055
|
-
promises.push(updateItem(conn, env, list, key, type, array, constraints));
|
|
1139
|
+
promises.push(updateItem(conn, env, list, key, type, array, constraints, skip));
|
|
1056
1140
|
}
|
|
1057
1141
|
await Promise.all(promises);
|
|
1058
1142
|
}
|
|
@@ -1063,9 +1147,10 @@ async function updateSub(conn, fields, list, env) {
|
|
|
1063
1147
|
* @param {Record<string, any>} data
|
|
1064
1148
|
* @param {Record<string, any>} newData
|
|
1065
1149
|
* @param {Environment} env
|
|
1150
|
+
* @param {Skip} [skip]
|
|
1066
1151
|
* @returns
|
|
1067
1152
|
*/
|
|
1068
|
-
async function updateData (conn, model, data, newData, env) {
|
|
1153
|
+
async function updateData (conn, model, data, newData, env, skip) {
|
|
1069
1154
|
const keys = Object.keys(newData);
|
|
1070
1155
|
if (!keys.length) {
|
|
1071
1156
|
return false;
|
|
@@ -1077,25 +1162,27 @@ async function updateData (conn, model, data, newData, env) {
|
|
|
1077
1162
|
const savedData = data;
|
|
1078
1163
|
const has = keys.filter(v => v in fields && (typeof fields[v]?.type === 'string' || fields[v]?.type && !fields[v]?.type.table)).length;
|
|
1079
1164
|
/** @type {WhereValue[]} */
|
|
1080
|
-
const
|
|
1165
|
+
const fieldWhere = getPrimaryKeys(fields).map(k => ({
|
|
1081
1166
|
field: k,
|
|
1082
1167
|
operator: '=',
|
|
1083
1168
|
value: savedData[k]
|
|
1084
1169
|
}));
|
|
1085
1170
|
const [columns, fieldColumns, tableFields] = toColumns(fields);
|
|
1171
|
+
const where = where2column(fieldWhere, fieldColumns);
|
|
1172
|
+
where.push(...getFixedValueWhere(columns));
|
|
1086
1173
|
let list = [];
|
|
1087
1174
|
if (!has) {
|
|
1088
1175
|
list = [data];
|
|
1089
1176
|
} else if (table && typeof table === 'object') {
|
|
1090
|
-
list = toFieldList(await table.updateReturn(env, conn, Object.keys(columns), columns, await getSetValue$1(columns, fieldColumns, newData),
|
|
1177
|
+
list = toFieldList(await table.updateReturn(env, conn, Object.keys(columns), columns, await getSetValue$1(columns, fieldColumns, newData), where), fieldColumns);
|
|
1091
1178
|
} else {
|
|
1092
|
-
list = toFieldList(await conn.updateReturn(env, Object.keys(columns), table || '', columns, await getSetValue$1(columns, fieldColumns, newData),
|
|
1179
|
+
list = toFieldList(await conn.updateReturn(env, Object.keys(columns), table || '', columns, await getSetValue$1(columns, fieldColumns, newData), where), fieldColumns);
|
|
1093
1180
|
}
|
|
1094
1181
|
const oldList = [data];
|
|
1095
1182
|
const newList = [newData];
|
|
1096
1183
|
/** @type {[Record<string, any>, Record<string, any>, Record<string, any>][]} */
|
|
1097
1184
|
const updatedOldNewList = list.map((v, k) => [v, oldList[k], newList[k]]);
|
|
1098
|
-
await updateSub(conn, tableFields, updatedOldNewList, env);
|
|
1185
|
+
await updateSub(conn, tableFields, updatedOldNewList, env, skip);
|
|
1099
1186
|
return list[0];
|
|
1100
1187
|
}
|
|
1101
1188
|
|
|
@@ -1153,18 +1240,20 @@ async function pseudoDestroy (conn, tableDef, data, update, env) {
|
|
|
1153
1240
|
fields
|
|
1154
1241
|
} = tableDef;
|
|
1155
1242
|
/** @type {WhereValue[]} */
|
|
1156
|
-
const
|
|
1243
|
+
const fieldWhere = getPrimaryKeys(fields).map(k => ({
|
|
1157
1244
|
field: k,
|
|
1158
1245
|
operator: '=',
|
|
1159
1246
|
value: data[k]
|
|
1160
1247
|
}));
|
|
1161
|
-
const [columns, fieldColumns] = toColumns(fields);
|
|
1162
1248
|
const pseudo = tableDef.pseudo || '';
|
|
1163
|
-
|
|
1249
|
+
fieldWhere.push({
|
|
1164
1250
|
field: pseudo,
|
|
1165
1251
|
operator: '=',
|
|
1166
1252
|
value: 0
|
|
1167
1253
|
});
|
|
1254
|
+
const [columns, fieldColumns] = toColumns(fields);
|
|
1255
|
+
const where = where2column(fieldWhere, fieldColumns);
|
|
1256
|
+
where.push(...getFixedValueWhere(columns));
|
|
1168
1257
|
const value = {
|
|
1169
1258
|
...update
|
|
1170
1259
|
};
|
|
@@ -1172,7 +1261,7 @@ async function pseudoDestroy (conn, tableDef, data, update, env) {
|
|
|
1172
1261
|
value[pseudo] = pseudoValue;
|
|
1173
1262
|
const set = set2column(value, fieldColumns);
|
|
1174
1263
|
await setDeleteTimestamp(columns, set);
|
|
1175
|
-
const dataUpdated = table && typeof table === 'object' ? await table.updateReturn(env, conn, Object.keys(columns), columns, set,
|
|
1264
|
+
const dataUpdated = table && typeof table === 'object' ? await table.updateReturn(env, conn, Object.keys(columns), columns, set, where) : await conn.updateReturn(env, Object.keys(columns), table || '', columns, set, where);
|
|
1176
1265
|
return toFieldList(dataUpdated, fieldColumns)[0] || null;
|
|
1177
1266
|
}
|
|
1178
1267
|
|
|
@@ -1196,6 +1285,7 @@ async function completelyDelete (conn, tableDef, data, env) {
|
|
|
1196
1285
|
operator: '=',
|
|
1197
1286
|
value: data[k]
|
|
1198
1287
|
})), fieldColumns);
|
|
1288
|
+
where.push(...getFixedValueWhere(columns));
|
|
1199
1289
|
const list = toFieldList(table && typeof table === 'object' ? await table.deleteReturn(env, conn, Object.keys(columns), columns, where) : await conn.deleteReturn(env, Object.keys(columns), table || '', columns, where), fieldColumns);
|
|
1200
1290
|
const baseFieldSet = new Set(Object.keys(fieldColumns || columns));
|
|
1201
1291
|
const [deleted] = await deleteSub(conn, list, tableFields, baseFieldSet, env, true);
|
|
@@ -1308,8 +1398,7 @@ async function findSub(conn, list, fields, baseFieldSet, env) {
|
|
|
1308
1398
|
if (!mapList) {
|
|
1309
1399
|
continue;
|
|
1310
1400
|
}
|
|
1311
|
-
const
|
|
1312
|
-
const where = where2column([subList2where(list, mapList), ...valueEntries2where(value)], fieldColumns);
|
|
1401
|
+
const where = [subList2where(list, mapList, fieldColumns), ...getValueWhere(constraints, columns, fieldColumns)];
|
|
1313
1402
|
const select = select2column(columns, fieldColumns);
|
|
1314
1403
|
const {
|
|
1315
1404
|
table
|
|
@@ -1351,21 +1440,23 @@ async function find(conn, p, env) {
|
|
|
1351
1440
|
fields
|
|
1352
1441
|
} = p;
|
|
1353
1442
|
// @ts-ignore
|
|
1443
|
+
const options = getOptions(p);
|
|
1354
1444
|
const {
|
|
1355
|
-
where,
|
|
1356
1445
|
sort,
|
|
1357
1446
|
offset,
|
|
1358
1447
|
limit
|
|
1359
|
-
} =
|
|
1448
|
+
} = options;
|
|
1360
1449
|
const [columns, fieldColumns, tableFields] = toColumns(fields);
|
|
1450
|
+
const where = where2column(options.where, fieldColumns);
|
|
1451
|
+
where.push(...getFixedValueWhere(columns));
|
|
1361
1452
|
const list = table && typeof table === 'object' ? await table.find(env, conn, columns, {
|
|
1362
|
-
where
|
|
1453
|
+
where,
|
|
1363
1454
|
offset,
|
|
1364
1455
|
limit,
|
|
1365
1456
|
sort: sort?.length ? sort2column(sort, fieldColumns) : getDefaultSort(columns),
|
|
1366
1457
|
select: select2column(columns, fieldColumns)
|
|
1367
1458
|
}) : await conn.find(env, table || '', columns, {
|
|
1368
|
-
where
|
|
1459
|
+
where,
|
|
1369
1460
|
offset,
|
|
1370
1461
|
limit,
|
|
1371
1462
|
sort: sort?.length ? sort2column(sort, fieldColumns) : getDefaultSort(columns),
|
|
@@ -1399,18 +1490,20 @@ async function first(conn, p, env) {
|
|
|
1399
1490
|
fields
|
|
1400
1491
|
} = p;
|
|
1401
1492
|
// @ts-ignore
|
|
1493
|
+
const options = getOptions(p);
|
|
1402
1494
|
const {
|
|
1403
|
-
where,
|
|
1404
1495
|
sort
|
|
1405
|
-
} =
|
|
1496
|
+
} = options;
|
|
1406
1497
|
const [columns, fieldColumns, tableFields] = toColumns(fields);
|
|
1498
|
+
const where = where2column(options.where, fieldColumns);
|
|
1499
|
+
where.push(...getFixedValueWhere(columns));
|
|
1407
1500
|
const list = table && typeof table === 'object' ? await table.find(env, conn, columns, {
|
|
1408
|
-
where
|
|
1501
|
+
where,
|
|
1409
1502
|
limit: 1,
|
|
1410
1503
|
sort: sort?.length ? sort2column(sort, fieldColumns) : getDefaultSort(columns),
|
|
1411
1504
|
select: select2column(columns, fieldColumns)
|
|
1412
1505
|
}) : await conn.find(env, table || '', columns, {
|
|
1413
|
-
where
|
|
1506
|
+
where,
|
|
1414
1507
|
limit: 1,
|
|
1415
1508
|
sort: sort?.length ? sort2column(sort, fieldColumns) : getDefaultSort(columns),
|
|
1416
1509
|
select: select2column(columns, fieldColumns)
|
|
@@ -1438,10 +1531,10 @@ async function pseudoDeleteMany(conn, queryable, update, env) {
|
|
|
1438
1531
|
table,
|
|
1439
1532
|
fields
|
|
1440
1533
|
} = queryable;
|
|
1441
|
-
const
|
|
1442
|
-
where
|
|
1443
|
-
} = getOptions(queryable);
|
|
1534
|
+
const options = getOptions(queryable);
|
|
1444
1535
|
const [columns, fieldColumns] = toColumns(fields);
|
|
1536
|
+
const where = where2column(options.where, fieldColumns);
|
|
1537
|
+
where.push(...getFixedValueWhere(columns));
|
|
1445
1538
|
const value = typeof update !== 'object' ? {} : {
|
|
1446
1539
|
...update
|
|
1447
1540
|
};
|
|
@@ -1450,7 +1543,7 @@ async function pseudoDeleteMany(conn, queryable, update, env) {
|
|
|
1450
1543
|
value[pseudo] = pseudoValue;
|
|
1451
1544
|
const set = set2column(value, fieldColumns);
|
|
1452
1545
|
await setDeleteTimestamp(columns, set);
|
|
1453
|
-
const list = table && typeof table === 'object' ? await table.updateReturn(env, conn, Object.keys(columns), columns, set,
|
|
1546
|
+
const list = table && typeof table === 'object' ? await table.updateReturn(env, conn, Object.keys(columns), columns, set, where) : await conn.updateReturn(env, Object.keys(columns), table || '', columns, set, where);
|
|
1454
1547
|
return list.length;
|
|
1455
1548
|
}
|
|
1456
1549
|
|
|
@@ -1725,20 +1818,21 @@ class Connection {
|
|
|
1725
1818
|
* @template {TableDefine} T
|
|
1726
1819
|
* @param {T} model
|
|
1727
1820
|
* @param {object | object[]} p
|
|
1821
|
+
* @param {Skip} [skip]
|
|
1728
1822
|
* @returns {Promise<object[] | (T extends Build<infer R> ? R : object)>}
|
|
1729
1823
|
*/
|
|
1730
|
-
async create(model, p) {
|
|
1824
|
+
async create(model, p, skip) {
|
|
1731
1825
|
if (Array.isArray(p) || typeof model[Create] !== 'function') {
|
|
1732
1826
|
const modelBuild = model[Build];
|
|
1733
1827
|
const md = typeof modelBuild === 'function' ? modelBuild.bind(model) : undefined;
|
|
1734
1828
|
const conn = await this.#getConnection();
|
|
1735
|
-
return create(conn, this.#env, model, p, md);
|
|
1829
|
+
return create(conn, this.#env, model, p, skip, md);
|
|
1736
1830
|
}
|
|
1737
1831
|
// @ts-ignore
|
|
1738
1832
|
const m = /** @type {Create} */model;
|
|
1739
1833
|
return m[Create](this, p, async data => {
|
|
1740
1834
|
const conn = await this.#getConnection();
|
|
1741
|
-
return create(conn, this.#env, model, data);
|
|
1835
|
+
return create(conn, this.#env, model, data, skip);
|
|
1742
1836
|
}, model);
|
|
1743
1837
|
}
|
|
1744
1838
|
/**
|
|
@@ -1766,27 +1860,28 @@ class Connection {
|
|
|
1766
1860
|
* @param {TableDefine} model
|
|
1767
1861
|
* @param {T} data
|
|
1768
1862
|
* @param {Record<string, any>?} [newData]
|
|
1863
|
+
* @param {Skip} [skip]
|
|
1769
1864
|
* @returns {Promise<(T extends Save ? T : object)?>}
|
|
1770
1865
|
*/
|
|
1771
|
-
async save(model, data, newData) {
|
|
1866
|
+
async save(model, data, newData, skip) {
|
|
1772
1867
|
if (typeof data[Save] !== 'function') {
|
|
1773
1868
|
const conn = await this.#getConnection();
|
|
1774
1869
|
if (newData) {
|
|
1775
1870
|
/** @type {any} */
|
|
1776
1871
|
const doc = data;
|
|
1777
1872
|
// @ts-ignore
|
|
1778
|
-
return updateData(conn, model, doc, newData, this.#env);
|
|
1873
|
+
return updateData(conn, model, doc, newData, this.#env, skip);
|
|
1779
1874
|
}
|
|
1780
|
-
return create(conn, this.#env, model, data);
|
|
1875
|
+
return create(conn, this.#env, model, data, skip);
|
|
1781
1876
|
}
|
|
1782
1877
|
const r = /** @type {Save}*/data;
|
|
1783
1878
|
// @ts-ignore
|
|
1784
1879
|
return r[Save](this, async (doc, newData) => {
|
|
1785
1880
|
const conn = await this.#getConnection();
|
|
1786
1881
|
if (newData) {
|
|
1787
|
-
return updateData(conn, model, doc, newData, this.#env);
|
|
1882
|
+
return updateData(conn, model, doc, newData, this.#env, skip);
|
|
1788
1883
|
}
|
|
1789
|
-
return create(conn, this.#env, model, doc);
|
|
1884
|
+
return create(conn, this.#env, model, doc, skip);
|
|
1790
1885
|
}, newData || null, model);
|
|
1791
1886
|
}
|
|
1792
1887
|
/**
|
|
@@ -1898,7 +1993,9 @@ class Connection {
|
|
|
1898
1993
|
}, data, keys) {
|
|
1899
1994
|
const [columns, fieldColumns] = toColumns(fields);
|
|
1900
1995
|
const isArray = Array.isArray(data);
|
|
1901
|
-
const insertValues = (isArray ? data : [data]).map(d => getInsertValue(d, columns, fieldColumns
|
|
1996
|
+
const insertValues = (isArray ? data : [data]).map(d => getInsertValue(d, columns, fieldColumns, {
|
|
1997
|
+
uncreatable: true
|
|
1998
|
+
}));
|
|
1902
1999
|
if (!insertValues.length) {
|
|
1903
2000
|
throw new Error();
|
|
1904
2001
|
}
|
|
@@ -1928,7 +2025,9 @@ class Connection {
|
|
|
1928
2025
|
}, update, where, skip) {
|
|
1929
2026
|
const [columns, fieldColumns, tableFields] = toColumns(fields);
|
|
1930
2027
|
const conn = await this.#getConnection();
|
|
1931
|
-
|
|
2028
|
+
const whereColumns = where2column(where, fieldColumns);
|
|
2029
|
+
whereColumns.push(...getFixedValueWhere(columns));
|
|
2030
|
+
return table && typeof table === 'object' ? table.update(this.#env, conn, columns, await getSetValue$1(columns, fieldColumns, update, skip), whereColumns) : conn.update(this.#env, table || '', columns, await getSetValue$1(columns, fieldColumns, update, skip), whereColumns);
|
|
1932
2031
|
}
|
|
1933
2032
|
/**
|
|
1934
2033
|
* @template {Fields<MainFieldType>} T
|
|
@@ -1945,7 +2044,9 @@ class Connection {
|
|
|
1945
2044
|
}, update, returning, where, skip) {
|
|
1946
2045
|
const [columns, fieldColumns, tableFields] = toColumns(fields);
|
|
1947
2046
|
const conn = await this.#getConnection();
|
|
1948
|
-
const
|
|
2047
|
+
const whereColumns = where2column(where, fieldColumns);
|
|
2048
|
+
whereColumns.push(...getFixedValueWhere(columns));
|
|
2049
|
+
const result = table && typeof table === 'object' ? await table.updateReturn(this.#env, conn, returning?.length ? field2column(returning, fieldColumns) : Object.keys(columns), columns, await getSetValue$1(columns, fieldColumns, update, skip), whereColumns) : await conn.updateReturn(this.#env, returning?.length ? field2column(returning, fieldColumns) : Object.keys(columns), table || '', columns, await getSetValue$1(columns, fieldColumns, update, skip), whereColumns);
|
|
1949
2050
|
return toFieldList(result, fieldColumns);
|
|
1950
2051
|
}
|
|
1951
2052
|
/**
|
|
@@ -1998,7 +2099,9 @@ class Connection {
|
|
|
1998
2099
|
}, where) {
|
|
1999
2100
|
const [columns, fieldColumns, tableFields] = toColumns(fields);
|
|
2000
2101
|
const conn = await this.#getConnection();
|
|
2001
|
-
|
|
2102
|
+
const whereColumns = where2column(where, fieldColumns);
|
|
2103
|
+
whereColumns.push(...getFixedValueWhere(columns));
|
|
2104
|
+
return table && typeof table === 'object' ? table.delete(this.#env, conn, columns, whereColumns) : conn.delete(this.#env, table || '', columns, whereColumns);
|
|
2002
2105
|
}
|
|
2003
2106
|
/**
|
|
2004
2107
|
* @template {Fields<MainFieldType>} T
|
|
@@ -2013,7 +2116,9 @@ class Connection {
|
|
|
2013
2116
|
}, returning, where) {
|
|
2014
2117
|
const [columns, fieldColumns, tableFields] = toColumns(fields);
|
|
2015
2118
|
const conn = await this.#getConnection();
|
|
2016
|
-
const
|
|
2119
|
+
const whereColumns = where2column(where, fieldColumns);
|
|
2120
|
+
whereColumns.push(...getFixedValueWhere(columns));
|
|
2121
|
+
const result = table && typeof table === 'object' ? await table.deleteReturn(this.#env, conn, returning?.length ? field2column(returning, fieldColumns) : Object.keys(columns), columns, whereColumns) : await conn.deleteReturn(this.#env, returning?.length ? field2column(returning, fieldColumns) : Object.keys(columns), table || '', columns, whereColumns);
|
|
2017
2122
|
return toFieldList(result, fieldColumns);
|
|
2018
2123
|
}
|
|
2019
2124
|
/**
|
|
@@ -2028,6 +2133,7 @@ class Connection {
|
|
|
2028
2133
|
} = queryable;
|
|
2029
2134
|
const options = getOptions(queryable);
|
|
2030
2135
|
const where = where2column(options.where, fieldColumns);
|
|
2136
|
+
where.push(...getFixedValueWhere(columns));
|
|
2031
2137
|
const conn = await this.#getConnection();
|
|
2032
2138
|
return table && typeof table === 'object' ? table.count(this.#env, conn, columns, where) : conn.count(this.#env, table || '', columns, where);
|
|
2033
2139
|
}
|
|
@@ -2046,6 +2152,7 @@ class Connection {
|
|
|
2046
2152
|
limit
|
|
2047
2153
|
} = options;
|
|
2048
2154
|
const where = where2column(options.where, fieldColumns);
|
|
2155
|
+
where.push(...getFixedValueWhere(columns));
|
|
2049
2156
|
const conn = await this.#getConnection();
|
|
2050
2157
|
const select = select2column(columns, fieldColumns, options.select);
|
|
2051
2158
|
const {
|
|
@@ -2495,6 +2602,7 @@ function submodel(type, constraints, p1, p2) {
|
|
|
2495
2602
|
unique,
|
|
2496
2603
|
index,
|
|
2497
2604
|
primary,
|
|
2605
|
+
uncreatable,
|
|
2498
2606
|
immutable,
|
|
2499
2607
|
text,
|
|
2500
2608
|
sort,
|
|
@@ -2506,6 +2614,7 @@ function submodel(type, constraints, p1, p2) {
|
|
|
2506
2614
|
type,
|
|
2507
2615
|
array,
|
|
2508
2616
|
nullable: false,
|
|
2617
|
+
uncreatable,
|
|
2509
2618
|
immutable
|
|
2510
2619
|
};
|
|
2511
2620
|
/**
|
|
@@ -2724,6 +2833,7 @@ function field$1(type, p1, p2) {
|
|
|
2724
2833
|
unique,
|
|
2725
2834
|
index,
|
|
2726
2835
|
primary,
|
|
2836
|
+
uncreatable,
|
|
2727
2837
|
immutable,
|
|
2728
2838
|
text,
|
|
2729
2839
|
sort,
|
|
@@ -2740,6 +2850,7 @@ function field$1(type, p1, p2) {
|
|
|
2740
2850
|
index,
|
|
2741
2851
|
unique: Boolean(unique),
|
|
2742
2852
|
primary,
|
|
2853
|
+
uncreatable,
|
|
2743
2854
|
immutable,
|
|
2744
2855
|
text,
|
|
2745
2856
|
sort
|
|
@@ -2889,6 +3000,12 @@ function primary(primary) {
|
|
|
2889
3000
|
return (_, ctx) => setProp(ctx, 'primary', primary || 1);
|
|
2890
3001
|
}
|
|
2891
3002
|
|
|
3003
|
+
/** @import { FieldDecorator } from './index.mjs' */
|
|
3004
|
+
/** @returns {FieldDecorator<any>} */
|
|
3005
|
+
function uncreatable() {
|
|
3006
|
+
return (_, ctx) => setProp(ctx, 'uncreatable', true);
|
|
3007
|
+
}
|
|
3008
|
+
|
|
2892
3009
|
/** @import { FieldDecorator } from './index.mjs' */
|
|
2893
3010
|
/** @returns {FieldDecorator<any>} */
|
|
2894
3011
|
function immutable() {
|
|
@@ -3034,6 +3151,35 @@ function pseudo(pseudo) {
|
|
|
3034
3151
|
};
|
|
3035
3152
|
}
|
|
3036
3153
|
|
|
3154
|
+
/** @import { now, uuid } from '../values.mjs' */
|
|
3155
|
+
/** @import { FieldDecorator } from './index.mjs' */
|
|
3156
|
+
/**
|
|
3157
|
+
* @template T
|
|
3158
|
+
* @overload
|
|
3159
|
+
* @param {now} value
|
|
3160
|
+
* @returns {FieldDecorator<string | Date>}
|
|
3161
|
+
*/
|
|
3162
|
+
/**
|
|
3163
|
+
* @template T
|
|
3164
|
+
* @overload
|
|
3165
|
+
* @param {uuid} value
|
|
3166
|
+
* @returns {FieldDecorator<string>}
|
|
3167
|
+
*/
|
|
3168
|
+
/**
|
|
3169
|
+
* @template T
|
|
3170
|
+
* @overload
|
|
3171
|
+
* @param {T} value
|
|
3172
|
+
* @returns {FieldDecorator<T>}
|
|
3173
|
+
*/
|
|
3174
|
+
/**
|
|
3175
|
+
* @template T
|
|
3176
|
+
* @param {T} value
|
|
3177
|
+
* @returns {FieldDecorator<any>}
|
|
3178
|
+
*/
|
|
3179
|
+
function defaultValue(value) {
|
|
3180
|
+
return (_, ctx) => setProp(ctx, 'default', value);
|
|
3181
|
+
}
|
|
3182
|
+
|
|
3037
3183
|
/** @typedef {{toWhereValue(): WhereValue[][]}} WhereLike */
|
|
3038
3184
|
/** @typedef {WhereLike | Record<string, any> | (WhereLike | Record<string, any>)[]} Wheres */
|
|
3039
3185
|
/**
|
|
@@ -5078,7 +5224,7 @@ defGetter('indexes', function () {
|
|
|
5078
5224
|
/**
|
|
5079
5225
|
* @typedef {object} Scene.Define
|
|
5080
5226
|
* @property {string} table
|
|
5081
|
-
* @property {
|
|
5227
|
+
* @property {{column: string, field?: string, fixedValue?: any, [v: string]: any}[]} fields
|
|
5082
5228
|
*/
|
|
5083
5229
|
/** @import {FieldDefine, Fields, TableDefine,} from './types/table' */
|
|
5084
5230
|
class Scene {
|
|
@@ -5103,24 +5249,37 @@ class Scene {
|
|
|
5103
5249
|
const allFields = new Map(Object.entries(sceneFields));
|
|
5104
5250
|
/** @type {Record<string, FieldDefine>} */
|
|
5105
5251
|
const fields = {};
|
|
5106
|
-
for (const
|
|
5107
|
-
|
|
5108
|
-
|
|
5109
|
-
|
|
5110
|
-
|
|
5111
|
-
|
|
5112
|
-
|
|
5113
|
-
|
|
5114
|
-
|
|
5115
|
-
|
|
5116
|
-
|
|
5117
|
-
|
|
5118
|
-
|
|
5119
|
-
|
|
5120
|
-
|
|
5252
|
+
for (const {
|
|
5253
|
+
column,
|
|
5254
|
+
field,
|
|
5255
|
+
fixedValue,
|
|
5256
|
+
...p
|
|
5257
|
+
} of table.fields) {
|
|
5258
|
+
let define = field && allFields.get(field) || null;
|
|
5259
|
+
if (!define?.type || typeof define.type !== 'string' && define.type?.table) {
|
|
5260
|
+
fields[`__${column}`] = {
|
|
5261
|
+
...p,
|
|
5262
|
+
fixedValue,
|
|
5263
|
+
type: p.type,
|
|
5264
|
+
array: p.array,
|
|
5265
|
+
nullable: p.nullable,
|
|
5266
|
+
primary: p.primary,
|
|
5267
|
+
size: p.size,
|
|
5268
|
+
scale: p.scale,
|
|
5121
5269
|
column
|
|
5122
5270
|
};
|
|
5271
|
+
continue;
|
|
5123
5272
|
}
|
|
5273
|
+
if (field) {
|
|
5274
|
+
allFields.delete(field);
|
|
5275
|
+
}
|
|
5276
|
+
fields[field || `__${column}`] = {
|
|
5277
|
+
...p,
|
|
5278
|
+
...define,
|
|
5279
|
+
nullable: p.nullable,
|
|
5280
|
+
primary: p.primary,
|
|
5281
|
+
column
|
|
5282
|
+
};
|
|
5124
5283
|
}
|
|
5125
5284
|
for (const [name, define] of allFields) {
|
|
5126
5285
|
if (!define.type) {
|
|
@@ -5132,7 +5291,8 @@ class Scene {
|
|
|
5132
5291
|
if (typeof define.type === 'string') {
|
|
5133
5292
|
throw new Error();
|
|
5134
5293
|
}
|
|
5135
|
-
if (typeof define.type === '
|
|
5294
|
+
if (define.type.table && typeof define.type.table === 'string') {
|
|
5295
|
+
// @ts-ignore
|
|
5136
5296
|
fields[name] = {
|
|
5137
5297
|
...define,
|
|
5138
5298
|
type: this.#getModel(define.type)
|
|
@@ -5327,6 +5487,7 @@ function field(type, {
|
|
|
5327
5487
|
default: def,
|
|
5328
5488
|
index,
|
|
5329
5489
|
primary,
|
|
5490
|
+
uncreatable,
|
|
5330
5491
|
immutable,
|
|
5331
5492
|
text,
|
|
5332
5493
|
sort,
|
|
@@ -5340,10 +5501,11 @@ function field(type, {
|
|
|
5340
5501
|
default: def,
|
|
5341
5502
|
index,
|
|
5342
5503
|
primary,
|
|
5504
|
+
uncreatable,
|
|
5343
5505
|
immutable,
|
|
5344
5506
|
text,
|
|
5345
5507
|
sort
|
|
5346
5508
|
};
|
|
5347
5509
|
}
|
|
5348
5510
|
|
|
5349
|
-
export { Build, Connection, Create, Destroy, Model, PseudoDestroy, Query, Save, Scene, Submodel, Where, column, creating, decrement, field as define, deleted, deleting, divide, field$1 as field, getPrimaryFields, getPrimaryKeys, immutable, increment, index, isDevelopment, isPseudo, submodel as model, multiply, now, primary, prop, pseudo, setDevelopment, sort, submodel, toNot, toPrimaries, toPrimary, undeleted, updating, uuid, values, withDeleted };
|
|
5511
|
+
export { Build, Connection, Create, Destroy, Model, PseudoDestroy, Query, Save, Scene, Submodel, Where, column, creating, decrement, defaultValue, field as define, deleted, deleting, divide, field$1 as field, getPrimaryFields, getPrimaryKeys, immutable, increment, index, isDevelopment, isPseudo, submodel as model, multiply, now, primary, prop, pseudo, setDevelopment, sort, submodel, toNot, toPrimaries, toPrimary, uncreatable, undeleted, updating, uuid, values, withDeleted };
|
package/migrate.d.mts
CHANGED
package/migrate.mjs
CHANGED