@cheetah.js/orm 0.1.61 → 0.1.63

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.
@@ -24,7 +24,7 @@ let EntityStorage = EntityStorage_1 = class EntityStorage {
24
24
  this.entities.set(entity.target, {
25
25
  properties: properties,
26
26
  hideProperties: Object.entries(properties)
27
- .filter(([key, value]) => value.options.hidden)
27
+ .filter(([_key, value]) => value.options.hidden)
28
28
  .map(([key]) => key),
29
29
  relations,
30
30
  indexes: indexes.map((index) => {
@@ -57,7 +57,7 @@ let EntityStorage = EntityStorage_1 = class EntityStorage {
57
57
  };
58
58
  }
59
59
  snapshotColumns(values) {
60
- let properties = Object.entries(values.properties).map(([key, value]) => {
60
+ let properties = Object.entries(values.properties).map(([_key, value]) => {
61
61
  return {
62
62
  name: value.options.columnName,
63
63
  type: value.options.dbType ?? value.type.name,
@@ -107,7 +107,7 @@ let EntityStorage = EntityStorage_1 = class EntityStorage {
107
107
  return [...properties, ...relations];
108
108
  }
109
109
  snapshotIndexes(values) {
110
- return Object.entries(values.properties).map(([key, value]) => {
110
+ return Object.entries(values.properties).map(([key, _value]) => {
111
111
  return {
112
112
  indexName: key,
113
113
  columnName: key,
@@ -120,7 +120,15 @@ let EntityStorage = EntityStorage_1 = class EntityStorage {
120
120
  if (!entity) {
121
121
  return "unknown";
122
122
  }
123
- return entity.properties[this.getFkKey(relation)].type.name;
123
+ const foreignKey = this.getFkKey(relation);
124
+ const property = entity.properties[foreignKey];
125
+ if (!property) {
126
+ return "unknown";
127
+ }
128
+ if (property.options?.dbType) {
129
+ return property.options.dbType;
130
+ }
131
+ return property.type?.name ?? "unknown";
124
132
  }
125
133
  getFkIncrement(relation) {
126
134
  const entity = this.get(relation.entity());
@@ -138,7 +146,7 @@ let EntityStorage = EntityStorage_1 = class EntityStorage {
138
146
  // se for nullable, deverá retornar o primary key da entidade target
139
147
  if (typeof relationShip.fkKey === "undefined") {
140
148
  const entity = this.entities.get(relationShip.entity());
141
- const property = Object.entries(entity.properties).find(([key, value]) => value.options.isPrimary === true);
149
+ const property = Object.entries(entity.properties).find(([_key, value]) => value.options.isPrimary === true);
142
150
  if (!property) {
143
151
  throw new Error(`Entity ${entity.tableName} does not have a primary key`);
144
152
  }
@@ -237,22 +237,43 @@ let OrmService = class OrmService {
237
237
  });
238
238
  }
239
239
  async onInit(customConfig = {}) {
240
- const configFile = globby.sync('cheetah.config.ts', { absolute: true });
241
- if (configFile.length === 0) {
242
- console.log('No config file found!');
243
- return;
240
+ const hasCustomConfig = Object.keys(customConfig).length > 0;
241
+ let config = null;
242
+ let setConfig;
243
+ if (!hasCustomConfig) {
244
+ const configFile = globby.sync('cheetah.config.ts', { absolute: true });
245
+ if (configFile.length === 0) {
246
+ console.log('No config file found!');
247
+ return;
248
+ }
249
+ config = await Promise.resolve(`${configFile[0]}`).then(s => __importStar(require(s)));
250
+ setConfig = config.default;
251
+ }
252
+ else {
253
+ setConfig = customConfig;
244
254
  }
245
- const config = await Promise.resolve(`${configFile[0]}`).then(s => __importStar(require(s)));
246
- const setConfig = Object.keys(customConfig).length > 0 ? customConfig : config.default;
247
255
  this.orm.setConnection(setConfig);
248
256
  await this.orm.connect();
249
- if (typeof config.default.entities === 'string') {
257
+ if (config && typeof config.default.entities === 'string') {
250
258
  const files = globby.sync([config.default.entities, '!node_modules'], { gitignore: true, absolute: true });
251
259
  for (const file of files) {
252
260
  await Promise.resolve(`${file}`).then(s => __importStar(require(s)));
253
261
  }
254
262
  }
255
- const entities = core_1.Metadata.get(constants_1.ENTITIES, Reflect);
263
+ let entities = core_1.Metadata.get(constants_1.ENTITIES, Reflect);
264
+ if (!entities) {
265
+ const entityPaths = this.getSourceFilePaths();
266
+ const entityFiles = globby.sync(entityPaths.filter(path => path.includes('.entity.') || path.includes('entities/')));
267
+ for (const file of entityFiles) {
268
+ try {
269
+ await Promise.resolve(`${file}`).then(s => __importStar(require(s)));
270
+ }
271
+ catch (error) {
272
+ console.warn(`Failed to import entity file ${file}:`, error);
273
+ }
274
+ }
275
+ entities = core_1.Metadata.get(constants_1.ENTITIES, Reflect);
276
+ }
256
277
  if (!entities) {
257
278
  console.log('No entities found!');
258
279
  return;
@@ -1,4 +1,37 @@
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 () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
2
35
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
36
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
37
  };
@@ -43,6 +76,12 @@ function selectLogger(options) {
43
76
  }
44
77
  async function initializeOrm(orm, options) {
45
78
  const storage = new entities_1.EntityStorage();
79
+ if (options.entityFile) {
80
+ const entityFiles = await (0, globby_1.default)(options.entityFile, { absolute: true });
81
+ for (const file of entityFiles) {
82
+ await Promise.resolve(`${file}`).then(s => __importStar(require(s)));
83
+ }
84
+ }
46
85
  const service = new orm_service_1.OrmService(orm, storage, options.entityFile);
47
86
  const connection = resolveConnection(options.connection);
48
87
  await service.onInit(connection);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cheetah.js/orm",
3
- "version": "0.1.61",
3
+ "version": "0.1.63",
4
4
  "description": "A simple ORM for Cheetah.js",
5
5
  "type": "commonjs",
6
6
  "main": "dist/index.js",
@@ -55,5 +55,5 @@
55
55
  "bun",
56
56
  "value-object"
57
57
  ],
58
- "gitHead": "b73d2cb98381f2f14ea460e24f4fec5469106235"
58
+ "gitHead": "b81c351dba661381729aa4aa04985276596f4ac9"
59
59
  }