-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgeneric.ts
More file actions
392 lines (333 loc) · 11.6 KB
/
generic.ts
File metadata and controls
392 lines (333 loc) · 11.6 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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
import { sql, eq, asc, desc, is, getTableColumns } from 'drizzle-orm';
import { SQL, Table, TableConfig, Column, ColumnBaseConfig, ColumnDataType } from 'drizzle-orm';
import { PgColumn, PgTableWithColumns } from 'drizzle-orm/pg-core';
import { PostgresJsDatabase } from 'drizzle-orm/postgres-js';
import { EventEmitter } from 'events';
import Err from '@openaddresses/batch-error';
import {
type InferSelectModel,
type InferInsertModel
} from 'drizzle-orm';
import Pool from './lib/pool.js';
export * from './lib/postgis.js'
export * from './lib/jsonb.js'
export function Param<T>(param?: T): T | null {
if (param === undefined) {
return null;
} else {
return param;
}
}
export interface GenericList<T> {
total: number;
items: Array<T>
}
export enum GenerateUpsert {
DO_NOTHING = 'DoNothing',
UPDATE = 'Update'
}
export enum GenericListOrder {
ASC = 'asc',
DESC = 'desc'
}
export type GenericListInput = {
limit?: number;
page?: number;
order?: GenericListOrder;
sort?: string;
where?: SQL<unknown>;
}
export type GenericIterInput = {
pagesize?: number;
order?: GenericListOrder;
where?: SQL<unknown>;
}
export type GenericCountInput = {
order?: GenericListOrder;
where?: SQL<unknown>;
}
export type GenericStreamInput = {
where?: SQL<unknown>;
}
type GenerateOptions = {
upsert?: GenerateUpsert,
upsertTarget?: PgColumn | Array<PgColumn>
};
type GenericTable = Table<TableConfig<Column<ColumnBaseConfig<ColumnDataType, string>, object, object>>>
& { enableRLS: () => Omit<PgTableWithColumns<any>, "enableRLS">; };
export class GenericEmitter<T extends GenericTable> extends EventEmitter {
pool: PostgresJsDatabase<any>;
generic: PgTableWithColumns<any>;
query: GenericStreamInput;
constructor(
pool: PostgresJsDatabase<any>,
generic: T,
query: GenericStreamInput
) {
super();
this.pool = pool;
this.generic = generic;
this.query = query;
}
async start() {
try {
const count = await this.pool.select({
count: sql<string>`count(*) OVER()`.as('count')
}).from(this.generic)
.where(this.query.where)
if (!count.length) {
this.emit('count', 0)
this.emit('end');
return;
}
this.emit('count', parseInt(count[0].count));
let it = 0;
let pgres: any = [];
do {
pgres = await this.pool.select()
.from(this.generic)
.where(this.query.where)
.limit(100)
.offset(100 * it)
++it;
for (const row of pgres) {
this.emit('data', row);
}
} while(pgres.length);
this.emit('end');
} catch (err) {
this.emit('error', err);
}
}
}
export default class Drizzle<T extends GenericTable> {
pool: PostgresJsDatabase<any>;
generic: PgTableWithColumns<any>;
constructor(
pool: PostgresJsDatabase<any>,
generic: T
) {
this.pool = pool;
this.generic = generic;
}
requiredPrimaryKey(): PgColumn {
const primaryKey = this.primaryKey();
if (!primaryKey) throw new Err(500, null, `Cannot access ${this.generic.name} without primaryKey`);
return primaryKey;
}
primaryKey(): PgColumn | null {
let primaryKey;
for (const key in this.generic) {
if (this.generic[key].primary) primaryKey = this.generic[key];
}
return primaryKey || null;
}
key(key: string): PgColumn {
if (this.generic[key]) return this.generic[key];
throw new Err(500, null, `Cannot access ${this.generic.name}.${key} as it does not exist`);
}
stream(query: GenericStreamInput = {}): GenericEmitter<T> {
const generic = new GenericEmitter(this.pool, this.generic, query);
generic.start();
return generic;
}
async *iter(query: GenericIterInput = {}): AsyncGenerator<InferSelectModel<T>> {
const pagesize = query.pagesize || 100;
let page = 0;
let pgres;
do {
pgres = await this.list({
page,
limit: pagesize,
order: query.order,
where: query.where
});
for (const row of pgres.items) {
yield row as InferSelectModel<T>;
}
page++;
} while (pgres.items.length);
}
/**
* Count features with an optional custom SQL clause
* @param {Object} query Query parameters
* @param {SQL} query.where Custom SQL clause to filter results
* @return {Number} Number of features matching the query
*/
async count(query: GenericCountInput = {}): Promise<number> {
const pgres = await this.pool.select({
count: sql<string>`count(*)`.as('count'),
}).from(this.generic)
.where(query.where)
return parseInt(pgres[0].count);
}
/**
* List features with pagination, sorting and filtering
* @param {Object} query Query parameters
* @param {Number} query.limit Number of items to return per page, defaults to 10
* @param {Number} query.page Page number to return, defaults to 0
* @param {String} query.order Order to return items in, either 'asc' or 'desc', defaults to 'asc'
* @param {String} query.sort Column to sort by, defaults to primary key
* @param {SQL} query.where Custom SQL clause to filter results
*/
async list(query: GenericListInput = {}): Promise<GenericList<InferSelectModel<T>>> {
const order = query.order && query.order === 'desc' ? desc : asc;
const orderBy = order(query.sort ? this.key(query.sort) : this.requiredPrimaryKey());
const limit = query.limit || 10;
const partial = this.pool.select({
count: sql<string>`count(*) OVER()`.as('count'),
generic: this.generic
}).from(this.generic)
.where(query.where)
.orderBy(orderBy)
if (limit !== Infinity) {
partial
.limit(query.limit || 10)
.offset((query.page || 0) * (query.limit || 10))
}
const pgres = await partial;
if (pgres.length === 0) {
return { total: 0, items: [] };
} else {
return {
total: parseInt(pgres[0].count),
items: pgres.map((t) => {
return t.generic as InferSelectModel<T>
})
};
}
}
/**
* Fetch a single feature either by primary key or by a custom SQL clause
*
* @param {String|Number|SQL} id Primary key of the feature to fetch, or a custom SQL clause
*/
async from(id: unknown | SQL<unknown>): Promise<InferSelectModel<T>> {
const pgres = await this.pool.select()
.from(this.generic)
.where(is(id, SQL)? id as SQL<unknown> : eq(this.requiredPrimaryKey(), id))
.limit(1)
if (pgres.length !== 1) throw new Err(404, null, `Item Not Found`);
return pgres[0] as InferSelectModel<T>;
}
/**
* Commit changes to a feature either by primary key or by a custom SQL clause
*
* @param {String|Number|SQL} id Primary key of the feature to update, or a custom SQL clause
* @param {Object} values Key/Value pairs of the fields to update
*/
async commit(id: unknown | SQL<unknown>, values: object): Promise<InferSelectModel<T>> {
const vs = Object.values(values);
if (
Object.keys(values).length === 0
|| vs.every(value => value === undefined)
) {
return await this.from(id);
}
const pgres = await this.pool.update(this.generic)
.set(values)
.where(is(id, SQL)? id as SQL<unknown> : eq(this.requiredPrimaryKey(), id))
.returning();
if (!pgres.length) throw new Err(404, null, 'Item Not Found');
return pgres[0] as InferSelectModel<T>;
}
async clear(): Promise<void> {
await this.pool.delete(this.generic)
}
async generate(
values: InferInsertModel<T>,
opts?: GenerateOptions
): Promise<InferSelectModel<T>>;
async generate(
values: Array<InferInsertModel<T>>,
opts?: GenerateOptions
): Promise<Array<InferSelectModel<T>>>;
/**
* Create a new feature
*
* @param {Object} values Key/Value pairs of the fields to create
* @param {Object} opts Options object
* @param {GenerateUpsert} opts.upsert If set, will perform an upsert operation instead of a create
* @param {String} opts.upsertTarget Column to target for the upsert operation, defaults to primary key
*/
async generate(
values: InferInsertModel<T> | Array<InferInsertModel<T>>,
opts?: GenerateOptions
): Promise<InferSelectModel<T> | Array<InferSelectModel<T>>> {
if (!opts) opts = {};
let pgres;
try {
if (opts.upsert && opts.upsert === GenerateUpsert.DO_NOTHING) {
pgres = await this.pool.insert(this.generic)
.values(values)
.onConflictDoNothing()
.returning()
} else if (opts.upsert && opts.upsert === GenerateUpsert.UPDATE) {
pgres = await this.pool.insert(this.generic)
.values(values)
.onConflictDoUpdate({
target: opts.upsertTarget ? opts.upsertTarget : this.requiredPrimaryKey(),
set: conflictUpdateAll(this.generic)
})
.returning()
} else {
pgres = await this.pool.insert(this.generic)
.values(values)
.returning()
}
} catch (err) {
if (
err instanceof Error &&
typeof err.cause === 'object' &&
err.cause !== null &&
'code' in err.cause &&
'severity' in err.cause
) {
const pgError = err.cause as {
code: string;
severity: string;
detail: string;
[key: string]: unknown;
};
if (pgError.code === '23505') {
throw new Err(400, err, pgError.detail);
} else {
throw err;
}
} else {
throw err;
}
}
if (Array.isArray(values)) {
return pgres as Array<InferSelectModel<T>>;
} else {
return pgres[0] as InferSelectModel<T>;
}
}
/**
* Delete a feature either by primary key or by a custom SQL clause
*
* @param {String|Number|SQL} id Primary key of the feature to delete, or a custom SQL clause
*/
async delete(id: unknown | SQL<unknown>): Promise<void> {
await this.pool.delete(this.generic)
.where(is(id, SQL)? id as SQL<unknown> : eq(this.requiredPrimaryKey(), id))
}
}
export function conflictUpdateAll<
T extends Table,
E extends (keyof T['$inferInsert'])[],
>(table: T) {
const columns = getTableColumns(table)
const updateColumns = Object.entries(columns)
return updateColumns.reduce(
(acc, [colName, table]) => ({
...acc,
[colName]: sql.raw(`excluded.${table.name}`),
}),
{},
) as Omit<Record<keyof typeof table.$inferInsert, SQL>, E[number]>
}
export {
Pool
}