imodel 0.4.1 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.mts +11 -3
- package/index.mjs +209 -104
- 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.
|
|
2
|
+
* imodel v0.5.0
|
|
3
3
|
* (c) 2019-2025 undefined
|
|
4
4
|
* @license undefined
|
|
5
5
|
*/
|
|
@@ -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;
|
|
@@ -640,6 +641,8 @@ interface FieldDefineOption {
|
|
|
640
641
|
deleting?: FieldSpecific;
|
|
641
642
|
/** 是否为固定值 */
|
|
642
643
|
immutable?: boolean;
|
|
644
|
+
/** 固定值 */
|
|
645
|
+
fixedValue?: any;
|
|
643
646
|
/** 文本查询 */
|
|
644
647
|
text?: boolean;
|
|
645
648
|
/** 排序顺序,绝对值表示在排序列表中的顺序,负数表示逆序排序, 0 表示不排序 */
|
|
@@ -1844,7 +1847,7 @@ declare function toPrimaries(model: any, documents: any): string | string[];
|
|
|
1844
1847
|
/**
|
|
1845
1848
|
* @typedef {object} Scene.Define
|
|
1846
1849
|
* @property {string} table
|
|
1847
|
-
* @property {
|
|
1850
|
+
* @property {{column: string, field?: string, fixedValue?: any, [v: string]: any}[]} fields
|
|
1848
1851
|
*/
|
|
1849
1852
|
/** @import {FieldDefine, Fields, TableDefine,} from './types/table' */
|
|
1850
1853
|
declare class Scene {
|
|
@@ -1864,7 +1867,12 @@ declare class Scene {
|
|
|
1864
1867
|
declare namespace Scene {
|
|
1865
1868
|
type Define = {
|
|
1866
1869
|
table: string;
|
|
1867
|
-
fields:
|
|
1870
|
+
fields: {
|
|
1871
|
+
column: string;
|
|
1872
|
+
field?: string;
|
|
1873
|
+
fixedValue?: any;
|
|
1874
|
+
[v: string]: any;
|
|
1875
|
+
}[];
|
|
1868
1876
|
};
|
|
1869
1877
|
}
|
|
1870
1878
|
|
package/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* imodel v0.
|
|
2
|
+
* imodel v0.5.0
|
|
3
3
|
* (c) 2019-2025 undefined
|
|
4
4
|
* @license undefined
|
|
5
5
|
*/
|
|
@@ -141,15 +141,23 @@ async function getSpecificValue(val) {
|
|
|
141
141
|
* @param {Record<string, any>} data
|
|
142
142
|
* @param {Fields} columns
|
|
143
143
|
* @param {Record<string, string> | null} fieldColumns
|
|
144
|
+
* @param {Set<string>?} [ignore]
|
|
144
145
|
* @returns {Promise<Record<string, any>>}
|
|
145
146
|
*/
|
|
146
|
-
async function getInsertValue(data, columns, fieldColumns) {
|
|
147
|
+
async function getInsertValue(data, columns, fieldColumns, ignore) {
|
|
147
148
|
const insertValue = {};
|
|
148
149
|
for (const [k, col] of fieldColumns ? Object.entries(fieldColumns) : Object.keys(columns).map(v => [v, v])) {
|
|
149
150
|
const column = columns[col];
|
|
150
151
|
if (!column) {
|
|
151
152
|
continue;
|
|
152
153
|
}
|
|
154
|
+
const {
|
|
155
|
+
fixedValue
|
|
156
|
+
} = column;
|
|
157
|
+
if (fixedValue !== void 0 && !ignore?.has(k)) {
|
|
158
|
+
insertValue[col] = fixedValue;
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
153
161
|
const creating = await getSpecificValue(column.creating);
|
|
154
162
|
if (creating !== void 0) {
|
|
155
163
|
insertValue[col] = creating;
|
|
@@ -419,7 +427,8 @@ async function createSub(conn, env, fields, newList, list) {
|
|
|
419
427
|
}
|
|
420
428
|
const [columns, fieldColumns, tableFields] = toColumns(fields);
|
|
421
429
|
const insertKeys = Object.entries(columns).map(v => v[0]);
|
|
422
|
-
const
|
|
430
|
+
const ignore = new Set(fieldColumns ? constraintEntries.map(([k]) => fieldColumns[k]) : constraintEntries.map(([k]) => k));
|
|
431
|
+
const insertValues = await Promise.all(fieldValues.map(d => getInsertValue(d, columns, fieldColumns, ignore)));
|
|
423
432
|
const {
|
|
424
433
|
table
|
|
425
434
|
} = type;
|
|
@@ -502,7 +511,7 @@ function getPrimaryKeys(fieldsOrColumns) {
|
|
|
502
511
|
/**
|
|
503
512
|
* @param {Fields} fields
|
|
504
513
|
* @param {Set<string>} parentFields
|
|
505
|
-
* @param {Record<string,
|
|
514
|
+
* @param {Record<string, Constraint> | undefined} constraints
|
|
506
515
|
* @returns {[string, string][]?}
|
|
507
516
|
*/
|
|
508
517
|
function getMapList(fields, parentFields, constraints) {
|
|
@@ -515,7 +524,12 @@ function getMapList(fields, parentFields, constraints) {
|
|
|
515
524
|
if (!(k in fields)) {
|
|
516
525
|
continue;
|
|
517
526
|
}
|
|
518
|
-
|
|
527
|
+
if (v.writeOnly) {
|
|
528
|
+
continue;
|
|
529
|
+
}
|
|
530
|
+
const {
|
|
531
|
+
field
|
|
532
|
+
} = v;
|
|
519
533
|
if (typeof field !== 'string') {
|
|
520
534
|
continue;
|
|
521
535
|
}
|
|
@@ -531,34 +545,73 @@ function getMapList(fields, parentFields, constraints) {
|
|
|
531
545
|
}
|
|
532
546
|
|
|
533
547
|
/** @import { Constraint, Fields, FieldType } from '../../types' */
|
|
548
|
+
/** @import { WhereValue } from '../../Where.mjs' */
|
|
534
549
|
/**
|
|
535
550
|
*
|
|
536
551
|
* @param {Record<string, Constraint> | undefined} value
|
|
537
552
|
* @param {Fields<keyof FieldType>} columns
|
|
538
|
-
* @
|
|
553
|
+
* @param {Record<string, string>?} fieldColumns
|
|
554
|
+
* @returns {WhereValue[]}
|
|
539
555
|
*/
|
|
540
|
-
function
|
|
556
|
+
function getValueWhere(value, columns, fieldColumns) {
|
|
541
557
|
if (!value) {
|
|
542
558
|
return [];
|
|
543
559
|
}
|
|
560
|
+
const keys = new Set();
|
|
544
561
|
/** @type {[string, unknown][]} */
|
|
545
562
|
const where = [];
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
563
|
+
if (!fieldColumns) {
|
|
564
|
+
for (const [k, v] of Object.entries(value)) {
|
|
565
|
+
if (!(k in columns)) {
|
|
566
|
+
continue;
|
|
567
|
+
}
|
|
568
|
+
keys.add(k);
|
|
569
|
+
if (v.writeOnly) {
|
|
570
|
+
continue;
|
|
571
|
+
}
|
|
572
|
+
const {
|
|
573
|
+
value
|
|
574
|
+
} = v;
|
|
575
|
+
if (value === undefined) {
|
|
576
|
+
continue;
|
|
577
|
+
}
|
|
578
|
+
where.push([k, value]);
|
|
549
579
|
}
|
|
550
|
-
|
|
580
|
+
} else {
|
|
581
|
+
for (const [k, v] of Object.entries(value)) {
|
|
582
|
+
if (!(k in fieldColumns)) {
|
|
583
|
+
continue;
|
|
584
|
+
}
|
|
585
|
+
const key = fieldColumns[k];
|
|
586
|
+
keys.add(key);
|
|
587
|
+
if (v.writeOnly) {
|
|
588
|
+
continue;
|
|
589
|
+
}
|
|
590
|
+
const {
|
|
591
|
+
value
|
|
592
|
+
} = v;
|
|
593
|
+
if (value === undefined) {
|
|
594
|
+
continue;
|
|
595
|
+
}
|
|
596
|
+
where.push([key, value]);
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
for (const [k, {
|
|
600
|
+
fixedValue
|
|
601
|
+
}] of Object.entries(columns)) {
|
|
602
|
+
if (keys.has(k)) {
|
|
551
603
|
continue;
|
|
552
604
|
}
|
|
553
|
-
|
|
554
|
-
value
|
|
555
|
-
} = v;
|
|
556
|
-
if (value === undefined) {
|
|
605
|
+
if (fixedValue === void 0) {
|
|
557
606
|
continue;
|
|
558
607
|
}
|
|
559
|
-
where.push([k,
|
|
608
|
+
where.push([k, fixedValue]);
|
|
560
609
|
}
|
|
561
|
-
return where
|
|
610
|
+
return where.map(([k, v]) => ({
|
|
611
|
+
field: k,
|
|
612
|
+
operator: '=',
|
|
613
|
+
value: v
|
|
614
|
+
}));
|
|
562
615
|
}
|
|
563
616
|
|
|
564
617
|
/** @import { WhereValue } from '../../Where.mjs' */
|
|
@@ -566,30 +619,19 @@ function getValueEntries(value, columns) {
|
|
|
566
619
|
*
|
|
567
620
|
* @param {Record<string, string>[]} list
|
|
568
621
|
* @param {[string, string][]} map
|
|
622
|
+
* @param {Record<string, string>?} fieldColumns
|
|
569
623
|
* @returns {WhereValue}
|
|
570
624
|
*/
|
|
571
|
-
function subList2where(list, map) {
|
|
625
|
+
function subList2where(list, map, fieldColumns) {
|
|
572
626
|
const valueKeys = map.map(([, v]) => v);
|
|
627
|
+
const field = fieldColumns ? map.map(([k]) => fieldColumns[k]) : map.map(([k]) => k);
|
|
628
|
+
const value = list.map(d => valueKeys.map(v => d[v]));
|
|
573
629
|
return {
|
|
574
|
-
field
|
|
575
|
-
value
|
|
630
|
+
field,
|
|
631
|
+
value
|
|
576
632
|
};
|
|
577
633
|
}
|
|
578
634
|
|
|
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
635
|
/** @import { WhereValue } from '../../Where.mjs' */
|
|
594
636
|
/**
|
|
595
637
|
*
|
|
@@ -640,6 +682,30 @@ function where2column(where, fieldColumns) {
|
|
|
640
682
|
}));
|
|
641
683
|
}
|
|
642
684
|
|
|
685
|
+
/** @import { Fields, FieldType } from '../../types' */
|
|
686
|
+
/** @import { WhereValue } from '../../Where.mjs' */
|
|
687
|
+
/**
|
|
688
|
+
*
|
|
689
|
+
* @param {Fields<keyof FieldType>} columns
|
|
690
|
+
* @param {Set<string>?} [ignore]
|
|
691
|
+
* @returns {Iterable<WhereValue>}
|
|
692
|
+
*/
|
|
693
|
+
function* getFixedValueWhere(columns, ignore) {
|
|
694
|
+
for (const [k, {
|
|
695
|
+
fixedValue
|
|
696
|
+
}] of Object.entries(columns)) {
|
|
697
|
+
if (ignore?.has(k)) {
|
|
698
|
+
continue;
|
|
699
|
+
}
|
|
700
|
+
if (fixedValue !== void 0) {
|
|
701
|
+
yield {
|
|
702
|
+
field: k,
|
|
703
|
+
value: fixedValue
|
|
704
|
+
};
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
|
|
643
709
|
/** @import { Environment, FieldDefine, IConnection, Queryable, TableDefine } from '../types' */
|
|
644
710
|
/**
|
|
645
711
|
*
|
|
@@ -671,8 +737,7 @@ async function deleteSub(conn, list, fields, baseFieldSet, env, hasResult) {
|
|
|
671
737
|
if (!mapList) {
|
|
672
738
|
continue;
|
|
673
739
|
}
|
|
674
|
-
const
|
|
675
|
-
const where = where2column([subList2where(list, mapList), ...valueEntries2where(value)], fieldColumns);
|
|
740
|
+
const where = [subList2where(list, mapList, fieldColumns), ...getValueWhere(constraints, columns, fieldColumns)];
|
|
676
741
|
const {
|
|
677
742
|
table
|
|
678
743
|
} = type;
|
|
@@ -714,6 +779,7 @@ async function deleteMany(conn, queryable, env) {
|
|
|
714
779
|
} = queryable;
|
|
715
780
|
const [columns, fieldColumns, tableFields] = toColumns(fields);
|
|
716
781
|
const where = where2column(queryable.options.where, fieldColumns);
|
|
782
|
+
where.push(...getFixedValueWhere(columns));
|
|
717
783
|
if (!tableFields.length) {
|
|
718
784
|
return table && typeof table === 'object' ? table.delete(env, conn, columns, where) : conn.delete(env, table || '', columns, where);
|
|
719
785
|
}
|
|
@@ -787,11 +853,18 @@ async function getSetValue$1(columns, fieldColumns, update, skip) {
|
|
|
787
853
|
if (field.primary) {
|
|
788
854
|
continue;
|
|
789
855
|
}
|
|
856
|
+
const {
|
|
857
|
+
fixedValue
|
|
858
|
+
} = field;
|
|
859
|
+
if (fixedValue !== void 0) {
|
|
860
|
+
data[name] = fixedValue;
|
|
861
|
+
continue;
|
|
862
|
+
}
|
|
790
863
|
const updating = await getSpecificValue(field.updating);
|
|
791
|
-
if (updating
|
|
864
|
+
if (updating !== void 0) {
|
|
865
|
+
data[name] = updating;
|
|
792
866
|
continue;
|
|
793
867
|
}
|
|
794
|
-
data[name] = updating;
|
|
795
868
|
}
|
|
796
869
|
}
|
|
797
870
|
return data;
|
|
@@ -844,12 +917,18 @@ async function updateItem(conn, env, updatedOldNewList, key, type, array, constr
|
|
|
844
917
|
const commonValue = {};
|
|
845
918
|
/** @type {[string, string][]} */
|
|
846
919
|
const commonMapKeys = [];
|
|
920
|
+
/** @type {[string, string][]} */
|
|
921
|
+
const commonFilterKeys = [];
|
|
847
922
|
for (const [k, {
|
|
848
923
|
field,
|
|
849
|
-
value: val
|
|
924
|
+
value: val,
|
|
925
|
+
writeOnly
|
|
850
926
|
}] of constraintEntries) {
|
|
851
927
|
if (field) {
|
|
852
928
|
commonMapKeys.push([k, field]);
|
|
929
|
+
if (!writeOnly) {
|
|
930
|
+
commonFilterKeys.push([k, field]);
|
|
931
|
+
}
|
|
853
932
|
} else if (val !== undefined) {
|
|
854
933
|
commonValue[k] = val;
|
|
855
934
|
}
|
|
@@ -903,17 +982,6 @@ async function updateItem(conn, env, updatedOldNewList, key, type, array, constr
|
|
|
903
982
|
return;
|
|
904
983
|
}
|
|
905
984
|
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
985
|
const oldList = oldListValues.flat();
|
|
918
986
|
const newList = newListValues.flat();
|
|
919
987
|
const primaryKeys = getPrimaryKeys(fields);
|
|
@@ -930,36 +998,39 @@ async function updateItem(conn, env, updatedOldNewList, key, type, array, constr
|
|
|
930
998
|
const needUpdate = [];
|
|
931
999
|
/** @type {Record<string, any>[]} */
|
|
932
1000
|
const unmatched = [];
|
|
933
|
-
const aKeys = fieldKeys.filter(v => !(v in
|
|
934
|
-
const pKeys = primaryKeys.filter(v => !(v in
|
|
1001
|
+
const aKeys = fieldKeys.filter(v => !(v in commonValue));
|
|
1002
|
+
const pKeys = primaryKeys.filter(v => !(v in commonValue));
|
|
935
1003
|
classify(newList, needDelete, aKeys, unmatched, eq);
|
|
936
1004
|
classify(unmatched, needDelete, pKeys, needCreate, needUpdate);
|
|
937
1005
|
const {
|
|
938
1006
|
table
|
|
939
1007
|
} = type;
|
|
1008
|
+
const ignore = new Set(fieldColumns ? constraintEntries.map(([k]) => fieldColumns[k]) : constraintEntries.map(([k]) => k));
|
|
940
1009
|
if (needDelete.length) {
|
|
941
1010
|
// 批量删除
|
|
942
1011
|
/** @type {WhereValue[]} */
|
|
943
|
-
const
|
|
1012
|
+
const fieldWhere = primaryKeys.filter(v => v in commonValue).map(k => ({
|
|
944
1013
|
field: k,
|
|
945
1014
|
operator: '=',
|
|
946
|
-
value:
|
|
1015
|
+
value: commonValue[k] ?? null
|
|
947
1016
|
}));
|
|
948
1017
|
if (pKeys.length === 1) {
|
|
949
1018
|
const [key] = pKeys;
|
|
950
|
-
|
|
1019
|
+
fieldWhere.push({
|
|
951
1020
|
field: key,
|
|
952
1021
|
operator: 'in',
|
|
953
1022
|
value: needDelete.map(v => v[key] ?? null)
|
|
954
1023
|
});
|
|
955
1024
|
} else if (pKeys.length) {
|
|
956
|
-
|
|
1025
|
+
fieldWhere.push({
|
|
957
1026
|
field: pKeys,
|
|
958
1027
|
operator: 'in',
|
|
959
1028
|
value: needDelete.map(v => pKeys.map(k => v[k] ?? null))
|
|
960
1029
|
});
|
|
961
1030
|
}
|
|
962
|
-
const
|
|
1031
|
+
const where = where2column(fieldWhere, fieldColumns);
|
|
1032
|
+
where.push(...getFixedValueWhere(columns, ignore));
|
|
1033
|
+
const promise = typeof table === 'object' ? table.deleteReturn(env, conn, columnKeys, columns, where) : conn.deleteReturn(env, columnKeys, table || '', columns, where);
|
|
963
1034
|
promises.push(promise.then(list => deleteSub(conn, toFieldList(list, fieldColumns), tableFields, new Set(Object.keys(fieldColumns || columns)), env)));
|
|
964
1035
|
}
|
|
965
1036
|
/** @type {typeof updatedOldNewList} */
|
|
@@ -982,7 +1053,7 @@ async function updateItem(conn, env, updatedOldNewList, key, type, array, constr
|
|
|
982
1053
|
}
|
|
983
1054
|
const setKeysColumn = Object.entries(columns).filter(([, v]) => !v.primary && !v.immutable).map(v => v[0]).filter(v => keysColumn.has(v));
|
|
984
1055
|
/** @type {WhereValue[]} */
|
|
985
|
-
const
|
|
1056
|
+
const fieldWhere = Object.entries(commonValue).filter(([k]) => primaryKeys.includes(k)).map(([k, v]) => ({
|
|
986
1057
|
field: k,
|
|
987
1058
|
operator: '=',
|
|
988
1059
|
value: v
|
|
@@ -995,13 +1066,15 @@ async function updateItem(conn, env, updatedOldNewList, key, type, array, constr
|
|
|
995
1066
|
value[key] = old[key];
|
|
996
1067
|
}
|
|
997
1068
|
}
|
|
998
|
-
const
|
|
1069
|
+
const where = where2column(fieldWhere, fieldColumns);
|
|
1070
|
+
where.push(...getFixedValueWhere(columns, ignore));
|
|
1071
|
+
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
1072
|
listPromises.push(list);
|
|
1000
1073
|
subPromises.push(list.then(u => u.map((u, i) => [u, ...needUpdate[i]])));
|
|
1001
1074
|
}
|
|
1002
1075
|
// 批量新建
|
|
1003
1076
|
if (needCreate.length) {
|
|
1004
|
-
const subColumnData = await Promise.all(needCreate.map(d => getInsertValue(d, columns, fieldColumns)));
|
|
1077
|
+
const subColumnData = await Promise.all(needCreate.map(d => getInsertValue(d, columns, fieldColumns, ignore)));
|
|
1005
1078
|
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
1079
|
listPromises.push(list);
|
|
1007
1080
|
promises.push(list.then(list => createSub(conn, env, tableFields, needCreate, list)));
|
|
@@ -1012,7 +1085,7 @@ async function updateItem(conn, env, updatedOldNewList, key, type, array, constr
|
|
|
1012
1085
|
const allList = Promise.all(listPromises).then(v => v.flat());
|
|
1013
1086
|
promises.push(array ? allList.then(async values => {
|
|
1014
1087
|
for (const [value] of updatedOldNewList) {
|
|
1015
|
-
let data = values.filter(createFilter(value,
|
|
1088
|
+
let data = values.filter(createFilter(value, commonFilterKeys));
|
|
1016
1089
|
if (noField) {
|
|
1017
1090
|
data = data.sort(({
|
|
1018
1091
|
[noField]: a
|
|
@@ -1024,7 +1097,7 @@ async function updateItem(conn, env, updatedOldNewList, key, type, array, constr
|
|
|
1024
1097
|
}
|
|
1025
1098
|
}) : allList.then(async values => {
|
|
1026
1099
|
for (const [value] of updatedOldNewList) {
|
|
1027
|
-
value[key] = values.find(createFilter(value,
|
|
1100
|
+
value[key] = values.find(createFilter(value, commonFilterKeys)) || null;
|
|
1028
1101
|
}
|
|
1029
1102
|
}));
|
|
1030
1103
|
await Promise.all(promises);
|
|
@@ -1077,19 +1150,21 @@ async function updateData (conn, model, data, newData, env) {
|
|
|
1077
1150
|
const savedData = data;
|
|
1078
1151
|
const has = keys.filter(v => v in fields && (typeof fields[v]?.type === 'string' || fields[v]?.type && !fields[v]?.type.table)).length;
|
|
1079
1152
|
/** @type {WhereValue[]} */
|
|
1080
|
-
const
|
|
1153
|
+
const fieldWhere = getPrimaryKeys(fields).map(k => ({
|
|
1081
1154
|
field: k,
|
|
1082
1155
|
operator: '=',
|
|
1083
1156
|
value: savedData[k]
|
|
1084
1157
|
}));
|
|
1085
1158
|
const [columns, fieldColumns, tableFields] = toColumns(fields);
|
|
1159
|
+
const where = where2column(fieldWhere, fieldColumns);
|
|
1160
|
+
where.push(...getFixedValueWhere(columns));
|
|
1086
1161
|
let list = [];
|
|
1087
1162
|
if (!has) {
|
|
1088
1163
|
list = [data];
|
|
1089
1164
|
} else if (table && typeof table === 'object') {
|
|
1090
|
-
list = toFieldList(await table.updateReturn(env, conn, Object.keys(columns), columns, await getSetValue$1(columns, fieldColumns, newData),
|
|
1165
|
+
list = toFieldList(await table.updateReturn(env, conn, Object.keys(columns), columns, await getSetValue$1(columns, fieldColumns, newData), where), fieldColumns);
|
|
1091
1166
|
} else {
|
|
1092
|
-
list = toFieldList(await conn.updateReturn(env, Object.keys(columns), table || '', columns, await getSetValue$1(columns, fieldColumns, newData),
|
|
1167
|
+
list = toFieldList(await conn.updateReturn(env, Object.keys(columns), table || '', columns, await getSetValue$1(columns, fieldColumns, newData), where), fieldColumns);
|
|
1093
1168
|
}
|
|
1094
1169
|
const oldList = [data];
|
|
1095
1170
|
const newList = [newData];
|
|
@@ -1153,18 +1228,20 @@ async function pseudoDestroy (conn, tableDef, data, update, env) {
|
|
|
1153
1228
|
fields
|
|
1154
1229
|
} = tableDef;
|
|
1155
1230
|
/** @type {WhereValue[]} */
|
|
1156
|
-
const
|
|
1231
|
+
const fieldWhere = getPrimaryKeys(fields).map(k => ({
|
|
1157
1232
|
field: k,
|
|
1158
1233
|
operator: '=',
|
|
1159
1234
|
value: data[k]
|
|
1160
1235
|
}));
|
|
1161
|
-
const [columns, fieldColumns] = toColumns(fields);
|
|
1162
1236
|
const pseudo = tableDef.pseudo || '';
|
|
1163
|
-
|
|
1237
|
+
fieldWhere.push({
|
|
1164
1238
|
field: pseudo,
|
|
1165
1239
|
operator: '=',
|
|
1166
1240
|
value: 0
|
|
1167
1241
|
});
|
|
1242
|
+
const [columns, fieldColumns] = toColumns(fields);
|
|
1243
|
+
const where = where2column(fieldWhere, fieldColumns);
|
|
1244
|
+
where.push(...getFixedValueWhere(columns));
|
|
1168
1245
|
const value = {
|
|
1169
1246
|
...update
|
|
1170
1247
|
};
|
|
@@ -1172,7 +1249,7 @@ async function pseudoDestroy (conn, tableDef, data, update, env) {
|
|
|
1172
1249
|
value[pseudo] = pseudoValue;
|
|
1173
1250
|
const set = set2column(value, fieldColumns);
|
|
1174
1251
|
await setDeleteTimestamp(columns, set);
|
|
1175
|
-
const dataUpdated = table && typeof table === 'object' ? await table.updateReturn(env, conn, Object.keys(columns), columns, set,
|
|
1252
|
+
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
1253
|
return toFieldList(dataUpdated, fieldColumns)[0] || null;
|
|
1177
1254
|
}
|
|
1178
1255
|
|
|
@@ -1196,6 +1273,7 @@ async function completelyDelete (conn, tableDef, data, env) {
|
|
|
1196
1273
|
operator: '=',
|
|
1197
1274
|
value: data[k]
|
|
1198
1275
|
})), fieldColumns);
|
|
1276
|
+
where.push(...getFixedValueWhere(columns));
|
|
1199
1277
|
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
1278
|
const baseFieldSet = new Set(Object.keys(fieldColumns || columns));
|
|
1201
1279
|
const [deleted] = await deleteSub(conn, list, tableFields, baseFieldSet, env, true);
|
|
@@ -1308,8 +1386,7 @@ async function findSub(conn, list, fields, baseFieldSet, env) {
|
|
|
1308
1386
|
if (!mapList) {
|
|
1309
1387
|
continue;
|
|
1310
1388
|
}
|
|
1311
|
-
const
|
|
1312
|
-
const where = where2column([subList2where(list, mapList), ...valueEntries2where(value)], fieldColumns);
|
|
1389
|
+
const where = [subList2where(list, mapList, fieldColumns), ...getValueWhere(constraints, columns, fieldColumns)];
|
|
1313
1390
|
const select = select2column(columns, fieldColumns);
|
|
1314
1391
|
const {
|
|
1315
1392
|
table
|
|
@@ -1351,21 +1428,23 @@ async function find(conn, p, env) {
|
|
|
1351
1428
|
fields
|
|
1352
1429
|
} = p;
|
|
1353
1430
|
// @ts-ignore
|
|
1431
|
+
const options = getOptions(p);
|
|
1354
1432
|
const {
|
|
1355
|
-
where,
|
|
1356
1433
|
sort,
|
|
1357
1434
|
offset,
|
|
1358
1435
|
limit
|
|
1359
|
-
} =
|
|
1436
|
+
} = options;
|
|
1360
1437
|
const [columns, fieldColumns, tableFields] = toColumns(fields);
|
|
1438
|
+
const where = where2column(options.where, fieldColumns);
|
|
1439
|
+
where.push(...getFixedValueWhere(columns));
|
|
1361
1440
|
const list = table && typeof table === 'object' ? await table.find(env, conn, columns, {
|
|
1362
|
-
where
|
|
1441
|
+
where,
|
|
1363
1442
|
offset,
|
|
1364
1443
|
limit,
|
|
1365
1444
|
sort: sort?.length ? sort2column(sort, fieldColumns) : getDefaultSort(columns),
|
|
1366
1445
|
select: select2column(columns, fieldColumns)
|
|
1367
1446
|
}) : await conn.find(env, table || '', columns, {
|
|
1368
|
-
where
|
|
1447
|
+
where,
|
|
1369
1448
|
offset,
|
|
1370
1449
|
limit,
|
|
1371
1450
|
sort: sort?.length ? sort2column(sort, fieldColumns) : getDefaultSort(columns),
|
|
@@ -1399,18 +1478,20 @@ async function first(conn, p, env) {
|
|
|
1399
1478
|
fields
|
|
1400
1479
|
} = p;
|
|
1401
1480
|
// @ts-ignore
|
|
1481
|
+
const options = getOptions(p);
|
|
1402
1482
|
const {
|
|
1403
|
-
where,
|
|
1404
1483
|
sort
|
|
1405
|
-
} =
|
|
1484
|
+
} = options;
|
|
1406
1485
|
const [columns, fieldColumns, tableFields] = toColumns(fields);
|
|
1486
|
+
const where = where2column(options.where, fieldColumns);
|
|
1487
|
+
where.push(...getFixedValueWhere(columns));
|
|
1407
1488
|
const list = table && typeof table === 'object' ? await table.find(env, conn, columns, {
|
|
1408
|
-
where
|
|
1489
|
+
where,
|
|
1409
1490
|
limit: 1,
|
|
1410
1491
|
sort: sort?.length ? sort2column(sort, fieldColumns) : getDefaultSort(columns),
|
|
1411
1492
|
select: select2column(columns, fieldColumns)
|
|
1412
1493
|
}) : await conn.find(env, table || '', columns, {
|
|
1413
|
-
where
|
|
1494
|
+
where,
|
|
1414
1495
|
limit: 1,
|
|
1415
1496
|
sort: sort?.length ? sort2column(sort, fieldColumns) : getDefaultSort(columns),
|
|
1416
1497
|
select: select2column(columns, fieldColumns)
|
|
@@ -1438,10 +1519,10 @@ async function pseudoDeleteMany(conn, queryable, update, env) {
|
|
|
1438
1519
|
table,
|
|
1439
1520
|
fields
|
|
1440
1521
|
} = queryable;
|
|
1441
|
-
const
|
|
1442
|
-
where
|
|
1443
|
-
} = getOptions(queryable);
|
|
1522
|
+
const options = getOptions(queryable);
|
|
1444
1523
|
const [columns, fieldColumns] = toColumns(fields);
|
|
1524
|
+
const where = where2column(options.where, fieldColumns);
|
|
1525
|
+
where.push(...getFixedValueWhere(columns));
|
|
1445
1526
|
const value = typeof update !== 'object' ? {} : {
|
|
1446
1527
|
...update
|
|
1447
1528
|
};
|
|
@@ -1450,7 +1531,7 @@ async function pseudoDeleteMany(conn, queryable, update, env) {
|
|
|
1450
1531
|
value[pseudo] = pseudoValue;
|
|
1451
1532
|
const set = set2column(value, fieldColumns);
|
|
1452
1533
|
await setDeleteTimestamp(columns, set);
|
|
1453
|
-
const list = table && typeof table === 'object' ? await table.updateReturn(env, conn, Object.keys(columns), columns, set,
|
|
1534
|
+
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
1535
|
return list.length;
|
|
1455
1536
|
}
|
|
1456
1537
|
|
|
@@ -1928,7 +2009,9 @@ class Connection {
|
|
|
1928
2009
|
}, update, where, skip) {
|
|
1929
2010
|
const [columns, fieldColumns, tableFields] = toColumns(fields);
|
|
1930
2011
|
const conn = await this.#getConnection();
|
|
1931
|
-
|
|
2012
|
+
const whereColumns = where2column(where, fieldColumns);
|
|
2013
|
+
whereColumns.push(...getFixedValueWhere(columns));
|
|
2014
|
+
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
2015
|
}
|
|
1933
2016
|
/**
|
|
1934
2017
|
* @template {Fields<MainFieldType>} T
|
|
@@ -1945,7 +2028,9 @@ class Connection {
|
|
|
1945
2028
|
}, update, returning, where, skip) {
|
|
1946
2029
|
const [columns, fieldColumns, tableFields] = toColumns(fields);
|
|
1947
2030
|
const conn = await this.#getConnection();
|
|
1948
|
-
const
|
|
2031
|
+
const whereColumns = where2column(where, fieldColumns);
|
|
2032
|
+
whereColumns.push(...getFixedValueWhere(columns));
|
|
2033
|
+
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
2034
|
return toFieldList(result, fieldColumns);
|
|
1950
2035
|
}
|
|
1951
2036
|
/**
|
|
@@ -1998,7 +2083,9 @@ class Connection {
|
|
|
1998
2083
|
}, where) {
|
|
1999
2084
|
const [columns, fieldColumns, tableFields] = toColumns(fields);
|
|
2000
2085
|
const conn = await this.#getConnection();
|
|
2001
|
-
|
|
2086
|
+
const whereColumns = where2column(where, fieldColumns);
|
|
2087
|
+
whereColumns.push(...getFixedValueWhere(columns));
|
|
2088
|
+
return table && typeof table === 'object' ? table.delete(this.#env, conn, columns, whereColumns) : conn.delete(this.#env, table || '', columns, whereColumns);
|
|
2002
2089
|
}
|
|
2003
2090
|
/**
|
|
2004
2091
|
* @template {Fields<MainFieldType>} T
|
|
@@ -2013,7 +2100,9 @@ class Connection {
|
|
|
2013
2100
|
}, returning, where) {
|
|
2014
2101
|
const [columns, fieldColumns, tableFields] = toColumns(fields);
|
|
2015
2102
|
const conn = await this.#getConnection();
|
|
2016
|
-
const
|
|
2103
|
+
const whereColumns = where2column(where, fieldColumns);
|
|
2104
|
+
whereColumns.push(...getFixedValueWhere(columns));
|
|
2105
|
+
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
2106
|
return toFieldList(result, fieldColumns);
|
|
2018
2107
|
}
|
|
2019
2108
|
/**
|
|
@@ -2028,6 +2117,7 @@ class Connection {
|
|
|
2028
2117
|
} = queryable;
|
|
2029
2118
|
const options = getOptions(queryable);
|
|
2030
2119
|
const where = where2column(options.where, fieldColumns);
|
|
2120
|
+
where.push(...getFixedValueWhere(columns));
|
|
2031
2121
|
const conn = await this.#getConnection();
|
|
2032
2122
|
return table && typeof table === 'object' ? table.count(this.#env, conn, columns, where) : conn.count(this.#env, table || '', columns, where);
|
|
2033
2123
|
}
|
|
@@ -2046,6 +2136,7 @@ class Connection {
|
|
|
2046
2136
|
limit
|
|
2047
2137
|
} = options;
|
|
2048
2138
|
const where = where2column(options.where, fieldColumns);
|
|
2139
|
+
where.push(...getFixedValueWhere(columns));
|
|
2049
2140
|
const conn = await this.#getConnection();
|
|
2050
2141
|
const select = select2column(columns, fieldColumns, options.select);
|
|
2051
2142
|
const {
|
|
@@ -5078,7 +5169,7 @@ defGetter('indexes', function () {
|
|
|
5078
5169
|
/**
|
|
5079
5170
|
* @typedef {object} Scene.Define
|
|
5080
5171
|
* @property {string} table
|
|
5081
|
-
* @property {
|
|
5172
|
+
* @property {{column: string, field?: string, fixedValue?: any, [v: string]: any}[]} fields
|
|
5082
5173
|
*/
|
|
5083
5174
|
/** @import {FieldDefine, Fields, TableDefine,} from './types/table' */
|
|
5084
5175
|
class Scene {
|
|
@@ -5103,24 +5194,37 @@ class Scene {
|
|
|
5103
5194
|
const allFields = new Map(Object.entries(sceneFields));
|
|
5104
5195
|
/** @type {Record<string, FieldDefine>} */
|
|
5105
5196
|
const fields = {};
|
|
5106
|
-
for (const
|
|
5107
|
-
|
|
5108
|
-
|
|
5109
|
-
|
|
5110
|
-
|
|
5111
|
-
|
|
5112
|
-
|
|
5113
|
-
|
|
5114
|
-
|
|
5115
|
-
|
|
5116
|
-
|
|
5117
|
-
|
|
5118
|
-
|
|
5119
|
-
|
|
5120
|
-
|
|
5197
|
+
for (const {
|
|
5198
|
+
column,
|
|
5199
|
+
field,
|
|
5200
|
+
fixedValue,
|
|
5201
|
+
...p
|
|
5202
|
+
} of table.fields) {
|
|
5203
|
+
let define = field && allFields.get(field) || null;
|
|
5204
|
+
if (!define?.type || typeof define.type !== 'string' && define.type?.table) {
|
|
5205
|
+
fields[`__${column}`] = {
|
|
5206
|
+
...p,
|
|
5207
|
+
fixedValue,
|
|
5208
|
+
type: p.type,
|
|
5209
|
+
array: p.array,
|
|
5210
|
+
nullable: p.nullable,
|
|
5211
|
+
primary: p.primary,
|
|
5212
|
+
size: p.size,
|
|
5213
|
+
scale: p.scale,
|
|
5121
5214
|
column
|
|
5122
5215
|
};
|
|
5216
|
+
continue;
|
|
5217
|
+
}
|
|
5218
|
+
if (field) {
|
|
5219
|
+
allFields.delete(field);
|
|
5123
5220
|
}
|
|
5221
|
+
fields[field || `__${column}`] = {
|
|
5222
|
+
...p,
|
|
5223
|
+
...define,
|
|
5224
|
+
nullable: p.nullable,
|
|
5225
|
+
primary: p.primary,
|
|
5226
|
+
column
|
|
5227
|
+
};
|
|
5124
5228
|
}
|
|
5125
5229
|
for (const [name, define] of allFields) {
|
|
5126
5230
|
if (!define.type) {
|
|
@@ -5132,7 +5236,8 @@ class Scene {
|
|
|
5132
5236
|
if (typeof define.type === 'string') {
|
|
5133
5237
|
throw new Error();
|
|
5134
5238
|
}
|
|
5135
|
-
if (typeof define.type === '
|
|
5239
|
+
if (define.type.table && typeof define.type.table === 'string') {
|
|
5240
|
+
// @ts-ignore
|
|
5136
5241
|
fields[name] = {
|
|
5137
5242
|
...define,
|
|
5138
5243
|
type: this.#getModel(define.type)
|
package/migrate.d.mts
CHANGED
package/migrate.mjs
CHANGED