@cheetah.js/orm 0.1.132 → 0.1.136

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.
@@ -1,3 +1,6 @@
1
1
  import { EntityName } from '../driver/driver.interface';
2
+ import { PropertyOptions } from './property.decorator';
2
3
  export declare function OneToMany<T>(entity: () => EntityName<T>, fkKey: (string & keyof T) | ((e: T) => any)): PropertyDecorator;
3
- export declare function ManyToOne<T>(entity?: () => EntityName<T>): PropertyDecorator;
4
+ type ManyToOneOptions = Partial<PropertyOptions>;
5
+ export declare function ManyToOne<T>(entityOrOptions?: (() => EntityName<T>) | ManyToOneOptions, maybeOptions?: ManyToOneOptions): PropertyDecorator;
6
+ export {};
@@ -5,6 +5,7 @@ exports.ManyToOne = ManyToOne;
5
5
  const constants_1 = require("../constants");
6
6
  const core_1 = require("@cheetah.js/core");
7
7
  const utils_1 = require("../utils");
8
+ const index_decorator_1 = require("./index.decorator");
8
9
  function OneToMany(entity, fkKey) {
9
10
  return (target, propertyKey) => {
10
11
  const existing = core_1.Metadata.get(constants_1.PROPERTIES_RELATIONS, target.constructor) || [];
@@ -15,14 +16,27 @@ function OneToMany(entity, fkKey) {
15
16
  core_1.Metadata.set(constants_1.PROPERTIES_RELATIONS, existing, target.constructor);
16
17
  };
17
18
  }
18
- function ManyToOne(entity) {
19
+ function ManyToOne(entityOrOptions, maybeOptions) {
19
20
  return (target, propertyKey) => {
20
21
  const existing = core_1.Metadata.get(constants_1.PROPERTIES_RELATIONS, target.constructor) || [];
21
- const entityValue = entity || '__AUTO_DETECT__';
22
- const options = { relation: 'many-to-one', propertyKey, isRelation: true, entity: entityValue, type: core_1.Metadata.getType(target, propertyKey), originalEntity: target.constructor };
23
- options['columnName'] = `${(0, utils_1.toSnakeCase)(propertyKey)}_id`;
24
- // @ts-ignore
25
- existing.push(options);
22
+ const hasEntity = typeof entityOrOptions === 'function';
23
+ const entity = hasEntity ? entityOrOptions : undefined;
24
+ const options = (!hasEntity ? entityOrOptions : maybeOptions) || {};
25
+ const columnName = options.columnName || `${(0, utils_1.toSnakeCase)(propertyKey)}_id`;
26
+ const relationOptions = {
27
+ relation: 'many-to-one',
28
+ propertyKey,
29
+ isRelation: true,
30
+ entity: entity || '__AUTO_DETECT__',
31
+ type: core_1.Metadata.getType(target, propertyKey),
32
+ originalEntity: target.constructor,
33
+ columnName,
34
+ ...options,
35
+ };
36
+ if (options.index) {
37
+ (0, index_decorator_1.Index)({ properties: [propertyKey] })(target, propertyKey);
38
+ }
39
+ existing.push(relationOptions);
26
40
  core_1.Metadata.set(constants_1.PROPERTIES_RELATIONS, existing, target.constructor);
27
41
  };
28
42
  }
@@ -217,13 +217,13 @@ class BunMysqlDriver extends bun_driver_base_1.BunDriverBase {
217
217
  });
218
218
  }
219
219
  async constraints(tableName, options) {
220
- const result = await this.sql.unsafe(`SELECT
221
- CONSTRAINT_NAME as index_name,
222
- CONCAT('REFERENCES \`', REFERENCED_TABLE_NAME, '\` (\`', REFERENCED_COLUMN_NAME, '\`)') as consdef,
223
- 'FOREIGN KEY' as constraint_type
224
- FROM information_schema.KEY_COLUMN_USAGE
225
- WHERE TABLE_NAME = '${tableName}'
226
- AND TABLE_SCHEMA = DATABASE()
220
+ const result = await this.sql.unsafe(`SELECT
221
+ CONSTRAINT_NAME as index_name,
222
+ CONCAT('REFERENCES \`', REFERENCED_TABLE_NAME, '\` (\`', REFERENCED_COLUMN_NAME, '\`)') as consdef,
223
+ 'FOREIGN KEY' as constraint_type
224
+ FROM information_schema.KEY_COLUMN_USAGE
225
+ WHERE TABLE_NAME = '${tableName}'
226
+ AND TABLE_SCHEMA = DATABASE()
227
227
  AND REFERENCED_TABLE_NAME IS NOT NULL`);
228
228
  return result.map((row) => {
229
229
  return {
@@ -182,8 +182,8 @@ class BunPgDriver extends bun_driver_base_1.BunDriverBase {
182
182
  }
183
183
  async index(tableName, options) {
184
184
  const schema = (options && options.schema) || 'public';
185
- let result = await this.sql.unsafe(`SELECT indexname AS index_name, indexdef AS column_name, tablename AS table_name
186
- FROM pg_indexes
185
+ let result = await this.sql.unsafe(`SELECT indexname AS index_name, indexdef AS column_name, tablename AS table_name
186
+ FROM pg_indexes
187
187
  WHERE tablename = '${tableName}' AND schemaname = '${schema}'`);
188
188
  result = result.filter((row) => row.index_name.includes('_pkey') || !row.column_name.includes('UNIQUE INDEX'));
189
189
  return result.map((row) => {
@@ -196,30 +196,30 @@ class BunPgDriver extends bun_driver_base_1.BunDriverBase {
196
196
  }
197
197
  async constraints(tableName, options) {
198
198
  const schema = (options && options.schema) || 'public';
199
- const result = await this.sql.unsafe(`SELECT
200
- conname AS index_name,
201
- pg_get_constraintdef(pg_constraint.oid) as consdef,
202
- CASE contype
203
- WHEN 'c' THEN 'CHECK'
204
- WHEN 'f' THEN 'FOREIGN KEY'
205
- WHEN 'p' THEN 'PRIMARY KEY'
206
- WHEN 'u' THEN 'UNIQUE'
207
- WHEN 't' THEN 'TRIGGER'
208
- WHEN 'x' THEN 'EXCLUSION'
209
- ELSE 'UNKNOWN'
210
- END AS constraint_type
211
- FROM pg_constraint
212
- where conrelid = (
213
- SELECT oid
214
- FROM pg_class
215
- WHERE relname = '${tableName}'
216
- AND relnamespace = (SELECT oid FROM pg_namespace WHERE nspname = '${schema}')
217
- )
218
- AND conkey @> ARRAY(
219
- SELECT attnum
220
- FROM pg_attribute
221
- WHERE attrelid = conrelid
222
- AND attname = '${schema}'
199
+ const result = await this.sql.unsafe(`SELECT
200
+ conname AS index_name,
201
+ pg_get_constraintdef(pg_constraint.oid) as consdef,
202
+ CASE contype
203
+ WHEN 'c' THEN 'CHECK'
204
+ WHEN 'f' THEN 'FOREIGN KEY'
205
+ WHEN 'p' THEN 'PRIMARY KEY'
206
+ WHEN 'u' THEN 'UNIQUE'
207
+ WHEN 't' THEN 'TRIGGER'
208
+ WHEN 'x' THEN 'EXCLUSION'
209
+ ELSE 'UNKNOWN'
210
+ END AS constraint_type
211
+ FROM pg_constraint
212
+ where conrelid = (
213
+ SELECT oid
214
+ FROM pg_class
215
+ WHERE relname = '${tableName}'
216
+ AND relnamespace = (SELECT oid FROM pg_namespace WHERE nspname = '${schema}')
217
+ )
218
+ AND conkey @> ARRAY(
219
+ SELECT attnum
220
+ FROM pg_attribute
221
+ WHERE attrelid = conrelid
222
+ AND attname = '${schema}'
223
223
  )`);
224
224
  return result.map((row) => {
225
225
  return {
@@ -230,10 +230,10 @@ class BunPgDriver extends bun_driver_base_1.BunDriverBase {
230
230
  });
231
231
  }
232
232
  async getEnums(tableName, schema) {
233
- const result = await this.sql.unsafe(`SELECT e.enumlabel as label, t.typname as type
234
- FROM pg_type t
235
- JOIN pg_enum e ON t.oid = e.enumtypid
236
- JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
233
+ const result = await this.sql.unsafe(`SELECT e.enumlabel as label, t.typname as type
234
+ FROM pg_type t
235
+ JOIN pg_enum e ON t.oid = e.enumtypid
236
+ JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
237
237
  WHERE n.nspname = '${schema}'`);
238
238
  return result.map((row) => {
239
239
  return {
package/dist/orm.js CHANGED
@@ -48,6 +48,9 @@ let Orm = Orm_1 = class Orm {
48
48
  if (!this.driverInstance) {
49
49
  throw new Error('Driver instance not initialized');
50
50
  }
51
+ if (transaction_context_1.transactionContext.hasContext()) {
52
+ return operation(transaction_context_1.transactionContext.getContext());
53
+ }
51
54
  return this.driverInstance.transaction(async (tx) => {
52
55
  return transaction_context_1.transactionContext.run(tx, () => operation(tx));
53
56
  });
@@ -41,6 +41,9 @@ var __importStar = (this && this.__importStar) || (function () {
41
41
  var __metadata = (this && this.__metadata) || function (k, v) {
42
42
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
43
43
  };
44
+ var __importDefault = (this && this.__importDefault) || function (mod) {
45
+ return (mod && mod.__esModule) ? mod : { "default": mod };
46
+ };
44
47
  Object.defineProperty(exports, "__esModule", { value: true });
45
48
  exports.OrmService = void 0;
46
49
  const core_1 = require("@cheetah.js/core");
@@ -49,6 +52,7 @@ const constants_1 = require("./constants");
49
52
  const ts_morph_1 = require("ts-morph");
50
53
  const orm_1 = require("./orm");
51
54
  const globby = __importStar(require("globby"));
55
+ const path_1 = __importDefault(require("path"));
52
56
  let OrmService = class OrmService {
53
57
  constructor(orm, storage, entityFile) {
54
58
  this.orm = orm;
@@ -315,19 +319,17 @@ let OrmService = class OrmService {
315
319
  }
316
320
  }
317
321
  getSourceFilePaths() {
318
- const projectRoot = process.cwd(); // Ajuste conforme a estrutura do seu projeto
319
- const getAllFiles = (dir) => {
320
- const patterns = [`${dir}/**/*.(ts|js)`, "!**/node_modules/**"];
321
- try {
322
- return globby.sync(patterns, { gitignore: true });
323
- }
324
- catch (error) {
325
- console.error('Erro ao obter arquivos:', error);
326
- return [];
327
- }
328
- };
329
- // Filtra os arquivos pelo padrão de nomenclatura
330
- return getAllFiles(projectRoot);
322
+ const projectRoot = process.cwd();
323
+ const patterns = ['**/*.ts', '**/*.js', '!**/node_modules/**'];
324
+ try {
325
+ return globby
326
+ .sync(patterns, { cwd: projectRoot, gitignore: true, absolute: false })
327
+ .map((filePath) => path_1.default.resolve(projectRoot, filePath));
328
+ }
329
+ catch (error) {
330
+ console.error('Erro ao obter arquivos:', error);
331
+ return [];
332
+ }
331
333
  }
332
334
  };
333
335
  exports.OrmService = OrmService;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cheetah.js/orm",
3
- "version": "0.1.132",
3
+ "version": "0.1.136",
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": "a8b11c0105b55a7b3a987246a059573ec877ab81"
58
+ "gitHead": "2109d17dd39ca357d6a0b48bf0b7bb08270fe8b6"
59
59
  }
package/build.d.ts DELETED
@@ -1 +0,0 @@
1
- export {};
@@ -1,3 +0,0 @@
1
- import { ConnectionSettings } from '@cheetah.js/orm/driver/driver.interface';
2
- declare const config: ConnectionSettings<any>;
3
- export default config;