corebasic 1.0.259 → 1.0.261

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.
@@ -182,10 +182,30 @@ const apiHandler = async (req, res) => {
182
182
  meta.outlet = 'DEFAULT_OUTLET';
183
183
  }
184
184
  meta.txn = meta.txn ?? Utils.uid();
185
+ function buildQuery(key, id) {
186
+ key = key ? key : "_id";
187
+ const array_paths = key.split(".$.");
188
+ const last_path = array_paths.pop();
189
+ const query = {};
190
+ let obj = query;
191
+ for (const path of array_paths) {
192
+ obj[path] = { $elemMatch: {} };
193
+ obj = obj[path].$elemMatch;
194
+ }
195
+ if (last_path?.endsWith(".$"))
196
+ obj[last_path.split(".$")[0]] = { $elemMatch: { $eq: id } };
197
+ else if (last_path)
198
+ obj[last_path] = id;
199
+ else
200
+ return undefined;
201
+ return query;
202
+ }
185
203
  const message = {
186
204
  meta,
187
205
  data: req.body.data,
188
206
  params: featureless ? req.params : req.body.params,
207
+ query: req.body.params?.key && (feature.key)?.includes(req.body.params?.key) ? buildQuery(req.body.params?.key ?? "_id", req.body.params?.id) : req.body.params?.id ? { _id: req.body.params?.id } : undefined,
208
+ update: req.body.params?.mut && (feature.mut)?.includes(req.body.params?.mut) ? { $set: { [req.body.params.mut]: req.body.data[req.body.params.mut], ...Dip.updateKeys({ meta }) } } : undefined,
189
209
  };
190
210
  const appReq = {
191
211
  meta: message.meta,
package/libs/features.ts CHANGED
@@ -63,6 +63,8 @@ type FeatureMessage<I = unknown, T extends keyof Schema = keyof Schema> = {
63
63
  meta: FeatureMeta & { topic: T }
64
64
  data: I
65
65
  params: FeatureParams
66
+ query?: unknown // This is the buildQuery not the express query
67
+ update?: unknown // This is the buildUpdate
66
68
 
67
69
  // feature // Only used as req.body.feature in features.ts, session.ts
68
70
  // client // Only used as req.body.client in auth.ts, session.ts and messaging.ts
@@ -143,7 +145,7 @@ type RemoteFeatureEntry = {
143
145
  headers?: {JWT?: string, service?: boolean, NDCURVE_DEVELOPER_LICENSE_ACCESS_TOKEN?: string}
144
146
  }
145
147
 
146
- type FeatureEntry = { api: string, topic: string, bypass: boolean, featureless: boolean, subscribe: string, handler: Handler<unknown, unknown, keyof Schema> }
148
+ type FeatureEntry = { api: string, topic: string, bypass: boolean, featureless: boolean, subscribe: string, handler: Handler<unknown, unknown, keyof Schema>, key?: string[], mut?: string[] }
147
149
 
148
150
 
149
151
  let features: Record<string, FeatureEntry> = {}
@@ -309,10 +311,34 @@ const apiHandler = async (req: ExpressRequest, res: ExpressResponse) => {
309
311
 
310
312
  meta.txn = meta.txn ?? Utils.uid()
311
313
 
314
+
315
+ function buildQuery(key: string, id: unknown): unknown {
316
+ key = key ? key : "_id"
317
+ const array_paths = key.split(".$.")
318
+ const last_path = array_paths.pop()
319
+ const query: Record<string, unknown> = {}
320
+ let obj = query
321
+ for (const path of array_paths) {
322
+ obj[path] = { $elemMatch: { } }
323
+ obj = (obj[path] as { $elemMatch: Record<string, unknown> }).$elemMatch
324
+ }
325
+
326
+ if (last_path?.endsWith(".$"))
327
+ obj[last_path.split(".$")[0]!] = { $elemMatch: {$eq: id} }
328
+ else if (last_path)
329
+ obj[last_path] = id
330
+ else
331
+ return undefined
332
+ return query
333
+ }
334
+
312
335
  const message: FeatureMessage = {
313
336
  meta,
314
337
  data: req.body.data,
315
338
  params: featureless ? req.params : req.body.params!,
339
+ query: req.body.params?.key && (feature.key!)?.includes(req.body.params?.key as string) ? buildQuery(req.body.params?.key ?? "_id", req.body.params?.id) : req.body.params?.id ? {_id: req.body.params?.id} : undefined,
340
+ update: req.body.params?.mut && (feature.mut!)?.includes(req.body.params?.mut as string) ? { $set: { [req.body.params!.mut]: (req.body.data as Record<string, unknown>)[req.body.params!.mut], ...Dip.updateKeys({meta}) } } : undefined,
341
+
316
342
  }
317
343
 
318
344
  const appReq: Req = {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "corebasic",
3
3
  "type": "module",
4
- "version": "1.0.259",
4
+ "version": "1.0.261",
5
5
  "description": "",
6
6
  "main": "dist/index.js",
7
7
  "types": "./index.ts",