imodel 0.2.0 → 0.3.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 +74 -48
- package/index.mjs +169 -216
- 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.0
|
|
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,7 +1382,7 @@ 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));
|
|
@@ -1416,7 +1397,7 @@ async function pseudoDeleteMany(conn, getName, queryable, update, env) {
|
|
|
1416
1397
|
/** @import { Fields, FieldType, FieldValue, IndexOptions, MainFieldType, TableDefine } from '../types/table' */
|
|
1417
1398
|
/** @import { Queryable } from '../types/options' */
|
|
1418
1399
|
/** @import { Column, ColumnOptions } from '../types/column' */
|
|
1419
|
-
/** @import { GetName, Skip, TableType } from '../types/connection' */
|
|
1400
|
+
/** @import { DBTable, GetName, Skip, TableType } from '../types/connection' */
|
|
1420
1401
|
/**
|
|
1421
1402
|
*
|
|
1422
1403
|
* @param {Fields<keyof FieldType>} fields
|
|
@@ -1451,8 +1432,6 @@ function signal2promise(signal) {
|
|
|
1451
1432
|
});
|
|
1452
1433
|
});
|
|
1453
1434
|
}
|
|
1454
|
-
/** @type {GetName} */
|
|
1455
|
-
const defGetName = s => s || '';
|
|
1456
1435
|
/**
|
|
1457
1436
|
* @template {{}} [E={}]
|
|
1458
1437
|
*/
|
|
@@ -1463,52 +1442,26 @@ class Connection {
|
|
|
1463
1442
|
#getConnection;
|
|
1464
1443
|
/** @type {E?} */
|
|
1465
1444
|
#baseTransaction;
|
|
1466
|
-
/** @type {Record<string, string>?} */
|
|
1467
|
-
#names;
|
|
1468
|
-
#getName = defGetName;
|
|
1469
1445
|
/**
|
|
1470
1446
|
*
|
|
1471
1447
|
* @param {PromiseLike<IConnection<E>> | IConnection<E>} connection
|
|
1472
1448
|
* @param {E?} [transaction]
|
|
1473
|
-
* @param {Record<string, string>?} [names]
|
|
1474
1449
|
*/
|
|
1475
|
-
constructor(connection, transaction = null
|
|
1450
|
+
constructor(connection, transaction = null) {
|
|
1476
1451
|
this.#baseTransaction = transaction;
|
|
1477
1452
|
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
1453
|
this.#env = {
|
|
1488
1454
|
isDevelopment,
|
|
1489
1455
|
values,
|
|
1490
|
-
transaction
|
|
1491
|
-
getName,
|
|
1492
|
-
names: names && {
|
|
1493
|
-
...names
|
|
1494
|
-
}
|
|
1456
|
+
transaction
|
|
1495
1457
|
};
|
|
1496
1458
|
}
|
|
1497
1459
|
/**
|
|
1498
1460
|
*
|
|
1499
|
-
* @param {object} [options]
|
|
1500
|
-
* @param {Record<string, string>?} [options.names]
|
|
1501
1461
|
* @returns {Connection<E>}
|
|
1502
1462
|
*/
|
|
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);
|
|
1463
|
+
clone() {
|
|
1464
|
+
return new Connection(this.#getConnection(), this.#baseTransaction);
|
|
1512
1465
|
}
|
|
1513
1466
|
/** @return {Promise<string>} */
|
|
1514
1467
|
async dbType() {
|
|
@@ -1545,7 +1498,7 @@ class Connection {
|
|
|
1545
1498
|
*/
|
|
1546
1499
|
async type(table) {
|
|
1547
1500
|
const conn = await this.#getConnection();
|
|
1548
|
-
return conn.type(this.#env,
|
|
1501
|
+
return conn.type(this.#env, table);
|
|
1549
1502
|
}
|
|
1550
1503
|
/**
|
|
1551
1504
|
* @template T
|
|
@@ -1615,13 +1568,13 @@ class Connection {
|
|
|
1615
1568
|
const modelBuild = model[Build];
|
|
1616
1569
|
const md = typeof modelBuild === 'function' ? modelBuild.bind(model) : undefined;
|
|
1617
1570
|
const conn = await this.#getConnection();
|
|
1618
|
-
return create(conn, this.#
|
|
1571
|
+
return create(conn, this.#env, model, p, md);
|
|
1619
1572
|
}
|
|
1620
1573
|
// @ts-ignore
|
|
1621
1574
|
const m = /** @type {Create} */model;
|
|
1622
1575
|
return m[Create](this, p, async data => {
|
|
1623
1576
|
const conn = await this.#getConnection();
|
|
1624
|
-
return create(conn, this.#
|
|
1577
|
+
return create(conn, this.#env, model, data);
|
|
1625
1578
|
}, model);
|
|
1626
1579
|
}
|
|
1627
1580
|
/**
|
|
@@ -1632,7 +1585,7 @@ class Connection {
|
|
|
1632
1585
|
*/
|
|
1633
1586
|
async find(sql) {
|
|
1634
1587
|
const conn = await this.#getConnection();
|
|
1635
|
-
return find(conn,
|
|
1588
|
+
return find(conn, sql, this.#env);
|
|
1636
1589
|
}
|
|
1637
1590
|
/**
|
|
1638
1591
|
*
|
|
@@ -1642,7 +1595,7 @@ class Connection {
|
|
|
1642
1595
|
*/
|
|
1643
1596
|
async first(sql) {
|
|
1644
1597
|
const conn = await this.#getConnection();
|
|
1645
|
-
return first(conn,
|
|
1598
|
+
return first(conn, sql, this.#env);
|
|
1646
1599
|
}
|
|
1647
1600
|
/**
|
|
1648
1601
|
* @template T
|
|
@@ -1657,18 +1610,18 @@ class Connection {
|
|
|
1657
1610
|
if (newData) {
|
|
1658
1611
|
/** @type {any} */
|
|
1659
1612
|
const doc = data;
|
|
1660
|
-
return updateData(conn,
|
|
1613
|
+
return updateData(conn, model, doc, newData, this.#env);
|
|
1661
1614
|
}
|
|
1662
|
-
return create(conn, this.#
|
|
1615
|
+
return create(conn, this.#env, model, data);
|
|
1663
1616
|
}
|
|
1664
1617
|
const r = /** @type {Save}*/data;
|
|
1665
1618
|
// @ts-ignore
|
|
1666
1619
|
return r[Save](this, async (doc, newData) => {
|
|
1667
1620
|
const conn = await this.#getConnection();
|
|
1668
1621
|
if (newData) {
|
|
1669
|
-
return updateData(conn,
|
|
1622
|
+
return updateData(conn, model, doc, newData, this.#env);
|
|
1670
1623
|
}
|
|
1671
|
-
return create(conn, this.#
|
|
1624
|
+
return create(conn, this.#env, model, doc);
|
|
1672
1625
|
}, newData || null, model);
|
|
1673
1626
|
}
|
|
1674
1627
|
/**
|
|
@@ -1681,13 +1634,13 @@ class Connection {
|
|
|
1681
1634
|
async completelyDestroy(tableDef, data) {
|
|
1682
1635
|
if (typeof data[Destroy] !== 'function') {
|
|
1683
1636
|
const conn = await this.#getConnection();
|
|
1684
|
-
return completelyDelete(conn,
|
|
1637
|
+
return completelyDelete(conn, tableDef, data, this.#env);
|
|
1685
1638
|
}
|
|
1686
1639
|
const r = /** @type {Destroy} */data;
|
|
1687
1640
|
// @ts-ignore
|
|
1688
1641
|
return r[Destroy](this, async data => {
|
|
1689
1642
|
const conn = await this.#getConnection();
|
|
1690
|
-
return completelyDelete(conn,
|
|
1643
|
+
return completelyDelete(conn, tableDef, data, this.#env);
|
|
1691
1644
|
}, tableDef);
|
|
1692
1645
|
}
|
|
1693
1646
|
/**
|
|
@@ -1707,13 +1660,13 @@ class Connection {
|
|
|
1707
1660
|
};
|
|
1708
1661
|
if (typeof data[PseudoDestroy] !== 'function') {
|
|
1709
1662
|
const conn = await this.#getConnection();
|
|
1710
|
-
return pseudoDestroy(conn,
|
|
1663
|
+
return pseudoDestroy(conn, tableDef, data, update, this.#env);
|
|
1711
1664
|
}
|
|
1712
1665
|
const r = /** @type {PseudoDestroy} */data;
|
|
1713
1666
|
// @ts-ignore
|
|
1714
1667
|
return r[PseudoDestroy](this, update, async data => {
|
|
1715
1668
|
const conn = await this.#getConnection();
|
|
1716
|
-
return pseudoDestroy(conn,
|
|
1669
|
+
return pseudoDestroy(conn, tableDef, data, update, this.#env);
|
|
1717
1670
|
}, tableDef);
|
|
1718
1671
|
}
|
|
1719
1672
|
/**
|
|
@@ -1762,9 +1715,9 @@ class Connection {
|
|
|
1762
1715
|
async destroyMany(queryable, update) {
|
|
1763
1716
|
const conn = await this.#getConnection();
|
|
1764
1717
|
if (update === false || !isPseudo(queryable)) {
|
|
1765
|
-
return deleteMany(conn,
|
|
1718
|
+
return deleteMany(conn, queryable, this.#env);
|
|
1766
1719
|
}
|
|
1767
|
-
return pseudoDeleteMany(conn,
|
|
1720
|
+
return pseudoDeleteMany(conn, queryable, update, this.#env);
|
|
1768
1721
|
}
|
|
1769
1722
|
/**
|
|
1770
1723
|
* @template {object | object[]} T
|
|
@@ -1788,10 +1741,10 @@ class Connection {
|
|
|
1788
1741
|
insertKeys = Object.entries(insertValues[0]).filter(([k, v]) => v !== undefined && k in baseFields).map(([k]) => k);
|
|
1789
1742
|
}
|
|
1790
1743
|
if (!insertKeys.length) {
|
|
1791
|
-
insertKeys = Object.entries(baseFields).
|
|
1744
|
+
insertKeys = Object.entries(baseFields).map(v => v[0]);
|
|
1792
1745
|
}
|
|
1793
1746
|
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,
|
|
1747
|
+
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
1748
|
return isArray ? (/** @type {*}*/p) : p[0];
|
|
1796
1749
|
}
|
|
1797
1750
|
/**
|
|
@@ -1811,7 +1764,7 @@ class Connection {
|
|
|
1811
1764
|
fields: toBaseFields(fields),
|
|
1812
1765
|
where: where || undefined
|
|
1813
1766
|
}, await getSetNewValue(fields, update, skip)) : conn.update(this.#env, {
|
|
1814
|
-
table:
|
|
1767
|
+
table: table || '',
|
|
1815
1768
|
fields: toBaseFields(fields),
|
|
1816
1769
|
where: where || undefined
|
|
1817
1770
|
}, await getSetNewValue(fields, update, skip));
|
|
@@ -1836,7 +1789,7 @@ class Connection {
|
|
|
1836
1789
|
fields: baseFields,
|
|
1837
1790
|
where: where || undefined
|
|
1838
1791
|
}, await getSetNewValue(fields, update, skip), returning?.length ? returning : Object.keys(baseFields)) : conn.updateReturn(this.#env, {
|
|
1839
|
-
table:
|
|
1792
|
+
table: table || '',
|
|
1840
1793
|
fields: baseFields,
|
|
1841
1794
|
where: where || undefined
|
|
1842
1795
|
}, await getSetNewValue(fields, update, skip), returning?.length ? returning : Object.keys(baseFields));
|
|
@@ -1857,7 +1810,7 @@ class Connection {
|
|
|
1857
1810
|
const update = await getSetNewValue(fields, null, skip);
|
|
1858
1811
|
const [whereFields, setFields] = getKeys(baseFields, update, pKeys, setKeys);
|
|
1859
1812
|
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,
|
|
1813
|
+
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
1814
|
}
|
|
1862
1815
|
/**
|
|
1863
1816
|
* @template {Fields<MainFieldType>} T
|
|
@@ -1877,7 +1830,7 @@ class Connection {
|
|
|
1877
1830
|
const update = getSetNewValue(fields, null, skip);
|
|
1878
1831
|
const [whereFields, setFields] = getKeys(baseFields, update, pKeys, setKeys);
|
|
1879
1832
|
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,
|
|
1833
|
+
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
1834
|
}
|
|
1882
1835
|
/**
|
|
1883
1836
|
* @param {TableDefine} tableDefine
|
|
@@ -1894,7 +1847,7 @@ class Connection {
|
|
|
1894
1847
|
fields: toBaseFields(fields),
|
|
1895
1848
|
where
|
|
1896
1849
|
}) : conn.delete(this.#env, {
|
|
1897
|
-
table:
|
|
1850
|
+
table: table || '',
|
|
1898
1851
|
fields: toBaseFields(fields),
|
|
1899
1852
|
where
|
|
1900
1853
|
});
|
|
@@ -1917,7 +1870,7 @@ class Connection {
|
|
|
1917
1870
|
fields: baseFields,
|
|
1918
1871
|
where
|
|
1919
1872
|
}, returning?.length ? returning : Object.keys(baseFields)) : conn.deleteReturn(this.#env, {
|
|
1920
|
-
table:
|
|
1873
|
+
table: table || '',
|
|
1921
1874
|
fields: baseFields,
|
|
1922
1875
|
where
|
|
1923
1876
|
}, returning?.length ? returning : Object.keys(baseFields));
|
|
@@ -1940,7 +1893,7 @@ class Connection {
|
|
|
1940
1893
|
fields: toBaseFields(fields)
|
|
1941
1894
|
}) : conn.count(this.#env, {
|
|
1942
1895
|
...options,
|
|
1943
|
-
table:
|
|
1896
|
+
table: table || '',
|
|
1944
1897
|
fields: toBaseFields(fields)
|
|
1945
1898
|
});
|
|
1946
1899
|
}
|
|
@@ -1967,7 +1920,7 @@ class Connection {
|
|
|
1967
1920
|
sort: sort?.length ? sort : getDefaultSort(fields)
|
|
1968
1921
|
}) : conn.select(this.#env, {
|
|
1969
1922
|
...options,
|
|
1970
|
-
table:
|
|
1923
|
+
table: table || '',
|
|
1971
1924
|
fields,
|
|
1972
1925
|
select,
|
|
1973
1926
|
sort: sort?.length ? sort : getDefaultSort(fields)
|
|
@@ -1982,7 +1935,7 @@ class Connection {
|
|
|
1982
1935
|
*/
|
|
1983
1936
|
async addColumn(table, column, type, options) {
|
|
1984
1937
|
const conn = await this.#getConnection();
|
|
1985
|
-
return conn.addColumn(this.#env,
|
|
1938
|
+
return conn.addColumn(this.#env, table, column, type, setDefaultOptions(options));
|
|
1986
1939
|
}
|
|
1987
1940
|
/**
|
|
1988
1941
|
* @param {string} table
|
|
@@ -1993,7 +1946,7 @@ class Connection {
|
|
|
1993
1946
|
*/
|
|
1994
1947
|
async changeColumn(table, column, type, options) {
|
|
1995
1948
|
const conn = await this.#getConnection();
|
|
1996
|
-
return conn.changeColumn(this.#env,
|
|
1949
|
+
return conn.changeColumn(this.#env, table, column, type, setDefaultOptions(options));
|
|
1997
1950
|
}
|
|
1998
1951
|
/**
|
|
1999
1952
|
* @param {string} table
|
|
@@ -2003,7 +1956,7 @@ class Connection {
|
|
|
2003
1956
|
*/
|
|
2004
1957
|
async changeColumnNull(table, column, nullable) {
|
|
2005
1958
|
const conn = await this.#getConnection();
|
|
2006
|
-
return conn.changeColumnNull(this.#env,
|
|
1959
|
+
return conn.changeColumnNull(this.#env, table, column, nullable);
|
|
2007
1960
|
}
|
|
2008
1961
|
/**
|
|
2009
1962
|
* @param {string} table
|
|
@@ -2012,7 +1965,7 @@ class Connection {
|
|
|
2012
1965
|
*/
|
|
2013
1966
|
async dropColumn(table, column) {
|
|
2014
1967
|
const conn = await this.#getConnection();
|
|
2015
|
-
return conn.dropColumn(this.#env,
|
|
1968
|
+
return conn.dropColumn(this.#env, table, column);
|
|
2016
1969
|
}
|
|
2017
1970
|
/**
|
|
2018
1971
|
* @param {string} table
|
|
@@ -2022,7 +1975,7 @@ class Connection {
|
|
|
2022
1975
|
*/
|
|
2023
1976
|
async renameColumn(table, column, newName) {
|
|
2024
1977
|
const conn = await this.#getConnection();
|
|
2025
|
-
return conn.renameColumn(this.#env,
|
|
1978
|
+
return conn.renameColumn(this.#env, table, column, newName);
|
|
2026
1979
|
}
|
|
2027
1980
|
/**
|
|
2028
1981
|
* @param {string} table
|
|
@@ -2033,7 +1986,7 @@ class Connection {
|
|
|
2033
1986
|
*/
|
|
2034
1987
|
async addIndex(table, name, fields, options = {}) {
|
|
2035
1988
|
const conn = await this.#getConnection();
|
|
2036
|
-
return conn.addIndex(this.#env,
|
|
1989
|
+
return conn.addIndex(this.#env, table, name, fields, options);
|
|
2037
1990
|
}
|
|
2038
1991
|
/**
|
|
2039
1992
|
* @param {string} table
|
|
@@ -2042,7 +1995,7 @@ class Connection {
|
|
|
2042
1995
|
*/
|
|
2043
1996
|
async dropIndex(table, name) {
|
|
2044
1997
|
const conn = await this.#getConnection();
|
|
2045
|
-
return conn.dropIndex(this.#env,
|
|
1998
|
+
return conn.dropIndex(this.#env, table, name);
|
|
2046
1999
|
}
|
|
2047
2000
|
/**
|
|
2048
2001
|
* @param {string} table
|
|
@@ -2051,7 +2004,7 @@ class Connection {
|
|
|
2051
2004
|
*/
|
|
2052
2005
|
async createTable(table, fields) {
|
|
2053
2006
|
const conn = await this.#getConnection();
|
|
2054
|
-
return conn.createTable(this.#env,
|
|
2007
|
+
return conn.createTable(this.#env, table, fields.map(setDefaultOptions));
|
|
2055
2008
|
}
|
|
2056
2009
|
/**
|
|
2057
2010
|
* @param {string} table
|
|
@@ -2059,7 +2012,7 @@ class Connection {
|
|
|
2059
2012
|
*/
|
|
2060
2013
|
async dropTable(table) {
|
|
2061
2014
|
const conn = await this.#getConnection();
|
|
2062
|
-
return conn.dropTable(this.#env,
|
|
2015
|
+
return conn.dropTable(this.#env, table);
|
|
2063
2016
|
}
|
|
2064
2017
|
/**
|
|
2065
2018
|
* @param {string} table
|
|
@@ -2068,7 +2021,7 @@ class Connection {
|
|
|
2068
2021
|
*/
|
|
2069
2022
|
async renameTable(table, newName) {
|
|
2070
2023
|
const conn = await this.#getConnection();
|
|
2071
|
-
return conn.renameTable(this.#env,
|
|
2024
|
+
return conn.renameTable(this.#env, table, newName);
|
|
2072
2025
|
}
|
|
2073
2026
|
/**
|
|
2074
2027
|
* @param {string} view
|
|
@@ -2083,7 +2036,7 @@ class Connection {
|
|
|
2083
2036
|
const fields = toBaseFields(queryable.fields);
|
|
2084
2037
|
const options = getOptions(queryable);
|
|
2085
2038
|
const select = toSelect(fields, options.select);
|
|
2086
|
-
return conn.createView(this.#env,
|
|
2039
|
+
return conn.createView(this.#env, view, {
|
|
2087
2040
|
...options,
|
|
2088
2041
|
table: typeof table === 'string' ? table : '',
|
|
2089
2042
|
fields,
|
|
@@ -2096,7 +2049,7 @@ class Connection {
|
|
|
2096
2049
|
*/
|
|
2097
2050
|
async dropView(view) {
|
|
2098
2051
|
const conn = await this.#getConnection();
|
|
2099
|
-
return conn.dropView(this.#env,
|
|
2052
|
+
return conn.dropView(this.#env, view);
|
|
2100
2053
|
}
|
|
2101
2054
|
/**
|
|
2102
2055
|
* @param {string} view
|
|
@@ -2111,7 +2064,7 @@ class Connection {
|
|
|
2111
2064
|
const fields = toBaseFields(queryable.fields);
|
|
2112
2065
|
const options = getOptions(queryable);
|
|
2113
2066
|
const select = toSelect(fields, options.select);
|
|
2114
|
-
return conn.createMaterializedView(this.#env,
|
|
2067
|
+
return conn.createMaterializedView(this.#env, view, {
|
|
2115
2068
|
...options,
|
|
2116
2069
|
table: typeof table === 'string' ? table : '',
|
|
2117
2070
|
fields,
|
|
@@ -2124,7 +2077,7 @@ class Connection {
|
|
|
2124
2077
|
*/
|
|
2125
2078
|
async dropMaterializedView(view) {
|
|
2126
2079
|
const conn = await this.#getConnection();
|
|
2127
|
-
return conn.dropMaterializedView(this.#env,
|
|
2080
|
+
return conn.dropMaterializedView(this.#env, view);
|
|
2128
2081
|
}
|
|
2129
2082
|
/** @type {AbortController[]} */
|
|
2130
2083
|
#layers = [];
|
|
@@ -2194,6 +2147,30 @@ class Connection {
|
|
|
2194
2147
|
}
|
|
2195
2148
|
return false;
|
|
2196
2149
|
}
|
|
2150
|
+
/**
|
|
2151
|
+
* @param {string} table
|
|
2152
|
+
* @returns {Promise<DBTable?>}
|
|
2153
|
+
*/
|
|
2154
|
+
async loadTable(table) {
|
|
2155
|
+
const conn = await this.#getConnection();
|
|
2156
|
+
return conn.loadTables(this.#env, [table]).then(v => v[0] || null);
|
|
2157
|
+
}
|
|
2158
|
+
/**
|
|
2159
|
+
* @param {string[]} tables
|
|
2160
|
+
* @returns {Promise<DBTable[]>}
|
|
2161
|
+
*/
|
|
2162
|
+
async loadTables(tables) {
|
|
2163
|
+
const conn = await this.#getConnection();
|
|
2164
|
+
return conn.loadTables(this.#env, tables);
|
|
2165
|
+
}
|
|
2166
|
+
/**
|
|
2167
|
+
* @param {DBTable[]} tables
|
|
2168
|
+
* @returns {Promise<void>}
|
|
2169
|
+
*/
|
|
2170
|
+
async syncTables(tables) {
|
|
2171
|
+
const conn = await this.#getConnection();
|
|
2172
|
+
return conn.syncTables(this.#env, tables);
|
|
2173
|
+
}
|
|
2197
2174
|
/**
|
|
2198
2175
|
* @template T
|
|
2199
2176
|
* @overload
|
|
@@ -2421,13 +2398,11 @@ function submodel(type, constraints, p1, p2) {
|
|
|
2421
2398
|
array: _array,
|
|
2422
2399
|
nullable,
|
|
2423
2400
|
default: def,
|
|
2424
|
-
precision,
|
|
2425
2401
|
scale,
|
|
2426
2402
|
size,
|
|
2427
2403
|
unique,
|
|
2428
2404
|
index,
|
|
2429
2405
|
primary,
|
|
2430
|
-
serial,
|
|
2431
2406
|
immutable,
|
|
2432
2407
|
text,
|
|
2433
2408
|
sort,
|
|
@@ -2657,13 +2632,11 @@ function field$1(type, p1, p2) {
|
|
|
2657
2632
|
array: _array,
|
|
2658
2633
|
nullable,
|
|
2659
2634
|
default: def,
|
|
2660
|
-
precision,
|
|
2661
2635
|
scale,
|
|
2662
2636
|
size,
|
|
2663
2637
|
unique,
|
|
2664
2638
|
index,
|
|
2665
2639
|
primary,
|
|
2666
|
-
serial,
|
|
2667
2640
|
immutable,
|
|
2668
2641
|
text,
|
|
2669
2642
|
sort,
|
|
@@ -2674,14 +2647,12 @@ function field$1(type, p1, p2) {
|
|
|
2674
2647
|
type,
|
|
2675
2648
|
array: Boolean(array),
|
|
2676
2649
|
nullable: Boolean(nullable),
|
|
2677
|
-
precision,
|
|
2678
2650
|
scale,
|
|
2679
2651
|
size,
|
|
2680
2652
|
default: def,
|
|
2681
2653
|
index,
|
|
2682
2654
|
unique: Boolean(unique),
|
|
2683
2655
|
primary,
|
|
2684
|
-
serial,
|
|
2685
2656
|
immutable,
|
|
2686
2657
|
text,
|
|
2687
2658
|
sort
|
|
@@ -2715,62 +2686,14 @@ function field$1(type, p1, p2) {
|
|
|
2715
2686
|
};
|
|
2716
2687
|
}
|
|
2717
2688
|
|
|
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
2689
|
/** @import { FieldDecorator } from './index.mjs' */
|
|
2767
2690
|
/**
|
|
2768
2691
|
*
|
|
2769
2692
|
* @param {ClassAccessorDecoratorContext} ctx
|
|
2770
|
-
* @param {*}
|
|
2693
|
+
* @param {*} size
|
|
2771
2694
|
* @param {'timestampCreating' | 'timestampUpdating' | 'timestampDeleting'} prop
|
|
2772
2695
|
*/
|
|
2773
|
-
function defineTimestamp(ctx,
|
|
2696
|
+
function defineTimestamp(ctx, size, prop) {
|
|
2774
2697
|
const name = check(ctx);
|
|
2775
2698
|
const {
|
|
2776
2699
|
metadata
|
|
@@ -2780,7 +2703,7 @@ function defineTimestamp(ctx, precision, prop) {
|
|
|
2780
2703
|
type: 'timestamp',
|
|
2781
2704
|
array: false,
|
|
2782
2705
|
nullable: true,
|
|
2783
|
-
|
|
2706
|
+
size,
|
|
2784
2707
|
[prop]: true
|
|
2785
2708
|
};
|
|
2786
2709
|
if (fields) {
|
|
@@ -2800,25 +2723,25 @@ function defineTimestamp(ctx, precision, prop) {
|
|
|
2800
2723
|
};
|
|
2801
2724
|
}
|
|
2802
2725
|
/**
|
|
2803
|
-
* @param {number} [
|
|
2726
|
+
* @param {number} [size]
|
|
2804
2727
|
* @returns {FieldDecorator<Date?>}
|
|
2805
2728
|
*/
|
|
2806
|
-
function creating(
|
|
2807
|
-
return (_, ctx) => defineTimestamp(ctx,
|
|
2729
|
+
function creating(size) {
|
|
2730
|
+
return (_, ctx) => defineTimestamp(ctx, size, 'timestampCreating');
|
|
2808
2731
|
}
|
|
2809
2732
|
/**
|
|
2810
|
-
* @param {number} [
|
|
2733
|
+
* @param {number} [size]
|
|
2811
2734
|
* @returns {FieldDecorator<Date?>}
|
|
2812
2735
|
*/
|
|
2813
|
-
function updating(
|
|
2814
|
-
return (_, ctx) => defineTimestamp(ctx,
|
|
2736
|
+
function updating(size) {
|
|
2737
|
+
return (_, ctx) => defineTimestamp(ctx, size, 'timestampUpdating');
|
|
2815
2738
|
}
|
|
2816
2739
|
/**
|
|
2817
|
-
* @param {number} [
|
|
2740
|
+
* @param {number} [size]
|
|
2818
2741
|
* @returns {FieldDecorator<Date?>}
|
|
2819
2742
|
*/
|
|
2820
|
-
function deleting(
|
|
2821
|
-
return (_, ctx) => defineTimestamp(ctx,
|
|
2743
|
+
function deleting(size) {
|
|
2744
|
+
return (_, ctx) => defineTimestamp(ctx, size, 'timestampDeleting');
|
|
2822
2745
|
}
|
|
2823
2746
|
|
|
2824
2747
|
/**
|
|
@@ -2866,7 +2789,7 @@ function prop(prop, value) {
|
|
|
2866
2789
|
* @returns {FieldDecorator<any>}
|
|
2867
2790
|
*/
|
|
2868
2791
|
function primary(primary) {
|
|
2869
|
-
return (_, ctx) => setProp(ctx, 'primary', primary ||
|
|
2792
|
+
return (_, ctx) => setProp(ctx, 'primary', primary || 1);
|
|
2870
2793
|
}
|
|
2871
2794
|
|
|
2872
2795
|
/** @import { FieldDecorator } from './index.mjs' */
|
|
@@ -3662,7 +3585,7 @@ function divide(value) {
|
|
|
3662
3585
|
};
|
|
3663
3586
|
}
|
|
3664
3587
|
|
|
3665
|
-
/** @import { Fields, Options, Queryable, Select } from './types' */
|
|
3588
|
+
/** @import { Fields, Options, Queryable, Select, VirtualTable } from './types' */
|
|
3666
3589
|
/** @import { WhereValue, Wheres } from './Where.mjs' */
|
|
3667
3590
|
/**
|
|
3668
3591
|
*
|
|
@@ -3717,7 +3640,7 @@ function selectFn(fn, select, newSelect) {
|
|
|
3717
3640
|
* @template {Fields} T
|
|
3718
3641
|
* @template {object} [TB=object]
|
|
3719
3642
|
* @typedef {object} QueryOptions
|
|
3720
|
-
* @property {string} table
|
|
3643
|
+
* @property {string | VirtualTable} [table]
|
|
3721
3644
|
* @property {T} fields
|
|
3722
3645
|
* @property {string} [pseudo]
|
|
3723
3646
|
* @property {((a: object, b?: object | boolean) => TB)?} [build]
|
|
@@ -3745,7 +3668,7 @@ class Query {
|
|
|
3745
3668
|
pseudo
|
|
3746
3669
|
});
|
|
3747
3670
|
}
|
|
3748
|
-
/** @readonly @type {string} */
|
|
3671
|
+
/** @readonly @type {string | VirtualTable | undefined} */
|
|
3749
3672
|
|
|
3750
3673
|
/** @readonly @type {T} */
|
|
3751
3674
|
|
|
@@ -4162,6 +4085,31 @@ class Query {
|
|
|
4162
4085
|
}
|
|
4163
4086
|
}
|
|
4164
4087
|
|
|
4088
|
+
/** @import { Fields, IndexInfo, FieldDefine } from './types' */
|
|
4089
|
+
/**
|
|
4090
|
+
*
|
|
4091
|
+
* @param {Fields} fields
|
|
4092
|
+
* @returns
|
|
4093
|
+
*/
|
|
4094
|
+
function getPrimaryFields(fields) {
|
|
4095
|
+
const idFields = Object.entries(fields).filter(([, v]) => v.primary).sort(([, a], [, b]) => Number(a.primary) - Number(b.primary)).map(([v]) => v);
|
|
4096
|
+
return idFields;
|
|
4097
|
+
}
|
|
4098
|
+
/**
|
|
4099
|
+
* @param {string[]} fields
|
|
4100
|
+
* @param {Record<string, any>} data
|
|
4101
|
+
*/
|
|
4102
|
+
function toPrimary(fields, data) {
|
|
4103
|
+
return fields.map(k => data[k]).join('$');
|
|
4104
|
+
} /**
|
|
4105
|
+
* @param {{fields: Fields}} model
|
|
4106
|
+
* @param {Record<string, any> | Record<string, any>[]} documents
|
|
4107
|
+
*/
|
|
4108
|
+
function toPrimaries(model, documents) {
|
|
4109
|
+
const fields = getPrimaryFields(model.fields);
|
|
4110
|
+
return Array.isArray(documents) ? documents.map(document => toPrimary(fields, document)) : toPrimary(fields, documents);
|
|
4111
|
+
}
|
|
4112
|
+
|
|
4165
4113
|
/* eslint-disable max-lines */
|
|
4166
4114
|
/** @import { Fields, IndexInfo, FieldDefine } from './types' */
|
|
4167
4115
|
/**
|
|
@@ -4659,6 +4607,13 @@ class Model {
|
|
|
4659
4607
|
const fields = Object.getOwnPropertyNames(this.#model.fields);
|
|
4660
4608
|
return fields.findIndex(f => changed.has(f)) >= 0;
|
|
4661
4609
|
}
|
|
4610
|
+
$primary() {
|
|
4611
|
+
const fields = getPrimaryFields(this.$model.fields);
|
|
4612
|
+
return toPrimary(fields, {
|
|
4613
|
+
...this.#data,
|
|
4614
|
+
...this.#newData
|
|
4615
|
+
});
|
|
4616
|
+
}
|
|
4662
4617
|
/**
|
|
4663
4618
|
* @param {Record<string, any>} values
|
|
4664
4619
|
*/
|
|
@@ -5121,7 +5076,6 @@ function field(type, {
|
|
|
5121
5076
|
default: def,
|
|
5122
5077
|
index,
|
|
5123
5078
|
primary,
|
|
5124
|
-
serial,
|
|
5125
5079
|
immutable,
|
|
5126
5080
|
text,
|
|
5127
5081
|
sort,
|
|
@@ -5135,11 +5089,10 @@ function field(type, {
|
|
|
5135
5089
|
default: def,
|
|
5136
5090
|
index,
|
|
5137
5091
|
primary,
|
|
5138
|
-
serial,
|
|
5139
5092
|
immutable,
|
|
5140
5093
|
text,
|
|
5141
5094
|
sort
|
|
5142
5095
|
};
|
|
5143
5096
|
}
|
|
5144
5097
|
|
|
5145
|
-
export { Build, Connection, Create, Destroy, Model, PseudoDestroy, Query, Save, Submodel, Where, creating, decrement, field as define, deleted, deleting, divide, field$1 as field,
|
|
5098
|
+
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 };
|