@cheetah.js/orm 0.1.54 → 0.1.56

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.
@@ -30,6 +30,7 @@ export declare class SqlBuilder<T> {
30
30
  limit(limit: number | undefined): SqlBuilder<T>;
31
31
  offset(offset: number | undefined): SqlBuilder<T>;
32
32
  load(load: string[]): SqlBuilder<T>;
33
+ count(): SqlBuilder<T>;
33
34
  private getPrimaryKeyColumnName;
34
35
  execute(): Promise<{
35
36
  query: any;
@@ -41,6 +42,7 @@ export declare class SqlBuilder<T> {
41
42
  executeAndReturnFirst(): Promise<T | undefined>;
42
43
  executeAndReturnFirstOrFail(): Promise<T>;
43
44
  executeAndReturnAll(): Promise<T[]>;
45
+ executeCount(): Promise<number>;
44
46
  private logExecution;
45
47
  inTransaction<T>(callback: (builder: SqlBuilder<T>) => Promise<T>): Promise<T>;
46
48
  private objectToStringMap;
@@ -110,6 +110,13 @@ class SqlBuilder {
110
110
  }
111
111
  return this;
112
112
  }
113
+ count() {
114
+ const { tableName, schema } = this.getTableName();
115
+ this.statements.statement = 'count';
116
+ this.statements.alias = this.getAlias(tableName);
117
+ this.statements.table = `"${schema}"."${tableName}"`;
118
+ return this;
119
+ }
113
120
  getPrimaryKeyColumnName(entity) {
114
121
  // Lógica para obter o nome da coluna de chave primária da entidade
115
122
  // Aqui você pode substituir por sua própria lógica, dependendo da estrutura do seu projeto
@@ -189,6 +196,13 @@ class SqlBuilder {
189
196
  }
190
197
  return results;
191
198
  }
199
+ async executeCount() {
200
+ const result = await this.execute();
201
+ if (result.query.rows.length === 0) {
202
+ return 0;
203
+ }
204
+ return parseInt(result.query.rows[0].count);
205
+ }
192
206
  logExecution(result) {
193
207
  this.logger.debug(`SQL: ${result.sql} [${Date.now() - result.startTime}ms]`);
194
208
  }
@@ -145,6 +145,8 @@ class BunDriverBase {
145
145
  return this.buildInsertSql(table, values, columns, alias);
146
146
  case 'update':
147
147
  return this.buildUpdateSql(table, values, alias);
148
+ case 'count':
149
+ return `SELECT COUNT(*) as count FROM ${table} ${alias}`;
148
150
  default:
149
151
  return '';
150
152
  }
@@ -104,7 +104,7 @@ export type JoinStatement<T> = {
104
104
  }[];
105
105
  };
106
106
  export type Statement<T> = {
107
- statement?: 'select' | 'insert' | 'update' | 'delete';
107
+ statement?: 'select' | 'insert' | 'update' | 'delete' | 'count';
108
108
  table?: string;
109
109
  alias?: string;
110
110
  customSchema?: string;
@@ -54,7 +54,6 @@ let OrmService = class OrmService {
54
54
  this.orm = orm;
55
55
  this.storage = storage;
56
56
  this.allEntities = new Map();
57
- console.log('Preparing entities...');
58
57
  const files = new ts_morph_1.Project({ skipLoadingLibFiles: true }).addSourceFilesAtPaths(entityFile ?? this.getSourceFilePaths());
59
58
  files.forEach(file => {
60
59
  file.getClasses().forEach(classDeclaration => {
@@ -143,7 +142,6 @@ let OrmService = class OrmService {
143
142
  }
144
143
  this.storage.add(entity, properties, relationship, hooks);
145
144
  }
146
- console.log('Entities prepared!');
147
145
  }
148
146
  getSourceFilePaths() {
149
147
  const projectRoot = process.cwd(); // Ajuste conforme a estrutura do seu projeto
@@ -140,8 +140,10 @@ class Repository {
140
140
  * Counts entities matching the criteria.
141
141
  */
142
142
  async count(where) {
143
- const results = await this.find({ where: where || {} });
144
- return results.length;
143
+ return this.createQueryBuilder()
144
+ .count()
145
+ .where(where || {})
146
+ .executeCount();
145
147
  }
146
148
  /**
147
149
  * Checks if any entity matches the criteria.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cheetah.js/orm",
3
- "version": "0.1.54",
3
+ "version": "0.1.56",
4
4
  "description": "A simple ORM for Cheetah.js",
5
5
  "type": "commonjs",
6
6
  "main": "dist/index.js",
@@ -50,5 +50,5 @@
50
50
  "bun",
51
51
  "value-object"
52
52
  ],
53
- "gitHead": "8292ae2a6631bfbdef48269ebbf6f3760150aeaa"
53
+ "gitHead": "75fdfd69a9ffc238a6057a11c55fc33b4bdfb5fe"
54
54
  }