@radatek/microserver 2.3.6 → 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.6
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.6
3
+ * @version 2.3.7
4
4
  * @package @radatek/microserver
5
5
  * @copyright Darius Kisonas 2022
6
6
  * @license MIT
@@ -2752,7 +2752,8 @@ export class Model {
2752
2752
  };
2753
2753
  else
2754
2754
  modelField.validate = validate;
2755
- 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;
2756
2757
  modelField.canWrite = this._fieldFunction(field.canWrite, typeof field.canWrite === 'string' && field.canWrite.startsWith('$') ? false : true);
2757
2758
  modelField.canRead = this._fieldFunction(field.canRead, typeof field.canRead === 'string' && field.canRead.startsWith('$') ? false : true);
2758
2759
  if (field.default !== undefined) {
@@ -2826,11 +2827,14 @@ export class Model {
2826
2827
  document(data, options) {
2827
2828
  options = options || {};
2828
2829
  const prefix = options.name ? options.name + '.' : '';
2829
- if (options.validate === false)
2830
- return data;
2831
2830
  const res = {};
2832
2831
  for (const name in this.model) {
2833
2832
  const field = this.model[name];
2833
+ if (options.validate === false) {
2834
+ if (name in data)
2835
+ res[name] = data[name];
2836
+ continue;
2837
+ }
2834
2838
  const paramOptions = { ...this.options, ...options, field, name: prefix + name, model: this };
2835
2839
  const canWrite = field.canWrite(paramOptions), canRead = field.canRead(paramOptions), required = field.required?.(paramOptions);
2836
2840
  if (options.readOnly) {
@@ -2900,19 +2904,10 @@ export class Model {
2900
2904
  if (!(name in res)) {
2901
2905
  const field = this.model[name];
2902
2906
  const paramOptions = { ...this.options, ...options, field, name, model: this };
2903
- if ((!options?.required && name in data) || (field.required && field.default)) {
2904
- if (typeof field.required === 'function' && field.required(paramOptions) && field.default && (!(name in data) || field.canWrite(options) === false))
2905
- res[name] = options?.default !== false ? field.default.length ? field.default(paramOptions) : field.default() : data[name];
2906
- else if (name in data)
2907
- res[name] = options?.validate !== false ? this._validateField(data[name], paramOptions) : data[name];
2908
- }
2907
+ if ((!options?.primaryKey && name in data) || field.primaryKey)
2908
+ res[name] = options?.validate !== false ? this._validateField(data[name], paramOptions) : data[name];
2909
2909
  }
2910
2910
  }
2911
- if (typeof options?.projection === 'object')
2912
- for (const name in options.projection) {
2913
- if (name !== '_id' && name in this.model && !res[name])
2914
- res[name] = options.projection[name];
2915
- }
2916
2911
  return res;
2917
2912
  }
2918
2913
  /** Find one document */
@@ -2957,7 +2952,7 @@ export class Model {
2957
2952
  delete query[n];
2958
2953
  }
2959
2954
  }
2960
- 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 });
2961
2956
  if (!res)
2962
2957
  throw new NotFound('Document not found');
2963
2958
  return res;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@radatek/microserver",
3
- "version": "2.3.6",
3
+ "version": "2.3.7",
4
4
  "description": "HTTP MicroServer",
5
5
  "author": "Darius Kisonas",
6
6
  "license": "MIT",