@onivoro/server-typeorm-postgres 22.0.4 → 22.0.6

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) {
@@ -82,11 +91,14 @@ class TypeOrmRepository {
82
91
  let whereClause = '';
83
92
  Object.entries(where || {}).forEach(([propertyPath, value], index) => {
84
93
  const key = this.columns[propertyPath].databasePath;
94
+ const where = Array.isArray(value.value)
95
+ ? `${key} in $${index + 1}`
96
+ : `${key} = $${index + 1}`;
85
97
  if (index === 0) {
86
- whereClause += ` WHERE ${key} = $${index + 1}`;
98
+ whereClause += ` WHERE ${where}`;
87
99
  }
88
100
  else {
89
- whereClause += ` AND ${key} = $${index + 1}`;
101
+ whereClause += ` AND ${where}`;
90
102
  }
91
103
  queryParams.push(value);
92
104
  });
@@ -97,7 +109,7 @@ class TypeOrmRepository {
97
109
  const values = Object.values(entity);
98
110
  const columnNames = keys.map(key => this.columns[key].databasePath).join(', ');
99
111
  const paramPlaceholders = keys.map((key, index) => this.mapPlaceholderExpression(0, index, key)).join(', ');
100
- const insertQuery = `INSERT INTO "${this.table}" (${columnNames}) VALUES (${paramPlaceholders})`;
112
+ const insertQuery = `INSERT INTO ${this.getTableNameExpression()} (${columnNames}) VALUES (${paramPlaceholders})`;
101
113
  return { insertQuery, values };
102
114
  }
103
115
  buildInsertManyQuery(entities) {
@@ -119,7 +131,7 @@ class TypeOrmRepository {
119
131
  const paramPlaceholders = Object.keys(keyMap).map((_, index) => this.mapPlaceholderExpression(length, index, _)).join(', ');
120
132
  valuesExpressions.push(`(${paramPlaceholders})`);
121
133
  });
122
- const insertQuery = `INSERT INTO "${this.table}" (${columnNames}) VALUES ${valuesExpressions.join(', ')}`;
134
+ const insertQuery = `INSERT INTO ${this.getTableNameExpression()} (${columnNames}) VALUES ${valuesExpressions.join(', ')}`;
123
135
  return { insertQuery, values };
124
136
  }
125
137
  mapPlaceholderExpression(length, index, column) {
@@ -143,7 +155,7 @@ class TypeOrmRepository {
143
155
  values.push((typeof entity[key] === 'undefined') ? this.columns[key].default : entity[key]);
144
156
  });
145
157
  const whereExpression = Object.keys(keyMap).map((_, index) => `(${this.columns[_].databasePath} = $${length + index + 1})`).join(' AND ');
146
- selectExpressions.push(`(select * from "${this.table}" where (${whereExpression}))`);
158
+ selectExpressions.push(`(select * from ${this.getTableNameExpression()} where (${whereExpression}))`);
147
159
  });
148
160
  const selectQuery = selectExpressions.join(' UNION ');
149
161
  return { selectQuery, values };
@@ -158,11 +170,11 @@ class TypeOrmRepository {
158
170
  }
159
171
  async query(query, parameters) {
160
172
  if (this.debug) {
161
- console.log({ table: this.table, query, parameters });
173
+ console.log({ schema: this.schema, table: this.table, query, parameters });
162
174
  }
163
175
  const result = await this.repo.query(query, parameters);
164
176
  if (this.debug) {
165
- console.log({ table: this.table, query, parameters, result });
177
+ console.log({ schema: this.schema, table: this.table, query, parameters, result });
166
178
  }
167
179
  return result;
168
180
  }
@@ -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) {
@@ -82,11 +91,14 @@ class TypeOrmRepository {
82
91
  let whereClause = '';
83
92
  Object.entries(where || {}).forEach(([propertyPath, value], index) => {
84
93
  const key = this.columns[propertyPath].databasePath;
94
+ const where = Array.isArray(value.value)
95
+ ? `${key} in $${index + 1}`
96
+ : `${key} = $${index + 1}`;
85
97
  if (index === 0) {
86
- whereClause += ` WHERE ${key} = $${index + 1}`;
98
+ whereClause += ` WHERE ${where}`;
87
99
  }
88
100
  else {
89
- whereClause += ` AND ${key} = $${index + 1}`;
101
+ whereClause += ` AND ${where}`;
90
102
  }
91
103
  queryParams.push(value);
92
104
  });
@@ -97,7 +109,7 @@ class TypeOrmRepository {
97
109
  const values = Object.values(entity);
98
110
  const columnNames = keys.map(key => this.columns[key].databasePath).join(', ');
99
111
  const paramPlaceholders = keys.map((key, index) => this.mapPlaceholderExpression(0, index, key)).join(', ');
100
- const insertQuery = `INSERT INTO "${this.table}" (${columnNames}) VALUES (${paramPlaceholders})`;
112
+ const insertQuery = `INSERT INTO ${this.getTableNameExpression()} (${columnNames}) VALUES (${paramPlaceholders})`;
101
113
  return { insertQuery, values };
102
114
  }
103
115
  buildInsertManyQuery(entities) {
@@ -119,7 +131,7 @@ class TypeOrmRepository {
119
131
  const paramPlaceholders = Object.keys(keyMap).map((_, index) => this.mapPlaceholderExpression(length, index, _)).join(', ');
120
132
  valuesExpressions.push(`(${paramPlaceholders})`);
121
133
  });
122
- const insertQuery = `INSERT INTO "${this.table}" (${columnNames}) VALUES ${valuesExpressions.join(', ')}`;
134
+ const insertQuery = `INSERT INTO ${this.getTableNameExpression()} (${columnNames}) VALUES ${valuesExpressions.join(', ')}`;
123
135
  return { insertQuery, values };
124
136
  }
125
137
  mapPlaceholderExpression(length, index, column) {
@@ -143,7 +155,7 @@ class TypeOrmRepository {
143
155
  values.push((typeof entity[key] === 'undefined') ? this.columns[key].default : entity[key]);
144
156
  });
145
157
  const whereExpression = Object.keys(keyMap).map((_, index) => `(${this.columns[_].databasePath} = $${length + index + 1})`).join(' AND ');
146
- selectExpressions.push(`(select * from "${this.table}" where (${whereExpression}))`);
158
+ selectExpressions.push(`(select * from ${this.getTableNameExpression()} where (${whereExpression}))`);
147
159
  });
148
160
  const selectQuery = selectExpressions.join(' UNION ');
149
161
  return { selectQuery, values };
@@ -158,11 +170,11 @@ class TypeOrmRepository {
158
170
  }
159
171
  async query(query, parameters) {
160
172
  if (this.debug) {
161
- console.log({ table: this.table, query, parameters });
173
+ console.log({ schema: this.schema, table: this.table, query, parameters });
162
174
  }
163
175
  const result = await this.repo.query(query, parameters);
164
176
  if (this.debug) {
165
- console.log({ table: this.table, query, parameters, result });
177
+ console.log({ schema: this.schema, table: this.table, query, parameters, result });
166
178
  }
167
179
  return result;
168
180
  }
@@ -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.6",
4
4
  "repository": {
5
5
  "url": "git+https://github.com/onivoro/server-typeorm-postgres.git"
6
6
  },