@ztimson/momentum 1.4.5 → 1.4.6

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/dist/data.d.ts CHANGED
@@ -62,23 +62,14 @@ export declare class Data extends PathEventEmitter {
62
62
  * @param {Document<T> | Document<T>[]} documents New document(s)
63
63
  * @return {Promise<Document<T> | Document<T>[]>} New saved document(s)
64
64
  */
65
- create<T>(collection: string, documents: Document<T> | Document<T>[]): Promise<Document<T> | Document<T>[]>;
66
- /**
67
- * Delete document(s) from collection
68
- * @param {string} collection Target collection
69
- * @param {number | Filter<T>} filter Document ID or filter object
70
- * @param {DeleteOptions} options Delete options
71
- * @return {Promise<number[]>} Array of deleted document IDs
72
- */
73
- delete(collection: string, id: number, options?: DeleteOptions): Promise<number[]>;
74
- delete(collection: string, ids: number[], options?: DeleteOptions): Promise<number[]>;
75
- delete<T>(collection: string, filter: Filter<T>, options?: DeleteOptions): Promise<number[]>;
65
+ create<T>(collection: string, documents: Document<T>): Promise<Document<T>>;
66
+ create<T>(collection: string, documents: Document<T>[]): Promise<Document<T>[]>;
76
67
  /**
77
68
  * Read document(s) from collection
78
69
  * @param {string} collection Target collection
79
70
  * @param {number | Filter<T>} filter Document ID or filter object
80
71
  * @param {ReadOptions} options Read options (limit, page, sortBy, sortDir)
81
- * @return {Promise<T | T[]>} Single document if ID provided, array of documents if filter or no filter provided
72
+ * @return {Promise<T | T[]>} Single document if numeric ID provided, array otherwise
82
73
  */
83
74
  read<T extends Meta>(collection: string, filter?: undefined, options?: ReadOptions): Promise<T[]>;
84
75
  read<T extends Meta>(collection: string, filter?: number, options?: ReadOptions): Promise<T>;
@@ -90,7 +81,22 @@ export declare class Data extends PathEventEmitter {
90
81
  * @param {UpdateOptions<T>} options Update options (filter, append, limit, upsert)
91
82
  * @return {Promise<Document<T> | Document<T>[]>} Updated document(s)
92
83
  */
84
+ update<T>(collection: string, documents: Document<T>, options?: UpdateOptions<T>): Promise<Document<T>>;
85
+ update<T>(collection: string, documents: Document<T>[], options?: UpdateOptions<T>): Promise<Document<T>[]>;
86
+ update<T>(collection: string, documents: Partial<T>, options: UpdateOptions<T> & {
87
+ filter: Filter<T>;
88
+ }): Promise<Document<T>[]>;
93
89
  update<T>(collection: string, documents: Document<T> | Document<T>[], options?: UpdateOptions<T>): Promise<Document<T> | Document<T>[]>;
90
+ /**
91
+ * Delete document(s) from collection
92
+ * @param {string} collection Target collection
93
+ * @param {number | Filter<T>} filter Document ID or filter object
94
+ * @param {DeleteOptions} options Delete options
95
+ * @return {Promise<number[]>} Array of deleted document IDs
96
+ */
97
+ delete(collection: string, id: number, options?: DeleteOptions): Promise<number[]>;
98
+ delete(collection: string, ids: number[], options?: DeleteOptions): Promise<number[]>;
99
+ delete<T>(collection: string, filter: Filter<T>, options?: DeleteOptions): Promise<number[]>;
94
100
  /**
95
101
  * Execute raw MongoDB query
96
102
  * @param {string} collection Target collection name
@@ -1 +1 @@
1
- {"version":3,"file":"data.d.ts","sourceRoot":"","sources":["../src/data.ts"],"names":[],"mappings":"AAAA,OAAO,EAAmB,WAAW,EAAC,MAAM,gBAAgB,CAAC;AAC7D,OAAO,EAAC,KAAK,IAAI,EAAE,gBAAgB,EAAM,MAAM,QAAQ,CAAC;AACxD,OAAO,EAAC,QAAQ,EAAC,MAAM,YAAY,CAAC;AAEpC,uCAAuC;AACvC,MAAM,MAAM,cAAc,GAAG,GAAG,GAAG,IAAI,GAAG,GAAG,GAAG,IAAI,GAAG,GAAG,GAAG,IAAI,GAAG,IAAI,GAAG,KAAK,GAAG,OAAO,GAAG,QAAQ,CAAC;AAEtG,mFAAmF;AACnF,MAAM,MAAM,WAAW,CAAC,CAAC,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,cAAc,GAAG,CAAC,GAAG,IAAI,GAAG,IAAI,EAAE,GAAG,CAAC,CAAC,GAAG,cAAc,GAAG,IAAI,GAAG,IAAI,CAAC,EAAE,CAAC,CAAC;AAEnH,6EAA6E;A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}
3192
+ function numericPk(filter) {
3193
+ if (filter == null || typeof filter !== "object" || Array.isArray(filter)) return null;
3194
+ const id = filter["_id"];
3195
+ return typeof id === "number" ? id : null;
3196
+ }
3192
3197
  class Data extends PathEventEmitter {
3193
3198
  constructor(momentum) {
3194
3199
  super();
@@ -3199,12 +3204,6 @@ Must be either: call, email or sms`);
3199
3204
  dataUrl(collection, pk) {
3200
3205
  return `api/data/${collection}${pk != null ? `/${pk}` : ""}`;
3201
3206
  }
3202
- /**
3203
- * Create new document(s) in collection
3204
- * @param {string} collection Target collection
3205
- * @param {Document<T> | Document<T>[]} documents New document(s)
3206
- * @return {Promise<Document<T> | Document<T>[]>} New saved document(s)
3207
- */
3208
3207
  create(collection, documents) {
3209
3208
  if (!collection || !documents) throw new Error("Cannot create document, missing collection or document");
3210
3209
  if (Array.isArray(documents) && !documents.length) return Promise.resolve([]);
@@ -3218,26 +3217,10 @@ Must be either: call, email or sms`);
3218
3217
  return resp;
3219
3218
  });
3220
3219
  }
3221
- async delete(collection, filter, options) {
3222
- if (!collection || filter == null) throw new Error("Cannot delete document, missing collection or filter");
3223
- if (Array.isArray(filter) && !filter.length) return [];
3224
- const pk = Array.isArray(filter) ? null : typeof filter == "object" ? filter["_id"] == null || Array.isArray(filter["_id"]) ? null : filter["_id"] : filter;
3225
- const url = this.dataUrl(collection, pk);
3226
- const ids = await this.momentum.api.request({
3227
- url,
3228
- method: "DELETE",
3229
- body: {
3230
- filter: Array.isArray(filter) ? { _id: ["in", filter] } : typeof filter == "object" && pk == null ? filter : null,
3231
- ...options
3232
- }
3233
- });
3234
- ids.forEach((id) => this.emit(PES`data/${collection}/${id}:d`, id));
3235
- return ids;
3236
- }
3237
3220
  read(collection, filter, options) {
3238
3221
  if (!collection) throw new Error("Cannot read documents, missing collection");
3239
- const pk = Array.isArray(filter) ? null : typeof filter == "object" ? filter["_id"] == null || Array.isArray(filter["_id"]) ? null : filter["_id"] : filter;
3240
- const q = typeof filter == "object" && filter != null && Object.keys(filter).length && pk == null ? filter : null;
3222
+ const pk = typeof filter === "number" ? filter : numericPk(filter);
3223
+ const q = typeof filter === "object" && filter != null && Object.keys(filter).length && pk == null ? filter : null;
3241
3224
  const url = this.dataUrl(collection, pk);
3242
3225
  return this.momentum.api.request({
3243
3226
  url,
@@ -3254,31 +3237,40 @@ Must be either: call, email or sms`);
3254
3237
  return resp;
3255
3238
  });
3256
3239
  }
3257
- /**
3258
- * Update document(s) in collection
3259
- * @param {string} collection Target collection
3260
- * @param {Document<T> | Document<T>[]} documents Document(s) to update
3261
- * @param {UpdateOptions<T>} options Update options (filter, append, limit, upsert)
3262
- * @return {Promise<Document<T> | Document<T>[]>} Updated document(s)
3263
- */
3264
3240
  update(collection, documents, options = {}) {
3265
3241
  if (!collection || !documents) throw new Error("Cannot update, missing collection or document");
3266
3242
  if (Array.isArray(documents) && !documents.length) return Promise.resolve([]);
3267
3243
  const { filter, append, ...queryOptions } = options;
3268
3244
  const isArray = Array.isArray(documents);
3269
3245
  const firstDoc = isArray ? documents[0] : documents;
3270
- const pk = (isArray ? null : filter && !Array.isArray(filter?._id) ? filter?._id : null) ?? firstDoc._id;
3246
+ const pk = !isArray && !filter ? typeof firstDoc._id === "number" ? firstDoc._id : null : null;
3271
3247
  const url = this.dataUrl(collection, pk);
3272
3248
  return this.momentum.api.request({
3273
3249
  url,
3274
3250
  method: append === false ? "PUT" : "PATCH",
3275
- body: { data: documents, filter: filter && filter?._id == null ? filter : null, ...queryOptions }
3251
+ body: { data: documents, filter: filter && numericPk(filter) == null ? filter : null, ...queryOptions }
3276
3252
  }).then((resp) => {
3277
3253
  if (!resp) return null;
3278
3254
  makeArray(resp).forEach((r) => this.emit(PES`data/${collection}/${r._id}:u`, r));
3279
3255
  return resp;
3280
3256
  });
3281
3257
  }
3258
+ async delete(collection, filter, options) {
3259
+ if (!collection || filter == null) throw new Error("Cannot delete document, missing collection or filter");
3260
+ if (Array.isArray(filter) && !filter.length) return [];
3261
+ const pk = typeof filter === "number" ? filter : null;
3262
+ const url = this.dataUrl(collection, pk);
3263
+ const ids = await this.momentum.api.request({
3264
+ url,
3265
+ method: "DELETE",
3266
+ body: {
3267
+ filter: Array.isArray(filter) ? { _id: ["in", filter] } : typeof filter === "object" ? filter : null,
3268
+ ...options
3269
+ }
3270
+ });
3271
+ ids.forEach((id) => this.emit(PES`data/${collection}/${id}:d`, id));
3272
+ return ids;
3273
+ }
3282
3274
  /**
3283
3275
  * Execute raw MongoDB query
3284
3276
  * @param {string} collection Target collection name
@@ -4338,7 +4330,7 @@ ${log}`;
4338
4330
  }
4339
4331
  momentum;
4340
4332
  }
4341
- const version = "1.4.5";
4333
+ const version = "1.4.6";
4342
4334
  class WebRtc extends PathEventEmitter {
4343
4335
  constructor(momentum) {
4344
4336
  super("webrtc");
package/dist/index.mjs CHANGED
@@ -3185,6 +3185,11 @@ class Client extends PathEventEmitter {
3185
3185
  }
3186
3186
  }
3187
3187
  }
3188
+ function numericPk(filter) {
3189
+ if (filter == null || typeof filter !== "object" || Array.isArray(filter)) return null;
3190
+ const id = filter["_id"];
3191
+ return typeof id === "number" ? id : null;
3192
+ }
3188
3193
  class Data extends PathEventEmitter {
3189
3194
  constructor(momentum) {
3190
3195
  super();
@@ -3195,12 +3200,6 @@ class Data extends PathEventEmitter {
3195
3200
  dataUrl(collection, pk) {
3196
3201
  return `api/data/${collection}${pk != null ? `/${pk}` : ""}`;
3197
3202
  }
3198
- /**
3199
- * Create new document(s) in collection
3200
- * @param {string} collection Target collection
3201
- * @param {Document<T> | Document<T>[]} documents New document(s)
3202
- * @return {Promise<Document<T> | Document<T>[]>} New saved document(s)
3203
- */
3204
3203
  create(collection, documents) {
3205
3204
  if (!collection || !documents) throw new Error("Cannot create document, missing collection or document");
3206
3205
  if (Array.isArray(documents) && !documents.length) return Promise.resolve([]);
@@ -3214,26 +3213,10 @@ class Data extends PathEventEmitter {
3214
3213
  return resp;
3215
3214
  });
3216
3215
  }
3217
- async delete(collection, filter, options) {
3218
- if (!collection || filter == null) throw new Error("Cannot delete document, missing collection or filter");
3219
- if (Array.isArray(filter) && !filter.length) return [];
3220
- const pk = Array.isArray(filter) ? null : typeof filter == "object" ? filter["_id"] == null || Array.isArray(filter["_id"]) ? null : filter["_id"] : filter;
3221
- const url = this.dataUrl(collection, pk);
3222
- const ids = await this.momentum.api.request({
3223
- url,
3224
- method: "DELETE",
3225
- body: {
3226
- filter: Array.isArray(filter) ? { _id: ["in", filter] } : typeof filter == "object" && pk == null ? filter : null,
3227
- ...options
3228
- }
3229
- });
3230
- ids.forEach((id) => this.emit(PES`data/${collection}/${id}:d`, id));
3231
- return ids;
3232
- }
3233
3216
  read(collection, filter, options) {
3234
3217
  if (!collection) throw new Error("Cannot read documents, missing collection");
3235
- const pk = Array.isArray(filter) ? null : typeof filter == "object" ? filter["_id"] == null || Array.isArray(filter["_id"]) ? null : filter["_id"] : filter;
3236
- const q = typeof filter == "object" && filter != null && Object.keys(filter).length && pk == null ? filter : null;
3218
+ const pk = typeof filter === "number" ? filter : numericPk(filter);
3219
+ const q = typeof filter === "object" && filter != null && Object.keys(filter).length && pk == null ? filter : null;
3237
3220
  const url = this.dataUrl(collection, pk);
3238
3221
  return this.momentum.api.request({
3239
3222
  url,
@@ -3250,31 +3233,40 @@ class Data extends PathEventEmitter {
3250
3233
  return resp;
3251
3234
  });
3252
3235
  }
3253
- /**
3254
- * Update document(s) in collection
3255
- * @param {string} collection Target collection
3256
- * @param {Document<T> | Document<T>[]} documents Document(s) to update
3257
- * @param {UpdateOptions<T>} options Update options (filter, append, limit, upsert)
3258
- * @return {Promise<Document<T> | Document<T>[]>} Updated document(s)
3259
- */
3260
3236
  update(collection, documents, options = {}) {
3261
3237
  if (!collection || !documents) throw new Error("Cannot update, missing collection or document");
3262
3238
  if (Array.isArray(documents) && !documents.length) return Promise.resolve([]);
3263
3239
  const { filter, append, ...queryOptions } = options;
3264
3240
  const isArray = Array.isArray(documents);
3265
3241
  const firstDoc = isArray ? documents[0] : documents;
3266
- const pk = (isArray ? null : filter && !Array.isArray(filter?._id) ? filter?._id : null) ?? firstDoc._id;
3242
+ const pk = !isArray && !filter ? typeof firstDoc._id === "number" ? firstDoc._id : null : null;
3267
3243
  const url = this.dataUrl(collection, pk);
3268
3244
  return this.momentum.api.request({
3269
3245
  url,
3270
3246
  method: append === false ? "PUT" : "PATCH",
3271
- body: { data: documents, filter: filter && filter?._id == null ? filter : null, ...queryOptions }
3247
+ body: { data: documents, filter: filter && numericPk(filter) == null ? filter : null, ...queryOptions }
3272
3248
  }).then((resp) => {
3273
3249
  if (!resp) return null;
3274
3250
  makeArray(resp).forEach((r) => this.emit(PES`data/${collection}/${r._id}:u`, r));
3275
3251
  return resp;
3276
3252
  });
3277
3253
  }
3254
+ async delete(collection, filter, options) {
3255
+ if (!collection || filter == null) throw new Error("Cannot delete document, missing collection or filter");
3256
+ if (Array.isArray(filter) && !filter.length) return [];
3257
+ const pk = typeof filter === "number" ? filter : null;
3258
+ const url = this.dataUrl(collection, pk);
3259
+ const ids = await this.momentum.api.request({
3260
+ url,
3261
+ method: "DELETE",
3262
+ body: {
3263
+ filter: Array.isArray(filter) ? { _id: ["in", filter] } : typeof filter === "object" ? filter : null,
3264
+ ...options
3265
+ }
3266
+ });
3267
+ ids.forEach((id) => this.emit(PES`data/${collection}/${id}:d`, id));
3268
+ return ids;
3269
+ }
3278
3270
  /**
3279
3271
  * Execute raw MongoDB query
3280
3272
  * @param {string} collection Target collection name
@@ -4334,7 +4326,7 @@ class Static extends Storage {
4334
4326
  }
4335
4327
  momentum;
4336
4328
  }
4337
- const version = "1.4.5";
4329
+ const version = "1.4.6";
4338
4330
  class WebRtc extends PathEventEmitter {
4339
4331
  constructor(momentum) {
4340
4332
  super("webrtc");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ztimson/momentum",
3
- "version": "1.4.5",
3
+ "version": "1.4.6",
4
4
  "description": "Client library for momentum",
5
5
  "keywords": [
6
6
  "Momentum"