@dudousxd/nestjs-media-database-drizzle 0.9.0 → 0.10.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-fAcBV6k_.d.cts} +7 -2
- package/dist/{drizzle-media-store-BHoJjohg.d.ts → drizzle-media-store-fAcBV6k_.d.ts} +7 -2
- package/dist/{drizzle-pg-upload-session-store-DWBmRKMi.d.cts → drizzle-pg-upload-session-store-BtXMFr3I.d.cts} +18 -3
- package/dist/{drizzle-pg-upload-session-store-DWBmRKMi.d.ts → drizzle-pg-upload-session-store-BtXMFr3I.d.ts} +18 -3
- package/dist/index.cjs +21 -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 +31 -12
- package/dist/index.js.map +1 -1
- package/dist/nest/index.cjs +162 -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 +167 -88
- package/dist/nest/index.js.map +1 -1
- package/dist/pg/index.cjs +187 -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 +189 -102
- package/dist/pg/index.js.map +1 -1
- package/package.json +3 -3
package/dist/pg/index.js
CHANGED
|
@@ -3,8 +3,29 @@ var __name = (target, value) => __defProp(target, "name", { value, configurable:
|
|
|
3
3
|
|
|
4
4
|
// src/pg/ddl.ts
|
|
5
5
|
import { sql } from "drizzle-orm";
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
|
|
7
|
+
// src/schema-name.ts
|
|
8
|
+
var SCHEMA_NAME = /^[A-Za-z_][A-Za-z0-9_$]*$/;
|
|
9
|
+
function assertSchemaName(schema) {
|
|
10
|
+
if (typeof schema !== "string" || !SCHEMA_NAME.test(schema) || schema.length > 63) {
|
|
11
|
+
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)}`);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
__name(assertSchemaName, "assertSchemaName");
|
|
15
|
+
function qualified(schema, table) {
|
|
16
|
+
return schema === void 0 ? `"${table}"` : `"${schema}"."${table}"`;
|
|
17
|
+
}
|
|
18
|
+
__name(qualified, "qualified");
|
|
19
|
+
|
|
20
|
+
// src/pg/ddl.ts
|
|
21
|
+
function mediaPgDdl(schema) {
|
|
22
|
+
if (schema !== void 0) assertSchemaName(schema);
|
|
23
|
+
const t = /* @__PURE__ */ __name((table) => qualified(schema, table), "t");
|
|
24
|
+
return [
|
|
25
|
+
...schema !== void 0 ? [
|
|
26
|
+
`CREATE SCHEMA IF NOT EXISTS "${schema}"`
|
|
27
|
+
] : [],
|
|
28
|
+
`CREATE TABLE IF NOT EXISTS ${t("media")} (
|
|
8
29
|
"id" text PRIMARY KEY NOT NULL,
|
|
9
30
|
"owner_type" text NOT NULL,
|
|
10
31
|
"owner_id" text NOT NULL,
|
|
@@ -22,14 +43,14 @@ var MEDIA_PG_DDL = [
|
|
|
22
43
|
"updated_at" timestamp with time zone NOT NULL,
|
|
23
44
|
"tenant_id" text
|
|
24
45
|
)`,
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
46
|
+
`ALTER TABLE ${t("media")} ADD COLUMN IF NOT EXISTS "tenant_id" text`,
|
|
47
|
+
`CREATE INDEX IF NOT EXISTS "idx_media_owner" ON ${t("media")} ("owner_type", "owner_id", "collection")`,
|
|
48
|
+
`CREATE INDEX IF NOT EXISTS "idx_media_collection" ON ${t("media")} ("collection")`,
|
|
49
|
+
`CREATE INDEX IF NOT EXISTS "idx_media_disk" ON ${t("media")} ("disk")`,
|
|
50
|
+
`CREATE INDEX IF NOT EXISTS "idx_media_created_at" ON ${t("media")} ("created_at")`,
|
|
51
|
+
`CREATE INDEX IF NOT EXISTS "idx_media_collection_created_at" ON ${t("media")} ("collection", "created_at", "id")`,
|
|
52
|
+
`CREATE INDEX IF NOT EXISTS "idx_media_tenant" ON ${t("media")} ("tenant_id", "owner_type", "owner_id", "collection")`,
|
|
53
|
+
`CREATE TABLE IF NOT EXISTS ${t("media_upload_sessions")} (
|
|
33
54
|
"id" text PRIMARY KEY NOT NULL,
|
|
34
55
|
"disk" text NOT NULL,
|
|
35
56
|
"key" text NOT NULL,
|
|
@@ -43,18 +64,34 @@ var MEDIA_PG_DDL = [
|
|
|
43
64
|
"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
|
|
44
65
|
"tenant_id" text
|
|
45
66
|
)`,
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
"session_id" text NOT NULL REFERENCES "media_upload_sessions" ("id") ON DELETE CASCADE,
|
|
67
|
+
`ALTER TABLE ${t("media_upload_sessions")} ADD COLUMN IF NOT EXISTS "tenant_id" text`,
|
|
68
|
+
`CREATE INDEX IF NOT EXISTS "idx_media_upload_sessions_tenant" ON ${t("media_upload_sessions")} ("tenant_id")`,
|
|
69
|
+
`CREATE TABLE IF NOT EXISTS ${t("media_upload_parts")} (
|
|
70
|
+
"session_id" text NOT NULL REFERENCES ${t("media_upload_sessions")} ("id") ON DELETE CASCADE,
|
|
50
71
|
"part_number" integer NOT NULL,
|
|
51
72
|
"etag" text NOT NULL,
|
|
52
73
|
PRIMARY KEY ("session_id", "part_number")
|
|
53
74
|
)`
|
|
54
|
-
];
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
75
|
+
];
|
|
76
|
+
}
|
|
77
|
+
__name(mediaPgDdl, "mediaPgDdl");
|
|
78
|
+
var MEDIA_PG_DDL = mediaPgDdl();
|
|
79
|
+
async function createMediaPgTables(db, options = {}) {
|
|
80
|
+
const [first, ...rest] = mediaPgDdl(options.schema);
|
|
81
|
+
if (options.schema !== void 0 && first !== void 0) {
|
|
82
|
+
try {
|
|
83
|
+
await db.execute(sql.raw(first));
|
|
84
|
+
} catch (error) {
|
|
85
|
+
throw Object.assign(new Error(`[nestjs-media] could not create the schema "${options.schema}". Create it yourself, or grant the role CREATE on the database: GRANT CREATE ON DATABASE <db> TO <role>. (${error.message})`), {
|
|
86
|
+
cause: error
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
for (const statement of options.schema !== void 0 ? rest : [
|
|
91
|
+
first,
|
|
92
|
+
...rest
|
|
93
|
+
]) {
|
|
94
|
+
if (statement !== void 0) await db.execute(sql.raw(statement));
|
|
58
95
|
}
|
|
59
96
|
}
|
|
60
97
|
__name(createMediaPgTables, "createMediaPgTables");
|
|
@@ -83,7 +120,7 @@ __name(decodeListCursor, "decodeListCursor");
|
|
|
83
120
|
|
|
84
121
|
// src/tenancy.ts
|
|
85
122
|
import { MediaTenancyUnavailableError } from "@dudousxd/nestjs-media-core";
|
|
86
|
-
import { eq, isNull, or } from "drizzle-orm";
|
|
123
|
+
import { eq, isNull, or, sql as sql2 } from "drizzle-orm";
|
|
87
124
|
function tenantCondition(column, match) {
|
|
88
125
|
if (match.id === null) return isNull(column);
|
|
89
126
|
return match.orNull ? or(eq(column, match.id), isNull(column)) : eq(column, match.id);
|
|
@@ -105,9 +142,17 @@ async function assertTenantColumn(table, probe, fix) {
|
|
|
105
142
|
}
|
|
106
143
|
}
|
|
107
144
|
__name(assertTenantColumn, "assertTenantColumn");
|
|
145
|
+
function nameContainsCondition(name, fileName, text3) {
|
|
146
|
+
const pattern = `%${text3.toLowerCase().replace(/[!%_]/g, (c) => `!${c}`)}%`;
|
|
147
|
+
return or(sql2`lower(${name}) like ${pattern} escape '!'`, sql2`lower(${fileName}) like ${pattern} escape '!'`);
|
|
148
|
+
}
|
|
149
|
+
__name(nameContainsCondition, "nameContainsCondition");
|
|
150
|
+
|
|
151
|
+
// src/pg/schema.ts
|
|
152
|
+
import { index, integer as integer2, pgSchema, pgTable, primaryKey, text as text2 } from "drizzle-orm/pg-core";
|
|
108
153
|
|
|
109
154
|
// src/pg/columns.ts
|
|
110
|
-
import { bigint, integer, jsonb,
|
|
155
|
+
import { bigint, integer, jsonb, text, timestamp } from "drizzle-orm/pg-core";
|
|
111
156
|
function mediaPgColumns() {
|
|
112
157
|
return {
|
|
113
158
|
id: text("id").primaryKey(),
|
|
@@ -162,44 +207,69 @@ function uploadSessionPgColumns() {
|
|
|
162
207
|
};
|
|
163
208
|
}
|
|
164
209
|
__name(uploadSessionPgColumns, "uploadSessionPgColumns");
|
|
165
|
-
var mediaPgTableWithoutTenant = pgTable("media", mediaPgColumns());
|
|
166
|
-
var mediaUploadSessionsPgTableWithoutTenant = pgTable("media_upload_sessions", uploadSessionPgColumns());
|
|
167
210
|
|
|
168
211
|
// src/pg/schema.ts
|
|
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
|
-
|
|
212
|
+
function tableIn(schema) {
|
|
213
|
+
if (schema === void 0) return pgTable;
|
|
214
|
+
assertSchemaName(schema);
|
|
215
|
+
if (schema === "public") {
|
|
216
|
+
throw new TypeError("[nestjs-media] `schema: 'public'` is Postgres' default schema: leave `schema` unset to use it.");
|
|
217
|
+
}
|
|
218
|
+
return pgSchema(schema).table;
|
|
219
|
+
}
|
|
220
|
+
__name(tableIn, "tableIn");
|
|
221
|
+
function mediaPgTables(schema) {
|
|
222
|
+
const table = tableIn(schema);
|
|
223
|
+
const media = table("media", {
|
|
224
|
+
...mediaPgColumns(),
|
|
225
|
+
/** Tenancy only: read and written once the store's `enableTenancy()` ran. */
|
|
226
|
+
tenantId: text2("tenant_id")
|
|
227
|
+
}, (t) => [
|
|
228
|
+
index("idx_media_owner").on(t.ownerType, t.ownerId, t.collection),
|
|
229
|
+
index("idx_media_collection").on(t.collection),
|
|
230
|
+
index("idx_media_disk").on(t.disk),
|
|
231
|
+
index("idx_media_created_at").on(t.createdAt),
|
|
232
|
+
index("idx_media_collection_created_at").on(t.collection, t.createdAt, t.id),
|
|
233
|
+
index("idx_media_tenant").on(t.tenantId, t.ownerType, t.ownerId, t.collection)
|
|
234
|
+
]);
|
|
235
|
+
const uploadSessions = table("media_upload_sessions", {
|
|
236
|
+
...uploadSessionPgColumns(),
|
|
237
|
+
/** Tenancy only: read and written once the store's `enableTenancy()` ran. */
|
|
238
|
+
tenantId: text2("tenant_id")
|
|
239
|
+
}, (t) => [
|
|
240
|
+
index("idx_media_upload_sessions_tenant").on(t.tenantId)
|
|
241
|
+
]);
|
|
242
|
+
const uploadParts = table("media_upload_parts", {
|
|
243
|
+
sessionId: text2("session_id").notNull().references(() => uploadSessions.id, {
|
|
244
|
+
onDelete: "cascade"
|
|
245
|
+
}),
|
|
246
|
+
partNumber: integer2("part_number").notNull(),
|
|
247
|
+
etag: text2("etag").notNull()
|
|
248
|
+
}, (t) => [
|
|
249
|
+
primaryKey({
|
|
250
|
+
columns: [
|
|
251
|
+
t.sessionId,
|
|
252
|
+
t.partNumber
|
|
253
|
+
]
|
|
254
|
+
})
|
|
255
|
+
]);
|
|
256
|
+
return {
|
|
257
|
+
media,
|
|
258
|
+
uploadSessions,
|
|
259
|
+
uploadParts,
|
|
260
|
+
/**
|
|
261
|
+
* Internal: the tables without `tenant_id`. The stores insert through them while tenancy is
|
|
262
|
+
* off, because Drizzle lists every column of a table in an `INSERT`.
|
|
263
|
+
*/
|
|
264
|
+
mediaWithoutTenant: table("media", mediaPgColumns()),
|
|
265
|
+
uploadSessionsWithoutTenant: table("media_upload_sessions", uploadSessionPgColumns())
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
__name(mediaPgTables, "mediaPgTables");
|
|
269
|
+
var unqualified = mediaPgTables();
|
|
270
|
+
var mediaPgTable = unqualified.media;
|
|
271
|
+
var mediaUploadSessionsPgTable = unqualified.uploadSessions;
|
|
272
|
+
var mediaUploadPartsPgTable = unqualified.uploadParts;
|
|
203
273
|
|
|
204
274
|
// src/pg/drizzle-pg-media-store.ts
|
|
205
275
|
function toRecord(row) {
|
|
@@ -223,12 +293,18 @@ var DrizzlePgMediaStore = class {
|
|
|
223
293
|
}
|
|
224
294
|
db;
|
|
225
295
|
tenancy = false;
|
|
226
|
-
|
|
296
|
+
t;
|
|
297
|
+
/**
|
|
298
|
+
* `schema`: the Postgres schema the `media` table lives in. Every statement names it
|
|
299
|
+
* (`search_path` is never used). Default: unqualified, the connection's current schema.
|
|
300
|
+
*/
|
|
301
|
+
constructor(db, options = {}) {
|
|
227
302
|
this.db = db;
|
|
303
|
+
this.t = mediaPgTables(options.schema);
|
|
228
304
|
}
|
|
229
305
|
/** The columns to read: `tenant_id` only once tenancy is on. */
|
|
230
306
|
get columns() {
|
|
231
|
-
return selectable(getTableColumns(
|
|
307
|
+
return selectable(getTableColumns(this.t.media), this.tenancy);
|
|
232
308
|
}
|
|
233
309
|
/**
|
|
234
310
|
* Tenancy: checks the `tenant_id` column exists, then reads and writes it. Called by the
|
|
@@ -236,66 +312,66 @@ var DrizzlePgMediaStore = class {
|
|
|
236
312
|
*/
|
|
237
313
|
async enableTenancy() {
|
|
238
314
|
await assertTenantColumn("media", () => this.db.select({
|
|
239
|
-
tenantId:
|
|
240
|
-
}).from(
|
|
315
|
+
tenantId: this.t.media.tenantId
|
|
316
|
+
}).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);`.");
|
|
241
317
|
this.tenancy = true;
|
|
242
318
|
}
|
|
243
319
|
async save(record) {
|
|
244
320
|
const row = toRow(record, this.tenancy);
|
|
245
321
|
const { id: _id, ...rest } = row;
|
|
246
|
-
await this.db.insert(this.tenancy ?
|
|
247
|
-
target:
|
|
322
|
+
await this.db.insert(this.tenancy ? this.t.media : this.t.mediaWithoutTenant).values(row).onConflictDoUpdate({
|
|
323
|
+
target: this.t.media.id,
|
|
248
324
|
set: rest
|
|
249
325
|
});
|
|
250
326
|
return row;
|
|
251
327
|
}
|
|
252
328
|
async find(id) {
|
|
253
|
-
const rows = await this.db.select(this.columns).from(
|
|
329
|
+
const rows = await this.db.select(this.columns).from(this.t.media).where(eq2(this.t.media.id, id)).limit(1);
|
|
254
330
|
const row = rows[0];
|
|
255
331
|
return row ? toRecord(row) : null;
|
|
256
332
|
}
|
|
257
333
|
async listByOwner(ownerType, ownerId, collection) {
|
|
258
|
-
const where = collection === void 0 ? and(eq2(
|
|
259
|
-
const rows = await this.db.select(this.columns).from(
|
|
334
|
+
const where = collection === void 0 ? and(eq2(this.t.media.ownerType, ownerType), eq2(this.t.media.ownerId, ownerId)) : and(eq2(this.t.media.ownerType, ownerType), eq2(this.t.media.ownerId, ownerId), eq2(this.t.media.collection, collection));
|
|
335
|
+
const rows = await this.db.select(this.columns).from(this.t.media).where(where).orderBy(asc(this.t.media.order), asc(this.t.media.id));
|
|
260
336
|
return rows.map(toRecord);
|
|
261
337
|
}
|
|
262
338
|
async delete(id) {
|
|
263
|
-
await this.db.delete(
|
|
339
|
+
await this.db.delete(this.t.media).where(eq2(this.t.media.id, id));
|
|
264
340
|
}
|
|
265
341
|
async nextOrder(ownerType, ownerId, collection) {
|
|
266
342
|
const rows = await this.db.select({
|
|
267
|
-
max: max(
|
|
268
|
-
}).from(
|
|
343
|
+
max: max(this.t.media.order)
|
|
344
|
+
}).from(this.t.media).where(and(eq2(this.t.media.ownerType, ownerType), eq2(this.t.media.ownerId, ownerId), eq2(this.t.media.collection, collection)));
|
|
269
345
|
const top = rows[0]?.max;
|
|
270
346
|
return top == null ? 0 : Number(top) + 1;
|
|
271
347
|
}
|
|
272
348
|
async count(filter = {}) {
|
|
273
349
|
const conditions = [
|
|
274
350
|
...filter.ownerType !== void 0 ? [
|
|
275
|
-
eq2(
|
|
351
|
+
eq2(this.t.media.ownerType, filter.ownerType)
|
|
276
352
|
] : [],
|
|
277
353
|
...filter.collection !== void 0 ? [
|
|
278
|
-
eq2(
|
|
354
|
+
eq2(this.t.media.collection, filter.collection)
|
|
279
355
|
] : [],
|
|
280
356
|
...filter.disk !== void 0 ? [
|
|
281
|
-
eq2(
|
|
357
|
+
eq2(this.t.media.disk, filter.disk)
|
|
282
358
|
] : [],
|
|
283
359
|
...filter.tenant ? [
|
|
284
|
-
tenantCondition(
|
|
360
|
+
tenantCondition(this.t.media.tenantId, filter.tenant)
|
|
285
361
|
] : []
|
|
286
362
|
];
|
|
287
363
|
const rows = await this.db.select({
|
|
288
364
|
value: count()
|
|
289
|
-
}).from(
|
|
365
|
+
}).from(this.t.media).where(conditions.length ? and(...conditions) : void 0);
|
|
290
366
|
return Number(rows[0]?.value ?? 0);
|
|
291
367
|
}
|
|
292
368
|
async aggregate(query) {
|
|
293
|
-
const column = query.groupBy === "collection" ?
|
|
369
|
+
const column = query.groupBy === "collection" ? this.t.media.collection : this.t.media.disk;
|
|
294
370
|
const rows = await this.db.select({
|
|
295
371
|
key: column,
|
|
296
372
|
count: count(),
|
|
297
|
-
sumSize: sum(
|
|
298
|
-
}).from(
|
|
373
|
+
sumSize: sum(this.t.media.size)
|
|
374
|
+
}).from(this.t.media).where(query.tenant ? tenantCondition(this.t.media.tenantId, query.tenant) : void 0).groupBy(column);
|
|
299
375
|
return rows.map((row) => ({
|
|
300
376
|
key: row.key,
|
|
301
377
|
count: Number(row.count),
|
|
@@ -308,22 +384,25 @@ var DrizzlePgMediaStore = class {
|
|
|
308
384
|
const cursor = page.cursor ? decodeListCursor(page.cursor) : null;
|
|
309
385
|
const filterConditions = [
|
|
310
386
|
...filter.ownerType !== void 0 ? [
|
|
311
|
-
eq2(
|
|
387
|
+
eq2(this.t.media.ownerType, filter.ownerType)
|
|
312
388
|
] : [],
|
|
313
389
|
...filter.collection !== void 0 ? [
|
|
314
|
-
eq2(
|
|
390
|
+
eq2(this.t.media.collection, filter.collection)
|
|
315
391
|
] : [],
|
|
316
392
|
...filter.disk !== void 0 ? [
|
|
317
|
-
eq2(
|
|
393
|
+
eq2(this.t.media.disk, filter.disk)
|
|
394
|
+
] : [],
|
|
395
|
+
...filter.nameContains !== void 0 ? [
|
|
396
|
+
nameContainsCondition(this.t.media.name, this.t.media.fileName, filter.nameContains)
|
|
318
397
|
] : [],
|
|
319
398
|
...filter.tenant ? [
|
|
320
|
-
tenantCondition(
|
|
399
|
+
tenantCondition(this.t.media.tenantId, filter.tenant)
|
|
321
400
|
] : [],
|
|
322
401
|
...cursor ? [
|
|
323
|
-
or2(gt(
|
|
402
|
+
or2(gt(this.t.media.createdAt, cursor.createdAt), and(eq2(this.t.media.createdAt, cursor.createdAt), gt(this.t.media.id, cursor.id)))
|
|
324
403
|
] : []
|
|
325
404
|
];
|
|
326
|
-
const rows = await this.db.select(this.columns).from(
|
|
405
|
+
const rows = await this.db.select(this.columns).from(this.t.media).where(filterConditions.length ? and(...filterConditions) : void 0).orderBy(asc(this.t.media.createdAt), asc(this.t.media.id)).limit(limit + 1);
|
|
327
406
|
const hasMore = rows.length > limit;
|
|
328
407
|
const records = rows.slice(0, limit).map(toRecord);
|
|
329
408
|
const result = {
|
|
@@ -338,7 +417,7 @@ var DrizzlePgMediaStore = class {
|
|
|
338
417
|
};
|
|
339
418
|
|
|
340
419
|
// src/pg/drizzle-pg-upload-session-store.ts
|
|
341
|
-
import { and as and2, asc as asc2, eq as eq3, getTableColumns as getTableColumns2, like, sql as
|
|
420
|
+
import { and as and2, asc as asc2, eq as eq3, getTableColumns as getTableColumns2, like, sql as sql3 } from "drizzle-orm";
|
|
342
421
|
function escapeLike(value) {
|
|
343
422
|
return value.replace(/[\\%_]/g, (char) => `\\${char}`);
|
|
344
423
|
}
|
|
@@ -382,16 +461,22 @@ var DrizzlePgUploadSessionStore = class {
|
|
|
382
461
|
}
|
|
383
462
|
db;
|
|
384
463
|
tenancy = false;
|
|
385
|
-
|
|
464
|
+
t;
|
|
465
|
+
/**
|
|
466
|
+
* `schema`: the Postgres schema of `media_upload_sessions` / `media_upload_parts`. Every
|
|
467
|
+
* statement names it (`search_path` is never used). Default: unqualified.
|
|
468
|
+
*/
|
|
469
|
+
constructor(db, options = {}) {
|
|
386
470
|
this.db = db;
|
|
471
|
+
this.t = mediaPgTables(options.schema);
|
|
387
472
|
}
|
|
388
473
|
/** Off: the table without `tenant_id`, which Drizzle would otherwise list in an INSERT. */
|
|
389
474
|
get sessionsTable() {
|
|
390
|
-
return this.tenancy ?
|
|
475
|
+
return this.tenancy ? this.t.uploadSessions : this.t.uploadSessionsWithoutTenant;
|
|
391
476
|
}
|
|
392
477
|
/** The columns to read: `tenant_id` only once tenancy is on. */
|
|
393
478
|
get columns() {
|
|
394
|
-
return selectable(getTableColumns2(
|
|
479
|
+
return selectable(getTableColumns2(this.t.uploadSessions), this.tenancy);
|
|
395
480
|
}
|
|
396
481
|
/**
|
|
397
482
|
* Tenancy: checks the `tenant_id` column exists, then reads and writes it. Called by the upload
|
|
@@ -399,8 +484,8 @@ var DrizzlePgUploadSessionStore = class {
|
|
|
399
484
|
*/
|
|
400
485
|
async enableTenancy() {
|
|
401
486
|
await assertTenantColumn("media_upload_sessions", () => this.db.select({
|
|
402
|
-
tenantId:
|
|
403
|
-
}).from(
|
|
487
|
+
tenantId: this.t.uploadSessions.tenantId
|
|
488
|
+
}).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);`.");
|
|
404
489
|
this.tenancy = true;
|
|
405
490
|
}
|
|
406
491
|
/** The session as persisted: off, no `tenantId`; on, `tenantId` or `null`. */
|
|
@@ -425,7 +510,7 @@ var DrizzlePgUploadSessionStore = class {
|
|
|
425
510
|
};
|
|
426
511
|
}
|
|
427
512
|
async get(id) {
|
|
428
|
-
const rows = await this.db.select(this.columns).from(
|
|
513
|
+
const rows = await this.db.select(this.columns).from(this.t.uploadSessions).where(eq3(this.t.uploadSessions.id, id)).limit(1);
|
|
429
514
|
const row = rows[0];
|
|
430
515
|
return row ? toSession(row) : null;
|
|
431
516
|
}
|
|
@@ -441,34 +526,34 @@ var DrizzlePgUploadSessionStore = class {
|
|
|
441
526
|
id: session.id,
|
|
442
527
|
...columns
|
|
443
528
|
}).onConflictDoUpdate({
|
|
444
|
-
target:
|
|
529
|
+
target: this.t.uploadSessions.id,
|
|
445
530
|
set: columns
|
|
446
531
|
});
|
|
447
532
|
return this.persisted(session);
|
|
448
533
|
}
|
|
449
534
|
async delete(id) {
|
|
450
|
-
await this.db.delete(
|
|
535
|
+
await this.db.delete(this.t.uploadSessions).where(eq3(this.t.uploadSessions.id, id));
|
|
451
536
|
}
|
|
452
537
|
async addPart(id, part) {
|
|
453
|
-
await this.db.insert(
|
|
538
|
+
await this.db.insert(this.t.uploadParts).values({
|
|
454
539
|
sessionId: id,
|
|
455
540
|
partNumber: part.partNumber,
|
|
456
541
|
etag: part.etag
|
|
457
542
|
}).onConflictDoUpdate({
|
|
458
543
|
target: [
|
|
459
|
-
|
|
460
|
-
|
|
544
|
+
this.t.uploadParts.sessionId,
|
|
545
|
+
this.t.uploadParts.partNumber
|
|
461
546
|
],
|
|
462
547
|
set: {
|
|
463
|
-
etag:
|
|
548
|
+
etag: sql3`excluded.etag`
|
|
464
549
|
}
|
|
465
550
|
});
|
|
466
551
|
}
|
|
467
552
|
async listParts(id) {
|
|
468
553
|
const rows = await this.db.select({
|
|
469
|
-
partNumber:
|
|
470
|
-
etag:
|
|
471
|
-
}).from(
|
|
554
|
+
partNumber: this.t.uploadParts.partNumber,
|
|
555
|
+
etag: this.t.uploadParts.etag
|
|
556
|
+
}).from(this.t.uploadParts).where(eq3(this.t.uploadParts.sessionId, id)).orderBy(asc2(this.t.uploadParts.partNumber));
|
|
472
557
|
return rows.map((row) => ({
|
|
473
558
|
partNumber: Number(row.partNumber),
|
|
474
559
|
etag: row.etag
|
|
@@ -477,16 +562,16 @@ var DrizzlePgUploadSessionStore = class {
|
|
|
477
562
|
async list(filter = {}) {
|
|
478
563
|
const conditions = [
|
|
479
564
|
...filter.disk !== void 0 ? [
|
|
480
|
-
eq3(
|
|
565
|
+
eq3(this.t.uploadSessions.disk, filter.disk)
|
|
481
566
|
] : [],
|
|
482
567
|
...filter.keyPrefix !== void 0 ? [
|
|
483
|
-
like(
|
|
568
|
+
like(this.t.uploadSessions.key, `${escapeLike(filter.keyPrefix)}%`)
|
|
484
569
|
] : [],
|
|
485
570
|
...filter.tenant ? [
|
|
486
|
-
tenantCondition(
|
|
571
|
+
tenantCondition(this.t.uploadSessions.tenantId, filter.tenant)
|
|
487
572
|
] : []
|
|
488
573
|
];
|
|
489
|
-
const rows = await this.db.select(this.columns).from(
|
|
574
|
+
const rows = await this.db.select(this.columns).from(this.t.uploadSessions).where(conditions.length ? and2(...conditions) : void 0).orderBy(asc2(this.t.uploadSessions.createdAt), asc2(this.t.uploadSessions.id));
|
|
490
575
|
return rows.map(toSession);
|
|
491
576
|
}
|
|
492
577
|
};
|
|
@@ -495,7 +580,9 @@ export {
|
|
|
495
580
|
DrizzlePgUploadSessionStore,
|
|
496
581
|
MEDIA_PG_DDL,
|
|
497
582
|
createMediaPgTables,
|
|
583
|
+
mediaPgDdl,
|
|
498
584
|
mediaPgTable,
|
|
585
|
+
mediaPgTables,
|
|
499
586
|
mediaUploadPartsPgTable,
|
|
500
587
|
mediaUploadSessionsPgTable
|
|
501
588
|
};
|