@radatek/microserver 2.3.5 → 2.3.7

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.5
3
+ * @version 2.3.7
4
4
  * @package @radatek/microserver
5
5
  * @copyright Darius Kisonas 2022
6
6
  * @license MIT
@@ -658,8 +658,8 @@ interface ModelContextOptions {
658
658
  validate?: boolean;
659
659
  /** use default */
660
660
  default?: boolean;
661
- /** is required */
662
- required?: boolean;
661
+ /** use primary key only for filter */
662
+ primaryKey?: boolean;
663
663
  /** projection fields */
664
664
  projection?: Record<string, 0 | 1 | true | false>;
665
665
  }
@@ -682,6 +682,8 @@ export interface ModelFieldSchema {
682
682
  type: ModelFieldSimpleType;
683
683
  /** Is array */
684
684
  array?: true | false;
685
+ /** Is primary key, used for filtering */
686
+ primaryKey?: true | false;
685
687
  /** Is required */
686
688
  required?: boolean | string | ModelCallbackFunc;
687
689
  /** Can read */
@@ -704,6 +706,7 @@ export interface ModelFieldSchema {
704
706
  interface ResolvedFieldSchema {
705
707
  type: string;
706
708
  model?: Model<any>;
709
+ primaryKey?: boolean;
707
710
  required?: ModelCallbackFunc;
708
711
  canRead: ModelCallbackFunc;
709
712
  canWrite: ModelCallbackFunc;
package/microserver.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * MicroServer
3
- * @version 2.3.5
3
+ * @version 2.3.7
4
4
  * @package @radatek/microserver
5
5
  * @copyright Darius Kisonas 2022
6
6
  * @license MIT
@@ -1369,10 +1369,14 @@ export class Router extends EventEmitter {
1369
1369
  /** Add hook */
1370
1370
  hook(url, ...mid) {
1371
1371
  const m = url.match(/^([A-Z]+) (.*)/);
1372
- let method = '*';
1373
- if (m)
1374
- [method, url] = [m[1], m[2]];
1375
- this._add(method, url, 'hook', mid);
1372
+ if (m) {
1373
+ const [method, url] = [m[1], m[2]];
1374
+ this._add(method, url, 'hook', mid);
1375
+ }
1376
+ else {
1377
+ for (const method of ['*', 'GET', 'POST', 'PUT', 'DELETE', 'PATCH'])
1378
+ this._add(method, url, 'hook', mid);
1379
+ }
1376
1380
  }
1377
1381
  /** Check if middleware allready added */
1378
1382
  has(mid) {
@@ -2748,7 +2752,8 @@ export class Model {
2748
2752
  };
2749
2753
  else
2750
2754
  modelField.validate = validate;
2751
- modelField.required = field.required ? this._fieldFunction(field.required, false) : undefined;
2755
+ modelField.primaryKey = field.primaryKey;
2756
+ modelField.required = (field.primaryKey || field.required) ? this._fieldFunction(field.primaryKey || field.required, false) : undefined;
2752
2757
  modelField.canWrite = this._fieldFunction(field.canWrite, typeof field.canWrite === 'string' && field.canWrite.startsWith('$') ? false : true);
2753
2758
  modelField.canRead = this._fieldFunction(field.canRead, typeof field.canRead === 'string' && field.canRead.startsWith('$') ? false : true);
2754
2759
  if (field.default !== undefined) {
@@ -2822,11 +2827,14 @@ export class Model {
2822
2827
  document(data, options) {
2823
2828
  options = options || {};
2824
2829
  const prefix = options.name ? options.name + '.' : '';
2825
- if (options.validate === false)
2826
- return data;
2827
2830
  const res = {};
2828
2831
  for (const name in this.model) {
2829
2832
  const field = this.model[name];
2833
+ if (options.validate === false) {
2834
+ if (name in data)
2835
+ res[name] = data[name];
2836
+ continue;
2837
+ }
2830
2838
  const paramOptions = { ...this.options, ...options, field, name: prefix + name, model: this };
2831
2839
  const canWrite = field.canWrite(paramOptions), canRead = field.canRead(paramOptions), required = field.required?.(paramOptions);
2832
2840
  if (options.readOnly) {
@@ -2896,19 +2904,10 @@ export class Model {
2896
2904
  if (!(name in res)) {
2897
2905
  const field = this.model[name];
2898
2906
  const paramOptions = { ...this.options, ...options, field, name, model: this };
2899
- if ((!options?.required && name in data) || (field.required && field.default)) {
2900
- if (typeof field.required === 'function' && field.required(paramOptions) && field.default && (!(name in data) || field.canWrite(options) === false))
2901
- res[name] = options?.default !== false ? field.default.length ? field.default(paramOptions) : field.default() : data[name];
2902
- else if (name in data)
2903
- res[name] = options?.validate !== false ? this._validateField(data[name], paramOptions) : data[name];
2904
- }
2907
+ if ((!options?.primaryKey && name in data) || field.primaryKey)
2908
+ res[name] = options?.validate !== false ? this._validateField(data[name], paramOptions) : data[name];
2905
2909
  }
2906
2910
  }
2907
- if (typeof options?.projection === 'object')
2908
- for (const name in options.projection) {
2909
- if (name !== '_id' && name in this.model && !res[name])
2910
- res[name] = options.projection[name];
2911
- }
2912
2911
  return res;
2913
2912
  }
2914
2913
  /** Find one document */
@@ -2953,7 +2952,7 @@ export class Model {
2953
2952
  delete query[n];
2954
2953
  }
2955
2954
  }
2956
- const res = await this.collection.findAndModify({ query: this.getFilter(query, { required: true, validate: false, default: false }), update: query, upsert: options?.insert });
2955
+ const res = await this.collection.findAndModify({ query: this.getFilter(query, { primaryKey: true, validate: false }), update: query, upsert: options?.insert });
2957
2956
  if (!res)
2958
2957
  throw new NotFound('Document not found');
2959
2958
  return res;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@radatek/microserver",
3
- "version": "2.3.5",
3
+ "version": "2.3.7",
4
4
  "description": "HTTP MicroServer",
5
5
  "author": "Darius Kisonas",
6
6
  "license": "MIT",