@radatek/microserver 2.3.6 → 2.3.8

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.8
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 */
@@ -658,8 +660,8 @@ interface ModelContextOptions {
658
660
  validate?: boolean;
659
661
  /** use default */
660
662
  default?: boolean;
661
- /** is required */
662
- required?: boolean;
663
+ /** use primary key only for filter */
664
+ primaryKey?: boolean;
663
665
  /** projection fields */
664
666
  projection?: Record<string, 0 | 1 | true | false>;
665
667
  }
@@ -682,6 +684,8 @@ export interface ModelFieldSchema {
682
684
  type: ModelFieldSimpleType;
683
685
  /** Is array */
684
686
  array?: true | false;
687
+ /** Is primary key, used for filtering */
688
+ primaryKey?: true | false;
685
689
  /** Is required */
686
690
  required?: boolean | string | ModelCallbackFunc;
687
691
  /** Can read */
@@ -704,6 +708,7 @@ export interface ModelFieldSchema {
704
708
  interface ResolvedFieldSchema {
705
709
  type: string;
706
710
  model?: Model<any>;
711
+ primaryKey?: boolean;
707
712
  required?: ModelCallbackFunc;
708
713
  canRead: ModelCallbackFunc;
709
714
  canWrite: ModelCallbackFunc;
package/microserver.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * MicroServer
3
- * @version 2.3.6
3
+ * @version 2.3.8
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,40 +2904,29 @@ 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 */
2919
2914
  async findOne(query, options) {
2920
- if (this.collection instanceof Promise)
2921
- this.collection = await this.collection;
2922
- if (!this.collection)
2915
+ const collection = await this.collection;
2916
+ if (!collection)
2923
2917
  throw new AccessDenied('Database not configured');
2924
2918
  options = { readOnly: true, ...this.options, ...options };
2925
- const doc = await this.collection.findOne(this.getFilter(query, options));
2919
+ const doc = await collection.findOne(this.getFilter(query, options));
2926
2920
  return doc ? this.document(doc, options) : undefined;
2927
2921
  }
2928
2922
  /** Find many documents */
2929
2923
  async findMany(query, options) {
2930
- if (this.collection instanceof Promise)
2931
- this.collection = await this.collection;
2932
- if (!this.collection)
2924
+ const collection = await this.collection;
2925
+ if (!collection)
2933
2926
  throw new AccessDenied('Database not configured');
2934
2927
  const res = [];
2935
2928
  options = { readOnly: true, ...this.options, ...options };
2936
- await this.collection.find(this.getFilter(query || {}, options)).forEach((doc) => res.push(this.document(doc, options)));
2929
+ await collection.find(this.getFilter(query || {}, options)).forEach((doc) => res.push(this.document(doc, options)));
2937
2930
  return res;
2938
2931
  }
2939
2932
  /** Insert a new document */
@@ -2942,9 +2935,8 @@ export class Model {
2942
2935
  }
2943
2936
  /** Update one matching document */
2944
2937
  async update(query, options) {
2945
- if (this.collection instanceof Promise)
2946
- this.collection = await this.collection;
2947
- if (!this.collection)
2938
+ const collection = await this.collection;
2939
+ if (!collection)
2948
2940
  throw new AccessDenied('Database not configured');
2949
2941
  options = { ...this.options, ...options };
2950
2942
  if (options?.validate !== false)
@@ -2957,19 +2949,18 @@ export class Model {
2957
2949
  delete query[n];
2958
2950
  }
2959
2951
  }
2960
- const res = await this.collection.findAndModify({ query: this.getFilter(query, { required: true, validate: false, default: false }), update: query, upsert: options?.insert });
2952
+ const res = await collection.findAndModify({ query: this.getFilter(query, { primaryKey: true, validate: false }), update: query, upsert: options?.insert });
2961
2953
  if (!res)
2962
2954
  throw new NotFound('Document not found');
2963
2955
  return res;
2964
2956
  }
2965
2957
  /** Delete one matching document */
2966
2958
  async delete(query, options) {
2967
- if (this.collection instanceof Promise)
2968
- this.collection = await this.collection;
2969
- if (!this.collection)
2959
+ const collection = await this.collection;
2960
+ if (!collection)
2970
2961
  throw new AccessDenied('Database not configured');
2971
2962
  if (query._id)
2972
- await this.collection.deleteOne(this.getFilter(query, { ...this.options, ...options }));
2963
+ await collection.deleteOne(this.getFilter(query, { ...this.options, ...options }));
2973
2964
  }
2974
2965
  /** Microserver middleware */
2975
2966
  handler(req, 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.8",
4
4
  "description": "HTTP MicroServer",
5
5
  "author": "Darius Kisonas",
6
6
  "license": "MIT",