@zuzjs/orm 0.3.4 → 0.3.5

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.
@@ -156,6 +156,49 @@ class MySqlDriver {
156
156
  WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ? AND REFERENCED_TABLE_NAME IS NOT NULL`, [this.conn.database, tableName]);
157
157
  foreignKeys[tableName] = fkResults;
158
158
  }
159
+ // Track inverse relations: { tableName: { fkColumn: targetTable } }
160
+ const inverseRelations = {};
161
+ // Populate inverse map
162
+ for (const [tableName, fks] of Object.entries(foreignKeys)) {
163
+ for (const fk of fks) {
164
+ const targetTable = fk.REFERENCED_TABLE_NAME;
165
+ const fkColumn = fk.COLUMN_NAME;
166
+ if (!inverseRelations[targetTable]) {
167
+ inverseRelations[targetTable] = [];
168
+ }
169
+ inverseRelations[targetTable].push({
170
+ fkColumn,
171
+ targetTable: tableName,
172
+ targetEntity: (0, index_js_1.toPascalCase)(tableName)
173
+ });
174
+ }
175
+ }
176
+ // Detect 2 Column Junction Tables for ManyToMany
177
+ const junctionTables = {};
178
+ for (const tableName of tableNames) {
179
+ const [cols] = await this.pool.execute(`SELECT COLUMN_NAME, COLUMN_KEY, EXTRA FROM INFORMATION_SCHEMA.COLUMNS
180
+ WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ?`, [this.conn.database, tableName]);
181
+ const fks = foreignKeys[String(tableName)] || [];
182
+ if (fks.length !== 2)
183
+ continue;
184
+ // Must have exactly 2 columns
185
+ if (cols.length !== 2)
186
+ continue;
187
+ // Both columns must be primary key
188
+ if (cols.filter(c => c.COLUMN_KEY !== 'PRI').length > 0)
189
+ continue;
190
+ const [left, right] = fks;
191
+ const [t1, t2] = [left.REFERENCED_TABLE_NAME, right.REFERENCED_TABLE_NAME].sort();
192
+ const key = `${t1}_${t2}`;
193
+ if (!junctionTables[key]) {
194
+ junctionTables[key] = {
195
+ left: t1,
196
+ right: t2,
197
+ leftCol: left.COLUMN_NAME,
198
+ rightCol: right.COLUMN_NAME
199
+ };
200
+ }
201
+ }
159
202
  for (const tableName of tableNames) {
160
203
  const enums = [];
161
204
  const imports = [];
@@ -222,6 +265,43 @@ class MySqlDriver {
222
265
  }
223
266
  }
224
267
  }
268
+ // Add OneToMany Relations
269
+ const inverse = inverseRelations[String(tableName)] || [];
270
+ for (const rel of inverse) {
271
+ const propName = rel.targetTable.endsWith('s') ? rel.targetTable : `${rel.targetTable}s`;
272
+ // Avoid duplicate
273
+ if (entityCode.some(line => line.includes(`@${propName}`)))
274
+ continue;
275
+ const importLine = `import { ${rel.targetEntity} } from "./${rel.targetTable}";`;
276
+ if (!imports.includes(importLine)) {
277
+ imports.push(importLine);
278
+ }
279
+ if (!_imports.includes('OneToMany'))
280
+ _imports.push('OneToMany');
281
+ entityCode.push(`\t@OneToMany(() => ${rel.targetEntity}, r => r.${rel.fkColumn})`);
282
+ entityCode.push(`\tfk${(0, index_js_1.toPascalCase)(propName)}!: ${rel.targetEntity}[];\n`);
283
+ }
284
+ // Add Many-to-Many Relations
285
+ const junctions = Object.values(junctionTables)
286
+ .filter(j => j.left === tableName || j.right === tableName);
287
+ for (const j of junctions) {
288
+ const targetTable = j.left === tableName ? j.right : j.left;
289
+ const targetEntity = (0, index_js_1.toPascalCase)(targetTable);
290
+ const propName = targetTable.endsWith('s') ? targetTable : `${targetTable}s`;
291
+ if (entityCode.some(line => line.includes(`@${propName}`)))
292
+ continue;
293
+ const importLine = `import { ${targetEntity} } from "./${targetTable}";`;
294
+ if (!imports.includes(importLine)) {
295
+ imports.push(importLine);
296
+ }
297
+ if (!_imports.includes('ManyToMany'))
298
+ _imports.push('ManyToMany');
299
+ if (!_imports.includes('JoinTable'))
300
+ _imports.push('JoinTable');
301
+ entityCode.push(`\t@ManyToMany(() => ${targetEntity})`);
302
+ entityCode.push(`\t@JoinTable()`);
303
+ entityCode.push(`\tom${propName}!: ${targetEntity}[];\n`);
304
+ }
225
305
  const Code = [
226
306
  `/**`,
227
307
  `* AutoGenerated by @zuzjs/orm.`,
package/dist/types.d.ts CHANGED
@@ -101,4 +101,4 @@ export type DeleteQueryResult = {
101
101
  count: number;
102
102
  error?: QueryError;
103
103
  };
104
- export { BaseEntity, Column, Entity, JoinColumn, OneToOne, PrimaryColumn, PrimaryGeneratedColumn } from "typeorm";
104
+ export { BaseEntity, Column, Entity, JoinColumn, ManyToMany, ManyToOne, OneToMany, OneToOne, PrimaryColumn, PrimaryGeneratedColumn } from "typeorm";
package/dist/types.js CHANGED
@@ -1,11 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.PrimaryGeneratedColumn = exports.PrimaryColumn = exports.OneToOne = exports.JoinColumn = exports.Entity = exports.Column = exports.BaseEntity = void 0;
3
+ exports.PrimaryGeneratedColumn = exports.PrimaryColumn = exports.OneToOne = exports.OneToMany = exports.ManyToOne = exports.ManyToMany = exports.JoinColumn = exports.Entity = exports.Column = exports.BaseEntity = void 0;
4
4
  var typeorm_1 = require("typeorm");
5
5
  Object.defineProperty(exports, "BaseEntity", { enumerable: true, get: function () { return typeorm_1.BaseEntity; } });
6
6
  Object.defineProperty(exports, "Column", { enumerable: true, get: function () { return typeorm_1.Column; } });
7
7
  Object.defineProperty(exports, "Entity", { enumerable: true, get: function () { return typeorm_1.Entity; } });
8
8
  Object.defineProperty(exports, "JoinColumn", { enumerable: true, get: function () { return typeorm_1.JoinColumn; } });
9
+ Object.defineProperty(exports, "ManyToMany", { enumerable: true, get: function () { return typeorm_1.ManyToMany; } });
10
+ Object.defineProperty(exports, "ManyToOne", { enumerable: true, get: function () { return typeorm_1.ManyToOne; } });
11
+ Object.defineProperty(exports, "OneToMany", { enumerable: true, get: function () { return typeorm_1.OneToMany; } });
9
12
  Object.defineProperty(exports, "OneToOne", { enumerable: true, get: function () { return typeorm_1.OneToOne; } });
10
13
  Object.defineProperty(exports, "PrimaryColumn", { enumerable: true, get: function () { return typeorm_1.PrimaryColumn; } });
11
14
  Object.defineProperty(exports, "PrimaryGeneratedColumn", { enumerable: true, get: function () { return typeorm_1.PrimaryGeneratedColumn; } });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zuzjs/orm",
3
- "version": "0.3.4",
3
+ "version": "0.3.5",
4
4
  "keywords": [
5
5
  "orm",
6
6
  "zuz",