imodel 0.2.0 → 0.3.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 +81 -50
- package/index.mjs +284 -222
- package/migrate.d.mts +2 -39
- package/migrate.mjs +9 -57
- package/package.json +1 -1
package/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* imodel v0.
|
|
2
|
+
* imodel v0.3.1
|
|
3
3
|
* (c) 2019-2025 undefined
|
|
4
4
|
* @license undefined
|
|
5
5
|
*/
|
|
@@ -88,7 +88,7 @@ function toBaseFields(fields) {
|
|
|
88
88
|
allFields[k] = {
|
|
89
89
|
...v,
|
|
90
90
|
type: 'object',
|
|
91
|
-
primary:
|
|
91
|
+
primary: 0
|
|
92
92
|
};
|
|
93
93
|
}
|
|
94
94
|
}
|
|
@@ -199,9 +199,6 @@ async function getSetNewValue(fields, update, skip) {
|
|
|
199
199
|
return;
|
|
200
200
|
}
|
|
201
201
|
}
|
|
202
|
-
if (field.serial && field.type === 'id') {
|
|
203
|
-
return;
|
|
204
|
-
}
|
|
205
202
|
return true;
|
|
206
203
|
}));
|
|
207
204
|
}
|
|
@@ -313,7 +310,7 @@ function separateFields(fields) {
|
|
|
313
310
|
base[k] = {
|
|
314
311
|
...v,
|
|
315
312
|
type: 'object',
|
|
316
|
-
primary:
|
|
313
|
+
primary: 0
|
|
317
314
|
};
|
|
318
315
|
} else {
|
|
319
316
|
// @ts-ignore
|
|
@@ -374,14 +371,13 @@ function defaultMap(value, others) {
|
|
|
374
371
|
/**
|
|
375
372
|
*
|
|
376
373
|
* @param {IConnection} conn
|
|
377
|
-
* @param {GetName} getName
|
|
378
374
|
* @param {Environment} env
|
|
379
375
|
* @param {[string, FieldDefine<TableDefine>][]} fields
|
|
380
376
|
* @param {Record<string, any>[]} newList
|
|
381
377
|
* @param {Record<string, any>[]} list
|
|
382
378
|
* @returns {Promise<Record<string, any>[]>}
|
|
383
379
|
*/
|
|
384
|
-
async function createSub(conn,
|
|
380
|
+
async function createSub(conn, env, fields, newList, list) {
|
|
385
381
|
// TODO: 可创建字段过滤
|
|
386
382
|
if (!list.length) {
|
|
387
383
|
return list;
|
|
@@ -452,13 +448,13 @@ async function createSub(conn, getName, env, fields, newList, list) {
|
|
|
452
448
|
continue;
|
|
453
449
|
}
|
|
454
450
|
const [baseFields, tableFields] = separateFields(fields);
|
|
455
|
-
const insertKeys = Object.entries(baseFields).
|
|
451
|
+
const insertKeys = Object.entries(baseFields).map(v => v[0]);
|
|
456
452
|
const insertValues = await Promise.all(fieldValues.map(d => getInsertValue(d, fields)));
|
|
457
453
|
const {
|
|
458
454
|
table
|
|
459
455
|
} = type;
|
|
460
|
-
const result = table && typeof table === 'object' ? table.insert(env, conn, '', baseFields, insertValues, insertKeys) : conn.insert(env,
|
|
461
|
-
promises.push(result.then(r => createSub(conn,
|
|
456
|
+
const result = table && typeof table === 'object' ? table.insert(env, conn, '', baseFields, insertValues, insertKeys) : conn.insert(env, table || '', baseFields, insertValues, insertKeys);
|
|
457
|
+
promises.push(result.then(r => createSub(conn, env, tableFields, fieldValues, r)));
|
|
462
458
|
promises.push(array ? result.then(values => {
|
|
463
459
|
for (const [value] of allList) {
|
|
464
460
|
value[key] = values.filter(createFilter(value, commonKeys));
|
|
@@ -475,14 +471,13 @@ async function createSub(conn, getName, env, fields, newList, list) {
|
|
|
475
471
|
/**
|
|
476
472
|
*
|
|
477
473
|
* @param {IConnection} conn
|
|
478
|
-
* @param {GetName} getName
|
|
479
474
|
* @param {Environment} env
|
|
480
475
|
* @param {TableDefine} model
|
|
481
476
|
* @param {any} p
|
|
482
477
|
* @param {(a,b) => object} mapDo
|
|
483
478
|
* @returns {Promise<any>}
|
|
484
479
|
*/
|
|
485
|
-
async function create(conn,
|
|
480
|
+
async function create(conn, env, model, p, mapDo = defaultMap) {
|
|
486
481
|
const {
|
|
487
482
|
table,
|
|
488
483
|
fields
|
|
@@ -510,10 +505,10 @@ async function create(conn, getName, env, model, p, mapDo = defaultMap) {
|
|
|
510
505
|
insertKeys = Object.entries(insertValues[0]).filter(([k, v]) => v !== undefined && k in baseFields).map(([k]) => k);
|
|
511
506
|
}
|
|
512
507
|
if (!insertKeys.length) {
|
|
513
|
-
insertKeys = Object.entries(baseFields).
|
|
508
|
+
insertKeys = Object.entries(baseFields).map(([k]) => k);
|
|
514
509
|
}
|
|
515
|
-
const list = table && typeof table === 'object' ? await table.insert(env, conn, '', baseFields, insertValues, insertKeys) : await conn.insert(env,
|
|
516
|
-
await createSub(conn,
|
|
510
|
+
const list = table && typeof table === 'object' ? await table.insert(env, conn, '', baseFields, insertValues, insertKeys) : await conn.insert(env, table || '', baseFields, insertValues, insertKeys);
|
|
511
|
+
await createSub(conn, env, tableFields, records, list);
|
|
517
512
|
const value = list.map((v, i) => mapDo(v, getOtherValues(records[i], fields)));
|
|
518
513
|
return isArray ? value : value[0];
|
|
519
514
|
}
|
|
@@ -530,10 +525,6 @@ function getPrimaryKeys(fields) {
|
|
|
530
525
|
if (primaryKeys.length) {
|
|
531
526
|
return primaryKeys.map(([k]) => k);
|
|
532
527
|
}
|
|
533
|
-
const idKeys = list.filter(([v, o]) => o.serial && o.type === 'id');
|
|
534
|
-
if (idKeys.length) {
|
|
535
|
-
return idKeys.map(([k]) => k);
|
|
536
|
-
}
|
|
537
528
|
return [list[0][0]];
|
|
538
529
|
}
|
|
539
530
|
|
|
@@ -633,7 +624,6 @@ function valueEntries2where(value) {
|
|
|
633
624
|
/**
|
|
634
625
|
*
|
|
635
626
|
* @param {IConnection} conn
|
|
636
|
-
* @param {GetName} getName
|
|
637
627
|
* @param {any[]} list
|
|
638
628
|
* @param {[string, FieldDefine<TableDefine>][]} fields
|
|
639
629
|
* @param {Fields<keyof FieldType>} baseFields
|
|
@@ -641,7 +631,7 @@ function valueEntries2where(value) {
|
|
|
641
631
|
* @param {boolean} [hasResult]
|
|
642
632
|
* @returns
|
|
643
633
|
*/
|
|
644
|
-
async function deleteSub(conn,
|
|
634
|
+
async function deleteSub(conn, list, fields, baseFields, env, hasResult) {
|
|
645
635
|
if (!list.length) {
|
|
646
636
|
return list;
|
|
647
637
|
}
|
|
@@ -668,7 +658,7 @@ async function deleteSub(conn, getName, list, fields, baseFields, env, hasResult
|
|
|
668
658
|
} = type;
|
|
669
659
|
/** @type {MatchArg} */
|
|
670
660
|
const argv = {
|
|
671
|
-
table: typeof table === 'string' ?
|
|
661
|
+
table: typeof table === 'string' ? table : '',
|
|
672
662
|
fields: subfields,
|
|
673
663
|
where
|
|
674
664
|
};
|
|
@@ -678,7 +668,7 @@ async function deleteSub(conn, getName, list, fields, baseFields, env, hasResult
|
|
|
678
668
|
continue;
|
|
679
669
|
}
|
|
680
670
|
const result = table && typeof table === 'object' ? table.deleteReturn(env, conn, argv, Object.keys(subfields)) : conn.deleteReturn(env, argv, Object.keys(subfields));
|
|
681
|
-
promises.push(result.then(l => deleteSub(conn,
|
|
671
|
+
promises.push(result.then(l => deleteSub(conn, l, tableFields, subfields, env, hasResult)));
|
|
682
672
|
constraintEntries.find(([k, v]) => v.noField)?.[0];
|
|
683
673
|
promises.push(array ? result.then(subList => {
|
|
684
674
|
for (const item of list) {
|
|
@@ -697,12 +687,11 @@ async function deleteSub(conn, getName, list, fields, baseFields, env, hasResult
|
|
|
697
687
|
// TODO: 嵌套
|
|
698
688
|
/**
|
|
699
689
|
* @param {IConnection} conn
|
|
700
|
-
* @param {GetName} getName
|
|
701
690
|
* @param {Queryable} queryable
|
|
702
691
|
* @param {Environment} env
|
|
703
692
|
* @returns {Promise<number>}
|
|
704
693
|
*/
|
|
705
|
-
async function deleteMany(conn,
|
|
694
|
+
async function deleteMany(conn, queryable, env) {
|
|
706
695
|
const {
|
|
707
696
|
table,
|
|
708
697
|
fields
|
|
@@ -717,7 +706,7 @@ async function deleteMany(conn, getName, queryable, env) {
|
|
|
717
706
|
fields: baseFields,
|
|
718
707
|
where
|
|
719
708
|
}) : conn.delete(env, {
|
|
720
|
-
table:
|
|
709
|
+
table: table || '',
|
|
721
710
|
fields: baseFields,
|
|
722
711
|
where
|
|
723
712
|
});
|
|
@@ -727,14 +716,14 @@ async function deleteMany(conn, getName, queryable, env) {
|
|
|
727
716
|
fields: baseFields,
|
|
728
717
|
where
|
|
729
718
|
}, Object.keys(baseFields)) : await conn.deleteReturn(env, {
|
|
730
|
-
table:
|
|
719
|
+
table: table || '',
|
|
731
720
|
fields: baseFields,
|
|
732
721
|
where
|
|
733
722
|
}, Object.keys(baseFields));
|
|
734
723
|
if (!list) {
|
|
735
724
|
return 0;
|
|
736
725
|
}
|
|
737
|
-
await deleteSub(conn,
|
|
726
|
+
await deleteSub(conn, list, tableFields, baseFields, env);
|
|
738
727
|
return list.length;
|
|
739
728
|
}
|
|
740
729
|
|
|
@@ -771,7 +760,6 @@ function classify(newList, oldList, keys, notFound, found) {
|
|
|
771
760
|
/**
|
|
772
761
|
*
|
|
773
762
|
* @param {IConnection} conn
|
|
774
|
-
* @param {GetName} getName
|
|
775
763
|
* @param {Environment} env
|
|
776
764
|
* @param {[Record<string, any>, Record<string, any>, Record<string, any>][]} updatedOldNewList
|
|
777
765
|
* @param {string} key
|
|
@@ -780,7 +768,7 @@ function classify(newList, oldList, keys, notFound, found) {
|
|
|
780
768
|
* @param {Record<string, Constraint>?} [constraints]
|
|
781
769
|
* @returns
|
|
782
770
|
*/
|
|
783
|
-
async function updateItem(conn,
|
|
771
|
+
async function updateItem(conn, env, updatedOldNewList, key, type, array, constraints) {
|
|
784
772
|
/** @type {any[]} */
|
|
785
773
|
const promises = [];
|
|
786
774
|
const constraintEntries = Object.entries(constraints || {});
|
|
@@ -880,8 +868,9 @@ async function updateItem(conn, getName, env, updatedOldNewList, key, type, arra
|
|
|
880
868
|
classify(newList, needDelete, aKeys, unmatched, eq);
|
|
881
869
|
const pKeys = primaryKeys.filter(v => !(v in commonKeys));
|
|
882
870
|
classify(unmatched, needDelete, pKeys, needCreate, needUpdate);
|
|
883
|
-
const
|
|
884
|
-
|
|
871
|
+
const {
|
|
872
|
+
table
|
|
873
|
+
} = type;
|
|
885
874
|
if (needDelete.length) {
|
|
886
875
|
// 批量删除
|
|
887
876
|
/** @type {WhereValue[]} */
|
|
@@ -904,16 +893,16 @@ async function updateItem(conn, getName, env, updatedOldNewList, key, type, arra
|
|
|
904
893
|
value: needDelete.map(v => pKeys.map(k => v[k] ?? null))
|
|
905
894
|
});
|
|
906
895
|
}
|
|
907
|
-
const promise = typeof table === '
|
|
908
|
-
table,
|
|
896
|
+
const promise = typeof table === 'object' ? table.deleteReturn(env, conn, {
|
|
897
|
+
table: '',
|
|
909
898
|
fields: baseFields,
|
|
910
899
|
where
|
|
911
|
-
}, Object.keys(baseFields)) :
|
|
912
|
-
table: '',
|
|
900
|
+
}, Object.keys(baseFields)) : conn.deleteReturn(env, {
|
|
901
|
+
table: table || '',
|
|
913
902
|
fields: baseFields,
|
|
914
903
|
where
|
|
915
904
|
}, Object.keys(baseFields));
|
|
916
|
-
promises.push(promise.then(list => deleteSub(conn,
|
|
905
|
+
promises.push(promise.then(list => deleteSub(conn, list, tableFields, baseFields, env)));
|
|
917
906
|
}
|
|
918
907
|
/** @type {typeof updatedOldNewList} */
|
|
919
908
|
const list = eq.map(([n, o]) => [{
|
|
@@ -945,20 +934,20 @@ async function updateItem(conn, getName, env, updatedOldNewList, key, type, arra
|
|
|
945
934
|
value[key] = old[key];
|
|
946
935
|
}
|
|
947
936
|
}
|
|
948
|
-
const list = typeof table === '
|
|
937
|
+
const list = typeof table === 'object' ? table.updateManyReturn(env, conn, '', baseFields, update, pKeys, setKeys, needUpdate.map(v => v[0]), where, Object.keys(baseFields)) : conn.updateManyReturn(env, table || '', baseFields, update, pKeys, setKeys, needUpdate.map(v => v[0]), where, Object.keys(baseFields));
|
|
949
938
|
listPromises.push(list);
|
|
950
939
|
subPromises.push(list.then(u => u.map((u, i) => [u, ...needUpdate[i]])));
|
|
951
940
|
}
|
|
952
941
|
// 批量新建
|
|
953
942
|
if (needCreate.length) {
|
|
954
943
|
const key = Object.entries(baseFields);
|
|
955
|
-
const list = typeof table === '
|
|
944
|
+
const list = typeof table === 'object' ? table.insert(env, conn, '', baseFields, await Promise.all(needCreate.map(d => getInsertValue(d, baseFields))), key.map(v => v[0])) : conn.insert(env, table || '', baseFields, await Promise.all(needCreate.map(d => getInsertValue(d, baseFields))), key.map(v => v[0]));
|
|
956
945
|
listPromises.push(list);
|
|
957
|
-
promises.push(list.then(list => createSub(conn,
|
|
946
|
+
promises.push(list.then(list => createSub(conn, env, tableFields, needCreate, list)));
|
|
958
947
|
}
|
|
959
948
|
promises.push(Promise.all(subPromises).then(
|
|
960
949
|
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
961
|
-
v => updateSub(conn,
|
|
950
|
+
v => updateSub(conn, tableFields, v.flat(), env)));
|
|
962
951
|
const allList = Promise.all(listPromises).then(v => v.flat());
|
|
963
952
|
promises.push(array ? allList.then(async values => {
|
|
964
953
|
for (const [value] of updatedOldNewList) {
|
|
@@ -982,13 +971,12 @@ async function updateItem(conn, getName, env, updatedOldNewList, key, type, arra
|
|
|
982
971
|
/**
|
|
983
972
|
*
|
|
984
973
|
* @param {IConnection} conn
|
|
985
|
-
* @param {GetName} getName
|
|
986
974
|
* @param {[string, FieldDefine<TableDefine>][]} fields
|
|
987
975
|
* @param {[Record<string, any>, Record<string, any>, Record<string, any>][]} list
|
|
988
976
|
* @param {Environment} env
|
|
989
977
|
* @returns
|
|
990
978
|
*/
|
|
991
|
-
async function updateSub(conn,
|
|
979
|
+
async function updateSub(conn, fields, list, env) {
|
|
992
980
|
if (!list.length) {
|
|
993
981
|
return;
|
|
994
982
|
}
|
|
@@ -1003,21 +991,20 @@ async function updateSub(conn, getName, fields, list, env) {
|
|
|
1003
991
|
if (immutable) {
|
|
1004
992
|
continue;
|
|
1005
993
|
}
|
|
1006
|
-
promises.push(updateItem(conn,
|
|
994
|
+
promises.push(updateItem(conn, env, list, key, type, array, constraints));
|
|
1007
995
|
}
|
|
1008
996
|
await Promise.all(promises);
|
|
1009
997
|
}
|
|
1010
998
|
/**
|
|
1011
999
|
*
|
|
1012
1000
|
* @param {IConnection} conn
|
|
1013
|
-
* @param {GetName} getName
|
|
1014
1001
|
* @param {TableDefine} model
|
|
1015
1002
|
* @param {Record<string, any>} data
|
|
1016
1003
|
* @param {Record<string, any>} newData
|
|
1017
1004
|
* @param {Environment} env
|
|
1018
1005
|
* @returns
|
|
1019
1006
|
*/
|
|
1020
|
-
async function updateData (conn,
|
|
1007
|
+
async function updateData (conn, model, data, newData, env) {
|
|
1021
1008
|
const keys = Object.keys(newData);
|
|
1022
1009
|
if (!keys.length) {
|
|
1023
1010
|
return false;
|
|
@@ -1046,7 +1033,7 @@ async function updateData (conn, getName, model, data, newData, env) {
|
|
|
1046
1033
|
}, await getSetNewValue(fields, newData), Object.keys(baseFields));
|
|
1047
1034
|
} else {
|
|
1048
1035
|
list = await conn.updateReturn(env, {
|
|
1049
|
-
table:
|
|
1036
|
+
table: table || '',
|
|
1050
1037
|
fields: baseFields,
|
|
1051
1038
|
where
|
|
1052
1039
|
}, await getSetNewValue(fields, newData), Object.keys(baseFields));
|
|
@@ -1055,7 +1042,7 @@ async function updateData (conn, getName, model, data, newData, env) {
|
|
|
1055
1042
|
const newList = [newData];
|
|
1056
1043
|
/** @type {[Record<string, any>, Record<string, any>, Record<string, any>][]} */
|
|
1057
1044
|
const updatedOldNewList = list.map((v, k) => [v, oldList[k], newList[k]]);
|
|
1058
|
-
await updateSub(conn,
|
|
1045
|
+
await updateSub(conn, tableFields, updatedOldNewList, env);
|
|
1059
1046
|
return list[0];
|
|
1060
1047
|
}
|
|
1061
1048
|
|
|
@@ -1086,14 +1073,13 @@ async function setDeleteTimestamp(fields, update) {
|
|
|
1086
1073
|
/**
|
|
1087
1074
|
*
|
|
1088
1075
|
* @param {IConnection} conn
|
|
1089
|
-
* @param {GetName} getName
|
|
1090
1076
|
* @param {TableDefine} tableDef
|
|
1091
1077
|
* @param {any} data
|
|
1092
1078
|
* @param {Record<string, SetValue>} update
|
|
1093
1079
|
* @param {Environment} env
|
|
1094
1080
|
* @returns
|
|
1095
1081
|
*/
|
|
1096
|
-
async function pseudoDestroy (conn,
|
|
1082
|
+
async function pseudoDestroy (conn, tableDef, data, update, env) {
|
|
1097
1083
|
const {
|
|
1098
1084
|
table,
|
|
1099
1085
|
fields
|
|
@@ -1122,7 +1108,7 @@ async function pseudoDestroy (conn, getName, tableDef, data, update, env) {
|
|
|
1122
1108
|
fields: baseFields,
|
|
1123
1109
|
where
|
|
1124
1110
|
}, value, Object.keys(baseFields)) : await conn.updateReturn(env, {
|
|
1125
|
-
table:
|
|
1111
|
+
table: table || '',
|
|
1126
1112
|
fields: baseFields,
|
|
1127
1113
|
where
|
|
1128
1114
|
}, value, Object.keys(baseFields));
|
|
@@ -1137,13 +1123,12 @@ async function pseudoDestroy (conn, getName, tableDef, data, update, env) {
|
|
|
1137
1123
|
/**
|
|
1138
1124
|
*
|
|
1139
1125
|
* @param {IConnection} conn
|
|
1140
|
-
* @param {GetName} getName
|
|
1141
1126
|
* @param {TableDefine} tableDef
|
|
1142
1127
|
* @param {any} data
|
|
1143
1128
|
* @param {Environment} env
|
|
1144
1129
|
* @returns
|
|
1145
1130
|
*/
|
|
1146
|
-
async function completelyDelete (conn,
|
|
1131
|
+
async function completelyDelete (conn, tableDef, data, env) {
|
|
1147
1132
|
const {
|
|
1148
1133
|
table,
|
|
1149
1134
|
fields
|
|
@@ -1160,11 +1145,11 @@ async function completelyDelete (conn, getName, tableDef, data, env) {
|
|
|
1160
1145
|
fields: baseFields,
|
|
1161
1146
|
where
|
|
1162
1147
|
}, Object.keys(baseFields)) : await conn.deleteReturn(env, {
|
|
1163
|
-
table:
|
|
1148
|
+
table: table || '',
|
|
1164
1149
|
fields: baseFields,
|
|
1165
1150
|
where
|
|
1166
1151
|
}, Object.keys(baseFields));
|
|
1167
|
-
const [deleted] = await deleteSub(conn,
|
|
1152
|
+
const [deleted] = await deleteSub(conn, list, tableFields, baseFields, env, true);
|
|
1168
1153
|
return deleted || null;
|
|
1169
1154
|
}
|
|
1170
1155
|
|
|
@@ -1209,14 +1194,13 @@ function getDefaultSort(fields, noField) {
|
|
|
1209
1194
|
/**
|
|
1210
1195
|
*
|
|
1211
1196
|
* @param {IConnection} conn
|
|
1212
|
-
* @param {GetName} getName
|
|
1213
1197
|
* @param {any[]} list
|
|
1214
1198
|
* @param {[string, FieldDefine<TableDefine>][]} fields
|
|
1215
1199
|
* @param {Fields<keyof FieldType>} baseFields
|
|
1216
1200
|
* @param {Environment} env
|
|
1217
1201
|
* @returns {Promise<any[]>}
|
|
1218
1202
|
*/
|
|
1219
|
-
async function findSub(conn,
|
|
1203
|
+
async function findSub(conn, list, fields, baseFields, env) {
|
|
1220
1204
|
if (!list.length) {
|
|
1221
1205
|
return list;
|
|
1222
1206
|
}
|
|
@@ -1250,13 +1234,13 @@ async function findSub(conn, getName, list, fields, baseFields, env) {
|
|
|
1250
1234
|
sort: getDefaultSort(subfields, noField),
|
|
1251
1235
|
select: toSelect(subfields)
|
|
1252
1236
|
}) : conn.select(env, {
|
|
1253
|
-
table:
|
|
1237
|
+
table: table || '',
|
|
1254
1238
|
fields: subfields,
|
|
1255
1239
|
where,
|
|
1256
1240
|
sort: getDefaultSort(subfields, noField),
|
|
1257
1241
|
select: toSelect(subfields)
|
|
1258
1242
|
});
|
|
1259
|
-
promises.push(result.then(list => findSub(conn,
|
|
1243
|
+
promises.push(result.then(list => findSub(conn, list, tableFields, subfields, env)));
|
|
1260
1244
|
promises.push(array ? result.then(subList => {
|
|
1261
1245
|
for (const item of list) {
|
|
1262
1246
|
item[key] = subList.filter(createFilter(item, mapList));
|
|
@@ -1273,12 +1257,11 @@ async function findSub(conn, getName, list, fields, baseFields, env) {
|
|
|
1273
1257
|
*
|
|
1274
1258
|
* @template {TableDefine} T
|
|
1275
1259
|
* @param {IConnection} conn
|
|
1276
|
-
* @param {GetName} getName
|
|
1277
1260
|
* @param {T} p
|
|
1278
1261
|
* @param {Environment} env
|
|
1279
1262
|
* @returns {Promise<any>}
|
|
1280
1263
|
*/
|
|
1281
|
-
async function find(conn,
|
|
1264
|
+
async function find(conn, p, env) {
|
|
1282
1265
|
const {
|
|
1283
1266
|
table,
|
|
1284
1267
|
fields
|
|
@@ -1302,7 +1285,7 @@ async function find(conn, getName, p, env) {
|
|
|
1302
1285
|
sort: sort?.length ? sort : getDefaultSort(baseFields),
|
|
1303
1286
|
select: toSelect(baseFields)
|
|
1304
1287
|
}) : await conn.select(env, {
|
|
1305
|
-
table:
|
|
1288
|
+
table: table || '',
|
|
1306
1289
|
fields: baseFields,
|
|
1307
1290
|
alias,
|
|
1308
1291
|
where,
|
|
@@ -1314,7 +1297,7 @@ async function find(conn, getName, p, env) {
|
|
|
1314
1297
|
if (!list.length) {
|
|
1315
1298
|
return [];
|
|
1316
1299
|
}
|
|
1317
|
-
await findSub(conn,
|
|
1300
|
+
await findSub(conn, list, tableFields, baseFields, env);
|
|
1318
1301
|
if (typeof p[Build] !== 'function') {
|
|
1319
1302
|
return list;
|
|
1320
1303
|
}
|
|
@@ -1328,12 +1311,11 @@ async function find(conn, getName, p, env) {
|
|
|
1328
1311
|
*
|
|
1329
1312
|
* @template {TableDefine} T
|
|
1330
1313
|
* @param {IConnection} conn
|
|
1331
|
-
* @param {GetName} getName
|
|
1332
1314
|
* @param {T} p
|
|
1333
1315
|
* @param {Environment} env
|
|
1334
1316
|
* @returns {Promise<any>}
|
|
1335
1317
|
*/
|
|
1336
|
-
async function first(conn,
|
|
1318
|
+
async function first(conn, p, env) {
|
|
1337
1319
|
const {
|
|
1338
1320
|
table,
|
|
1339
1321
|
fields
|
|
@@ -1354,7 +1336,7 @@ async function first(conn, getName, p, env) {
|
|
|
1354
1336
|
sort: sort?.length ? sort : getDefaultSort(baseFields),
|
|
1355
1337
|
select: toSelect(baseFields)
|
|
1356
1338
|
}) : await conn.select(env, {
|
|
1357
|
-
table:
|
|
1339
|
+
table: table || '',
|
|
1358
1340
|
fields: baseFields,
|
|
1359
1341
|
alias,
|
|
1360
1342
|
where,
|
|
@@ -1362,7 +1344,7 @@ async function first(conn, getName, p, env) {
|
|
|
1362
1344
|
sort: sort?.length ? sort : getDefaultSort(baseFields),
|
|
1363
1345
|
select: toSelect(baseFields)
|
|
1364
1346
|
});
|
|
1365
|
-
const [item] = await findSub(conn,
|
|
1347
|
+
const [item] = await findSub(conn, list, tableFields, baseFields, env);
|
|
1366
1348
|
if (!item) {
|
|
1367
1349
|
return null;
|
|
1368
1350
|
}
|
|
@@ -1375,12 +1357,11 @@ async function first(conn, getName, p, env) {
|
|
|
1375
1357
|
/** @import { Environment, GetName, IConnection, Queryable } from '../types' */
|
|
1376
1358
|
/**
|
|
1377
1359
|
* @param {IConnection} conn
|
|
1378
|
-
* @param {GetName} getName
|
|
1379
1360
|
* @param {Queryable} queryable
|
|
1380
1361
|
* @param {Record<string, any> | boolean | undefined} update
|
|
1381
1362
|
* @param {Environment} env
|
|
1382
1363
|
*/
|
|
1383
|
-
async function pseudoDeleteMany(conn,
|
|
1364
|
+
async function pseudoDeleteMany(conn, queryable, update, env) {
|
|
1384
1365
|
const {
|
|
1385
1366
|
table,
|
|
1386
1367
|
fields
|
|
@@ -1401,22 +1382,123 @@ async function pseudoDeleteMany(conn, getName, queryable, update, env) {
|
|
|
1401
1382
|
fields: baseFields,
|
|
1402
1383
|
where
|
|
1403
1384
|
}, value, Object.keys(baseFields)) : await conn.updateReturn(env, {
|
|
1404
|
-
table:
|
|
1385
|
+
table: table || '',
|
|
1405
1386
|
fields: baseFields,
|
|
1406
1387
|
where
|
|
1407
1388
|
}, value, Object.keys(baseFields));
|
|
1408
1389
|
return list.length;
|
|
1409
1390
|
}
|
|
1410
1391
|
|
|
1392
|
+
/** @import { TableDefine } from '../types/table' */
|
|
1393
|
+
/** @import { DBIndex, DBTable } from '../types/connection' */
|
|
1394
|
+
/**
|
|
1395
|
+
*
|
|
1396
|
+
* @param {(TableDefine | DBTable)[]} tables
|
|
1397
|
+
*/
|
|
1398
|
+
function table2db(tables) {
|
|
1399
|
+
/** @type {DBTable[]} */
|
|
1400
|
+
const allTables = [];
|
|
1401
|
+
/** @type {Map<string, DBTable & {indexes: DBIndex[]}>} */
|
|
1402
|
+
const map = new Map();
|
|
1403
|
+
const saved = new Set();
|
|
1404
|
+
/** @type {(TableDefine | DBTable)[]} */
|
|
1405
|
+
const list = [...tables];
|
|
1406
|
+
for (let define = list.pop(); define; define = list.pop()) {
|
|
1407
|
+
if (saved.has(define)) {
|
|
1408
|
+
continue;
|
|
1409
|
+
}
|
|
1410
|
+
saved.add(define);
|
|
1411
|
+
const {
|
|
1412
|
+
table,
|
|
1413
|
+
fields,
|
|
1414
|
+
indexes
|
|
1415
|
+
} = define;
|
|
1416
|
+
if (table && typeof table === 'string') {
|
|
1417
|
+
let t = map.get(table);
|
|
1418
|
+
if (!t) {
|
|
1419
|
+
t = {
|
|
1420
|
+
table,
|
|
1421
|
+
fields: Object.create(null),
|
|
1422
|
+
indexes: []
|
|
1423
|
+
};
|
|
1424
|
+
map.set(table, t);
|
|
1425
|
+
allTables.push(t);
|
|
1426
|
+
}
|
|
1427
|
+
const dbFields = t.fields;
|
|
1428
|
+
for (const [name, field] of Object.entries(fields)) {
|
|
1429
|
+
const {
|
|
1430
|
+
type,
|
|
1431
|
+
size,
|
|
1432
|
+
scale,
|
|
1433
|
+
default: value,
|
|
1434
|
+
nullable,
|
|
1435
|
+
array,
|
|
1436
|
+
primary
|
|
1437
|
+
} = field;
|
|
1438
|
+
if (!type) {
|
|
1439
|
+
continue;
|
|
1440
|
+
}
|
|
1441
|
+
const old = dbFields[name];
|
|
1442
|
+
if (typeof type === 'string') {
|
|
1443
|
+
if (!old) {
|
|
1444
|
+
t.fields[name] = {
|
|
1445
|
+
type,
|
|
1446
|
+
size,
|
|
1447
|
+
scale,
|
|
1448
|
+
default: value,
|
|
1449
|
+
nullable,
|
|
1450
|
+
array,
|
|
1451
|
+
primary
|
|
1452
|
+
};
|
|
1453
|
+
} else {
|
|
1454
|
+
t.fields[name] = {
|
|
1455
|
+
type,
|
|
1456
|
+
size: size || old.size,
|
|
1457
|
+
scale: scale || old.scale,
|
|
1458
|
+
default: value ?? old.default,
|
|
1459
|
+
nullable: nullable ?? old.nullable,
|
|
1460
|
+
array: array ?? old.array,
|
|
1461
|
+
primary: primary || old.primary
|
|
1462
|
+
};
|
|
1463
|
+
}
|
|
1464
|
+
continue;
|
|
1465
|
+
}
|
|
1466
|
+
if (typeof type !== 'object') {
|
|
1467
|
+
continue;
|
|
1468
|
+
}
|
|
1469
|
+
if (type.table) {
|
|
1470
|
+
continue;
|
|
1471
|
+
}
|
|
1472
|
+
if (!old) {
|
|
1473
|
+
t.fields[name] = {
|
|
1474
|
+
type: 'object',
|
|
1475
|
+
nullable,
|
|
1476
|
+
array
|
|
1477
|
+
};
|
|
1478
|
+
} else {
|
|
1479
|
+
old.type = 'object';
|
|
1480
|
+
old.array = array ?? old.array;
|
|
1481
|
+
old.nullable = nullable ?? old.nullable;
|
|
1482
|
+
}
|
|
1483
|
+
}
|
|
1484
|
+
for (const index of indexes || []) {
|
|
1485
|
+
t.indexes.push(index);
|
|
1486
|
+
}
|
|
1487
|
+
}
|
|
1488
|
+
for (const {
|
|
1489
|
+
type
|
|
1490
|
+
} of Object.values(fields)) {
|
|
1491
|
+
if (!type || typeof type !== 'object') {
|
|
1492
|
+
continue;
|
|
1493
|
+
}
|
|
1494
|
+
list.push(type);
|
|
1495
|
+
}
|
|
1496
|
+
}
|
|
1497
|
+
return allTables;
|
|
1498
|
+
}
|
|
1499
|
+
|
|
1411
1500
|
/* eslint-disable max-lines */
|
|
1412
1501
|
/* eslint-disable max-len */
|
|
1413
|
-
/** @import { WhereValue } from '../Where.mjs' */
|
|
1414
|
-
/** @import { SetValue } from '../set.mjs' */
|
|
1415
|
-
/** @import { Environment, IConnection } from '../types' */
|
|
1416
|
-
/** @import { Fields, FieldType, FieldValue, IndexOptions, MainFieldType, TableDefine } from '../types/table' */
|
|
1417
|
-
/** @import { Queryable } from '../types/options' */
|
|
1418
|
-
/** @import { Column, ColumnOptions } from '../types/column' */
|
|
1419
|
-
/** @import { GetName, Skip, TableType } from '../types/connection' */
|
|
1420
1502
|
/**
|
|
1421
1503
|
*
|
|
1422
1504
|
* @param {Fields<keyof FieldType>} fields
|
|
@@ -1451,8 +1533,6 @@ function signal2promise(signal) {
|
|
|
1451
1533
|
});
|
|
1452
1534
|
});
|
|
1453
1535
|
}
|
|
1454
|
-
/** @type {GetName} */
|
|
1455
|
-
const defGetName = s => s || '';
|
|
1456
1536
|
/**
|
|
1457
1537
|
* @template {{}} [E={}]
|
|
1458
1538
|
*/
|
|
@@ -1463,52 +1543,26 @@ class Connection {
|
|
|
1463
1543
|
#getConnection;
|
|
1464
1544
|
/** @type {E?} */
|
|
1465
1545
|
#baseTransaction;
|
|
1466
|
-
/** @type {Record<string, string>?} */
|
|
1467
|
-
#names;
|
|
1468
|
-
#getName = defGetName;
|
|
1469
1546
|
/**
|
|
1470
1547
|
*
|
|
1471
1548
|
* @param {PromiseLike<IConnection<E>> | IConnection<E>} connection
|
|
1472
1549
|
* @param {E?} [transaction]
|
|
1473
|
-
* @param {Record<string, string>?} [names]
|
|
1474
1550
|
*/
|
|
1475
|
-
constructor(connection, transaction = null
|
|
1551
|
+
constructor(connection, transaction = null) {
|
|
1476
1552
|
this.#baseTransaction = transaction;
|
|
1477
1553
|
this.#getConnection = async () => connection;
|
|
1478
|
-
let getName = defGetName;
|
|
1479
|
-
if (names) {
|
|
1480
|
-
this.#names = names;
|
|
1481
|
-
const allNames = Object.assign(Object.create(null), names);
|
|
1482
|
-
getName = n => allNames[n || ''] || n;
|
|
1483
|
-
} else {
|
|
1484
|
-
this.#names = null;
|
|
1485
|
-
}
|
|
1486
|
-
this.#getName = getName;
|
|
1487
1554
|
this.#env = {
|
|
1488
1555
|
isDevelopment,
|
|
1489
1556
|
values,
|
|
1490
|
-
transaction
|
|
1491
|
-
getName,
|
|
1492
|
-
names: names && {
|
|
1493
|
-
...names
|
|
1494
|
-
}
|
|
1557
|
+
transaction
|
|
1495
1558
|
};
|
|
1496
1559
|
}
|
|
1497
1560
|
/**
|
|
1498
1561
|
*
|
|
1499
|
-
* @param {object} [options]
|
|
1500
|
-
* @param {Record<string, string>?} [options.names]
|
|
1501
1562
|
* @returns {Connection<E>}
|
|
1502
1563
|
*/
|
|
1503
|
-
clone({
|
|
1504
|
-
|
|
1505
|
-
} = {}) {
|
|
1506
|
-
// eslint-disable-next-line no-nested-ternary
|
|
1507
|
-
const newNames = this.#names ? names ? {
|
|
1508
|
-
...this.#names,
|
|
1509
|
-
...names
|
|
1510
|
-
} : this.#names : names;
|
|
1511
|
-
return new Connection(this.#getConnection(), this.#baseTransaction, newNames);
|
|
1564
|
+
clone() {
|
|
1565
|
+
return new Connection(this.#getConnection(), this.#baseTransaction);
|
|
1512
1566
|
}
|
|
1513
1567
|
/** @return {Promise<string>} */
|
|
1514
1568
|
async dbType() {
|
|
@@ -1545,7 +1599,7 @@ class Connection {
|
|
|
1545
1599
|
*/
|
|
1546
1600
|
async type(table) {
|
|
1547
1601
|
const conn = await this.#getConnection();
|
|
1548
|
-
return conn.type(this.#env,
|
|
1602
|
+
return conn.type(this.#env, table);
|
|
1549
1603
|
}
|
|
1550
1604
|
/**
|
|
1551
1605
|
* @template T
|
|
@@ -1615,13 +1669,13 @@ class Connection {
|
|
|
1615
1669
|
const modelBuild = model[Build];
|
|
1616
1670
|
const md = typeof modelBuild === 'function' ? modelBuild.bind(model) : undefined;
|
|
1617
1671
|
const conn = await this.#getConnection();
|
|
1618
|
-
return create(conn, this.#
|
|
1672
|
+
return create(conn, this.#env, model, p, md);
|
|
1619
1673
|
}
|
|
1620
1674
|
// @ts-ignore
|
|
1621
1675
|
const m = /** @type {Create} */model;
|
|
1622
1676
|
return m[Create](this, p, async data => {
|
|
1623
1677
|
const conn = await this.#getConnection();
|
|
1624
|
-
return create(conn, this.#
|
|
1678
|
+
return create(conn, this.#env, model, data);
|
|
1625
1679
|
}, model);
|
|
1626
1680
|
}
|
|
1627
1681
|
/**
|
|
@@ -1632,7 +1686,7 @@ class Connection {
|
|
|
1632
1686
|
*/
|
|
1633
1687
|
async find(sql) {
|
|
1634
1688
|
const conn = await this.#getConnection();
|
|
1635
|
-
return find(conn,
|
|
1689
|
+
return find(conn, sql, this.#env);
|
|
1636
1690
|
}
|
|
1637
1691
|
/**
|
|
1638
1692
|
*
|
|
@@ -1642,7 +1696,7 @@ class Connection {
|
|
|
1642
1696
|
*/
|
|
1643
1697
|
async first(sql) {
|
|
1644
1698
|
const conn = await this.#getConnection();
|
|
1645
|
-
return first(conn,
|
|
1699
|
+
return first(conn, sql, this.#env);
|
|
1646
1700
|
}
|
|
1647
1701
|
/**
|
|
1648
1702
|
* @template T
|
|
@@ -1657,18 +1711,18 @@ class Connection {
|
|
|
1657
1711
|
if (newData) {
|
|
1658
1712
|
/** @type {any} */
|
|
1659
1713
|
const doc = data;
|
|
1660
|
-
return updateData(conn,
|
|
1714
|
+
return updateData(conn, model, doc, newData, this.#env);
|
|
1661
1715
|
}
|
|
1662
|
-
return create(conn, this.#
|
|
1716
|
+
return create(conn, this.#env, model, data);
|
|
1663
1717
|
}
|
|
1664
1718
|
const r = /** @type {Save}*/data;
|
|
1665
1719
|
// @ts-ignore
|
|
1666
1720
|
return r[Save](this, async (doc, newData) => {
|
|
1667
1721
|
const conn = await this.#getConnection();
|
|
1668
1722
|
if (newData) {
|
|
1669
|
-
return updateData(conn,
|
|
1723
|
+
return updateData(conn, model, doc, newData, this.#env);
|
|
1670
1724
|
}
|
|
1671
|
-
return create(conn, this.#
|
|
1725
|
+
return create(conn, this.#env, model, doc);
|
|
1672
1726
|
}, newData || null, model);
|
|
1673
1727
|
}
|
|
1674
1728
|
/**
|
|
@@ -1681,13 +1735,13 @@ class Connection {
|
|
|
1681
1735
|
async completelyDestroy(tableDef, data) {
|
|
1682
1736
|
if (typeof data[Destroy] !== 'function') {
|
|
1683
1737
|
const conn = await this.#getConnection();
|
|
1684
|
-
return completelyDelete(conn,
|
|
1738
|
+
return completelyDelete(conn, tableDef, data, this.#env);
|
|
1685
1739
|
}
|
|
1686
1740
|
const r = /** @type {Destroy} */data;
|
|
1687
1741
|
// @ts-ignore
|
|
1688
1742
|
return r[Destroy](this, async data => {
|
|
1689
1743
|
const conn = await this.#getConnection();
|
|
1690
|
-
return completelyDelete(conn,
|
|
1744
|
+
return completelyDelete(conn, tableDef, data, this.#env);
|
|
1691
1745
|
}, tableDef);
|
|
1692
1746
|
}
|
|
1693
1747
|
/**
|
|
@@ -1707,13 +1761,13 @@ class Connection {
|
|
|
1707
1761
|
};
|
|
1708
1762
|
if (typeof data[PseudoDestroy] !== 'function') {
|
|
1709
1763
|
const conn = await this.#getConnection();
|
|
1710
|
-
return pseudoDestroy(conn,
|
|
1764
|
+
return pseudoDestroy(conn, tableDef, data, update, this.#env);
|
|
1711
1765
|
}
|
|
1712
1766
|
const r = /** @type {PseudoDestroy} */data;
|
|
1713
1767
|
// @ts-ignore
|
|
1714
1768
|
return r[PseudoDestroy](this, update, async data => {
|
|
1715
1769
|
const conn = await this.#getConnection();
|
|
1716
|
-
return pseudoDestroy(conn,
|
|
1770
|
+
return pseudoDestroy(conn, tableDef, data, update, this.#env);
|
|
1717
1771
|
}, tableDef);
|
|
1718
1772
|
}
|
|
1719
1773
|
/**
|
|
@@ -1762,9 +1816,9 @@ class Connection {
|
|
|
1762
1816
|
async destroyMany(queryable, update) {
|
|
1763
1817
|
const conn = await this.#getConnection();
|
|
1764
1818
|
if (update === false || !isPseudo(queryable)) {
|
|
1765
|
-
return deleteMany(conn,
|
|
1819
|
+
return deleteMany(conn, queryable, this.#env);
|
|
1766
1820
|
}
|
|
1767
|
-
return pseudoDeleteMany(conn,
|
|
1821
|
+
return pseudoDeleteMany(conn, queryable, update, this.#env);
|
|
1768
1822
|
}
|
|
1769
1823
|
/**
|
|
1770
1824
|
* @template {object | object[]} T
|
|
@@ -1788,10 +1842,10 @@ class Connection {
|
|
|
1788
1842
|
insertKeys = Object.entries(insertValues[0]).filter(([k, v]) => v !== undefined && k in baseFields).map(([k]) => k);
|
|
1789
1843
|
}
|
|
1790
1844
|
if (!insertKeys.length) {
|
|
1791
|
-
insertKeys = Object.entries(baseFields).
|
|
1845
|
+
insertKeys = Object.entries(baseFields).map(v => v[0]);
|
|
1792
1846
|
}
|
|
1793
1847
|
const conn = await this.#getConnection();
|
|
1794
|
-
const p = table && typeof table === 'object' ? await table.insert(this.#env, conn, '', baseFields, insertValues, insertKeys) : await conn.insert(this.#env,
|
|
1848
|
+
const p = table && typeof table === 'object' ? await table.insert(this.#env, conn, '', baseFields, insertValues, insertKeys) : await conn.insert(this.#env, table || '', baseFields, insertValues, insertKeys);
|
|
1795
1849
|
return isArray ? (/** @type {*}*/p) : p[0];
|
|
1796
1850
|
}
|
|
1797
1851
|
/**
|
|
@@ -1811,7 +1865,7 @@ class Connection {
|
|
|
1811
1865
|
fields: toBaseFields(fields),
|
|
1812
1866
|
where: where || undefined
|
|
1813
1867
|
}, await getSetNewValue(fields, update, skip)) : conn.update(this.#env, {
|
|
1814
|
-
table:
|
|
1868
|
+
table: table || '',
|
|
1815
1869
|
fields: toBaseFields(fields),
|
|
1816
1870
|
where: where || undefined
|
|
1817
1871
|
}, await getSetNewValue(fields, update, skip));
|
|
@@ -1836,7 +1890,7 @@ class Connection {
|
|
|
1836
1890
|
fields: baseFields,
|
|
1837
1891
|
where: where || undefined
|
|
1838
1892
|
}, await getSetNewValue(fields, update, skip), returning?.length ? returning : Object.keys(baseFields)) : conn.updateReturn(this.#env, {
|
|
1839
|
-
table:
|
|
1893
|
+
table: table || '',
|
|
1840
1894
|
fields: baseFields,
|
|
1841
1895
|
where: where || undefined
|
|
1842
1896
|
}, await getSetNewValue(fields, update, skip), returning?.length ? returning : Object.keys(baseFields));
|
|
@@ -1857,7 +1911,7 @@ class Connection {
|
|
|
1857
1911
|
const update = await getSetNewValue(fields, null, skip);
|
|
1858
1912
|
const [whereFields, setFields] = getKeys(baseFields, update, pKeys, setKeys);
|
|
1859
1913
|
const conn = await this.#getConnection();
|
|
1860
|
-
return table && typeof table === 'object' ? table.updateMany(this.#env, conn, '', baseFields, update, whereFields, setFields, list, []) : conn.updateMany(this.#env,
|
|
1914
|
+
return table && typeof table === 'object' ? table.updateMany(this.#env, conn, '', baseFields, update, whereFields, setFields, list, []) : conn.updateMany(this.#env, table || '', baseFields, update, whereFields, setFields, list, []);
|
|
1861
1915
|
}
|
|
1862
1916
|
/**
|
|
1863
1917
|
* @template {Fields<MainFieldType>} T
|
|
@@ -1877,7 +1931,7 @@ class Connection {
|
|
|
1877
1931
|
const update = getSetNewValue(fields, null, skip);
|
|
1878
1932
|
const [whereFields, setFields] = getKeys(baseFields, update, pKeys, setKeys);
|
|
1879
1933
|
const conn = await this.#getConnection();
|
|
1880
|
-
return table && typeof table === 'object' ? table.updateManyReturn(this.#env, conn, '', baseFields, update, whereFields, setFields, list, [], returning?.length ? returning : Object.keys(baseFields)) : conn.updateManyReturn(this.#env,
|
|
1934
|
+
return table && typeof table === 'object' ? table.updateManyReturn(this.#env, conn, '', baseFields, update, whereFields, setFields, list, [], returning?.length ? returning : Object.keys(baseFields)) : conn.updateManyReturn(this.#env, table || '', baseFields, update, whereFields, setFields, list, [], returning?.length ? returning : Object.keys(baseFields));
|
|
1881
1935
|
}
|
|
1882
1936
|
/**
|
|
1883
1937
|
* @param {TableDefine} tableDefine
|
|
@@ -1894,7 +1948,7 @@ class Connection {
|
|
|
1894
1948
|
fields: toBaseFields(fields),
|
|
1895
1949
|
where
|
|
1896
1950
|
}) : conn.delete(this.#env, {
|
|
1897
|
-
table:
|
|
1951
|
+
table: table || '',
|
|
1898
1952
|
fields: toBaseFields(fields),
|
|
1899
1953
|
where
|
|
1900
1954
|
});
|
|
@@ -1917,7 +1971,7 @@ class Connection {
|
|
|
1917
1971
|
fields: baseFields,
|
|
1918
1972
|
where
|
|
1919
1973
|
}, returning?.length ? returning : Object.keys(baseFields)) : conn.deleteReturn(this.#env, {
|
|
1920
|
-
table:
|
|
1974
|
+
table: table || '',
|
|
1921
1975
|
fields: baseFields,
|
|
1922
1976
|
where
|
|
1923
1977
|
}, returning?.length ? returning : Object.keys(baseFields));
|
|
@@ -1940,7 +1994,7 @@ class Connection {
|
|
|
1940
1994
|
fields: toBaseFields(fields)
|
|
1941
1995
|
}) : conn.count(this.#env, {
|
|
1942
1996
|
...options,
|
|
1943
|
-
table:
|
|
1997
|
+
table: table || '',
|
|
1944
1998
|
fields: toBaseFields(fields)
|
|
1945
1999
|
});
|
|
1946
2000
|
}
|
|
@@ -1967,7 +2021,7 @@ class Connection {
|
|
|
1967
2021
|
sort: sort?.length ? sort : getDefaultSort(fields)
|
|
1968
2022
|
}) : conn.select(this.#env, {
|
|
1969
2023
|
...options,
|
|
1970
|
-
table:
|
|
2024
|
+
table: table || '',
|
|
1971
2025
|
fields,
|
|
1972
2026
|
select,
|
|
1973
2027
|
sort: sort?.length ? sort : getDefaultSort(fields)
|
|
@@ -1982,7 +2036,7 @@ class Connection {
|
|
|
1982
2036
|
*/
|
|
1983
2037
|
async addColumn(table, column, type, options) {
|
|
1984
2038
|
const conn = await this.#getConnection();
|
|
1985
|
-
return conn.addColumn(this.#env,
|
|
2039
|
+
return conn.addColumn(this.#env, table, column, type, setDefaultOptions(options));
|
|
1986
2040
|
}
|
|
1987
2041
|
/**
|
|
1988
2042
|
* @param {string} table
|
|
@@ -1993,7 +2047,7 @@ class Connection {
|
|
|
1993
2047
|
*/
|
|
1994
2048
|
async changeColumn(table, column, type, options) {
|
|
1995
2049
|
const conn = await this.#getConnection();
|
|
1996
|
-
return conn.changeColumn(this.#env,
|
|
2050
|
+
return conn.changeColumn(this.#env, table, column, type, setDefaultOptions(options));
|
|
1997
2051
|
}
|
|
1998
2052
|
/**
|
|
1999
2053
|
* @param {string} table
|
|
@@ -2003,7 +2057,7 @@ class Connection {
|
|
|
2003
2057
|
*/
|
|
2004
2058
|
async changeColumnNull(table, column, nullable) {
|
|
2005
2059
|
const conn = await this.#getConnection();
|
|
2006
|
-
return conn.changeColumnNull(this.#env,
|
|
2060
|
+
return conn.changeColumnNull(this.#env, table, column, nullable);
|
|
2007
2061
|
}
|
|
2008
2062
|
/**
|
|
2009
2063
|
* @param {string} table
|
|
@@ -2012,7 +2066,7 @@ class Connection {
|
|
|
2012
2066
|
*/
|
|
2013
2067
|
async dropColumn(table, column) {
|
|
2014
2068
|
const conn = await this.#getConnection();
|
|
2015
|
-
return conn.dropColumn(this.#env,
|
|
2069
|
+
return conn.dropColumn(this.#env, table, column);
|
|
2016
2070
|
}
|
|
2017
2071
|
/**
|
|
2018
2072
|
* @param {string} table
|
|
@@ -2022,7 +2076,7 @@ class Connection {
|
|
|
2022
2076
|
*/
|
|
2023
2077
|
async renameColumn(table, column, newName) {
|
|
2024
2078
|
const conn = await this.#getConnection();
|
|
2025
|
-
return conn.renameColumn(this.#env,
|
|
2079
|
+
return conn.renameColumn(this.#env, table, column, newName);
|
|
2026
2080
|
}
|
|
2027
2081
|
/**
|
|
2028
2082
|
* @param {string} table
|
|
@@ -2033,7 +2087,7 @@ class Connection {
|
|
|
2033
2087
|
*/
|
|
2034
2088
|
async addIndex(table, name, fields, options = {}) {
|
|
2035
2089
|
const conn = await this.#getConnection();
|
|
2036
|
-
return conn.addIndex(this.#env,
|
|
2090
|
+
return conn.addIndex(this.#env, table, name, fields, options);
|
|
2037
2091
|
}
|
|
2038
2092
|
/**
|
|
2039
2093
|
* @param {string} table
|
|
@@ -2042,7 +2096,7 @@ class Connection {
|
|
|
2042
2096
|
*/
|
|
2043
2097
|
async dropIndex(table, name) {
|
|
2044
2098
|
const conn = await this.#getConnection();
|
|
2045
|
-
return conn.dropIndex(this.#env,
|
|
2099
|
+
return conn.dropIndex(this.#env, table, name);
|
|
2046
2100
|
}
|
|
2047
2101
|
/**
|
|
2048
2102
|
* @param {string} table
|
|
@@ -2051,7 +2105,7 @@ class Connection {
|
|
|
2051
2105
|
*/
|
|
2052
2106
|
async createTable(table, fields) {
|
|
2053
2107
|
const conn = await this.#getConnection();
|
|
2054
|
-
return conn.createTable(this.#env,
|
|
2108
|
+
return conn.createTable(this.#env, table, fields.map(setDefaultOptions));
|
|
2055
2109
|
}
|
|
2056
2110
|
/**
|
|
2057
2111
|
* @param {string} table
|
|
@@ -2059,7 +2113,7 @@ class Connection {
|
|
|
2059
2113
|
*/
|
|
2060
2114
|
async dropTable(table) {
|
|
2061
2115
|
const conn = await this.#getConnection();
|
|
2062
|
-
return conn.dropTable(this.#env,
|
|
2116
|
+
return conn.dropTable(this.#env, table);
|
|
2063
2117
|
}
|
|
2064
2118
|
/**
|
|
2065
2119
|
* @param {string} table
|
|
@@ -2068,7 +2122,7 @@ class Connection {
|
|
|
2068
2122
|
*/
|
|
2069
2123
|
async renameTable(table, newName) {
|
|
2070
2124
|
const conn = await this.#getConnection();
|
|
2071
|
-
return conn.renameTable(this.#env,
|
|
2125
|
+
return conn.renameTable(this.#env, table, newName);
|
|
2072
2126
|
}
|
|
2073
2127
|
/**
|
|
2074
2128
|
* @param {string} view
|
|
@@ -2083,7 +2137,7 @@ class Connection {
|
|
|
2083
2137
|
const fields = toBaseFields(queryable.fields);
|
|
2084
2138
|
const options = getOptions(queryable);
|
|
2085
2139
|
const select = toSelect(fields, options.select);
|
|
2086
|
-
return conn.createView(this.#env,
|
|
2140
|
+
return conn.createView(this.#env, view, {
|
|
2087
2141
|
...options,
|
|
2088
2142
|
table: typeof table === 'string' ? table : '',
|
|
2089
2143
|
fields,
|
|
@@ -2096,7 +2150,7 @@ class Connection {
|
|
|
2096
2150
|
*/
|
|
2097
2151
|
async dropView(view) {
|
|
2098
2152
|
const conn = await this.#getConnection();
|
|
2099
|
-
return conn.dropView(this.#env,
|
|
2153
|
+
return conn.dropView(this.#env, view);
|
|
2100
2154
|
}
|
|
2101
2155
|
/**
|
|
2102
2156
|
* @param {string} view
|
|
@@ -2111,7 +2165,7 @@ class Connection {
|
|
|
2111
2165
|
const fields = toBaseFields(queryable.fields);
|
|
2112
2166
|
const options = getOptions(queryable);
|
|
2113
2167
|
const select = toSelect(fields, options.select);
|
|
2114
|
-
return conn.createMaterializedView(this.#env,
|
|
2168
|
+
return conn.createMaterializedView(this.#env, view, {
|
|
2115
2169
|
...options,
|
|
2116
2170
|
table: typeof table === 'string' ? table : '',
|
|
2117
2171
|
fields,
|
|
@@ -2124,7 +2178,7 @@ class Connection {
|
|
|
2124
2178
|
*/
|
|
2125
2179
|
async dropMaterializedView(view) {
|
|
2126
2180
|
const conn = await this.#getConnection();
|
|
2127
|
-
return conn.dropMaterializedView(this.#env,
|
|
2181
|
+
return conn.dropMaterializedView(this.#env, view);
|
|
2128
2182
|
}
|
|
2129
2183
|
/** @type {AbortController[]} */
|
|
2130
2184
|
#layers = [];
|
|
@@ -2194,6 +2248,38 @@ class Connection {
|
|
|
2194
2248
|
}
|
|
2195
2249
|
return false;
|
|
2196
2250
|
}
|
|
2251
|
+
/**
|
|
2252
|
+
* @param {string} table
|
|
2253
|
+
* @returns {Promise<DBTable?>}
|
|
2254
|
+
*/
|
|
2255
|
+
async loadTable(table) {
|
|
2256
|
+
const conn = await this.#getConnection();
|
|
2257
|
+
return conn.loadTables(this.#env, [table]).then(v => v[0] || null);
|
|
2258
|
+
}
|
|
2259
|
+
/**
|
|
2260
|
+
* @param {string[]} tables
|
|
2261
|
+
* @returns {Promise<DBTable[]>}
|
|
2262
|
+
*/
|
|
2263
|
+
async loadTables(tables) {
|
|
2264
|
+
const conn = await this.#getConnection();
|
|
2265
|
+
return conn.loadTables(this.#env, tables);
|
|
2266
|
+
}
|
|
2267
|
+
/**
|
|
2268
|
+
* @param {TableDefine | DBTable} table
|
|
2269
|
+
* @returns {Promise<void>}
|
|
2270
|
+
*/
|
|
2271
|
+
async syncTable(table) {
|
|
2272
|
+
const conn = await this.#getConnection();
|
|
2273
|
+
return conn.syncTables(this.#env, table2db([table]));
|
|
2274
|
+
}
|
|
2275
|
+
/**
|
|
2276
|
+
* @param {(TableDefine | DBTable)[]} tables
|
|
2277
|
+
* @returns {Promise<void>}
|
|
2278
|
+
*/
|
|
2279
|
+
async syncTables(tables) {
|
|
2280
|
+
const conn = await this.#getConnection();
|
|
2281
|
+
return conn.syncTables(this.#env, table2db(tables));
|
|
2282
|
+
}
|
|
2197
2283
|
/**
|
|
2198
2284
|
* @template T
|
|
2199
2285
|
* @overload
|
|
@@ -2421,13 +2507,11 @@ function submodel(type, constraints, p1, p2) {
|
|
|
2421
2507
|
array: _array,
|
|
2422
2508
|
nullable,
|
|
2423
2509
|
default: def,
|
|
2424
|
-
precision,
|
|
2425
2510
|
scale,
|
|
2426
2511
|
size,
|
|
2427
2512
|
unique,
|
|
2428
2513
|
index,
|
|
2429
2514
|
primary,
|
|
2430
|
-
serial,
|
|
2431
2515
|
immutable,
|
|
2432
2516
|
text,
|
|
2433
2517
|
sort,
|
|
@@ -2657,13 +2741,11 @@ function field$1(type, p1, p2) {
|
|
|
2657
2741
|
array: _array,
|
|
2658
2742
|
nullable,
|
|
2659
2743
|
default: def,
|
|
2660
|
-
precision,
|
|
2661
2744
|
scale,
|
|
2662
2745
|
size,
|
|
2663
2746
|
unique,
|
|
2664
2747
|
index,
|
|
2665
2748
|
primary,
|
|
2666
|
-
serial,
|
|
2667
2749
|
immutable,
|
|
2668
2750
|
text,
|
|
2669
2751
|
sort,
|
|
@@ -2674,14 +2756,12 @@ function field$1(type, p1, p2) {
|
|
|
2674
2756
|
type,
|
|
2675
2757
|
array: Boolean(array),
|
|
2676
2758
|
nullable: Boolean(nullable),
|
|
2677
|
-
precision,
|
|
2678
2759
|
scale,
|
|
2679
2760
|
size,
|
|
2680
2761
|
default: def,
|
|
2681
2762
|
index,
|
|
2682
2763
|
unique: Boolean(unique),
|
|
2683
2764
|
primary,
|
|
2684
|
-
serial,
|
|
2685
2765
|
immutable,
|
|
2686
2766
|
text,
|
|
2687
2767
|
sort
|
|
@@ -2715,62 +2795,14 @@ function field$1(type, p1, p2) {
|
|
|
2715
2795
|
};
|
|
2716
2796
|
}
|
|
2717
2797
|
|
|
2718
|
-
/** @import { ToFieldType } from '../types' */
|
|
2719
|
-
/** @import { FieldDecorator } from './index.mjs' */
|
|
2720
|
-
/**
|
|
2721
|
-
* @param {object} [options]
|
|
2722
|
-
* @param {number?} [options.sort]
|
|
2723
|
-
* @returns {FieldDecorator<ToFieldType<'id'>>}
|
|
2724
|
-
*/
|
|
2725
|
-
function id({
|
|
2726
|
-
sort
|
|
2727
|
-
} = {}) {
|
|
2728
|
-
const define = {
|
|
2729
|
-
type: 'id',
|
|
2730
|
-
index: true,
|
|
2731
|
-
unique: true,
|
|
2732
|
-
primary: true,
|
|
2733
|
-
serial: true,
|
|
2734
|
-
immutable: true,
|
|
2735
|
-
sort
|
|
2736
|
-
};
|
|
2737
|
-
/**
|
|
2738
|
-
*
|
|
2739
|
-
* @param {*} _
|
|
2740
|
-
* @param {ClassAccessorDecoratorContext} ctx
|
|
2741
|
-
*/
|
|
2742
|
-
return (_, ctx) => {
|
|
2743
|
-
const name = check(ctx);
|
|
2744
|
-
const {
|
|
2745
|
-
metadata
|
|
2746
|
-
} = ctx;
|
|
2747
|
-
const fields = metadata[fieldKey];
|
|
2748
|
-
if (fields) {
|
|
2749
|
-
fields[name] = define;
|
|
2750
|
-
} else {
|
|
2751
|
-
metadata[fieldKey] = Object.assign(Object.create(null), {
|
|
2752
|
-
[name]: define
|
|
2753
|
-
});
|
|
2754
|
-
}
|
|
2755
|
-
return {
|
|
2756
|
-
get() {
|
|
2757
|
-
return this.$get(name);
|
|
2758
|
-
},
|
|
2759
|
-
set(value) {
|
|
2760
|
-
return this.$set(name, value);
|
|
2761
|
-
}
|
|
2762
|
-
};
|
|
2763
|
-
};
|
|
2764
|
-
}
|
|
2765
|
-
|
|
2766
2798
|
/** @import { FieldDecorator } from './index.mjs' */
|
|
2767
2799
|
/**
|
|
2768
2800
|
*
|
|
2769
2801
|
* @param {ClassAccessorDecoratorContext} ctx
|
|
2770
|
-
* @param {*}
|
|
2802
|
+
* @param {*} size
|
|
2771
2803
|
* @param {'timestampCreating' | 'timestampUpdating' | 'timestampDeleting'} prop
|
|
2772
2804
|
*/
|
|
2773
|
-
function defineTimestamp(ctx,
|
|
2805
|
+
function defineTimestamp(ctx, size, prop) {
|
|
2774
2806
|
const name = check(ctx);
|
|
2775
2807
|
const {
|
|
2776
2808
|
metadata
|
|
@@ -2780,7 +2812,7 @@ function defineTimestamp(ctx, precision, prop) {
|
|
|
2780
2812
|
type: 'timestamp',
|
|
2781
2813
|
array: false,
|
|
2782
2814
|
nullable: true,
|
|
2783
|
-
|
|
2815
|
+
size,
|
|
2784
2816
|
[prop]: true
|
|
2785
2817
|
};
|
|
2786
2818
|
if (fields) {
|
|
@@ -2800,25 +2832,25 @@ function defineTimestamp(ctx, precision, prop) {
|
|
|
2800
2832
|
};
|
|
2801
2833
|
}
|
|
2802
2834
|
/**
|
|
2803
|
-
* @param {number} [
|
|
2835
|
+
* @param {number} [size]
|
|
2804
2836
|
* @returns {FieldDecorator<Date?>}
|
|
2805
2837
|
*/
|
|
2806
|
-
function creating(
|
|
2807
|
-
return (_, ctx) => defineTimestamp(ctx,
|
|
2838
|
+
function creating(size) {
|
|
2839
|
+
return (_, ctx) => defineTimestamp(ctx, size, 'timestampCreating');
|
|
2808
2840
|
}
|
|
2809
2841
|
/**
|
|
2810
|
-
* @param {number} [
|
|
2842
|
+
* @param {number} [size]
|
|
2811
2843
|
* @returns {FieldDecorator<Date?>}
|
|
2812
2844
|
*/
|
|
2813
|
-
function updating(
|
|
2814
|
-
return (_, ctx) => defineTimestamp(ctx,
|
|
2845
|
+
function updating(size) {
|
|
2846
|
+
return (_, ctx) => defineTimestamp(ctx, size, 'timestampUpdating');
|
|
2815
2847
|
}
|
|
2816
2848
|
/**
|
|
2817
|
-
* @param {number} [
|
|
2849
|
+
* @param {number} [size]
|
|
2818
2850
|
* @returns {FieldDecorator<Date?>}
|
|
2819
2851
|
*/
|
|
2820
|
-
function deleting(
|
|
2821
|
-
return (_, ctx) => defineTimestamp(ctx,
|
|
2852
|
+
function deleting(size) {
|
|
2853
|
+
return (_, ctx) => defineTimestamp(ctx, size, 'timestampDeleting');
|
|
2822
2854
|
}
|
|
2823
2855
|
|
|
2824
2856
|
/**
|
|
@@ -2866,7 +2898,7 @@ function prop(prop, value) {
|
|
|
2866
2898
|
* @returns {FieldDecorator<any>}
|
|
2867
2899
|
*/
|
|
2868
2900
|
function primary(primary) {
|
|
2869
|
-
return (_, ctx) => setProp(ctx, 'primary', primary ||
|
|
2901
|
+
return (_, ctx) => setProp(ctx, 'primary', primary || 1);
|
|
2870
2902
|
}
|
|
2871
2903
|
|
|
2872
2904
|
/** @import { FieldDecorator } from './index.mjs' */
|
|
@@ -3662,7 +3694,7 @@ function divide(value) {
|
|
|
3662
3694
|
};
|
|
3663
3695
|
}
|
|
3664
3696
|
|
|
3665
|
-
/** @import { Fields, Options, Queryable, Select } from './types' */
|
|
3697
|
+
/** @import { Fields, Options, Queryable, Select, VirtualTable } from './types' */
|
|
3666
3698
|
/** @import { WhereValue, Wheres } from './Where.mjs' */
|
|
3667
3699
|
/**
|
|
3668
3700
|
*
|
|
@@ -3717,7 +3749,7 @@ function selectFn(fn, select, newSelect) {
|
|
|
3717
3749
|
* @template {Fields} T
|
|
3718
3750
|
* @template {object} [TB=object]
|
|
3719
3751
|
* @typedef {object} QueryOptions
|
|
3720
|
-
* @property {string} table
|
|
3752
|
+
* @property {string | VirtualTable} [table]
|
|
3721
3753
|
* @property {T} fields
|
|
3722
3754
|
* @property {string} [pseudo]
|
|
3723
3755
|
* @property {((a: object, b?: object | boolean) => TB)?} [build]
|
|
@@ -3745,7 +3777,7 @@ class Query {
|
|
|
3745
3777
|
pseudo
|
|
3746
3778
|
});
|
|
3747
3779
|
}
|
|
3748
|
-
/** @readonly @type {string} */
|
|
3780
|
+
/** @readonly @type {string | VirtualTable | undefined} */
|
|
3749
3781
|
|
|
3750
3782
|
/** @readonly @type {T} */
|
|
3751
3783
|
|
|
@@ -4162,6 +4194,31 @@ class Query {
|
|
|
4162
4194
|
}
|
|
4163
4195
|
}
|
|
4164
4196
|
|
|
4197
|
+
/** @import { Fields, IndexInfo, FieldDefine } from './types' */
|
|
4198
|
+
/**
|
|
4199
|
+
*
|
|
4200
|
+
* @param {Fields} fields
|
|
4201
|
+
* @returns
|
|
4202
|
+
*/
|
|
4203
|
+
function getPrimaryFields(fields) {
|
|
4204
|
+
const idFields = Object.entries(fields).filter(([, v]) => v.primary).sort(([, a], [, b]) => Number(a.primary) - Number(b.primary)).map(([v]) => v);
|
|
4205
|
+
return idFields;
|
|
4206
|
+
}
|
|
4207
|
+
/**
|
|
4208
|
+
* @param {string[]} fields
|
|
4209
|
+
* @param {Record<string, any>} data
|
|
4210
|
+
*/
|
|
4211
|
+
function toPrimary(fields, data) {
|
|
4212
|
+
return fields.map(k => data[k]).join('$');
|
|
4213
|
+
} /**
|
|
4214
|
+
* @param {{fields: Fields}} model
|
|
4215
|
+
* @param {Record<string, any> | Record<string, any>[]} documents
|
|
4216
|
+
*/
|
|
4217
|
+
function toPrimaries(model, documents) {
|
|
4218
|
+
const fields = getPrimaryFields(model.fields);
|
|
4219
|
+
return Array.isArray(documents) ? documents.map(document => toPrimary(fields, document)) : toPrimary(fields, documents);
|
|
4220
|
+
}
|
|
4221
|
+
|
|
4165
4222
|
/* eslint-disable max-lines */
|
|
4166
4223
|
/** @import { Fields, IndexInfo, FieldDefine } from './types' */
|
|
4167
4224
|
/**
|
|
@@ -4659,6 +4716,13 @@ class Model {
|
|
|
4659
4716
|
const fields = Object.getOwnPropertyNames(this.#model.fields);
|
|
4660
4717
|
return fields.findIndex(f => changed.has(f)) >= 0;
|
|
4661
4718
|
}
|
|
4719
|
+
$primary() {
|
|
4720
|
+
const fields = getPrimaryFields(this.$model.fields);
|
|
4721
|
+
return toPrimary(fields, {
|
|
4722
|
+
...this.#data,
|
|
4723
|
+
...this.#newData
|
|
4724
|
+
});
|
|
4725
|
+
}
|
|
4662
4726
|
/**
|
|
4663
4727
|
* @param {Record<string, any>} values
|
|
4664
4728
|
*/
|
|
@@ -5121,7 +5185,6 @@ function field(type, {
|
|
|
5121
5185
|
default: def,
|
|
5122
5186
|
index,
|
|
5123
5187
|
primary,
|
|
5124
|
-
serial,
|
|
5125
5188
|
immutable,
|
|
5126
5189
|
text,
|
|
5127
5190
|
sort,
|
|
@@ -5135,11 +5198,10 @@ function field(type, {
|
|
|
5135
5198
|
default: def,
|
|
5136
5199
|
index,
|
|
5137
5200
|
primary,
|
|
5138
|
-
serial,
|
|
5139
5201
|
immutable,
|
|
5140
5202
|
text,
|
|
5141
5203
|
sort
|
|
5142
5204
|
};
|
|
5143
5205
|
}
|
|
5144
5206
|
|
|
5145
|
-
export { Build, Connection, Create, Destroy, Model, PseudoDestroy, Query, Save, Submodel, Where, creating, decrement, field as define, deleted, deleting, divide, field$1 as field,
|
|
5207
|
+
export { Build, Connection, Create, Destroy, Model, PseudoDestroy, Query, Save, Submodel, Where, 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, values, withDeleted };
|