@stonyx/orm 0.0.2 → 0.0.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/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "stonyx-async",
5
5
  "stonyx-module"
6
6
  ],
7
- "version": "0.0.2",
7
+ "version": "0.0.3",
8
8
  "description": "",
9
9
  "main": "src/main.js",
10
10
  "type": "module",
package/src/db.js CHANGED
@@ -22,7 +22,7 @@ export default class DB {
22
22
  }
23
23
 
24
24
  async init() {
25
- this.data = await this.retrieve();
25
+ await this.retrieve();
26
26
 
27
27
  const { autosave, saveInterval } = config.orm.db;
28
28
 
@@ -36,6 +36,7 @@ export default class DB {
36
36
  }
37
37
 
38
38
  async create() {
39
+ const { rootPath } = config;
39
40
  const { file, schema } = config.orm.db;
40
41
 
41
42
  if (!file) throw new Error('Configuration Error: ORM DB file path must be defined.');
@@ -43,16 +44,15 @@ export default class DB {
43
44
  let dbSchema;
44
45
 
45
46
  try {
46
- dbSchema = await import(schema);
47
+ dbSchema = (await import(`${rootPath}/${schema}`)).default;
47
48
  } catch (error) {
48
49
  dbSchema = {};
49
50
  log.db('Unable to load DB schema from file, using empty schema instead');
50
51
  }
51
52
 
52
- const data = deepCopy(schema);
53
- createFile(file, this.rawData);
53
+ this.data = deepCopy(dbSchema);
54
54
 
55
- return data;
55
+ createFile(`${rootPath}/${file}`, this.rawData);
56
56
  }
57
57
 
58
58
  async save() {
@@ -66,7 +66,9 @@ export default class DB {
66
66
  async retrieve() {
67
67
  const { file } = config.orm.db;
68
68
 
69
- return readFile(file, { json: true, missingFileCallback: this.create.bind(this) });
69
+ const data = await readFile(file, { json: true, missingFileCallback: this.create.bind(this) });
70
+
71
+ if (data) this.data = data;
70
72
  }
71
73
 
72
74
  /** TODO: We need ORM specific reload logic that replaces models attributes when loading from DB */
package/src/main.js CHANGED
@@ -26,7 +26,6 @@ import { kebabCaseToPascalCase, pluralize } from "@stonyx/utils/string";
26
26
  import baseTransforms from "./transforms.js";
27
27
 
28
28
  export default class Orm {
29
- ready = this.loadDependencies();
30
29
  initialized = false;
31
30
 
32
31
  models = {};
@@ -35,13 +34,13 @@ export default class Orm {
35
34
 
36
35
  constructor() {
37
36
  if (Orm.instance) return Orm.instance;
37
+
38
38
  Orm.instance = this;
39
39
  }
40
40
 
41
- async loadDependencies() {
41
+ async init() {
42
42
  const { paths } = config.orm;
43
-
44
- await Promise.all(['Model', 'Serializer', 'Transform'].map(type => {
43
+ const promises = ['Model', 'Serializer', 'Transform'].map(type => {
45
44
  const lowerCaseType = type.toLowerCase();
46
45
  const path = paths[lowerCaseType];
47
46
  if (!path) throw new Error(`Configuration Error: ORM path for "${type}" must be defined.`);
@@ -52,9 +51,12 @@ export default class Orm {
52
51
 
53
52
  return this[pluralize(lowerCaseType)][alias] = exported;
54
53
  }, { ignoreAccessFailure: true, rawName: true });
55
- }));
54
+ });
55
+
56
+ promises.push(new DB().init());
56
57
 
57
- await new DB().init();
58
+ await Promise.all(promises);
59
+
58
60
  this.initialized = true;
59
61
  }
60
62