@radatek/microserver 2.3.8 → 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.8
3
+ * @version 2.3.9
4
4
  * @package @radatek/microserver
5
5
  * @copyright Darius Kisonas 2022
6
6
  * @license MIT
@@ -782,8 +782,12 @@ export declare class Model<TSchema extends ModelSchema> {
782
782
  insert(data: Record<string, any>, options?: ModelContextOptions): Promise<ModelDocument<TSchema>>;
783
783
  /** Update one matching document */
784
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>>;
785
787
  /** Delete one matching document */
786
- 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>;
787
791
  /** Microserver middleware */
788
792
  handler(req: ServerRequest, res: ServerResponse): any;
789
793
  }
package/microserver.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * MicroServer
3
- * @version 2.3.8
3
+ * @version 2.3.9
4
4
  * @package @radatek/microserver
5
5
  * @copyright Darius Kisonas 2022
6
6
  * @license MIT
@@ -2913,8 +2913,6 @@ export class Model {
2913
2913
  /** Find one document */
2914
2914
  async findOne(query, options) {
2915
2915
  const collection = await this.collection;
2916
- if (!collection)
2917
- throw new AccessDenied('Database not configured');
2918
2916
  options = { readOnly: true, ...this.options, ...options };
2919
2917
  const doc = await collection.findOne(this.getFilter(query, options));
2920
2918
  return doc ? this.document(doc, options) : undefined;
@@ -2922,8 +2920,6 @@ export class Model {
2922
2920
  /** Find many documents */
2923
2921
  async findMany(query, options) {
2924
2922
  const collection = await this.collection;
2925
- if (!collection)
2926
- throw new AccessDenied('Database not configured');
2927
2923
  const res = [];
2928
2924
  options = { readOnly: true, ...this.options, ...options };
2929
2925
  await collection.find(this.getFilter(query || {}, options)).forEach((doc) => res.push(this.document(doc, options)));
@@ -2936,8 +2932,6 @@ export class Model {
2936
2932
  /** Update one matching document */
2937
2933
  async update(query, options) {
2938
2934
  const collection = await this.collection;
2939
- if (!collection)
2940
- throw new AccessDenied('Database not configured');
2941
2935
  options = { ...this.options, ...options };
2942
2936
  if (options?.validate !== false)
2943
2937
  query = this.document(query, options);
@@ -2954,13 +2948,34 @@ export class Model {
2954
2948
  throw new NotFound('Document not found');
2955
2949
  return res;
2956
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);
2966
+ if (!res)
2967
+ throw new NotFound('Document not found');
2968
+ return res;
2969
+ }
2957
2970
  /** Delete one matching document */
2958
2971
  async delete(query, options) {
2959
2972
  const collection = await this.collection;
2960
- if (!collection)
2961
- throw new AccessDenied('Database not configured');
2962
- if (query._id)
2963
- await collection.deleteOne(this.getFilter(query, { ...this.options, ...options }));
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 }));
2964
2979
  }
2965
2980
  /** Microserver middleware */
2966
2981
  handler(req, res) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@radatek/microserver",
3
- "version": "2.3.8",
3
+ "version": "2.3.9",
4
4
  "description": "HTTP MicroServer",
5
5
  "author": "Darius Kisonas",
6
6
  "license": "MIT",