@dnax/core 0.77.3 → 0.77.5
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/driver/mongo/@types.ts +14 -0
- package/driver/mongo/rest.ts +194 -8
- package/driver/mongo/utils.ts +1 -1
- package/package.json +1 -1
package/driver/mongo/@types.ts
CHANGED
|
@@ -88,3 +88,17 @@ export type bulkUpdateOperations = Array<{
|
|
|
88
88
|
update: updateParams;
|
|
89
89
|
};
|
|
90
90
|
}>;
|
|
91
|
+
|
|
92
|
+
export type bulkOperations = Array<{
|
|
93
|
+
updateOne?: {
|
|
94
|
+
filter?: object;
|
|
95
|
+
update: updateParams;
|
|
96
|
+
};
|
|
97
|
+
updateMany?: {
|
|
98
|
+
filter?: object;
|
|
99
|
+
update: updateParams;
|
|
100
|
+
};
|
|
101
|
+
insertOne?: {
|
|
102
|
+
document: object;
|
|
103
|
+
};
|
|
104
|
+
}>;
|
package/driver/mongo/rest.ts
CHANGED
|
@@ -18,7 +18,6 @@ import {
|
|
|
18
18
|
import { contextError, fn, toJson, dotJson, getEntryBykeys } from "../../utils";
|
|
19
19
|
import v, { type Schema } from "joi";
|
|
20
20
|
import type { Context } from "hono";
|
|
21
|
-
import type { SearchParams } from "meilisearch";
|
|
22
21
|
|
|
23
22
|
import type {
|
|
24
23
|
findOneParam,
|
|
@@ -27,6 +26,7 @@ import type {
|
|
|
27
26
|
restEventType,
|
|
28
27
|
updateParams,
|
|
29
28
|
bulkUpdateOperations,
|
|
29
|
+
bulkOperations,
|
|
30
30
|
} from "./@types";
|
|
31
31
|
import {
|
|
32
32
|
buildPipeline,
|
|
@@ -143,9 +143,6 @@ class useRest {
|
|
|
143
143
|
}
|
|
144
144
|
) => Promise<Array<any>>;
|
|
145
145
|
};
|
|
146
|
-
meilisearch: {
|
|
147
|
-
client: InstanceType<typeof MeiliSearch>;
|
|
148
|
-
};
|
|
149
146
|
|
|
150
147
|
constructor(options: options) {
|
|
151
148
|
this.#useValidator = true;
|
|
@@ -156,9 +153,7 @@ class useRest {
|
|
|
156
153
|
this.#tenant = getTenant(this.#tenant_id);
|
|
157
154
|
this.#useHook = options?.useHook ?? true;
|
|
158
155
|
this.db = this.#tenant.database.db;
|
|
159
|
-
|
|
160
|
-
client: this.#tenant.searchEngine?.meilisearch?.client!,
|
|
161
|
-
};
|
|
156
|
+
|
|
162
157
|
this.activity = {
|
|
163
158
|
aggregate: async (
|
|
164
159
|
pipeline: Array<object>,
|
|
@@ -2058,6 +2053,194 @@ class useRest {
|
|
|
2058
2053
|
});
|
|
2059
2054
|
}
|
|
2060
2055
|
|
|
2056
|
+
async bulkWrite(
|
|
2057
|
+
collection: string,
|
|
2058
|
+
operations: bulkOperations,
|
|
2059
|
+
options?: Omit<optionCb, "withMeta" | "elementAt"> & {
|
|
2060
|
+
disableReadOnlyFields?: string[];
|
|
2061
|
+
}
|
|
2062
|
+
): Promise<any | typeof fn.error> {
|
|
2063
|
+
return new Promise(async (resolve, reject) => {
|
|
2064
|
+
try {
|
|
2065
|
+
let col = getCollection(collection, this.#tenant_id);
|
|
2066
|
+
if (!col) return fn.error(`Collection ${collection} not found`, 404);
|
|
2067
|
+
|
|
2068
|
+
// controle operations
|
|
2069
|
+
for (const operation of operations) {
|
|
2070
|
+
if (!operation) return fn.error(`Operation required`, 400);
|
|
2071
|
+
}
|
|
2072
|
+
let disableReadOnlyFields = omit(
|
|
2073
|
+
col?.api?.fields?.readOnly || [],
|
|
2074
|
+
options?.disableReadOnlyFields || []
|
|
2075
|
+
) as string[];
|
|
2076
|
+
|
|
2077
|
+
// obtenir toutes les keys de operations
|
|
2078
|
+
let keys = Object.keys(operations);
|
|
2079
|
+
|
|
2080
|
+
// controle si les keys sont valides
|
|
2081
|
+
for (const key of keys) {
|
|
2082
|
+
if (!["updateOne", "updateMany", "insertOne"].includes(key)) {
|
|
2083
|
+
return fn.error(`Invalid key ${key} for bulk write`, 400);
|
|
2084
|
+
}
|
|
2085
|
+
}
|
|
2086
|
+
|
|
2087
|
+
// controle des elements requis pour chaque operation
|
|
2088
|
+
for (const operation of operations) {
|
|
2089
|
+
if (operation?.updateOne) {
|
|
2090
|
+
if (!operation.updateOne?.filter) {
|
|
2091
|
+
return fn.error(`Filter required`, 400);
|
|
2092
|
+
}
|
|
2093
|
+
}
|
|
2094
|
+
if (operation?.updateMany) {
|
|
2095
|
+
if (!operation.updateMany?.filter) {
|
|
2096
|
+
return fn.error(`Filter required`, 400);
|
|
2097
|
+
}
|
|
2098
|
+
}
|
|
2099
|
+
|
|
2100
|
+
if (operation?.insertOne) {
|
|
2101
|
+
if (!operation.insertOne?.document) {
|
|
2102
|
+
return fn.error(`Document required`, 400);
|
|
2103
|
+
}
|
|
2104
|
+
}
|
|
2105
|
+
}
|
|
2106
|
+
|
|
2107
|
+
// formatage des data des operations
|
|
2108
|
+
for (const operation of operations) {
|
|
2109
|
+
if (operation?.updateOne || operation?.updateMany) {
|
|
2110
|
+
let operationInstance = operation.updateOne || operation.updateMany;
|
|
2111
|
+
|
|
2112
|
+
if (operationInstance?.filter) {
|
|
2113
|
+
operationInstance.filter = formatData(operationInstance.filter);
|
|
2114
|
+
}
|
|
2115
|
+
if (operationInstance?.update?.$set) {
|
|
2116
|
+
operationInstance.update.$set = deepSetId(
|
|
2117
|
+
col,
|
|
2118
|
+
omit(operationInstance.update.$set, [
|
|
2119
|
+
"createdAt",
|
|
2120
|
+
"updatedAt",
|
|
2121
|
+
"_id",
|
|
2122
|
+
])
|
|
2123
|
+
);
|
|
2124
|
+
operationInstance.update.$set = transformAllDate(
|
|
2125
|
+
operationInstance.update.$set
|
|
2126
|
+
);
|
|
2127
|
+
operationInstance.update.$set = await hashPasswordAuto(
|
|
2128
|
+
operationInstance.update.$set,
|
|
2129
|
+
col
|
|
2130
|
+
);
|
|
2131
|
+
operationInstance.update.$currentDate = {
|
|
2132
|
+
updatedAt: true,
|
|
2133
|
+
};
|
|
2134
|
+
operationInstance.update.$set = formatData(
|
|
2135
|
+
operationInstance.update.$set,
|
|
2136
|
+
{
|
|
2137
|
+
collection: collection,
|
|
2138
|
+
tenant_id: this.#tenant_id,
|
|
2139
|
+
action: "updateOne",
|
|
2140
|
+
}
|
|
2141
|
+
);
|
|
2142
|
+
var { valid, output, error } = this.validator(
|
|
2143
|
+
collection,
|
|
2144
|
+
dotJson.object(operationInstance.update.$set),
|
|
2145
|
+
{
|
|
2146
|
+
partial: true,
|
|
2147
|
+
}
|
|
2148
|
+
);
|
|
2149
|
+
if (!valid) fn.error(error, 400);
|
|
2150
|
+
}
|
|
2151
|
+
}
|
|
2152
|
+
|
|
2153
|
+
if (operation?.insertOne) {
|
|
2154
|
+
operation.insertOne.document = await setUUID(
|
|
2155
|
+
operation.insertOne.document,
|
|
2156
|
+
col
|
|
2157
|
+
);
|
|
2158
|
+
operation.insertOne.document = await randomCode(
|
|
2159
|
+
operation.insertOne.document,
|
|
2160
|
+
col
|
|
2161
|
+
);
|
|
2162
|
+
operation.insertOne.document = await hashPasswordAuto(
|
|
2163
|
+
operation.insertOne.document,
|
|
2164
|
+
col
|
|
2165
|
+
);
|
|
2166
|
+
operation.insertOne.document = transformAllDate(
|
|
2167
|
+
operation.insertOne.document
|
|
2168
|
+
);
|
|
2169
|
+
operation.insertOne.document = deepSetId(
|
|
2170
|
+
col,
|
|
2171
|
+
operation.insertOne.document
|
|
2172
|
+
);
|
|
2173
|
+
operation.insertOne.document = setDefaultValue(
|
|
2174
|
+
operation.insertOne.document,
|
|
2175
|
+
col,
|
|
2176
|
+
"insertOne"
|
|
2177
|
+
);
|
|
2178
|
+
operation.insertOne.document = omit(
|
|
2179
|
+
operation.insertOne.document,
|
|
2180
|
+
["_id", "createdAt", "updatedAt"].concat(disableReadOnlyFields)
|
|
2181
|
+
);
|
|
2182
|
+
|
|
2183
|
+
var { valid, output, error } = this.validator(
|
|
2184
|
+
collection,
|
|
2185
|
+
operation.insertOne.document
|
|
2186
|
+
);
|
|
2187
|
+
if (!valid) fn.error(error, 400);
|
|
2188
|
+
operation.insertOne.document = {
|
|
2189
|
+
...operation.insertOne.document,
|
|
2190
|
+
...(output || {}),
|
|
2191
|
+
//createdAt: new Date(),
|
|
2192
|
+
};
|
|
2193
|
+
|
|
2194
|
+
operation.insertOne.document = formatData(
|
|
2195
|
+
operation.insertOne.document,
|
|
2196
|
+
{
|
|
2197
|
+
collection: collection,
|
|
2198
|
+
tenant_id: this.#tenant_id,
|
|
2199
|
+
action: "insertOne",
|
|
2200
|
+
}
|
|
2201
|
+
);
|
|
2202
|
+
}
|
|
2203
|
+
}
|
|
2204
|
+
|
|
2205
|
+
let result = await this.#tenant.database.db
|
|
2206
|
+
?.collection(collection)
|
|
2207
|
+
.bulkWrite(operations, {
|
|
2208
|
+
session: this.#session ? this.#session : undefined,
|
|
2209
|
+
});
|
|
2210
|
+
restActivity.save(this.#tenant, {
|
|
2211
|
+
ip: sessionStorage()?.get()?._v?.ip,
|
|
2212
|
+
state: sessionStorage()?.get()?.state,
|
|
2213
|
+
uuid: sessionStorage()?.get()?.uuid,
|
|
2214
|
+
operation: {
|
|
2215
|
+
uuid: sessionStorage()?.get()?.uuid!,
|
|
2216
|
+
type: "bulkWrite",
|
|
2217
|
+
target: collection,
|
|
2218
|
+
transaction: this.#session ? true : false,
|
|
2219
|
+
internal: sessionStorage()?.get() ? false : true,
|
|
2220
|
+
auth: sessionStorage()?.get()?._v?.isAuth ?? false,
|
|
2221
|
+
role: sessionStorage()?.get()?.role!,
|
|
2222
|
+
user: sessionStorage()?.get()?.state?.user,
|
|
2223
|
+
state: sessionStorage()?.get()?.state,
|
|
2224
|
+
ip: sessionStorage()?.get()?._v?.ip,
|
|
2225
|
+
performedBy: {
|
|
2226
|
+
user: sessionStorage()?.get()?.state?.user,
|
|
2227
|
+
role: sessionStorage()?.get()?.role!,
|
|
2228
|
+
},
|
|
2229
|
+
payload: {
|
|
2230
|
+
update: operations,
|
|
2231
|
+
},
|
|
2232
|
+
},
|
|
2233
|
+
});
|
|
2234
|
+
|
|
2235
|
+
return resolve(toJson(result || {}));
|
|
2236
|
+
|
|
2237
|
+
// controle updateOne
|
|
2238
|
+
} catch (err) {
|
|
2239
|
+
return reject(err);
|
|
2240
|
+
}
|
|
2241
|
+
});
|
|
2242
|
+
}
|
|
2243
|
+
|
|
2061
2244
|
async bulkUpdate(
|
|
2062
2245
|
collection: string,
|
|
2063
2246
|
updateOperations: bulkUpdateOperations
|
|
@@ -2132,6 +2315,9 @@ class useRest {
|
|
|
2132
2315
|
);
|
|
2133
2316
|
if (!valid) fn.error(error, 400);
|
|
2134
2317
|
}
|
|
2318
|
+
operationInstance.update.$currentDate = {
|
|
2319
|
+
updatedAt: true,
|
|
2320
|
+
};
|
|
2135
2321
|
}
|
|
2136
2322
|
}
|
|
2137
2323
|
|
|
@@ -2164,7 +2350,7 @@ class useRest {
|
|
|
2164
2350
|
},
|
|
2165
2351
|
},
|
|
2166
2352
|
});
|
|
2167
|
-
return resolve(result);
|
|
2353
|
+
return resolve(toJson(result || {}));
|
|
2168
2354
|
} catch (err) {
|
|
2169
2355
|
return reject(err);
|
|
2170
2356
|
}
|
package/driver/mongo/utils.ts
CHANGED