@umituz/web-database 1.2.1 → 1.3.0
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.
- package/package.json +1 -1
- package/src/index.ts +26 -0
- package/src/query.ts +241 -6
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@umituz/web-database",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "Database layer for the fleet — the single vendor-coupled module (Neon serverless HTTP driver today): sql(url) raw executor, createDb(url, schema) drizzle factory, Db type alias, query utilities (batchQueries, findFirst, findMany, exists, countRows), and db lifecycle tooling (reset / snapshot / seed)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.ts",
|
package/src/index.ts
CHANGED
|
@@ -8,16 +8,42 @@ export {
|
|
|
8
8
|
} from './lifecycle';
|
|
9
9
|
export type { SeedSnapshot, SnapshotBytesResult, SnapshotDb, TableSpec } from './lifecycle';
|
|
10
10
|
export {
|
|
11
|
+
and,
|
|
12
|
+
asc,
|
|
11
13
|
batchQueries,
|
|
12
14
|
countRows,
|
|
15
|
+
deleteRow,
|
|
16
|
+
deleteWhere,
|
|
17
|
+
desc,
|
|
18
|
+
eq,
|
|
13
19
|
exists,
|
|
14
20
|
findById,
|
|
15
21
|
findFirst,
|
|
16
22
|
findMany,
|
|
23
|
+
gt,
|
|
24
|
+
gte,
|
|
25
|
+
ilike,
|
|
26
|
+
inArray,
|
|
27
|
+
insertMany,
|
|
28
|
+
insertRow,
|
|
29
|
+
isNotNull,
|
|
30
|
+
isNull,
|
|
31
|
+
like,
|
|
32
|
+
lt,
|
|
33
|
+
lte,
|
|
34
|
+
ne,
|
|
35
|
+
or,
|
|
36
|
+
softDeleteRow,
|
|
37
|
+
softDeleteWhere,
|
|
38
|
+
updateRow,
|
|
39
|
+
updateWhere,
|
|
17
40
|
} from './query';
|
|
18
41
|
export type {
|
|
19
42
|
AnyDb,
|
|
20
43
|
AnyPgTable,
|
|
21
44
|
BatchCapableDb,
|
|
22
45
|
FindManyOptions,
|
|
46
|
+
MutationOptions,
|
|
47
|
+
OrderByItem,
|
|
48
|
+
WhereInput,
|
|
23
49
|
} from './query';
|
package/src/query.ts
CHANGED
|
@@ -1,14 +1,56 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import {
|
|
2
|
+
and,
|
|
3
|
+
asc,
|
|
4
|
+
count,
|
|
5
|
+
desc,
|
|
6
|
+
eq,
|
|
7
|
+
gt,
|
|
8
|
+
gte,
|
|
9
|
+
ilike,
|
|
10
|
+
inArray,
|
|
11
|
+
isNotNull,
|
|
12
|
+
isNull,
|
|
13
|
+
like,
|
|
14
|
+
lt,
|
|
15
|
+
lte,
|
|
16
|
+
ne,
|
|
17
|
+
or,
|
|
18
|
+
sql,
|
|
19
|
+
} from 'drizzle-orm';
|
|
20
|
+
import type { InferInsertModel, InferSelectModel, SQL, SQLWrapper } from 'drizzle-orm';
|
|
3
21
|
import type { PgTable, TableConfig } from 'drizzle-orm/pg-core';
|
|
4
22
|
import type { Db } from './driver';
|
|
5
23
|
|
|
24
|
+
export {
|
|
25
|
+
and,
|
|
26
|
+
asc,
|
|
27
|
+
desc,
|
|
28
|
+
eq,
|
|
29
|
+
gt,
|
|
30
|
+
gte,
|
|
31
|
+
ilike,
|
|
32
|
+
inArray,
|
|
33
|
+
isNotNull,
|
|
34
|
+
isNull,
|
|
35
|
+
like,
|
|
36
|
+
lt,
|
|
37
|
+
lte,
|
|
38
|
+
ne,
|
|
39
|
+
or,
|
|
40
|
+
};
|
|
41
|
+
|
|
6
42
|
export type AnyPgTable = PgTable<TableConfig>;
|
|
7
43
|
|
|
8
44
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
9
45
|
export type AnyDb = Db<any> | {
|
|
10
46
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
11
47
|
select: Db<any>['select'];
|
|
48
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
49
|
+
insert: Db<any>['insert'];
|
|
50
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
51
|
+
update: Db<any>['update'];
|
|
52
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
53
|
+
delete: Db<any>['delete'];
|
|
12
54
|
batch?: unknown;
|
|
13
55
|
};
|
|
14
56
|
|
|
@@ -16,21 +58,61 @@ export type BatchCapableDb = AnyDb;
|
|
|
16
58
|
|
|
17
59
|
type TableColumns<T extends AnyPgTable> = T['_']['columns'];
|
|
18
60
|
|
|
19
|
-
type
|
|
61
|
+
type WhereObject<T extends AnyPgTable> = {
|
|
62
|
+
[K in keyof TableColumns<T>]?: unknown;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
export type WhereInput<T extends AnyPgTable> =
|
|
20
66
|
| SQL
|
|
67
|
+
| WhereObject<T>
|
|
21
68
|
| undefined
|
|
22
69
|
| ((cols: TableColumns<T>) => SQL | undefined);
|
|
23
70
|
|
|
71
|
+
export type OrderByItem = SQL | SQLWrapper;
|
|
72
|
+
|
|
24
73
|
export interface FindManyOptions<T extends AnyPgTable> {
|
|
25
74
|
where?: WhereInput<T>;
|
|
26
75
|
limit?: number;
|
|
27
76
|
offset?: number;
|
|
28
|
-
orderBy?:
|
|
77
|
+
orderBy?: OrderByItem | OrderByItem[] | ((cols: TableColumns<T>) => OrderByItem | OrderByItem[]);
|
|
78
|
+
withDeleted?: boolean;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export interface MutationOptions {
|
|
29
82
|
withDeleted?: boolean;
|
|
30
83
|
}
|
|
31
84
|
|
|
85
|
+
function isPlainObject(value: unknown): value is Record<string, unknown> {
|
|
86
|
+
if (value === null || typeof value !== 'object') return false;
|
|
87
|
+
const proto = Object.getPrototypeOf(value);
|
|
88
|
+
return proto === null || proto === Object.prototype;
|
|
89
|
+
}
|
|
90
|
+
|
|
32
91
|
function resolveWhere<T extends AnyPgTable>(table: T, where?: WhereInput<T>, withDeleted = false): SQL | undefined {
|
|
33
|
-
|
|
92
|
+
let customCondition: SQL | undefined;
|
|
93
|
+
|
|
94
|
+
if (typeof where === 'function') {
|
|
95
|
+
customCondition = where(table as unknown as TableColumns<T>);
|
|
96
|
+
} else if (isPlainObject(where)) {
|
|
97
|
+
const tableCols = table as unknown as Record<string, unknown>;
|
|
98
|
+
const conditions: SQL[] = [];
|
|
99
|
+
for (const [key, val] of Object.entries(where)) {
|
|
100
|
+
const col = tableCols[key];
|
|
101
|
+
if (col && typeof col === 'object') {
|
|
102
|
+
if (val === null) {
|
|
103
|
+
conditions.push(isNull(col as never));
|
|
104
|
+
} else if (Array.isArray(val)) {
|
|
105
|
+
conditions.push(inArray(col as never, val));
|
|
106
|
+
} else {
|
|
107
|
+
conditions.push(eq(col as never, val));
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
customCondition = conditions.length > 0 ? and(...conditions) : undefined;
|
|
112
|
+
} else {
|
|
113
|
+
customCondition = where;
|
|
114
|
+
}
|
|
115
|
+
|
|
34
116
|
const hasDeletedAt = 'deletedAt' in table && Boolean((table as Record<string, unknown>).deletedAt);
|
|
35
117
|
|
|
36
118
|
if (!withDeleted && hasDeletedAt) {
|
|
@@ -41,6 +123,13 @@ function resolveWhere<T extends AnyPgTable>(table: T, where?: WhereInput<T>, wit
|
|
|
41
123
|
return customCondition;
|
|
42
124
|
}
|
|
43
125
|
|
|
126
|
+
function currentTimestampForColumn(col: unknown): unknown {
|
|
127
|
+
if (col && typeof col === 'object' && 'dataType' in col && (col as { dataType: string }).dataType === 'string') {
|
|
128
|
+
return new Date().toISOString();
|
|
129
|
+
}
|
|
130
|
+
return new Date();
|
|
131
|
+
}
|
|
132
|
+
|
|
44
133
|
export async function batchQueries<T extends Record<string, PromiseLike<unknown>>>(
|
|
45
134
|
db: BatchCapableDb,
|
|
46
135
|
queries: T
|
|
@@ -114,7 +203,7 @@ export async function findMany<T extends AnyPgTable>(
|
|
|
114
203
|
? options.orderBy(table as unknown as TableColumns<T>)
|
|
115
204
|
: options.orderBy;
|
|
116
205
|
const orderClause = Array.isArray(order) ? order : [order];
|
|
117
|
-
query = (query as never as { orderBy: (...clauses:
|
|
206
|
+
query = (query as never as { orderBy: (...clauses: OrderByItem[]) => typeof query }).orderBy(...orderClause);
|
|
118
207
|
}
|
|
119
208
|
|
|
120
209
|
if (typeof options?.offset === 'number') {
|
|
@@ -171,3 +260,149 @@ export async function findById<T extends AnyPgTable & { id: unknown }>(
|
|
|
171
260
|
options
|
|
172
261
|
);
|
|
173
262
|
}
|
|
263
|
+
|
|
264
|
+
export async function insertRow<T extends AnyPgTable>(
|
|
265
|
+
db: AnyDb,
|
|
266
|
+
table: T,
|
|
267
|
+
values: InferInsertModel<T>
|
|
268
|
+
): Promise<InferSelectModel<T>> {
|
|
269
|
+
const rows = (await (db
|
|
270
|
+
.insert(table as never)
|
|
271
|
+
.values(values as never)
|
|
272
|
+
.returning() as unknown as Promise<InferSelectModel<T>[]>));
|
|
273
|
+
const row = rows[0];
|
|
274
|
+
if (!row) {
|
|
275
|
+
throw new Error('Insert failed to return row');
|
|
276
|
+
}
|
|
277
|
+
return row;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
export async function insertMany<T extends AnyPgTable>(
|
|
281
|
+
db: AnyDb,
|
|
282
|
+
table: T,
|
|
283
|
+
values: InferInsertModel<T>[]
|
|
284
|
+
): Promise<InferSelectModel<T>[]> {
|
|
285
|
+
if (values.length === 0) return [];
|
|
286
|
+
return (await (db
|
|
287
|
+
.insert(table as never)
|
|
288
|
+
.values(values as never)
|
|
289
|
+
.returning() as unknown as Promise<InferSelectModel<T>[]>));
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
export async function updateRow<T extends AnyPgTable & { id: unknown }>(
|
|
293
|
+
db: AnyDb,
|
|
294
|
+
table: T,
|
|
295
|
+
id: string,
|
|
296
|
+
values: Partial<InferInsertModel<T>>,
|
|
297
|
+
options?: MutationOptions
|
|
298
|
+
): Promise<InferSelectModel<T> | null> {
|
|
299
|
+
const patch: Record<string, unknown> = { ...values };
|
|
300
|
+
if ('updatedAt' in table && patch.updatedAt === undefined) {
|
|
301
|
+
patch.updatedAt = currentTimestampForColumn((table as Record<string, unknown>).updatedAt);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
let condition: SQL = eq(table.id as never, id);
|
|
305
|
+
if (!options?.withDeleted && 'deletedAt' in table) {
|
|
306
|
+
condition = and(condition, isNull((table as Record<string, unknown>).deletedAt as never)) as SQL;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
const rows = (await (db
|
|
310
|
+
.update(table as never)
|
|
311
|
+
.set(patch as never)
|
|
312
|
+
.where(condition)
|
|
313
|
+
.returning() as unknown as Promise<InferSelectModel<T>[]>));
|
|
314
|
+
return rows[0] ?? null;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
export async function updateWhere<T extends AnyPgTable>(
|
|
318
|
+
db: AnyDb,
|
|
319
|
+
table: T,
|
|
320
|
+
where: WhereInput<T>,
|
|
321
|
+
values: Partial<InferInsertModel<T>>,
|
|
322
|
+
options?: MutationOptions
|
|
323
|
+
): Promise<InferSelectModel<T>[]> {
|
|
324
|
+
const patch: Record<string, unknown> = { ...values };
|
|
325
|
+
if ('updatedAt' in table && patch.updatedAt === undefined) {
|
|
326
|
+
patch.updatedAt = currentTimestampForColumn((table as Record<string, unknown>).updatedAt);
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
const condition = resolveWhere(table, where, options?.withDeleted);
|
|
330
|
+
let query = db.update(table as never).set(patch as never);
|
|
331
|
+
if (condition) {
|
|
332
|
+
query = (query as never as { where: (c: SQL) => typeof query }).where(condition);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
return (await (query as never as { returning: () => Promise<InferSelectModel<T>[]> }).returning());
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
export async function softDeleteRow<T extends AnyPgTable & { id: unknown }>(
|
|
339
|
+
db: AnyDb,
|
|
340
|
+
table: T,
|
|
341
|
+
id: string
|
|
342
|
+
): Promise<InferSelectModel<T> | null> {
|
|
343
|
+
const col = (table as Record<string, unknown>).deletedAt;
|
|
344
|
+
const timestamp = currentTimestampForColumn(col);
|
|
345
|
+
const patch: Record<string, unknown> = { deletedAt: timestamp };
|
|
346
|
+
if ('updatedAt' in table) {
|
|
347
|
+
patch.updatedAt = currentTimestampForColumn((table as Record<string, unknown>).updatedAt);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
let condition: SQL = eq(table.id as never, id);
|
|
351
|
+
if ('deletedAt' in table) {
|
|
352
|
+
condition = and(condition, isNull(col as never)) as SQL;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
const rows = (await (db
|
|
356
|
+
.update(table as never)
|
|
357
|
+
.set(patch as never)
|
|
358
|
+
.where(condition)
|
|
359
|
+
.returning() as unknown as Promise<InferSelectModel<T>[]>));
|
|
360
|
+
return rows[0] ?? null;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
export async function softDeleteWhere<T extends AnyPgTable>(
|
|
364
|
+
db: AnyDb,
|
|
365
|
+
table: T,
|
|
366
|
+
where: WhereInput<T>
|
|
367
|
+
): Promise<InferSelectModel<T>[]> {
|
|
368
|
+
const col = (table as Record<string, unknown>).deletedAt;
|
|
369
|
+
const timestamp = currentTimestampForColumn(col);
|
|
370
|
+
const patch: Record<string, unknown> = { deletedAt: timestamp };
|
|
371
|
+
if ('updatedAt' in table) {
|
|
372
|
+
patch.updatedAt = currentTimestampForColumn((table as Record<string, unknown>).updatedAt);
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
const condition = resolveWhere(table, where, false);
|
|
376
|
+
let query = db.update(table as never).set(patch as never);
|
|
377
|
+
if (condition) {
|
|
378
|
+
query = (query as never as { where: (c: SQL) => typeof query }).where(condition);
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
return (await (query as never as { returning: () => Promise<InferSelectModel<T>[]> }).returning());
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
export async function deleteRow<T extends AnyPgTable & { id: unknown }>(
|
|
385
|
+
db: AnyDb,
|
|
386
|
+
table: T,
|
|
387
|
+
id: string
|
|
388
|
+
): Promise<InferSelectModel<T> | null> {
|
|
389
|
+
const rows = (await (db
|
|
390
|
+
.delete(table as never)
|
|
391
|
+
.where(eq(table.id as never, id))
|
|
392
|
+
.returning() as unknown as Promise<InferSelectModel<T>[]>));
|
|
393
|
+
return rows[0] ?? null;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
export async function deleteWhere<T extends AnyPgTable>(
|
|
397
|
+
db: AnyDb,
|
|
398
|
+
table: T,
|
|
399
|
+
where: WhereInput<T>
|
|
400
|
+
): Promise<InferSelectModel<T>[]> {
|
|
401
|
+
const condition = resolveWhere(table, where, true);
|
|
402
|
+
let query = db.delete(table as never);
|
|
403
|
+
if (condition) {
|
|
404
|
+
query = (query as never as { where: (c: SQL) => typeof query }).where(condition);
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
return (await (query as never as { returning: () => Promise<InferSelectModel<T>[]> }).returning());
|
|
408
|
+
}
|