sedentary 0.0.21 → 0.0.22

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/README.md CHANGED
@@ -81,10 +81,9 @@ $ npm install --save sedentary
81
81
 
82
82
  # Disclaimer
83
83
 
84
- **Do not use this package itself in production! This package uses a simple self made JSON-DB engine just for testing
85
- purposes.**
84
+ **Do not use this package itself! It does not support any DB engine.**
86
85
 
87
- For a real environment a _DB engine dedicated extension_ must be used:
86
+ A _DB engine dedicated extension_ must be used:
88
87
 
89
88
  - MySQL: planned
90
89
  - PostgreSQL: [sedentary-pg](https://github.com/iccicci/sedentary-pg#readme)
package/index.js CHANGED
@@ -112,13 +112,13 @@ class Sedentary {
112
112
  let constraints = [{ attribute: aarray[0], constraintName: `${tableName}_id_unique`, type: "u" }];
113
113
  const iarray = [];
114
114
  const pk = aarray[0];
115
- if (methods && !(methods instanceof Object))
115
+ if (!methods)
116
+ methods = {};
117
+ if (!(methods instanceof Object))
116
118
  throw new Error(`Sedentary.model: '${modelName}' model: 'methods' option: Wrong type, expected 'Object'`);
117
- const originalMethods = methods;
118
119
  if (parent)
119
120
  if (!parent.attributes)
120
121
  throw new Error(`Sedentary.model: '${modelName}' model: 'parent' option: Wrong type, expected 'Model'`);
121
- //methods = (methods ? { ...(parent.methods || {}), ...methods } : parent.methods) as never;
122
122
  if (primaryKey && typeof primaryKey !== "string")
123
123
  throw new Error(`Sedentary.model: '${modelName}' model: 'primaryKey' option: Wrong type, expected 'string'`);
124
124
  if (primaryKey && !Object.keys(attributes).includes(primaryKey))
@@ -269,13 +269,12 @@ class Sedentary {
269
269
  for (const foreignKey in foreignKeys) {
270
270
  if (foreignKey + "Load" in attributes)
271
271
  throw new Error(`Sedentary.model: '${modelName}' model: '${foreignKey}' attribute: '${foreignKey}Load' inferred methods conflicts with an attribute`);
272
- if (originalMethods && foreignKey + "Load" in originalMethods)
272
+ if (foreignKey + "Load" in methods)
273
273
  throw new Error(`Sedentary.model: '${modelName}' model: '${foreignKey}' attribute: '${foreignKey}Load' inferred methods conflicts with a method`);
274
274
  }
275
- if (originalMethods)
276
- for (const method in originalMethods)
277
- if (method in attributes)
278
- throw new Error(`Sedentary.model: '${modelName}' model: '${method}' method: conflicts with an attribute`);
275
+ for (const method in methods)
276
+ if (method in attributes)
277
+ throw new Error(`Sedentary.model: '${modelName}' model: '${method}' method: conflicts with an attribute`);
279
278
  const checkParent = (parent) => {
280
279
  if (!parent)
281
280
  return;
@@ -294,21 +293,20 @@ class Sedentary {
294
293
  if (foreignKey + "Load" in parent.methods)
295
294
  throw new Error(`Sedentary.model: '${modelName}' model: '${foreignKey}' attribute: '${foreignKey}Load' inferred methods conflicts with a method of '${parent.modelName}' model`);
296
295
  }
297
- if (originalMethods) {
298
- for (const method in originalMethods) {
299
- if (method in parent.attributes)
300
- throw new Error(`Sedentary.model: '${modelName}' model: '${method}' method: conflicts with an attribute of '${parent.modelName}' model`);
301
- for (const foreignKey in parent.foreignKeys)
302
- if (foreignKey + "Load" === method)
303
- throw new Error(`Sedentary.model: '${modelName}' model: '${method}' method: conflicts with an inferred methods of '${parent.modelName}' model`);
304
- if (method in parent.methods)
305
- throw new Error(`Sedentary.model: '${modelName}' model: '${method}' method: conflicts with a method of '${parent.modelName}' model`);
306
- }
296
+ for (const method in methods) {
297
+ if (method in parent.attributes)
298
+ throw new Error(`Sedentary.model: '${modelName}' model: '${method}' method: conflicts with an attribute of '${parent.modelName}' model`);
299
+ for (const foreignKey in parent.foreignKeys)
300
+ if (foreignKey + "Load" === method)
301
+ throw new Error(`Sedentary.model: '${modelName}' model: '${method}' method: conflicts with an inferred methods of '${parent.modelName}' model`);
302
+ if (method in parent.methods)
303
+ throw new Error(`Sedentary.model: '${modelName}' model: '${method}' method: conflicts with a method of '${parent.modelName}' model`);
307
304
  }
308
305
  checkParent(parent.parent);
309
306
  };
310
307
  checkParent(parent);
311
- const ret = function () { };
308
+ const ret = class extends (parent || db_1.EntryBase) {
309
+ };
312
310
  const load = (boh) => new Promise((resolve, reject) => setTimeout(() => {
313
311
  if (boh)
314
312
  return resolve([new ret()]);
@@ -319,9 +317,8 @@ class Sedentary {
319
317
  Object.defineProperty(ret, "load", { value: load });
320
318
  Object.defineProperty(ret, "attributes", { value: attributes });
321
319
  Object.defineProperty(ret, "foreignKeys", { value: foreignKeys });
322
- Object.defineProperty(ret, "methods", { value: methods || {} });
323
- if (methods)
324
- Object.assign(ret.prototype, methods);
320
+ Object.defineProperty(ret, "methods", { value: methods });
321
+ Object.assign(ret.prototype, methods);
325
322
  ret.prototype.save = function () {
326
323
  return Promise.resolve(false);
327
324
  };
package/lib/db.d.ts CHANGED
@@ -1,7 +1,9 @@
1
1
  export declare type Natural = Date | Record<string, unknown> | boolean | number | string;
2
2
  export declare class EntryBase {
3
+ constructor(from?: Partial<EntryBase> | "load");
3
4
  construct(): void;
4
5
  postLoad(): void;
6
+ postSave(): void;
5
7
  preLoad(): void;
6
8
  preSave(): void;
7
9
  save(): Promise<boolean>;
package/lib/db.js CHANGED
@@ -2,8 +2,18 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.DB = exports.Table = exports.Attribute = exports.Type = exports.EntryBase = void 0;
4
4
  class EntryBase {
5
+ constructor(from) {
6
+ if (from === "load")
7
+ this.preLoad();
8
+ else {
9
+ if (from)
10
+ Object.assign(this, from);
11
+ this.construct();
12
+ }
13
+ }
5
14
  construct() { }
6
15
  postLoad() { }
16
+ postSave() { }
7
17
  preLoad() { }
8
18
  preSave() { }
9
19
  async save() {
package/package.json CHANGED
@@ -1,6 +1,10 @@
1
1
  {
2
2
  "author": "Daniele Ricci <daniele.icc@gmail.com> (https://github.com/iccicci)",
3
3
  "bugs": "https://github.com/iccicci/sedentary/issues",
4
+ "contributors": [
5
+ "Daniele Ricci <daniele.icc@gmail.com> (https://github.com/iccicci)",
6
+ "yossarian <sergiybiluk@gmail.com> (https://github.com/captain-yossarian)"
7
+ ],
4
8
  "dependencies": {},
5
9
  "description": "The ORM which never needs to migrate",
6
10
  "devDependencies": {
@@ -74,5 +78,5 @@
74
78
  }
75
79
  },
76
80
  "types": "index.d.ts",
77
- "version": "0.0.21"
81
+ "version": "0.0.22"
78
82
  }