@zuzjs/orm 0.3.6 → 0.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.
@@ -327,7 +327,7 @@ class MySqlDriver {
327
327
  // Write entry file i.e index.ts
328
328
  const entry = tableNames
329
329
  .map(tableName => `import { ${(0, index_js_1.toPascalCase)(tableName)} } from "./${tableName}";`);
330
- entry.push(`import Zorm from "@zuzjs/orm";`, `import de from "dotenv";`, `de.config()`, `const zorm = Zorm.get(process.env.DATABASE_URL!);`, `zorm.connect([${tableNames.map(t => (0, index_js_1.toPascalCase)(t)).join(`, `)}]);`, `export default zorm`, `export { ${tableNames.map(t => (0, index_js_1.toPascalCase)(t)).join(`, `)} }`);
330
+ entry.push(`import Zorm from "@zuzjs/orm";`, `import de from "dotenv";`, `de.config()`, `const zormEntities = [${tableNames.map(t => (0, index_js_1.toPascalCase)(t)).join(`, `)}];`, `const zorm = Zorm.get(`, `\tprocess.env.DATABASE_URL!,`, `\tzormEntities`, `);`, `zorm.connect(zormEntities);`, `export default zorm`, `export { ${tableNames.map(t => (0, index_js_1.toPascalCase)(t)).join(`, `)} }`);
331
331
  fs_1.default.writeFileSync(path_1.default.join(this.dist, `index.ts`), entry.join(`\n`));
332
332
  await self.pool.end();
333
333
  console.log(picocolors_1.default.green(`✓ ${tables.length} Tables Processed.`));
package/dist/index.d.ts CHANGED
@@ -26,6 +26,7 @@ declare class Zorm {
26
26
  * @private
27
27
  */
28
28
  private usePromise;
29
+ private _init;
29
30
  /**
30
31
  * Private constructor to enforce singleton pattern.
31
32
  * @param {string} connectionString - The database connection string.
@@ -41,13 +42,15 @@ declare class Zorm {
41
42
  * @param {boolean} [usePromise] - Whether to use Promises for queries.
42
43
  * @returns {Zorm} The singleton instance of Zorm.
43
44
  */
44
- static get(connectionString: string, entitiesPath?: string | null, usePromise?: boolean): Zorm;
45
+ static get(connectionString: string, entities?: MixedList<string | Function | EntitySchema<any>>, entitiesPath?: string | null, usePromise?: boolean): Zorm;
45
46
  /**
46
47
  * Connects to the database and initializes entities.
47
48
  * @param {MixedList<string | Function | EntitySchema<any>>} entities - List of entity schemas.
48
49
  * @returns {Promise<void>} Resolves when the connection is initialized.
49
50
  */
50
- connect(entities: MixedList<string | Function | EntitySchema<any>>): Promise<void>;
51
+ connect(entities?: MixedList<string | Function | EntitySchema<any>>): Promise<void>;
52
+ whenReady<T = this>(): T;
53
+ private wrapQueryBuilder;
51
54
  /**
52
55
  * Returns the appropriate QueryBuilder based on the database type.
53
56
  * @param {EntityTarget<T>} entity - The entity target.
package/dist/index.js CHANGED
@@ -48,6 +48,7 @@ class Zorm {
48
48
  * @private
49
49
  */
50
50
  usePromise = false;
51
+ _init;
51
52
  /**
52
53
  * Private constructor to enforce singleton pattern.
53
54
  * @param {string} connectionString - The database connection string.
@@ -55,7 +56,7 @@ class Zorm {
55
56
  * @param {boolean} [usePromise] - Whether to use Promises for queries.
56
57
  * @private
57
58
  */
58
- constructor(connectionString, entitiesPath, usePromise) {
59
+ constructor(connectionString, entities, entitiesPath, usePromise) {
59
60
  const _dist = entitiesPath || path_1.default.join(`src`, `zorm`);
60
61
  const dist = path_1.default.join(process.cwd(), _dist);
61
62
  const _checkDist = (0, index_js_1.checkDirectory)(dist, false);
@@ -73,8 +74,10 @@ class Zorm {
73
74
  password: conn.password,
74
75
  host: conn.host,
75
76
  port: Number(conn.port),
76
- database: conn.database
77
+ database: conn.database,
78
+ entities: entities || []
77
79
  });
80
+ this._init = this.dataSource.initialize.bind(this.dataSource);
78
81
  }
79
82
  else {
80
83
  console.log(`Only MySQL is supported for now`);
@@ -88,9 +91,9 @@ class Zorm {
88
91
  * @param {boolean} [usePromise] - Whether to use Promises for queries.
89
92
  * @returns {Zorm} The singleton instance of Zorm.
90
93
  */
91
- static get(connectionString, entitiesPath, usePromise) {
94
+ static get(connectionString, entities, entitiesPath, usePromise) {
92
95
  if (!Zorm.instance) {
93
- Zorm.instance = new Zorm(connectionString, entitiesPath, usePromise);
96
+ Zorm.instance = new Zorm(connectionString, entities, entitiesPath, usePromise);
94
97
  }
95
98
  return Zorm.instance;
96
99
  }
@@ -102,8 +105,10 @@ class Zorm {
102
105
  async connect(entities) {
103
106
  if (!this.initialized) {
104
107
  try {
105
- this.dataSource.setOptions({ entities });
106
- await this.dataSource.initialize();
108
+ if (entities && !this.dataSource.options.entities?.length) {
109
+ this.dataSource.setOptions({ entities });
110
+ }
111
+ await this._init();
107
112
  this.initialized = true;
108
113
  console.log(picocolors_1.default.green("○ Zorm is connected"));
109
114
  }
@@ -112,6 +117,50 @@ class Zorm {
112
117
  }
113
118
  }
114
119
  }
120
+ whenReady() {
121
+ if (this.initialized)
122
+ return this;
123
+ const handler = {
124
+ get: (target, prop) => {
125
+ if (prop === 'then')
126
+ return undefined;
127
+ const value = target[prop];
128
+ if (typeof value === 'function') {
129
+ return (...args) => {
130
+ const result = value.apply(target, args);
131
+ if (result && result.constructor.name === 'ZormQueryBuilder') {
132
+ return this.wrapQueryBuilder(result);
133
+ }
134
+ this._init().then(() => result);
135
+ };
136
+ }
137
+ return value;
138
+ }
139
+ };
140
+ return new Proxy(this, handler);
141
+ }
142
+ wrapQueryBuilder(qb) {
143
+ const handler = {
144
+ get: (target, prop) => {
145
+ if (prop === 'then')
146
+ return undefined;
147
+ const value = target[prop];
148
+ if (typeof value === 'function') {
149
+ return (...args) => {
150
+ const result = value.apply(target, args);
151
+ // Chain: if returns new QB, wrap it
152
+ if (result && result.constructor.name === 'ZormQueryBuilder') {
153
+ return this.wrapQueryBuilder(result);
154
+ }
155
+ // Final: return Promise
156
+ return this._init().then(() => result);
157
+ };
158
+ }
159
+ return value;
160
+ }
161
+ };
162
+ return new Proxy(qb, handler);
163
+ }
115
164
  /**
116
165
  * Returns the appropriate QueryBuilder based on the database type.
117
166
  * @param {EntityTarget<T>} entity - The entity target.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zuzjs/orm",
3
- "version": "0.3.6",
3
+ "version": "0.3.7",
4
4
  "keywords": [
5
5
  "orm",
6
6
  "zuz",