@quatrain/core 0.4.1 → 0.4.3

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/lib/Core.d.ts CHANGED
@@ -7,6 +7,9 @@ export declare type BackendRegistry<T extends AbstractAdapter> = {
7
7
  export declare class Core {
8
8
  static defaultBackend: string;
9
9
  static currentUser: UserClass;
10
+ static classRegistry: {
11
+ [key: string]: any;
12
+ };
10
13
  static logger: {
11
14
  (...data: any[]): void;
12
15
  (message?: any, ...optionalParams: any[]): void;
package/lib/Core.js CHANGED
@@ -40,10 +40,11 @@ class Core {
40
40
  * @param level string
41
41
  */
42
42
  static log(message, src = 'Core', level = 'NOTICE') {
43
- Core.logger(`[${src}] ${message}`, level);
43
+ Core.logger(`${Date.now()} - [${src}] ${message}`);
44
44
  }
45
45
  }
46
46
  exports.Core = Core;
47
47
  Core.defaultBackend = '@default';
48
+ Core.classRegistry = {};
48
49
  Core.logger = console.log;
49
50
  Core._backends = {};
@@ -40,7 +40,7 @@ class AbstractAdapter {
40
40
  */
41
41
  query(query) {
42
42
  return __awaiter(this, void 0, void 0, function* () {
43
- return this.find(yield query.obj.daoFactory(), query.filters, query.sortAndLimit);
43
+ return yield this.find(yield query.obj.daoFactory(), query.filters, query.sortAndLimit);
44
44
  });
45
45
  }
46
46
  log(message) {
@@ -28,13 +28,13 @@ class BaseObject extends AbstractObject_1.AbstractObject {
28
28
  static daoFactory(src = undefined, child = this) {
29
29
  return __awaiter(this, void 0, void 0, function* () {
30
30
  // merge base properties with additional or redefined ones
31
- const base = BaseObjectProperties_1.BaseObjectProperties;
31
+ const base = [...BaseObject.PROPS_DEFINITION];
32
32
  // this.PROPS_DEFINITION &&
33
- this.PROPS_DEFINITION.forEach((property) => {
33
+ child.PROPS_DEFINITION.forEach((property) => {
34
34
  // manage parent properties potential redeclaration
35
35
  const found = base.findIndex((el) => el.name === property.name);
36
36
  if (found !== -1) {
37
- base[found] = Object.assign(base[found], property);
37
+ base[found] = property;
38
38
  }
39
39
  else {
40
40
  base.push(property);
@@ -66,8 +66,7 @@ class BaseObject extends AbstractObject_1.AbstractObject {
66
66
  return Reflect.construct(this, [dao]);
67
67
  }
68
68
  catch (err) {
69
- console.log(err.message);
70
- throw new Error(`Unable to build instance for '${this.constructor.name}': ${err.message}`);
69
+ throw new Error(`Unable to build instance for '${this.name}': ${err.message}`);
71
70
  }
72
71
  });
73
72
  }
@@ -171,6 +171,9 @@ class DataObject {
171
171
  const prop = Reflect.get(this._properties, key);
172
172
  const value = prop.val();
173
173
  switch (prop.constructor.name) {
174
+ case 'CollectionProperty':
175
+ // ignore
176
+ break;
174
177
  case 'ObjectProperty':
175
178
  Reflect.set(data, key, value ? value.toJSON() : null);
176
179
  break;
@@ -1,5 +1,6 @@
1
1
  import { BaseObject } from './BaseObject';
2
2
  import { EntityClass } from './types/EntityClass';
3
+ export declare const EntityProperties: any;
3
4
  export declare class Entity extends BaseObject implements EntityClass {
4
5
  static PROPS_DEFINITION: any;
5
6
  static factory(src?: any): Promise<Entity>;
@@ -1,4 +1,27 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
2
25
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
26
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
27
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -9,9 +32,28 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
32
  });
10
33
  };
11
34
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.Entity = void 0;
35
+ exports.Entity = exports.EntityProperties = void 0;
13
36
  const BaseObject_1 = require("./BaseObject");
14
- const EntityProperties_1 = require("./EntityProperties");
37
+ const CollectionProperty_1 = require("../properties/CollectionProperty");
38
+ const StringProperty_1 = require("../properties/StringProperty");
39
+ const htmlType = __importStar(require("../properties/types/PropertyHTMLType"));
40
+ exports.EntityProperties = [
41
+ {
42
+ // surcharge property minLength and htmlType
43
+ name: 'name',
44
+ type: StringProperty_1.StringProperty.TYPE,
45
+ mandatory: true,
46
+ minLength: 1,
47
+ htmlType: htmlType.ORG,
48
+ },
49
+ {
50
+ name: 'users',
51
+ mandatory: true,
52
+ type: CollectionProperty_1.CollectionProperty.TYPE,
53
+ instanceOf: 'User',
54
+ parentKey: 'entity',
55
+ },
56
+ ];
15
57
  class Entity extends BaseObject_1.BaseObject {
16
58
  static factory(src = undefined) {
17
59
  const _super = Object.create(null, {
@@ -23,4 +65,4 @@ class Entity extends BaseObject_1.BaseObject {
23
65
  }
24
66
  }
25
67
  exports.Entity = Entity;
26
- Entity.PROPS_DEFINITION = EntityProperties_1.EntityProperties;
68
+ Entity.PROPS_DEFINITION = exports.EntityProperties;
@@ -19,7 +19,13 @@ class CollectionProperty extends BaseProperty_1.BaseProperty {
19
19
  super(config);
20
20
  this._value = undefined;
21
21
  this._filters = undefined;
22
- this._instanceOf = config.instanceOf;
22
+ if (!config.instanceOf) {
23
+ throw new Error('Parameter instanceOf is mandatory');
24
+ }
25
+ this._instanceOf =
26
+ typeof config.instanceOf === 'string'
27
+ ? Core_1.Core.classRegistry[config.instanceOf]
28
+ : config.instanceOf;
23
29
  this._backend = config.backend
24
30
  ? Core_1.Core.getBackend(config.backend)
25
31
  : undefined;
@@ -33,6 +33,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
33
33
  };
34
34
  Object.defineProperty(exports, "__esModule", { value: true });
35
35
  exports.DataGenerator = void 0;
36
+ const Core_1 = require("../Core");
36
37
  const htmlType = __importStar(require("../properties/types/PropertyHTMLType"));
37
38
  const faker_1 = require("@faker-js/faker");
38
39
  /**
@@ -44,8 +45,9 @@ const faker_1 = require("@faker-js/faker");
44
45
  */
45
46
  const DataGenerator = (model, qty = 5, forcedValues = {}) => __awaiter(void 0, void 0, void 0, function* () {
46
47
  const promises = [];
48
+ Core_1.Core.log(`Starting to create ${qty} ${model.constructor.name} records`);
47
49
  for (let i = 0; i < qty; i++) {
48
- const dao = yield model.dataObject.clone(model.dataObject.toJSON());
50
+ const dao = yield model.dataObject.clone(); //model.dataObject.toJSON())
49
51
  Object.keys(dao.properties).forEach((key) => {
50
52
  const property = dao.get(key);
51
53
  if (forcedValues[key]) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quatrain/core",
3
- "version": "0.4.1",
3
+ "version": "0.4.3",
4
4
  "description": "Core objects for business apps",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -1 +0,0 @@
1
- export declare const EntityProperties: any;
@@ -1,47 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.EntityProperties = void 0;
27
- const properties_1 = require("../properties");
28
- const StringProperty_1 = require("../properties/StringProperty");
29
- const htmlType = __importStar(require("../properties/types/PropertyHTMLType"));
30
- const User_1 = require("./User");
31
- exports.EntityProperties = [
32
- {
33
- // surcharge property minLength and htmlType
34
- name: 'name',
35
- type: StringProperty_1.StringProperty.TYPE,
36
- mandatory: true,
37
- minLength: 1,
38
- htmlType: htmlType.ORG,
39
- },
40
- {
41
- name: 'users',
42
- mandatory: true,
43
- type: properties_1.CollectionProperty.TYPE,
44
- instanceOf: User_1.User,
45
- parentKey: 'entity',
46
- },
47
- ];