@onivoro/server-typeorm-postgres 22.0.4 → 22.0.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.
@@ -56,7 +56,7 @@ let RedshiftRepository = class RedshiftRepository extends type_orm_repository_cl
56
56
  setExpressions.push(`${this.columns[key].databasePath} = ${this.mapPlaceholderExpression(length, index, key)}`);
57
57
  setParams.push(value);
58
58
  });
59
- let query = `UPDATE "${this.table}" SET ${setExpressions.join(', ')} ${whereClause}`;
59
+ let query = `UPDATE ${this.getTableNameExpression()} SET ${setExpressions.join(', ')} ${whereClause}`;
60
60
  await this.query(query, [...queryParams, ...setParams]);
61
61
  }
62
62
  async postOne(entity) {
@@ -8,6 +8,7 @@ export declare class TypeOrmRepository<TEntity> implements IEntityProvider<TEnti
8
8
  entityManager: EntityManager;
9
9
  protected columns: TKeysOf<TEntity, TTableMeta>;
10
10
  protected table: string;
11
+ protected schema: string;
11
12
  debug: boolean;
12
13
  constructor(entityType: any, entityManager: EntityManager);
13
14
  forTransaction(entityManager: EntityManager): TypeOrmRepository<TEntity>;
@@ -23,6 +24,8 @@ export declare class TypeOrmRepository<TEntity> implements IEntityProvider<TEnti
23
24
  get repo(): import("typeorm").Repository<import("typeorm").ObjectLiteral>;
24
25
  protected insertAndReturn(entityToInsert: TEntity): Promise<TEntity>;
25
26
  protected insertAndReturnMany(entitiesToInsert: TEntity[]): Promise<TEntity[]>;
27
+ protected getSchemaPrefix(): string;
28
+ protected getTableNameExpression(): string;
26
29
  protected buildSelectStatement(options: FindManyOptions<TEntity>): {
27
30
  query: string;
28
31
  queryParams: any[];
@@ -6,12 +6,14 @@ class TypeOrmRepository {
6
6
  entityManager;
7
7
  columns = {};
8
8
  table;
9
+ schema;
9
10
  debug = false;
10
11
  constructor(entityType, entityManager) {
11
12
  this.entityType = entityType;
12
13
  this.entityManager = entityManager;
13
- const { tableName } = this.repo.metadata;
14
+ const { tableName, schema } = this.repo.metadata;
14
15
  this.table = tableName;
16
+ this.schema = schema;
15
17
  this.repo.metadata.columns.forEach((_) => {
16
18
  const { databasePath, propertyPath, type, isPrimary } = _;
17
19
  this.columns[propertyPath] = { databasePath, type, propertyPath, isPrimary, default: _.default };
@@ -67,14 +69,21 @@ class TypeOrmRepository {
67
69
  const insertedEntity = insertionResult.generatedMaps;
68
70
  return insertedEntity;
69
71
  }
72
+ getSchemaPrefix() {
73
+ return this.schema ? `"${this.schema}".` : '';
74
+ }
75
+ getTableNameExpression() {
76
+ const schemaPrefix = this.getSchemaPrefix();
77
+ return `${schemaPrefix}"${this.table}"`;
78
+ }
70
79
  buildSelectStatement(options) {
71
80
  const { whereClause, queryParams } = this.buildWhereExpression(options.where);
72
- const query = `SELECT * FROM "${this.table}"${whereClause};`;
81
+ const query = `SELECT * FROM ${this.getTableNameExpression()}${whereClause};`;
73
82
  return { query, queryParams };
74
83
  }
75
84
  buildDeleteStatement(where) {
76
85
  const { whereClause, queryParams } = this.buildWhereExpression(where);
77
- const query = `DELETE FROM "${this.table}"${whereClause};`;
86
+ const query = `DELETE FROM ${this.getTableNameExpression()}${whereClause};`;
78
87
  return { query, queryParams };
79
88
  }
80
89
  buildWhereExpression(where) {
@@ -97,7 +106,7 @@ class TypeOrmRepository {
97
106
  const values = Object.values(entity);
98
107
  const columnNames = keys.map(key => this.columns[key].databasePath).join(', ');
99
108
  const paramPlaceholders = keys.map((key, index) => this.mapPlaceholderExpression(0, index, key)).join(', ');
100
- const insertQuery = `INSERT INTO "${this.table}" (${columnNames}) VALUES (${paramPlaceholders})`;
109
+ const insertQuery = `INSERT INTO ${this.getTableNameExpression()} (${columnNames}) VALUES (${paramPlaceholders})`;
101
110
  return { insertQuery, values };
102
111
  }
103
112
  buildInsertManyQuery(entities) {
@@ -119,7 +128,7 @@ class TypeOrmRepository {
119
128
  const paramPlaceholders = Object.keys(keyMap).map((_, index) => this.mapPlaceholderExpression(length, index, _)).join(', ');
120
129
  valuesExpressions.push(`(${paramPlaceholders})`);
121
130
  });
122
- const insertQuery = `INSERT INTO "${this.table}" (${columnNames}) VALUES ${valuesExpressions.join(', ')}`;
131
+ const insertQuery = `INSERT INTO ${this.getTableNameExpression()} (${columnNames}) VALUES ${valuesExpressions.join(', ')}`;
123
132
  return { insertQuery, values };
124
133
  }
125
134
  mapPlaceholderExpression(length, index, column) {
@@ -143,7 +152,7 @@ class TypeOrmRepository {
143
152
  values.push((typeof entity[key] === 'undefined') ? this.columns[key].default : entity[key]);
144
153
  });
145
154
  const whereExpression = Object.keys(keyMap).map((_, index) => `(${this.columns[_].databasePath} = $${length + index + 1})`).join(' AND ');
146
- selectExpressions.push(`(select * from "${this.table}" where (${whereExpression}))`);
155
+ selectExpressions.push(`(select * from ${this.getTableNameExpression()} where (${whereExpression}))`);
147
156
  });
148
157
  const selectQuery = selectExpressions.join(' UNION ');
149
158
  return { selectQuery, values };
@@ -158,11 +167,11 @@ class TypeOrmRepository {
158
167
  }
159
168
  async query(query, parameters) {
160
169
  if (this.debug) {
161
- console.log({ table: this.table, query, parameters });
170
+ console.log({ schema: this.schema, table: this.table, query, parameters });
162
171
  }
163
172
  const result = await this.repo.query(query, parameters);
164
173
  if (this.debug) {
165
- console.log({ table: this.table, query, parameters, result });
174
+ console.log({ schema: this.schema, table: this.table, query, parameters, result });
166
175
  }
167
176
  return result;
168
177
  }
@@ -56,7 +56,7 @@ let RedshiftRepository = class RedshiftRepository extends type_orm_repository_cl
56
56
  setExpressions.push(`${this.columns[key].databasePath} = ${this.mapPlaceholderExpression(length, index, key)}`);
57
57
  setParams.push(value);
58
58
  });
59
- let query = `UPDATE "${this.table}" SET ${setExpressions.join(', ')} ${whereClause}`;
59
+ let query = `UPDATE ${this.getTableNameExpression()} SET ${setExpressions.join(', ')} ${whereClause}`;
60
60
  await this.query(query, [...queryParams, ...setParams]);
61
61
  }
62
62
  async postOne(entity) {
@@ -8,6 +8,7 @@ export declare class TypeOrmRepository<TEntity> implements IEntityProvider<TEnti
8
8
  entityManager: EntityManager;
9
9
  protected columns: TKeysOf<TEntity, TTableMeta>;
10
10
  protected table: string;
11
+ protected schema: string;
11
12
  debug: boolean;
12
13
  constructor(entityType: any, entityManager: EntityManager);
13
14
  forTransaction(entityManager: EntityManager): TypeOrmRepository<TEntity>;
@@ -23,6 +24,8 @@ export declare class TypeOrmRepository<TEntity> implements IEntityProvider<TEnti
23
24
  get repo(): import("typeorm").Repository<import("typeorm").ObjectLiteral>;
24
25
  protected insertAndReturn(entityToInsert: TEntity): Promise<TEntity>;
25
26
  protected insertAndReturnMany(entitiesToInsert: TEntity[]): Promise<TEntity[]>;
27
+ protected getSchemaPrefix(): string;
28
+ protected getTableNameExpression(): string;
26
29
  protected buildSelectStatement(options: FindManyOptions<TEntity>): {
27
30
  query: string;
28
31
  queryParams: any[];
@@ -6,12 +6,14 @@ class TypeOrmRepository {
6
6
  entityManager;
7
7
  columns = {};
8
8
  table;
9
+ schema;
9
10
  debug = false;
10
11
  constructor(entityType, entityManager) {
11
12
  this.entityType = entityType;
12
13
  this.entityManager = entityManager;
13
- const { tableName } = this.repo.metadata;
14
+ const { tableName, schema } = this.repo.metadata;
14
15
  this.table = tableName;
16
+ this.schema = schema;
15
17
  this.repo.metadata.columns.forEach((_) => {
16
18
  const { databasePath, propertyPath, type, isPrimary } = _;
17
19
  this.columns[propertyPath] = { databasePath, type, propertyPath, isPrimary, default: _.default };
@@ -67,14 +69,21 @@ class TypeOrmRepository {
67
69
  const insertedEntity = insertionResult.generatedMaps;
68
70
  return insertedEntity;
69
71
  }
72
+ getSchemaPrefix() {
73
+ return this.schema ? `"${this.schema}".` : '';
74
+ }
75
+ getTableNameExpression() {
76
+ const schemaPrefix = this.getSchemaPrefix();
77
+ return `${schemaPrefix}"${this.table}"`;
78
+ }
70
79
  buildSelectStatement(options) {
71
80
  const { whereClause, queryParams } = this.buildWhereExpression(options.where);
72
- const query = `SELECT * FROM "${this.table}"${whereClause};`;
81
+ const query = `SELECT * FROM ${this.getTableNameExpression()}${whereClause};`;
73
82
  return { query, queryParams };
74
83
  }
75
84
  buildDeleteStatement(where) {
76
85
  const { whereClause, queryParams } = this.buildWhereExpression(where);
77
- const query = `DELETE FROM "${this.table}"${whereClause};`;
86
+ const query = `DELETE FROM ${this.getTableNameExpression()}${whereClause};`;
78
87
  return { query, queryParams };
79
88
  }
80
89
  buildWhereExpression(where) {
@@ -97,7 +106,7 @@ class TypeOrmRepository {
97
106
  const values = Object.values(entity);
98
107
  const columnNames = keys.map(key => this.columns[key].databasePath).join(', ');
99
108
  const paramPlaceholders = keys.map((key, index) => this.mapPlaceholderExpression(0, index, key)).join(', ');
100
- const insertQuery = `INSERT INTO "${this.table}" (${columnNames}) VALUES (${paramPlaceholders})`;
109
+ const insertQuery = `INSERT INTO ${this.getTableNameExpression()} (${columnNames}) VALUES (${paramPlaceholders})`;
101
110
  return { insertQuery, values };
102
111
  }
103
112
  buildInsertManyQuery(entities) {
@@ -119,7 +128,7 @@ class TypeOrmRepository {
119
128
  const paramPlaceholders = Object.keys(keyMap).map((_, index) => this.mapPlaceholderExpression(length, index, _)).join(', ');
120
129
  valuesExpressions.push(`(${paramPlaceholders})`);
121
130
  });
122
- const insertQuery = `INSERT INTO "${this.table}" (${columnNames}) VALUES ${valuesExpressions.join(', ')}`;
131
+ const insertQuery = `INSERT INTO ${this.getTableNameExpression()} (${columnNames}) VALUES ${valuesExpressions.join(', ')}`;
123
132
  return { insertQuery, values };
124
133
  }
125
134
  mapPlaceholderExpression(length, index, column) {
@@ -143,7 +152,7 @@ class TypeOrmRepository {
143
152
  values.push((typeof entity[key] === 'undefined') ? this.columns[key].default : entity[key]);
144
153
  });
145
154
  const whereExpression = Object.keys(keyMap).map((_, index) => `(${this.columns[_].databasePath} = $${length + index + 1})`).join(' AND ');
146
- selectExpressions.push(`(select * from "${this.table}" where (${whereExpression}))`);
155
+ selectExpressions.push(`(select * from ${this.getTableNameExpression()} where (${whereExpression}))`);
147
156
  });
148
157
  const selectQuery = selectExpressions.join(' UNION ');
149
158
  return { selectQuery, values };
@@ -158,11 +167,11 @@ class TypeOrmRepository {
158
167
  }
159
168
  async query(query, parameters) {
160
169
  if (this.debug) {
161
- console.log({ table: this.table, query, parameters });
170
+ console.log({ schema: this.schema, table: this.table, query, parameters });
162
171
  }
163
172
  const result = await this.repo.query(query, parameters);
164
173
  if (this.debug) {
165
- console.log({ table: this.table, query, parameters, result });
174
+ console.log({ schema: this.schema, table: this.table, query, parameters, result });
166
175
  }
167
176
  return result;
168
177
  }
@@ -8,6 +8,7 @@ export declare class TypeOrmRepository<TEntity> implements IEntityProvider<TEnti
8
8
  entityManager: EntityManager;
9
9
  protected columns: TKeysOf<TEntity, TTableMeta>;
10
10
  protected table: string;
11
+ protected schema: string;
11
12
  debug: boolean;
12
13
  constructor(entityType: any, entityManager: EntityManager);
13
14
  forTransaction(entityManager: EntityManager): TypeOrmRepository<TEntity>;
@@ -23,6 +24,8 @@ export declare class TypeOrmRepository<TEntity> implements IEntityProvider<TEnti
23
24
  get repo(): import("typeorm").Repository<import("typeorm").ObjectLiteral>;
24
25
  protected insertAndReturn(entityToInsert: TEntity): Promise<TEntity>;
25
26
  protected insertAndReturnMany(entitiesToInsert: TEntity[]): Promise<TEntity[]>;
27
+ protected getSchemaPrefix(): string;
28
+ protected getTableNameExpression(): string;
26
29
  protected buildSelectStatement(options: FindManyOptions<TEntity>): {
27
30
  query: string;
28
31
  queryParams: any[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onivoro/server-typeorm-postgres",
3
- "version": "22.0.4",
3
+ "version": "22.0.5",
4
4
  "repository": {
5
5
  "url": "git+https://github.com/onivoro/server-typeorm-postgres.git"
6
6
  },