@dnax/core 0.40.2 → 0.41.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.
@@ -1077,6 +1077,120 @@ class useRest {
1077
1077
  });
1078
1078
  }
1079
1079
 
1080
+ async findAndUpdate(
1081
+ collection: string,
1082
+ filter: object,
1083
+ update: updateParams,
1084
+ options?: optionCb
1085
+ ): Promise<object> {
1086
+ return new Promise(async (resolve, reject) => {
1087
+ try {
1088
+ let result = {
1089
+ doc: null,
1090
+ };
1091
+ let col = getCollection(collection, this.#tenant_id);
1092
+ if (!col) return fn.error(`Collection ${collection} not found`, 404);
1093
+ update = toJson(update);
1094
+ 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
+ //@ts-expect-error
1132
+ update = omit(update, omitUpdate);
1133
+ if (update.$set) {
1134
+ // update.$set = deepSetId(col, update.$set);
1135
+ update.$set = deepSetId(col, update.$set);
1136
+ update.$set = transformAllDate(update.$set);
1137
+ update.$set = await hashPasswordAuto(update.$set, col);
1138
+ var { valid, output, error } = this.validator(
1139
+ collection,
1140
+ update.$set,
1141
+ {
1142
+ partial: true,
1143
+ }
1144
+ );
1145
+ // update.$set = output;
1146
+ if (!valid) fn.error(error, 400);
1147
+ }
1148
+
1149
+ result.doc = await this.#tenant.database.db
1150
+ ?.collection(collection)
1151
+ .updateMany(
1152
+ {
1153
+ ...(formatData(filter) || {}),
1154
+ },
1155
+ {
1156
+ ...formatData(omit(update, omitUpdate), {
1157
+ collection: collection,
1158
+ tenant_id: this.#tenant_id,
1159
+ }),
1160
+ $currentDate: {
1161
+ updatedAt: true,
1162
+ },
1163
+ },
1164
+ {
1165
+ session: this.#session ? this.#session : undefined,
1166
+ }
1167
+ );
1168
+
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);
1188
+ } catch (err) {
1189
+ return reject(err);
1190
+ }
1191
+ });
1192
+ }
1193
+
1080
1194
  async findOneAndUpdate(
1081
1195
  collection: string,
1082
1196
  filter: object,
@@ -1176,7 +1290,7 @@ class useRest {
1176
1290
  c: this.#c,
1177
1291
  driver: "mongodb",
1178
1292
  filter: filter,
1179
- action: "updateOne",
1293
+ action: "findOneAndUpdate",
1180
1294
  update: update,
1181
1295
  session: sessionStorage(),
1182
1296
  result: result.doc,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dnax/core",
3
- "version": "0.40.2",
3
+ "version": "0.41.0",
4
4
  "module": "index.ts",
5
5
  "type": "module",
6
6
  "bin": {
package/types/index.ts CHANGED
@@ -42,6 +42,7 @@ export type Tenant = {
42
42
  };
43
43
  };
44
44
  export type Actions =
45
+ | "findAndUpdate"
45
46
  | "findOneAndUpdate"
46
47
  | "find"
47
48
  | "findOne"