@dudousxd/nestjs-media-database-drizzle 0.9.0 → 0.11.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/dist/{drizzle-media-store-BHoJjohg.d.cts → drizzle-media-store-DNubIa8W.d.cts} +9 -2
- package/dist/{drizzle-media-store-BHoJjohg.d.ts → drizzle-media-store-DNubIa8W.d.ts} +9 -2
- package/dist/{drizzle-pg-upload-session-store-DWBmRKMi.d.cts → drizzle-pg-upload-session-store-ODZRub0i.d.cts} +20 -3
- package/dist/{drizzle-pg-upload-session-store-DWBmRKMi.d.ts → drizzle-pg-upload-session-store-ODZRub0i.d.ts} +20 -3
- package/dist/index.cjs +35 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +45 -12
- package/dist/index.js.map +1 -1
- package/dist/nest/index.cjs +180 -83
- package/dist/nest/index.cjs.map +1 -1
- package/dist/nest/index.d.cts +7 -2
- package/dist/nest/index.d.ts +7 -2
- package/dist/nest/index.js +185 -88
- package/dist/nest/index.js.map +1 -1
- package/dist/pg/index.cjs +201 -98
- package/dist/pg/index.cjs.map +1 -1
- package/dist/pg/index.d.cts +1048 -563
- package/dist/pg/index.d.ts +1048 -563
- package/dist/pg/index.js +203 -102
- package/dist/pg/index.js.map +1 -1
- package/package.json +3 -3
package/dist/nest/index.js
CHANGED
|
@@ -5,7 +5,7 @@ var __name = (target, value) => __defProp(target, "name", { value, configurable:
|
|
|
5
5
|
import { getDrizzleToken } from "@nestjs/drizzle";
|
|
6
6
|
|
|
7
7
|
// src/drizzle-media-store.ts
|
|
8
|
-
import { and, asc, count, eq as eq2, getTableColumns, gt, max, or as or2, sql, sum } from "drizzle-orm";
|
|
8
|
+
import { and, asc, count, eq as eq2, getTableColumns, gt, max, or as or2, sql as sql2, sum } from "drizzle-orm";
|
|
9
9
|
|
|
10
10
|
// src/list-cursor.ts
|
|
11
11
|
function encodeListCursor(record) {
|
|
@@ -25,6 +25,16 @@ function decodeListCursor(cursor) {
|
|
|
25
25
|
};
|
|
26
26
|
}
|
|
27
27
|
__name(decodeListCursor, "decodeListCursor");
|
|
28
|
+
function isEncodedListCursor(cursor) {
|
|
29
|
+
const decoded = Buffer.from(cursor, "base64").toString("utf8");
|
|
30
|
+
if (Buffer.from(decoded, "utf8").toString("base64") !== cursor) return false;
|
|
31
|
+
const separator = decoded.indexOf("|");
|
|
32
|
+
if (separator === -1 || separator === decoded.length - 1) return false;
|
|
33
|
+
const createdAt = decoded.slice(0, separator);
|
|
34
|
+
const date = new Date(createdAt);
|
|
35
|
+
return !Number.isNaN(date.getTime()) && date.toISOString() === createdAt;
|
|
36
|
+
}
|
|
37
|
+
__name(isEncodedListCursor, "isEncodedListCursor");
|
|
28
38
|
|
|
29
39
|
// src/media-columns.ts
|
|
30
40
|
import { integer, sqliteTable, text } from "drizzle-orm/sqlite-core";
|
|
@@ -73,9 +83,24 @@ var mediaTable = sqliteTable2("media", {
|
|
|
73
83
|
tenantIdx: index("idx_media_tenant").on(table.tenantId, table.ownerType, table.ownerId, table.collection)
|
|
74
84
|
}));
|
|
75
85
|
|
|
86
|
+
// src/schema-name.ts
|
|
87
|
+
var SCHEMA_NAME = /^[A-Za-z_][A-Za-z0-9_$]*$/;
|
|
88
|
+
function assertSchemaName(schema) {
|
|
89
|
+
if (typeof schema !== "string" || !SCHEMA_NAME.test(schema) || schema.length > 63) {
|
|
90
|
+
throw new TypeError(`[nestjs-media] \`schema\` must match /^[A-Za-z_][A-Za-z0-9_$]*$/ and be at most 63 characters, got ${JSON.stringify(schema)}`);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
__name(assertSchemaName, "assertSchemaName");
|
|
94
|
+
function rejectSqliteSchema(schema) {
|
|
95
|
+
if (schema !== void 0) {
|
|
96
|
+
throw new Error(`[nestjs-media] \`schema: ${JSON.stringify(schema)}\` was set, but SQLite has no schemas. Remove \`schema\`, or use the Postgres store (\`@dudousxd/nestjs-media-database-drizzle/pg\`).`);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
__name(rejectSqliteSchema, "rejectSqliteSchema");
|
|
100
|
+
|
|
76
101
|
// src/tenancy.ts
|
|
77
102
|
import { MediaTenancyUnavailableError } from "@dudousxd/nestjs-media-core";
|
|
78
|
-
import { eq, isNull, or } from "drizzle-orm";
|
|
103
|
+
import { eq, isNull, or, sql } from "drizzle-orm";
|
|
79
104
|
function tenantCondition(column, match) {
|
|
80
105
|
if (match.id === null) return isNull(column);
|
|
81
106
|
return match.orNull ? or(eq(column, match.id), isNull(column)) : eq(column, match.id);
|
|
@@ -97,6 +122,11 @@ async function assertTenantColumn(table, probe, fix) {
|
|
|
97
122
|
}
|
|
98
123
|
}
|
|
99
124
|
__name(assertTenantColumn, "assertTenantColumn");
|
|
125
|
+
function nameContainsCondition(name, fileName, text5) {
|
|
126
|
+
const pattern = `%${text5.toLowerCase().replace(/[!%_]/g, (c) => `!${c}`)}%`;
|
|
127
|
+
return or(sql`lower(${name}) like ${pattern} escape '!'`, sql`lower(${fileName}) like ${pattern} escape '!'`);
|
|
128
|
+
}
|
|
129
|
+
__name(nameContainsCondition, "nameContainsCondition");
|
|
100
130
|
|
|
101
131
|
// src/drizzle-media-store.ts
|
|
102
132
|
function toRow(record, tenancy) {
|
|
@@ -113,8 +143,10 @@ var DrizzleMediaStore = class {
|
|
|
113
143
|
}
|
|
114
144
|
db;
|
|
115
145
|
tenancy = false;
|
|
116
|
-
|
|
146
|
+
/** `schema` is for Postgres only: SQLite has no schemas, so setting it throws. */
|
|
147
|
+
constructor(db, options = {}) {
|
|
117
148
|
this.db = db;
|
|
149
|
+
rejectSqliteSchema(options.schema);
|
|
118
150
|
}
|
|
119
151
|
/** The columns to read: `tenant_id` only once tenancy is on. */
|
|
120
152
|
get columns() {
|
|
@@ -190,6 +222,10 @@ var DrizzleMediaStore = class {
|
|
|
190
222
|
sumSize: query.sum === "size" ? Number(row.sumSize ?? 0) : 0
|
|
191
223
|
}));
|
|
192
224
|
}
|
|
225
|
+
/** {@inheritDoc MediaStore.isListCursor} */
|
|
226
|
+
isListCursor(cursor) {
|
|
227
|
+
return isEncodedListCursor(cursor);
|
|
228
|
+
}
|
|
193
229
|
async list(filter = {}, page = {}) {
|
|
194
230
|
const limit = page.limit ?? 50;
|
|
195
231
|
const cursor = page.cursor ? decodeListCursor(page.cursor) : null;
|
|
@@ -203,6 +239,9 @@ var DrizzleMediaStore = class {
|
|
|
203
239
|
...filter.disk !== void 0 ? [
|
|
204
240
|
eq2(mediaTable.disk, filter.disk)
|
|
205
241
|
] : [],
|
|
242
|
+
...filter.nameContains !== void 0 ? [
|
|
243
|
+
nameContainsCondition(mediaTable.name, mediaTable.fileName, filter.nameContains)
|
|
244
|
+
] : [],
|
|
206
245
|
...filter.tenant ? [
|
|
207
246
|
tenantCondition(mediaTable.tenantId, filter.tenant)
|
|
208
247
|
] : [],
|
|
@@ -229,8 +268,11 @@ var DrizzleMediaStore = class {
|
|
|
229
268
|
// src/pg/drizzle-pg-media-store.ts
|
|
230
269
|
import { and as and2, asc as asc2, count as count2, eq as eq3, getTableColumns as getTableColumns2, gt as gt2, max as max2, or as or3, sum as sum2 } from "drizzle-orm";
|
|
231
270
|
|
|
271
|
+
// src/pg/schema.ts
|
|
272
|
+
import { index as index2, integer as integer3, pgSchema, pgTable, primaryKey, text as text4 } from "drizzle-orm/pg-core";
|
|
273
|
+
|
|
232
274
|
// src/pg/columns.ts
|
|
233
|
-
import { bigint, integer as integer2, jsonb,
|
|
275
|
+
import { bigint, integer as integer2, jsonb, text as text3, timestamp } from "drizzle-orm/pg-core";
|
|
234
276
|
function mediaPgColumns() {
|
|
235
277
|
return {
|
|
236
278
|
id: text3("id").primaryKey(),
|
|
@@ -285,44 +327,69 @@ function uploadSessionPgColumns() {
|
|
|
285
327
|
};
|
|
286
328
|
}
|
|
287
329
|
__name(uploadSessionPgColumns, "uploadSessionPgColumns");
|
|
288
|
-
var mediaPgTableWithoutTenant = pgTable("media", mediaPgColumns());
|
|
289
|
-
var mediaUploadSessionsPgTableWithoutTenant = pgTable("media_upload_sessions", uploadSessionPgColumns());
|
|
290
330
|
|
|
291
331
|
// src/pg/schema.ts
|
|
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
|
-
|
|
332
|
+
function tableIn(schema) {
|
|
333
|
+
if (schema === void 0) return pgTable;
|
|
334
|
+
assertSchemaName(schema);
|
|
335
|
+
if (schema === "public") {
|
|
336
|
+
throw new TypeError("[nestjs-media] `schema: 'public'` is Postgres' default schema: leave `schema` unset to use it.");
|
|
337
|
+
}
|
|
338
|
+
return pgSchema(schema).table;
|
|
339
|
+
}
|
|
340
|
+
__name(tableIn, "tableIn");
|
|
341
|
+
function mediaPgTables(schema) {
|
|
342
|
+
const table = tableIn(schema);
|
|
343
|
+
const media = table("media", {
|
|
344
|
+
...mediaPgColumns(),
|
|
345
|
+
/** Tenancy only: read and written once the store's `enableTenancy()` ran. */
|
|
346
|
+
tenantId: text4("tenant_id")
|
|
347
|
+
}, (t) => [
|
|
348
|
+
index2("idx_media_owner").on(t.ownerType, t.ownerId, t.collection),
|
|
349
|
+
index2("idx_media_collection").on(t.collection),
|
|
350
|
+
index2("idx_media_disk").on(t.disk),
|
|
351
|
+
index2("idx_media_created_at").on(t.createdAt),
|
|
352
|
+
index2("idx_media_collection_created_at").on(t.collection, t.createdAt, t.id),
|
|
353
|
+
index2("idx_media_tenant").on(t.tenantId, t.ownerType, t.ownerId, t.collection)
|
|
354
|
+
]);
|
|
355
|
+
const uploadSessions = table("media_upload_sessions", {
|
|
356
|
+
...uploadSessionPgColumns(),
|
|
357
|
+
/** Tenancy only: read and written once the store's `enableTenancy()` ran. */
|
|
358
|
+
tenantId: text4("tenant_id")
|
|
359
|
+
}, (t) => [
|
|
360
|
+
index2("idx_media_upload_sessions_tenant").on(t.tenantId)
|
|
361
|
+
]);
|
|
362
|
+
const uploadParts = table("media_upload_parts", {
|
|
363
|
+
sessionId: text4("session_id").notNull().references(() => uploadSessions.id, {
|
|
364
|
+
onDelete: "cascade"
|
|
365
|
+
}),
|
|
366
|
+
partNumber: integer3("part_number").notNull(),
|
|
367
|
+
etag: text4("etag").notNull()
|
|
368
|
+
}, (t) => [
|
|
369
|
+
primaryKey({
|
|
370
|
+
columns: [
|
|
371
|
+
t.sessionId,
|
|
372
|
+
t.partNumber
|
|
373
|
+
]
|
|
374
|
+
})
|
|
375
|
+
]);
|
|
376
|
+
return {
|
|
377
|
+
media,
|
|
378
|
+
uploadSessions,
|
|
379
|
+
uploadParts,
|
|
380
|
+
/**
|
|
381
|
+
* Internal: the tables without `tenant_id`. The stores insert through them while tenancy is
|
|
382
|
+
* off, because Drizzle lists every column of a table in an `INSERT`.
|
|
383
|
+
*/
|
|
384
|
+
mediaWithoutTenant: table("media", mediaPgColumns()),
|
|
385
|
+
uploadSessionsWithoutTenant: table("media_upload_sessions", uploadSessionPgColumns())
|
|
386
|
+
};
|
|
387
|
+
}
|
|
388
|
+
__name(mediaPgTables, "mediaPgTables");
|
|
389
|
+
var unqualified = mediaPgTables();
|
|
390
|
+
var mediaPgTable = unqualified.media;
|
|
391
|
+
var mediaUploadSessionsPgTable = unqualified.uploadSessions;
|
|
392
|
+
var mediaUploadPartsPgTable = unqualified.uploadParts;
|
|
326
393
|
|
|
327
394
|
// src/pg/drizzle-pg-media-store.ts
|
|
328
395
|
function toRecord(row) {
|
|
@@ -346,12 +413,18 @@ var DrizzlePgMediaStore = class {
|
|
|
346
413
|
}
|
|
347
414
|
db;
|
|
348
415
|
tenancy = false;
|
|
349
|
-
|
|
416
|
+
t;
|
|
417
|
+
/**
|
|
418
|
+
* `schema`: the Postgres schema the `media` table lives in. Every statement names it
|
|
419
|
+
* (`search_path` is never used). Default: unqualified, the connection's current schema.
|
|
420
|
+
*/
|
|
421
|
+
constructor(db, options = {}) {
|
|
350
422
|
this.db = db;
|
|
423
|
+
this.t = mediaPgTables(options.schema);
|
|
351
424
|
}
|
|
352
425
|
/** The columns to read: `tenant_id` only once tenancy is on. */
|
|
353
426
|
get columns() {
|
|
354
|
-
return selectable(getTableColumns2(
|
|
427
|
+
return selectable(getTableColumns2(this.t.media), this.tenancy);
|
|
355
428
|
}
|
|
356
429
|
/**
|
|
357
430
|
* Tenancy: checks the `tenant_id` column exists, then reads and writes it. Called by the
|
|
@@ -359,66 +432,66 @@ var DrizzlePgMediaStore = class {
|
|
|
359
432
|
*/
|
|
360
433
|
async enableTenancy() {
|
|
361
434
|
await assertTenantColumn("media", () => this.db.select({
|
|
362
|
-
tenantId:
|
|
363
|
-
}).from(
|
|
435
|
+
tenantId: this.t.media.tenantId
|
|
436
|
+
}).from(this.t.media).limit(1), "Generate a migration with drizzle-kit from `mediaPgTable` (or the tables of `mediaPgTables(schema)`; it declares `tenant_id` and the `idx_media_tenant` index), or run `ALTER TABLE media ADD COLUMN tenant_id text; CREATE INDEX idx_media_tenant ON media (tenant_id, owner_type, owner_id, collection);`.");
|
|
364
437
|
this.tenancy = true;
|
|
365
438
|
}
|
|
366
439
|
async save(record) {
|
|
367
440
|
const row = toRow2(record, this.tenancy);
|
|
368
441
|
const { id: _id, ...rest } = row;
|
|
369
|
-
await this.db.insert(this.tenancy ?
|
|
370
|
-
target:
|
|
442
|
+
await this.db.insert(this.tenancy ? this.t.media : this.t.mediaWithoutTenant).values(row).onConflictDoUpdate({
|
|
443
|
+
target: this.t.media.id,
|
|
371
444
|
set: rest
|
|
372
445
|
});
|
|
373
446
|
return row;
|
|
374
447
|
}
|
|
375
448
|
async find(id) {
|
|
376
|
-
const rows = await this.db.select(this.columns).from(
|
|
449
|
+
const rows = await this.db.select(this.columns).from(this.t.media).where(eq3(this.t.media.id, id)).limit(1);
|
|
377
450
|
const row = rows[0];
|
|
378
451
|
return row ? toRecord(row) : null;
|
|
379
452
|
}
|
|
380
453
|
async listByOwner(ownerType, ownerId, collection) {
|
|
381
|
-
const where = collection === void 0 ? and2(eq3(
|
|
382
|
-
const rows = await this.db.select(this.columns).from(
|
|
454
|
+
const where = collection === void 0 ? and2(eq3(this.t.media.ownerType, ownerType), eq3(this.t.media.ownerId, ownerId)) : and2(eq3(this.t.media.ownerType, ownerType), eq3(this.t.media.ownerId, ownerId), eq3(this.t.media.collection, collection));
|
|
455
|
+
const rows = await this.db.select(this.columns).from(this.t.media).where(where).orderBy(asc2(this.t.media.order), asc2(this.t.media.id));
|
|
383
456
|
return rows.map(toRecord);
|
|
384
457
|
}
|
|
385
458
|
async delete(id) {
|
|
386
|
-
await this.db.delete(
|
|
459
|
+
await this.db.delete(this.t.media).where(eq3(this.t.media.id, id));
|
|
387
460
|
}
|
|
388
461
|
async nextOrder(ownerType, ownerId, collection) {
|
|
389
462
|
const rows = await this.db.select({
|
|
390
|
-
max: max2(
|
|
391
|
-
}).from(
|
|
463
|
+
max: max2(this.t.media.order)
|
|
464
|
+
}).from(this.t.media).where(and2(eq3(this.t.media.ownerType, ownerType), eq3(this.t.media.ownerId, ownerId), eq3(this.t.media.collection, collection)));
|
|
392
465
|
const top = rows[0]?.max;
|
|
393
466
|
return top == null ? 0 : Number(top) + 1;
|
|
394
467
|
}
|
|
395
468
|
async count(filter = {}) {
|
|
396
469
|
const conditions = [
|
|
397
470
|
...filter.ownerType !== void 0 ? [
|
|
398
|
-
eq3(
|
|
471
|
+
eq3(this.t.media.ownerType, filter.ownerType)
|
|
399
472
|
] : [],
|
|
400
473
|
...filter.collection !== void 0 ? [
|
|
401
|
-
eq3(
|
|
474
|
+
eq3(this.t.media.collection, filter.collection)
|
|
402
475
|
] : [],
|
|
403
476
|
...filter.disk !== void 0 ? [
|
|
404
|
-
eq3(
|
|
477
|
+
eq3(this.t.media.disk, filter.disk)
|
|
405
478
|
] : [],
|
|
406
479
|
...filter.tenant ? [
|
|
407
|
-
tenantCondition(
|
|
480
|
+
tenantCondition(this.t.media.tenantId, filter.tenant)
|
|
408
481
|
] : []
|
|
409
482
|
];
|
|
410
483
|
const rows = await this.db.select({
|
|
411
484
|
value: count2()
|
|
412
|
-
}).from(
|
|
485
|
+
}).from(this.t.media).where(conditions.length ? and2(...conditions) : void 0);
|
|
413
486
|
return Number(rows[0]?.value ?? 0);
|
|
414
487
|
}
|
|
415
488
|
async aggregate(query) {
|
|
416
|
-
const column = query.groupBy === "collection" ?
|
|
489
|
+
const column = query.groupBy === "collection" ? this.t.media.collection : this.t.media.disk;
|
|
417
490
|
const rows = await this.db.select({
|
|
418
491
|
key: column,
|
|
419
492
|
count: count2(),
|
|
420
|
-
sumSize: sum2(
|
|
421
|
-
}).from(
|
|
493
|
+
sumSize: sum2(this.t.media.size)
|
|
494
|
+
}).from(this.t.media).where(query.tenant ? tenantCondition(this.t.media.tenantId, query.tenant) : void 0).groupBy(column);
|
|
422
495
|
return rows.map((row) => ({
|
|
423
496
|
key: row.key,
|
|
424
497
|
count: Number(row.count),
|
|
@@ -426,27 +499,34 @@ var DrizzlePgMediaStore = class {
|
|
|
426
499
|
sumSize: query.sum === "size" ? Number(row.sumSize ?? 0) : 0
|
|
427
500
|
}));
|
|
428
501
|
}
|
|
502
|
+
/** {@inheritDoc MediaStore.isListCursor} */
|
|
503
|
+
isListCursor(cursor) {
|
|
504
|
+
return isEncodedListCursor(cursor);
|
|
505
|
+
}
|
|
429
506
|
async list(filter = {}, page = {}) {
|
|
430
507
|
const limit = page.limit ?? 50;
|
|
431
508
|
const cursor = page.cursor ? decodeListCursor(page.cursor) : null;
|
|
432
509
|
const filterConditions = [
|
|
433
510
|
...filter.ownerType !== void 0 ? [
|
|
434
|
-
eq3(
|
|
511
|
+
eq3(this.t.media.ownerType, filter.ownerType)
|
|
435
512
|
] : [],
|
|
436
513
|
...filter.collection !== void 0 ? [
|
|
437
|
-
eq3(
|
|
514
|
+
eq3(this.t.media.collection, filter.collection)
|
|
438
515
|
] : [],
|
|
439
516
|
...filter.disk !== void 0 ? [
|
|
440
|
-
eq3(
|
|
517
|
+
eq3(this.t.media.disk, filter.disk)
|
|
518
|
+
] : [],
|
|
519
|
+
...filter.nameContains !== void 0 ? [
|
|
520
|
+
nameContainsCondition(this.t.media.name, this.t.media.fileName, filter.nameContains)
|
|
441
521
|
] : [],
|
|
442
522
|
...filter.tenant ? [
|
|
443
|
-
tenantCondition(
|
|
523
|
+
tenantCondition(this.t.media.tenantId, filter.tenant)
|
|
444
524
|
] : [],
|
|
445
525
|
...cursor ? [
|
|
446
|
-
or3(gt2(
|
|
526
|
+
or3(gt2(this.t.media.createdAt, cursor.createdAt), and2(eq3(this.t.media.createdAt, cursor.createdAt), gt2(this.t.media.id, cursor.id)))
|
|
447
527
|
] : []
|
|
448
528
|
];
|
|
449
|
-
const rows = await this.db.select(this.columns).from(
|
|
529
|
+
const rows = await this.db.select(this.columns).from(this.t.media).where(filterConditions.length ? and2(...filterConditions) : void 0).orderBy(asc2(this.t.media.createdAt), asc2(this.t.media.id)).limit(limit + 1);
|
|
450
530
|
const hasMore = rows.length > limit;
|
|
451
531
|
const records = rows.slice(0, limit).map(toRecord);
|
|
452
532
|
const result = {
|
|
@@ -461,7 +541,7 @@ var DrizzlePgMediaStore = class {
|
|
|
461
541
|
};
|
|
462
542
|
|
|
463
543
|
// src/pg/drizzle-pg-upload-session-store.ts
|
|
464
|
-
import { and as and3, asc as asc3, eq as eq4, getTableColumns as getTableColumns3, like, sql as
|
|
544
|
+
import { and as and3, asc as asc3, eq as eq4, getTableColumns as getTableColumns3, like, sql as sql3 } from "drizzle-orm";
|
|
465
545
|
function escapeLike(value) {
|
|
466
546
|
return value.replace(/[\\%_]/g, (char) => `\\${char}`);
|
|
467
547
|
}
|
|
@@ -505,16 +585,22 @@ var DrizzlePgUploadSessionStore = class {
|
|
|
505
585
|
}
|
|
506
586
|
db;
|
|
507
587
|
tenancy = false;
|
|
508
|
-
|
|
588
|
+
t;
|
|
589
|
+
/**
|
|
590
|
+
* `schema`: the Postgres schema of `media_upload_sessions` / `media_upload_parts`. Every
|
|
591
|
+
* statement names it (`search_path` is never used). Default: unqualified.
|
|
592
|
+
*/
|
|
593
|
+
constructor(db, options = {}) {
|
|
509
594
|
this.db = db;
|
|
595
|
+
this.t = mediaPgTables(options.schema);
|
|
510
596
|
}
|
|
511
597
|
/** Off: the table without `tenant_id`, which Drizzle would otherwise list in an INSERT. */
|
|
512
598
|
get sessionsTable() {
|
|
513
|
-
return this.tenancy ?
|
|
599
|
+
return this.tenancy ? this.t.uploadSessions : this.t.uploadSessionsWithoutTenant;
|
|
514
600
|
}
|
|
515
601
|
/** The columns to read: `tenant_id` only once tenancy is on. */
|
|
516
602
|
get columns() {
|
|
517
|
-
return selectable(getTableColumns3(
|
|
603
|
+
return selectable(getTableColumns3(this.t.uploadSessions), this.tenancy);
|
|
518
604
|
}
|
|
519
605
|
/**
|
|
520
606
|
* Tenancy: checks the `tenant_id` column exists, then reads and writes it. Called by the upload
|
|
@@ -522,8 +608,8 @@ var DrizzlePgUploadSessionStore = class {
|
|
|
522
608
|
*/
|
|
523
609
|
async enableTenancy() {
|
|
524
610
|
await assertTenantColumn("media_upload_sessions", () => this.db.select({
|
|
525
|
-
tenantId:
|
|
526
|
-
}).from(
|
|
611
|
+
tenantId: this.t.uploadSessions.tenantId
|
|
612
|
+
}).from(this.t.uploadSessions).limit(1), "Generate a migration with drizzle-kit from `mediaUploadSessionsPgTable` (or the tables of `mediaPgTables(schema)`; it declares `tenant_id` and its index), or run `ALTER TABLE media_upload_sessions ADD COLUMN tenant_id text; CREATE INDEX idx_media_upload_sessions_tenant ON media_upload_sessions (tenant_id);`.");
|
|
527
613
|
this.tenancy = true;
|
|
528
614
|
}
|
|
529
615
|
/** The session as persisted: off, no `tenantId`; on, `tenantId` or `null`. */
|
|
@@ -548,7 +634,7 @@ var DrizzlePgUploadSessionStore = class {
|
|
|
548
634
|
};
|
|
549
635
|
}
|
|
550
636
|
async get(id) {
|
|
551
|
-
const rows = await this.db.select(this.columns).from(
|
|
637
|
+
const rows = await this.db.select(this.columns).from(this.t.uploadSessions).where(eq4(this.t.uploadSessions.id, id)).limit(1);
|
|
552
638
|
const row = rows[0];
|
|
553
639
|
return row ? toSession(row) : null;
|
|
554
640
|
}
|
|
@@ -564,34 +650,34 @@ var DrizzlePgUploadSessionStore = class {
|
|
|
564
650
|
id: session.id,
|
|
565
651
|
...columns
|
|
566
652
|
}).onConflictDoUpdate({
|
|
567
|
-
target:
|
|
653
|
+
target: this.t.uploadSessions.id,
|
|
568
654
|
set: columns
|
|
569
655
|
});
|
|
570
656
|
return this.persisted(session);
|
|
571
657
|
}
|
|
572
658
|
async delete(id) {
|
|
573
|
-
await this.db.delete(
|
|
659
|
+
await this.db.delete(this.t.uploadSessions).where(eq4(this.t.uploadSessions.id, id));
|
|
574
660
|
}
|
|
575
661
|
async addPart(id, part) {
|
|
576
|
-
await this.db.insert(
|
|
662
|
+
await this.db.insert(this.t.uploadParts).values({
|
|
577
663
|
sessionId: id,
|
|
578
664
|
partNumber: part.partNumber,
|
|
579
665
|
etag: part.etag
|
|
580
666
|
}).onConflictDoUpdate({
|
|
581
667
|
target: [
|
|
582
|
-
|
|
583
|
-
|
|
668
|
+
this.t.uploadParts.sessionId,
|
|
669
|
+
this.t.uploadParts.partNumber
|
|
584
670
|
],
|
|
585
671
|
set: {
|
|
586
|
-
etag:
|
|
672
|
+
etag: sql3`excluded.etag`
|
|
587
673
|
}
|
|
588
674
|
});
|
|
589
675
|
}
|
|
590
676
|
async listParts(id) {
|
|
591
677
|
const rows = await this.db.select({
|
|
592
|
-
partNumber:
|
|
593
|
-
etag:
|
|
594
|
-
}).from(
|
|
678
|
+
partNumber: this.t.uploadParts.partNumber,
|
|
679
|
+
etag: this.t.uploadParts.etag
|
|
680
|
+
}).from(this.t.uploadParts).where(eq4(this.t.uploadParts.sessionId, id)).orderBy(asc3(this.t.uploadParts.partNumber));
|
|
595
681
|
return rows.map((row) => ({
|
|
596
682
|
partNumber: Number(row.partNumber),
|
|
597
683
|
etag: row.etag
|
|
@@ -600,21 +686,29 @@ var DrizzlePgUploadSessionStore = class {
|
|
|
600
686
|
async list(filter = {}) {
|
|
601
687
|
const conditions = [
|
|
602
688
|
...filter.disk !== void 0 ? [
|
|
603
|
-
eq4(
|
|
689
|
+
eq4(this.t.uploadSessions.disk, filter.disk)
|
|
604
690
|
] : [],
|
|
605
691
|
...filter.keyPrefix !== void 0 ? [
|
|
606
|
-
like(
|
|
692
|
+
like(this.t.uploadSessions.key, `${escapeLike(filter.keyPrefix)}%`)
|
|
607
693
|
] : [],
|
|
608
694
|
...filter.tenant ? [
|
|
609
|
-
tenantCondition(
|
|
695
|
+
tenantCondition(this.t.uploadSessions.tenantId, filter.tenant)
|
|
610
696
|
] : []
|
|
611
697
|
];
|
|
612
|
-
const rows = await this.db.select(this.columns).from(
|
|
698
|
+
const rows = await this.db.select(this.columns).from(this.t.uploadSessions).where(conditions.length ? and3(...conditions) : void 0).orderBy(asc3(this.t.uploadSessions.createdAt), asc3(this.t.uploadSessions.id));
|
|
613
699
|
return rows.map(toSession);
|
|
614
700
|
}
|
|
615
701
|
};
|
|
616
702
|
|
|
617
703
|
// src/nest/index.ts
|
|
704
|
+
function storeOptions(options) {
|
|
705
|
+
if (options.schema === void 0) return {};
|
|
706
|
+
mediaPgTables(options.schema);
|
|
707
|
+
return {
|
|
708
|
+
schema: options.schema
|
|
709
|
+
};
|
|
710
|
+
}
|
|
711
|
+
__name(storeOptions, "storeOptions");
|
|
618
712
|
function factory(options, build) {
|
|
619
713
|
return {
|
|
620
714
|
inject: [
|
|
@@ -625,15 +719,18 @@ function factory(options, build) {
|
|
|
625
719
|
}
|
|
626
720
|
__name(factory, "factory");
|
|
627
721
|
function drizzleMediaStore(options = {}) {
|
|
722
|
+
rejectSqliteSchema(options.schema);
|
|
628
723
|
return factory(options, (db) => new DrizzleMediaStore(db));
|
|
629
724
|
}
|
|
630
725
|
__name(drizzleMediaStore, "drizzleMediaStore");
|
|
631
726
|
function drizzlePgMediaStore(options = {}) {
|
|
632
|
-
|
|
727
|
+
const store = storeOptions(options);
|
|
728
|
+
return factory(options, (db) => new DrizzlePgMediaStore(db, store));
|
|
633
729
|
}
|
|
634
730
|
__name(drizzlePgMediaStore, "drizzlePgMediaStore");
|
|
635
731
|
function drizzlePgUploadSessions(options = {}) {
|
|
636
|
-
|
|
732
|
+
const store = storeOptions(options);
|
|
733
|
+
return factory(options, (db) => new DrizzlePgUploadSessionStore(db, store));
|
|
637
734
|
}
|
|
638
735
|
__name(drizzlePgUploadSessions, "drizzlePgUploadSessions");
|
|
639
736
|
export {
|