-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathPostgresEntityDatabaseAdapter.ts
More file actions
373 lines (336 loc) · 13.2 KB
/
Copy pathPostgresEntityDatabaseAdapter.ts
File metadata and controls
373 lines (336 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
import type { EntityConfiguration, FieldTransformer, FieldTransformerMap } from '@expo/entity';
import { getDatabaseFieldForEntityField, RESERVED_ENTITY_COUNT_QUERY_ALIAS } from '@expo/entity';
import type { Knex } from 'knex';
import type {
TableFieldMultiValueEqualityCondition,
TableFieldSingleValueEqualityCondition,
TableQuerySelectionModifiers,
} from './BasePostgresEntityDatabaseAdapter.ts';
import {
BasePostgresEntityDatabaseAdapter,
NullsOrdering,
OrderByOrdering,
} from './BasePostgresEntityDatabaseAdapter.ts';
import { JSONArrayField, MaybeJSONArrayField } from './EntityFields.ts';
import type { PostgresEntityDatabaseAdapterConfiguration } from './PostgresEntityDatabaseAdapterProvider.ts';
import type { SQLFragment } from './SQLOperator.ts';
import { wrapNativePostgresCallAsync } from './errors/wrapNativePostgresCallAsync.ts';
export class PostgresEntityDatabaseAdapter<
TFields extends Record<string, any>,
TIDField extends keyof TFields,
> extends BasePostgresEntityDatabaseAdapter<TFields, TIDField> {
constructor(
entityConfiguration: EntityConfiguration<TFields, TIDField>,
private readonly adapterConfiguration: PostgresEntityDatabaseAdapterConfiguration = {},
) {
super(entityConfiguration);
}
override get paginationMaxPageSize(): number | undefined {
return this.adapterConfiguration.paginationMaxPageSize;
}
protected getFieldTransformerMap(): FieldTransformerMap {
return new Map<string, FieldTransformer<any>>([
[
JSONArrayField.name,
{
/**
* JSON array fields must be stringified before insertion using Knex.
* http://knexjs.org/#Schema-json
*/
write: (val: any[]) => JSON.stringify(val),
},
],
[
MaybeJSONArrayField.name,
{
/**
* JSON array fields must be stringified before insertion using Knex.
* For this field it is only an array some of the time.
* http://knexjs.org/#Schema-json
*/
write: (val: any[] | any) => (Array.isArray(val) ? JSON.stringify(val) : val),
},
],
]);
}
protected async fetchManyWhereInternalAsync(
queryInterface: Knex,
tableName: string,
tableColumns: readonly string[],
tableTuples: any[][],
): Promise<object[]> {
// For single column queries, use the ANY operator to derive a consistent
// query shape in the postgres query stats table.
// This produces a query of the form `SELECT * FROM table WHERE ("id") = ANY(?)`
// with value bindings of the form `[[1]]`, thus not making different value cardinalities
// produce different query shapes.
//
// But for multi-column queries, we must use the IN operator as the ANY operator
// does not support anonymous composite types. The solution to keep using the ANY operator would be explicit
// postgres type casting on each value in each tableTuple, thus creating a unique query shape and defeating the purpose.
// The same applies to using UNNEST on anonymous composite types.
// Note that this solution is not possible in entity though since we don't have the postgres column types and they
// can't be derived dynamically.
//
// Therefore, for multi-column quries, we use the IN operator which produces a query of the form
// `SELECT * FROM table WHERE ("id", "name") IN ((?, ?), (?, ?))` with value bindings of the form
// `[[1, 'a'], [2, 'b']]`, which will produce a unique query shape in the postgres query stats table for
// each value cardinality.
//
// We could use the IN operator for single column queries as well, but we prefer to use ANY to at least keep some
// consistency in the query shape for the stats table.
if (tableColumns.length === 1) {
return await wrapNativePostgresCallAsync(() =>
queryInterface
.select()
.from(tableName)
.whereRaw(`(??) = ANY(?)`, [
tableColumns[0],
tableTuples.map((tableTuple) => tableTuple[0]),
]),
);
}
return await wrapNativePostgresCallAsync(() =>
queryInterface.select().from(tableName).whereIn(tableColumns, tableTuples),
);
}
protected async fetchOneWhereInternalAsync(
queryInterface: Knex,
tableName: string,
tableColumns: readonly string[],
tableTuple: readonly any[],
): Promise<object | null> {
const results = await this.fetchManyByFieldEqualityConjunctionInternalAsync(
queryInterface,
tableName,
tableColumns.map((column, index) => ({
tableField: column,
tableValue: tableTuple[index],
})),
[],
{ limit: 1, orderBy: undefined, offset: undefined },
);
return results[0] ?? null;
}
private applyQueryModifiersToQuery(
query: Knex.QueryBuilder,
querySelectionModifiers: TableQuerySelectionModifiers<TFields>,
): Knex.QueryBuilder {
const { orderBy, offset, limit } = querySelectionModifiers;
let ret = query;
if (orderBy !== undefined) {
for (const orderBySpecification of orderBy) {
if ('columnName' in orderBySpecification) {
ret = ret.orderBy(
orderBySpecification.columnName,
orderBySpecification.order,
orderBySpecification.nulls,
);
} else {
const orderDirection =
orderBySpecification.order === OrderByOrdering.ASCENDING ? 'ASC' : 'DESC';
const nullsSuffix = orderBySpecification.nulls
? ` NULLS ${orderBySpecification.nulls === NullsOrdering.FIRST ? 'FIRST' : 'LAST'}`
: '';
ret = ret.orderByRaw(
`(${orderBySpecification.columnFragment.sql}) ${orderDirection}${nullsSuffix}`,
orderBySpecification.columnFragment.getKnexBindings((fieldName) =>
getDatabaseFieldForEntityField(this.entityConfiguration, fieldName),
),
);
}
}
}
if (offset !== undefined) {
ret = ret.offset(offset);
}
if (limit !== undefined) {
ret = ret.limit(limit);
}
return ret;
}
private applyFieldEqualityConjunctionWhereClause(
query: Knex.QueryBuilder,
tableFieldSingleValueEqualityOperands: TableFieldSingleValueEqualityCondition[],
tableFieldMultiValueEqualityOperands: TableFieldMultiValueEqualityCondition[],
): Knex.QueryBuilder {
let result = query;
if (tableFieldSingleValueEqualityOperands.length > 0) {
const whereObject: { [key: string]: any } = {};
const nonNullTableFieldSingleValueEqualityOperands =
tableFieldSingleValueEqualityOperands.filter(({ tableValue }) => tableValue !== null);
const nullTableFieldSingleValueEqualityOperands =
tableFieldSingleValueEqualityOperands.filter(({ tableValue }) => tableValue === null);
if (nonNullTableFieldSingleValueEqualityOperands.length > 0) {
for (const { tableField, tableValue } of nonNullTableFieldSingleValueEqualityOperands) {
whereObject[tableField] = tableValue;
}
result = result.where(whereObject);
}
if (nullTableFieldSingleValueEqualityOperands.length > 0) {
for (const { tableField } of nullTableFieldSingleValueEqualityOperands) {
result = result.whereNull(tableField);
}
}
}
if (tableFieldMultiValueEqualityOperands.length > 0) {
for (const { tableField, tableValues } of tableFieldMultiValueEqualityOperands) {
const nonNullTableValues = tableValues.filter((tableValue) => tableValue !== null);
result = result.where((builder) => {
builder.whereRaw('?? = ANY(?)', [tableField, [...nonNullTableValues]]);
// there was at least one null, allow null in this equality clause
if (nonNullTableValues.length !== tableValues.length) {
builder.orWhereNull(tableField);
}
});
}
}
return result;
}
protected async fetchManyByFieldEqualityConjunctionInternalAsync(
queryInterface: Knex,
tableName: string,
tableFieldSingleValueEqualityOperands: TableFieldSingleValueEqualityCondition[],
tableFieldMultiValueEqualityOperands: TableFieldMultiValueEqualityCondition[],
querySelectionModifiers: TableQuerySelectionModifiers<TFields>,
): Promise<object[]> {
let query = this.applyFieldEqualityConjunctionWhereClause(
queryInterface.select().from(tableName),
tableFieldSingleValueEqualityOperands,
tableFieldMultiValueEqualityOperands,
);
query = this.applyQueryModifiersToQuery(query, querySelectionModifiers);
return await wrapNativePostgresCallAsync(() => query);
}
private applySQLFragmentWhereClause(
query: Knex.QueryBuilder,
sqlFragment: SQLFragment<TFields>,
): Knex.QueryBuilder {
return query.whereRaw(
sqlFragment.sql,
sqlFragment.getKnexBindings((fieldName) =>
getDatabaseFieldForEntityField(this.entityConfiguration, fieldName),
),
);
}
protected async fetchManyBySQLFragmentInternalAsync(
queryInterface: Knex,
tableName: string,
sqlFragment: SQLFragment<TFields>,
querySelectionModifiers: TableQuerySelectionModifiers<TFields>,
): Promise<object[]> {
let query = this.applySQLFragmentWhereClause(
queryInterface.select().from(tableName),
sqlFragment,
);
query = this.applyQueryModifiersToQuery(query, querySelectionModifiers);
return await wrapNativePostgresCallAsync(() => query);
}
protected async countByFieldEqualityConjunctionInternalAsync(
queryInterface: Knex,
tableName: string,
tableFieldSingleValueEqualityOperands: TableFieldSingleValueEqualityCondition[],
tableFieldMultiValueEqualityOperands: TableFieldMultiValueEqualityCondition[],
): Promise<number> {
const query = this.applyFieldEqualityConjunctionWhereClause(
queryInterface.count('*', { as: RESERVED_ENTITY_COUNT_QUERY_ALIAS }).from(tableName),
tableFieldSingleValueEqualityOperands,
tableFieldMultiValueEqualityOperands,
);
const result = await wrapNativePostgresCallAsync(() => query);
return parseInt(String(result[0][RESERVED_ENTITY_COUNT_QUERY_ALIAS]), 10);
}
protected async countBySQLFragmentInternalAsync(
queryInterface: Knex,
tableName: string,
sqlFragment: SQLFragment<TFields>,
): Promise<number> {
const query = this.applySQLFragmentWhereClause(
queryInterface.count('*', { as: RESERVED_ENTITY_COUNT_QUERY_ALIAS }).from(tableName),
sqlFragment,
);
const result = await wrapNativePostgresCallAsync(() => query);
return parseInt(String(result[0][RESERVED_ENTITY_COUNT_QUERY_ALIAS]), 10);
}
protected async insertManyInternalAsync(
queryInterface: Knex,
tableName: string,
objects: readonly object[],
): Promise<object[]> {
return await wrapNativePostgresCallAsync(() =>
queryInterface
.insert([...objects])
.into(tableName)
.returning('*'),
);
}
protected async updateManyInternalAsync(
queryInterface: Knex,
tableName: string,
tableIdField: string,
items: readonly { id: any; object: object }[],
): Promise<readonly { updatedRowCount: number }[]> {
if (items.length === 0) {
return [];
}
if (items.length === 1) {
const item = items[0]!;
const updatedRowCount = await wrapNativePostgresCallAsync(() =>
queryInterface.update(item.object).into(tableName).where(tableIdField, item.id),
);
return [{ updatedRowCount }];
}
// Bulk update using UPDATE ... FROM (VALUES ...) for same-column-set items.
// All items are guaranteed to have the same set of columns.
const columns = Object.keys(items[0]!.object);
const allColumns = [tableIdField, ...columns];
const valuePlaceholders = items
.map(() => `(${allColumns.map(() => '?').join(', ')})`)
.join(', ');
const bindings: any[] = items.flatMap((item) => [
item.id,
...columns.map((col) => (item.object as Record<string, any>)[col]),
]);
const setClause = columns.map(() => `?? = ??`).join(', ');
const setBindings = columns.flatMap((col) => [col, `_data_table_.${col}`]);
const columnList = allColumns.map(() => '??').join(', ');
const columnBindings = allColumns;
const sql = [
`UPDATE ?? SET ${setClause}`,
`FROM (VALUES ${valuePlaceholders}) AS "_data_table_"(${columnList})`,
`WHERE ?? = ??`,
`RETURNING ??`,
].join(' ');
const allBindings = [
tableName,
...setBindings,
...bindings,
...columnBindings,
`${tableName}.${tableIdField}`,
`_data_table_.${tableIdField}`,
`${tableName}.${tableIdField}`,
];
const result = await wrapNativePostgresCallAsync(() => queryInterface.raw(sql, allBindings));
const updatedIdCounts = new Map<any, number>();
for (const row of result.rows as Record<string, any>[]) {
const id = row[tableIdField];
updatedIdCounts.set(id, (updatedIdCounts.get(id) ?? 0) + 1);
}
return items.map((item) => ({
updatedRowCount: updatedIdCounts.get(item.id) ?? 0,
}));
}
protected async deleteManyInternalAsync(
queryInterface: Knex,
tableName: string,
tableIdField: string,
ids: readonly any[],
): Promise<number> {
return await wrapNativePostgresCallAsync(() =>
queryInterface
.from(tableName)
.whereIn(tableIdField, [...ids])
.del(),
);
}
}