@dnax/core 0.41.0 → 0.43.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/app/hono.ts CHANGED
@@ -412,16 +412,35 @@ function HonoInstance(): typeof app {
412
412
  if (col?.slug && col?.media?.enabled) {
413
413
  let data = JSON.parse(parseBody?.data) || {};
414
414
  // rest.startTransaction();
415
+
416
+ let files = [];
417
+ if (Array.isArray(parseBody.file)) {
418
+ files = parseBody.file;
419
+ } else {
420
+ files.push(parseBody.file);
421
+ }
422
+
423
+ if (col?.hooks?.beforeUpload) {
424
+ await col?.hooks?.beforeUpload({
425
+ io: Cfg.io,
426
+ data: data,
427
+ c: c,
428
+ rest: rest,
429
+ action: "upload",
430
+ session: sessionStorage(),
431
+ error: fn.error,
432
+ files: files,
433
+ });
434
+ }
415
435
  const drive = new MediaDrive({
416
436
  location: col?.media?.overwriteFolderName ?? col?.slug,
417
437
  visibility: col?.media?.visibility ?? "public",
418
438
  });
419
439
 
420
- if (Array.isArray(parseBody?.file)) {
440
+ if (Array.isArray(files)) {
421
441
  let insertedFiles = [];
422
-
423
442
  let indexedData = 0;
424
- let allFiles = parseBody?.file;
443
+ let allFiles = files;
425
444
  for await (let file of allFiles) {
426
445
  let d;
427
446
  if (Array.isArray(data) && data.length) {
@@ -430,13 +449,28 @@ function HonoInstance(): typeof app {
430
449
  d = data;
431
450
  }
432
451
  let insertedFile = await drive.write(file.name, file);
452
+
433
453
  insertedFiles.push({
434
454
  ...d,
435
455
  _file: insertedFile,
436
456
  });
437
457
  indexedData++;
438
458
  }
459
+
439
460
  response = await rest.insertMany(col?.slug, insertedFiles);
461
+ if (col?.hooks?.afterUpload) {
462
+ await col?.hooks?.afterUpload({
463
+ io: Cfg.io,
464
+ data: data,
465
+ c: c,
466
+ rest: rest,
467
+ action: "upload",
468
+ session: sessionStorage(),
469
+ error: fn.error,
470
+ files: files,
471
+ result: response,
472
+ });
473
+ }
440
474
  } else if (typeof parseBody?.file == "object") {
441
475
  let insertedFile = await drive.write(
442
476
  parseBody.file?.name,
@@ -188,7 +188,7 @@ class useRest {
188
188
  async aggregate(
189
189
  collection: string,
190
190
  pipeline: Array<object>,
191
- options?: optionCb
191
+ options?: Omit<optionCb, "withMeta" | "elementAt">
192
192
  ) {
193
193
  return new Promise(async (resolve, reject) => {
194
194
  try {
@@ -264,7 +264,11 @@ class useRest {
264
264
  }
265
265
  });
266
266
  }
267
- async insertOne(collection: string, data: object, options?: optionCb) {
267
+ async insertOne(
268
+ collection: string,
269
+ data: object,
270
+ options?: Omit<optionCb, "withMeta" | "elementAt">
271
+ ) {
268
272
  return new Promise(async (resolve, reject) => {
269
273
  try {
270
274
  let sharedData = {};
@@ -388,7 +392,7 @@ class useRest {
388
392
  async insertMany(
389
393
  collection: string,
390
394
  data: Array<object>,
391
- options?: optionCb
395
+ options?: Omit<optionCb, "withMeta" | "elementAt">
392
396
  ) {
393
397
  return new Promise(async (resolve, reject) => {
394
398
  try {
@@ -689,7 +693,10 @@ class useRest {
689
693
  async count(
690
694
  collection: string,
691
695
  params: findParam,
692
- options?: optionCb
696
+ options?: Omit<
697
+ optionCb,
698
+ "useCustomApi" | "withMeta" | "elementAt" | "useHook"
699
+ >
693
700
  ): Promise<number> {
694
701
  return new Promise(async (resolve, reject) => {
695
702
  try {
@@ -806,7 +813,7 @@ class useRest {
806
813
  collection: string,
807
814
  id: string,
808
815
  params?: findOneParam,
809
- options?: optionCb
816
+ options?: Omit<optionCb, "elementAt" | "withMeta">
810
817
  ): Promise<object | null> {
811
818
  return new Promise(async (resolve, reject) => {
812
819
  try {
@@ -918,7 +925,7 @@ class useRest {
918
925
  collection: string,
919
926
  id: string,
920
927
  update: updateParams,
921
- options?: optionCb
928
+ options?: Omit<optionCb, "withMeta" | "elementAt" | "withMeta">
922
929
  ): Promise<object> {
923
930
  return new Promise(async (resolve, reject) => {
924
931
  try {
@@ -1081,53 +1088,20 @@ class useRest {
1081
1088
  collection: string,
1082
1089
  filter: object,
1083
1090
  update: updateParams,
1084
- options?: optionCb
1085
- ): Promise<object> {
1091
+ options?: Omit<
1092
+ optionCb,
1093
+ "useHook" | "useCustomApi" | "elementAt" | "withMeta"
1094
+ >
1095
+ ): Promise<{ docs: object[]; matchedCount: number; modifiedCount: number }> {
1086
1096
  return new Promise(async (resolve, reject) => {
1087
1097
  try {
1088
1098
  let result = {
1089
- doc: null,
1099
+ docs: [] as Array<object>,
1090
1100
  };
1091
1101
  let col = getCollection(collection, this.#tenant_id);
1092
1102
  if (!col) return fn.error(`Collection ${collection} not found`, 404);
1093
1103
  update = toJson(update);
1094
1104
  let sharedData = {};
1095
- let useHook = options?.useHook ?? this.#useHook;
1096
- if (col?.hooks?.beforeOperation && useHook) {
1097
- await col.hooks.beforeOperation({
1098
- sharedData: sharedData,
1099
- filter: filter || {},
1100
- c: this.#c,
1101
- error: fn.error,
1102
- io: Cfg.io,
1103
- driver: "mongodb",
1104
- action: "findAndUpdate",
1105
- update: update,
1106
- session: sessionStorage(),
1107
- rest: new useRest({
1108
- useHook: false,
1109
- tenant_id: this.#tenant_id,
1110
- }),
1111
- });
1112
- }
1113
-
1114
- if (col?.hooks?.beforeUpdate && useHook) {
1115
- await col.hooks.beforeUpdate({
1116
- sharedData: sharedData,
1117
- c: this.#c,
1118
- filter: filter || {},
1119
- driver: "mongodb",
1120
- io: Cfg.io,
1121
- action: "findAndUpdate",
1122
- update: update,
1123
- session: sessionStorage(),
1124
- rest: new useRest({
1125
- useHook: false,
1126
- tenant_id: this.#tenant_id,
1127
- }),
1128
- });
1129
- }
1130
-
1131
1105
  //@ts-expect-error
1132
1106
  update = omit(update, omitUpdate);
1133
1107
  if (update.$set) {
@@ -1146,7 +1120,7 @@ class useRest {
1146
1120
  if (!valid) fn.error(error, 400);
1147
1121
  }
1148
1122
 
1149
- result.doc = await this.#tenant.database.db
1123
+ let up_ = await this.#tenant.database.db
1150
1124
  ?.collection(collection)
1151
1125
  .updateMany(
1152
1126
  {
@@ -1166,25 +1140,18 @@ class useRest {
1166
1140
  }
1167
1141
  );
1168
1142
 
1169
- if (col?.hooks?.afterUpdate && useHook) {
1170
- await col.hooks.afterUpdate({
1171
- sharedData: sharedData,
1172
- c: this.#c,
1173
- driver: "mongodb",
1174
- filter: filter,
1175
- action: "findAndUpdate",
1176
- update: update,
1177
- session: sessionStorage(),
1178
- result: result.doc,
1179
- io: Cfg.io,
1180
- rest: new useRest({
1181
- useHook: false,
1182
- tenant_id: this.#tenant_id,
1183
- }),
1184
- });
1185
- }
1186
-
1187
- return resolve(result.doc);
1143
+ result.docs = await this.#tenant.database.db
1144
+ .collection(collection)
1145
+ .find(filter, {
1146
+ session: this.#session ? this.#session : undefined,
1147
+ allowDiskUse: true,
1148
+ })
1149
+ .toArray();
1150
+ return resolve({
1151
+ docs: result.docs,
1152
+ matchedCount: up_?.matchedCount || 0,
1153
+ modifiedCount: up_?.modifiedCount || 0,
1154
+ });
1188
1155
  } catch (err) {
1189
1156
  return reject(err);
1190
1157
  }
@@ -1195,8 +1162,11 @@ class useRest {
1195
1162
  collection: string,
1196
1163
  filter: object,
1197
1164
  update: updateParams,
1198
- options?: optionCb
1199
- ): Promise<object> {
1165
+ options?: Omit<
1166
+ optionCb,
1167
+ "useHook" | "useCustomApi" | "elementAt" | "withMeta"
1168
+ >
1169
+ ): Promise<object | null> {
1200
1170
  return new Promise(async (resolve, reject) => {
1201
1171
  try {
1202
1172
  let result = {
@@ -1206,41 +1176,6 @@ class useRest {
1206
1176
  if (!col) return fn.error(`Collection ${collection} not found`, 404);
1207
1177
  update = toJson(update);
1208
1178
  let sharedData = {};
1209
- let useHook = options?.useHook ?? this.#useHook;
1210
- if (col?.hooks?.beforeOperation && useHook) {
1211
- await col.hooks.beforeOperation({
1212
- sharedData: sharedData,
1213
- filter: filter || {},
1214
- c: this.#c,
1215
- error: fn.error,
1216
- io: Cfg.io,
1217
- driver: "mongodb",
1218
- action: "findOneAndUpdate",
1219
- update: update,
1220
- session: sessionStorage(),
1221
- rest: new useRest({
1222
- useHook: false,
1223
- tenant_id: this.#tenant_id,
1224
- }),
1225
- });
1226
- }
1227
-
1228
- if (col?.hooks?.beforeUpdate && useHook) {
1229
- await col.hooks.beforeUpdate({
1230
- sharedData: sharedData,
1231
- c: this.#c,
1232
- filter: filter || {},
1233
- driver: "mongodb",
1234
- io: Cfg.io,
1235
- action: "findOneAndUpdate",
1236
- update: update,
1237
- session: sessionStorage(),
1238
- rest: new useRest({
1239
- useHook: false,
1240
- tenant_id: this.#tenant_id,
1241
- }),
1242
- });
1243
- }
1244
1179
 
1245
1180
  //@ts-expect-error
1246
1181
  update = omit(update, omitUpdate);
@@ -1284,24 +1219,6 @@ class useRest {
1284
1219
  }
1285
1220
  );
1286
1221
 
1287
- if (col?.hooks?.afterUpdate && useHook) {
1288
- await col.hooks.afterUpdate({
1289
- sharedData: sharedData,
1290
- c: this.#c,
1291
- driver: "mongodb",
1292
- filter: filter,
1293
- action: "findOneAndUpdate",
1294
- update: update,
1295
- session: sessionStorage(),
1296
- result: result.doc,
1297
- io: Cfg.io,
1298
- rest: new useRest({
1299
- useHook: false,
1300
- tenant_id: this.#tenant_id,
1301
- }),
1302
- });
1303
- }
1304
-
1305
1222
  return resolve(result.doc);
1306
1223
  } catch (err) {
1307
1224
  return reject(err);
@@ -1482,7 +1399,7 @@ class useRest {
1482
1399
  async deleteOne(
1483
1400
  collection: string,
1484
1401
  id: string,
1485
- options?: optionCb
1402
+ options?: Omit<optionCb, "useCustomApi" | "withMeta" | "elementAt">
1486
1403
  ): Promise<object> {
1487
1404
  return new Promise(async (resolve, reject) => {
1488
1405
  try {
@@ -1587,7 +1504,11 @@ class useRest {
1587
1504
  }
1588
1505
  });
1589
1506
  }
1590
- async deleteMany(collection: string, ids: Array<string>, options?: optionCb) {
1507
+ async deleteMany(
1508
+ collection: string,
1509
+ ids: Array<string>,
1510
+ options?: Omit<optionCb, "useCustomApi" | "withMeta" | "elementAt">
1511
+ ) {
1591
1512
  return new Promise(async (resolve, reject) => {
1592
1513
  try {
1593
1514
  let useHook = options?.useHook ?? this.#useHook;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dnax/core",
3
- "version": "0.41.0",
3
+ "version": "0.43.0",
4
4
  "module": "index.ts",
5
5
  "type": "module",
6
6
  "bin": {
package/types/index.ts CHANGED
@@ -53,7 +53,8 @@ export type Actions =
53
53
  | "deleteOne"
54
54
  | "deleteMany"
55
55
  | "authCollection"
56
- | "aggregate";
56
+ | "aggregate"
57
+ | "upload";
57
58
 
58
59
  export type Field = {
59
60
  name: string;
@@ -212,6 +213,7 @@ export type hooksCtx = (ctx: {
212
213
  rest: InstanceType<typeof useRest>;
213
214
  session?: sessionCtx;
214
215
  io: socketIoType;
216
+ files: Array<any> | Object;
215
217
  error: typeof fn.error;
216
218
  }) => any;
217
219
 
@@ -334,6 +336,8 @@ export type Collection = {
334
336
  afterAggregate?: hooksCtx;
335
337
  beforeCount?: hooksCtx;
336
338
  afterCount?: hooksCtx;
339
+ beforeUpload?: hooksCtx;
340
+ afterUpload?: hooksCtx;
337
341
  };
338
342
  access?: {
339
343
  "*"?: accessCtx;