@dnax/core 0.77.0 → 0.77.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.
@@ -77,3 +77,14 @@ export type updateParams = {
77
77
  };
78
78
 
79
79
  export type queryParam = object;
80
+
81
+ export type bulkUpdateOperations = Array<{
82
+ updateOne?: {
83
+ filter?: object;
84
+ update: updateParams;
85
+ };
86
+ updateMany?: {
87
+ filter?: object;
88
+ update: updateParams;
89
+ };
90
+ }>;
@@ -26,6 +26,7 @@ import type {
26
26
  queryParam,
27
27
  restEventType,
28
28
  updateParams,
29
+ bulkUpdateOperations,
29
30
  } from "./@types";
30
31
  import {
31
32
  buildPipeline,
@@ -2057,6 +2058,112 @@ class useRest {
2057
2058
  });
2058
2059
  }
2059
2060
 
2061
+ async bulkUpdate(
2062
+ collection: string,
2063
+ updateOperations: bulkUpdateOperations
2064
+ ): Promise<Array<object>> {
2065
+ return new Promise(async (resolve, reject) => {
2066
+ try {
2067
+ let col = getCollection(collection, this.#tenant_id);
2068
+ if (!col) return fn.error(`Collection ${collection} not found`, 404);
2069
+
2070
+ // controle updateOperations filter
2071
+ for (const operation of updateOperations) {
2072
+ if (!operation) return fn.error(`Operation required`, 400);
2073
+ if (
2074
+ !operation.hasOwnProperty("updateOne") ||
2075
+ !operation.hasOwnProperty("updateMany")
2076
+ )
2077
+ return fn.error(`Only updateOne or updateMany are allowed`, 400);
2078
+ if (operation?.updateOne) {
2079
+ if (!operation.updateOne?.filter) {
2080
+ return fn.error(`Filter required`, 400);
2081
+ }
2082
+ }
2083
+ if (operation?.updateMany) {
2084
+ if (!operation.updateMany?.filter) {
2085
+ return fn.error(`Filter required`, 400);
2086
+ }
2087
+ }
2088
+ }
2089
+
2090
+ for (const operation of updateOperations) {
2091
+ let operationInstance = operation.updateMany || operation.updateOne;
2092
+
2093
+ if (operationInstance?.update) {
2094
+ if (operationInstance?.update?.$set) {
2095
+ operationInstance.update.$set = deepSetId(
2096
+ col,
2097
+ omit(operationInstance.update.$set, [
2098
+ "createdAt",
2099
+ "updatedAt",
2100
+ "_id",
2101
+ ])
2102
+ );
2103
+ operationInstance.update.$set = transformAllDate(
2104
+ operationInstance.update.$set
2105
+ );
2106
+ operationInstance.update.$set = await hashPasswordAuto(
2107
+ operationInstance.update.$set,
2108
+ col
2109
+ );
2110
+ operationInstance.update.$set = formatData(
2111
+ operationInstance.update.$set,
2112
+ {
2113
+ collection: collection,
2114
+ tenant_id: this.#tenant_id,
2115
+ action: "updateOne",
2116
+ // upsert: true,
2117
+ }
2118
+ );
2119
+ var { valid, output, error } = this.validator(
2120
+ collection,
2121
+ dotJson.object(operationInstance.update.$set),
2122
+ {
2123
+ partial: true,
2124
+ }
2125
+ );
2126
+ if (!valid) fn.error(error, 400);
2127
+ }
2128
+ }
2129
+ }
2130
+
2131
+ let result = await this.#tenant.database.db
2132
+ ?.collection(collection)
2133
+ .bulkWrite(updateOperations, {
2134
+ session: this.#session ? this.#session : undefined,
2135
+ });
2136
+ restActivity.save(this.#tenant, {
2137
+ ip: sessionStorage()?.get()?._v?.ip,
2138
+ state: sessionStorage()?.get()?.state,
2139
+ uuid: sessionStorage()?.get()?.uuid,
2140
+ operation: {
2141
+ uuid: sessionStorage()?.get()?.uuid!,
2142
+ type: "bulkUpdate",
2143
+ target: collection,
2144
+ transaction: this.#session ? true : false,
2145
+ internal: sessionStorage()?.get() ? false : true,
2146
+ auth: sessionStorage()?.get()?._v?.isAuth ?? false,
2147
+ role: sessionStorage()?.get()?.role!,
2148
+ user: sessionStorage()?.get()?.state?.user,
2149
+ state: sessionStorage()?.get()?.state,
2150
+ ip: sessionStorage()?.get()?._v?.ip,
2151
+ performedBy: {
2152
+ user: sessionStorage()?.get()?.state?.user,
2153
+ role: sessionStorage()?.get()?.role!,
2154
+ },
2155
+ payload: {
2156
+ update: updateOperations,
2157
+ },
2158
+ },
2159
+ });
2160
+ return resolve(result);
2161
+ } catch (err) {
2162
+ return reject(err);
2163
+ }
2164
+ });
2165
+ }
2166
+
2060
2167
  async updateMany(
2061
2168
  collection: string,
2062
2169
  ids: Array<string>,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dnax/core",
3
- "version": "0.77.0",
3
+ "version": "0.77.1",
4
4
  "module": "index.ts",
5
5
  "type": "module",
6
6
  "bin": {},