@radatek/microserver 2.3.7 → 2.3.9

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/microserver.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * MicroServer
3
- * @version 2.3.7
3
+ * @version 2.3.9
4
4
  * @package @radatek/microserver
5
5
  * @copyright Darius Kisonas 2022
6
6
  * @license MIT
@@ -495,8 +495,10 @@ export declare class ProxyPlugin extends Plugin {
495
495
  }
496
496
  /** User info */
497
497
  export interface UserInfo {
498
+ /** User _id */
499
+ _id?: string;
498
500
  /** User id */
499
- id: string;
501
+ id?: string;
500
502
  /** User password plain or hash */
501
503
  password?: string;
502
504
  /** ACL options */
@@ -780,8 +782,12 @@ export declare class Model<TSchema extends ModelSchema> {
780
782
  insert(data: Record<string, any>, options?: ModelContextOptions): Promise<ModelDocument<TSchema>>;
781
783
  /** Update one matching document */
782
784
  update(query: Record<string, any>, options?: ModelContextOptions): Promise<ModelDocument<TSchema>>;
785
+ /** Update many matching documents */
786
+ updateMany(query: Record<string, any>, update: Record<string, any>, options?: ModelContextOptions): Promise<ModelDocument<TSchema>>;
783
787
  /** Delete one matching document */
784
- delete(query: Query, options?: ModelContextOptions): Promise<void>;
788
+ delete(query: Query, options?: ModelContextOptions): Promise<number>;
789
+ /** Delete many matching documents */
790
+ deleteMany(query: Query, options?: ModelContextOptions): Promise<number>;
785
791
  /** Microserver middleware */
786
792
  handler(req: ServerRequest, res: ServerResponse): any;
787
793
  }
package/microserver.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * MicroServer
3
- * @version 2.3.7
3
+ * @version 2.3.9
4
4
  * @package @radatek/microserver
5
5
  * @copyright Darius Kisonas 2022
6
6
  * @license MIT
@@ -2912,23 +2912,17 @@ export class Model {
2912
2912
  }
2913
2913
  /** Find one document */
2914
2914
  async findOne(query, options) {
2915
- if (this.collection instanceof Promise)
2916
- this.collection = await this.collection;
2917
- if (!this.collection)
2918
- throw new AccessDenied('Database not configured');
2915
+ const collection = await this.collection;
2919
2916
  options = { readOnly: true, ...this.options, ...options };
2920
- const doc = await this.collection.findOne(this.getFilter(query, options));
2917
+ const doc = await collection.findOne(this.getFilter(query, options));
2921
2918
  return doc ? this.document(doc, options) : undefined;
2922
2919
  }
2923
2920
  /** Find many documents */
2924
2921
  async findMany(query, options) {
2925
- if (this.collection instanceof Promise)
2926
- this.collection = await this.collection;
2927
- if (!this.collection)
2928
- throw new AccessDenied('Database not configured');
2922
+ const collection = await this.collection;
2929
2923
  const res = [];
2930
2924
  options = { readOnly: true, ...this.options, ...options };
2931
- await this.collection.find(this.getFilter(query || {}, options)).forEach((doc) => res.push(this.document(doc, options)));
2925
+ await collection.find(this.getFilter(query || {}, options)).forEach((doc) => res.push(this.document(doc, options)));
2932
2926
  return res;
2933
2927
  }
2934
2928
  /** Insert a new document */
@@ -2937,10 +2931,7 @@ export class Model {
2937
2931
  }
2938
2932
  /** Update one matching document */
2939
2933
  async update(query, options) {
2940
- if (this.collection instanceof Promise)
2941
- this.collection = await this.collection;
2942
- if (!this.collection)
2943
- throw new AccessDenied('Database not configured');
2934
+ const collection = await this.collection;
2944
2935
  options = { ...this.options, ...options };
2945
2936
  if (options?.validate !== false)
2946
2937
  query = this.document(query, options);
@@ -2952,19 +2943,39 @@ export class Model {
2952
2943
  delete query[n];
2953
2944
  }
2954
2945
  }
2955
- const res = await this.collection.findAndModify({ query: this.getFilter(query, { primaryKey: true, validate: false }), update: query, upsert: options?.insert });
2946
+ const res = await collection.findAndModify({ query: this.getFilter(query, { primaryKey: true, validate: false }), update: query, upsert: options?.insert });
2947
+ if (!res)
2948
+ throw new NotFound('Document not found');
2949
+ return res;
2950
+ }
2951
+ /** Update many matching documents */
2952
+ async updateMany(query, update, options) {
2953
+ const collection = await this.collection;
2954
+ options = { ...this.options, ...options };
2955
+ if (options?.validate !== false)
2956
+ update = this.document(update, options);
2957
+ const unset = query.$unset || {};
2958
+ for (const n in update) {
2959
+ if (update[n] === undefined || update[n] === null) {
2960
+ update.$unset = unset;
2961
+ unset[n] = 1;
2962
+ delete update[n];
2963
+ }
2964
+ }
2965
+ const res = await collection.updateMany(this.getFilter(query, { primaryKey: true, validate: false }), update);
2956
2966
  if (!res)
2957
2967
  throw new NotFound('Document not found');
2958
2968
  return res;
2959
2969
  }
2960
2970
  /** Delete one matching document */
2961
2971
  async delete(query, options) {
2962
- if (this.collection instanceof Promise)
2963
- this.collection = await this.collection;
2964
- if (!this.collection)
2965
- throw new AccessDenied('Database not configured');
2966
- if (query._id)
2967
- await this.collection.deleteOne(this.getFilter(query, { ...this.options, ...options }));
2972
+ const collection = await this.collection;
2973
+ return await collection.deleteOne(this.getFilter(query, { ...this.options, ...options }));
2974
+ }
2975
+ /** Delete many matching documents */
2976
+ async deleteMany(query, options) {
2977
+ const collection = await this.collection;
2978
+ return await collection.deleteMany(this.getFilter(query, { ...this.options, ...options }));
2968
2979
  }
2969
2980
  /** Microserver middleware */
2970
2981
  handler(req, res) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@radatek/microserver",
3
- "version": "2.3.7",
3
+ "version": "2.3.9",
4
4
  "description": "HTTP MicroServer",
5
5
  "author": "Darius Kisonas",
6
6
  "license": "MIT",