@dudousxd/nestjs-media-database-drizzle 0.8.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.
@@ -97,6 +97,21 @@ var mediaTable = (0, import_sqlite_core2.sqliteTable)("media", {
97
97
  tenantIdx: (0, import_sqlite_core2.index)("idx_media_tenant").on(table.tenantId, table.ownerType, table.ownerId, table.collection)
98
98
  }));
99
99
 
100
+ // src/schema-name.ts
101
+ var SCHEMA_NAME = /^[A-Za-z_][A-Za-z0-9_$]*$/;
102
+ function assertSchemaName(schema) {
103
+ if (typeof schema !== "string" || !SCHEMA_NAME.test(schema) || schema.length > 63) {
104
+ 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)}`);
105
+ }
106
+ }
107
+ __name(assertSchemaName, "assertSchemaName");
108
+ function rejectSqliteSchema(schema) {
109
+ if (schema !== void 0) {
110
+ 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\`).`);
111
+ }
112
+ }
113
+ __name(rejectSqliteSchema, "rejectSqliteSchema");
114
+
100
115
  // src/tenancy.ts
101
116
  var import_nestjs_media_core = require("@dudousxd/nestjs-media-core");
102
117
  var import_drizzle_orm = require("drizzle-orm");
@@ -121,6 +136,11 @@ async function assertTenantColumn(table, probe, fix) {
121
136
  }
122
137
  }
123
138
  __name(assertTenantColumn, "assertTenantColumn");
139
+ function nameContainsCondition(name, fileName, text5) {
140
+ const pattern = `%${text5.toLowerCase().replace(/[!%_]/g, (c) => `!${c}`)}%`;
141
+ return (0, import_drizzle_orm.or)(import_drizzle_orm.sql`lower(${name}) like ${pattern} escape '!'`, import_drizzle_orm.sql`lower(${fileName}) like ${pattern} escape '!'`);
142
+ }
143
+ __name(nameContainsCondition, "nameContainsCondition");
124
144
 
125
145
  // src/drizzle-media-store.ts
126
146
  function toRow(record, tenancy) {
@@ -137,8 +157,10 @@ var DrizzleMediaStore = class {
137
157
  }
138
158
  db;
139
159
  tenancy = false;
140
- constructor(db) {
160
+ /** `schema` is for Postgres only: SQLite has no schemas, so setting it throws. */
161
+ constructor(db, options = {}) {
141
162
  this.db = db;
163
+ rejectSqliteSchema(options.schema);
142
164
  }
143
165
  /** The columns to read: `tenant_id` only once tenancy is on. */
144
166
  get columns() {
@@ -227,6 +249,9 @@ var DrizzleMediaStore = class {
227
249
  ...filter.disk !== void 0 ? [
228
250
  (0, import_drizzle_orm2.eq)(mediaTable.disk, filter.disk)
229
251
  ] : [],
252
+ ...filter.nameContains !== void 0 ? [
253
+ nameContainsCondition(mediaTable.name, mediaTable.fileName, filter.nameContains)
254
+ ] : [],
230
255
  ...filter.tenant ? [
231
256
  tenantCondition(mediaTable.tenantId, filter.tenant)
232
257
  ] : [],
@@ -253,6 +278,9 @@ var DrizzleMediaStore = class {
253
278
  // src/pg/drizzle-pg-media-store.ts
254
279
  var import_drizzle_orm3 = require("drizzle-orm");
255
280
 
281
+ // src/pg/schema.ts
282
+ var import_pg_core2 = require("drizzle-orm/pg-core");
283
+
256
284
  // src/pg/columns.ts
257
285
  var import_pg_core = require("drizzle-orm/pg-core");
258
286
  function mediaPgColumns() {
@@ -309,44 +337,69 @@ function uploadSessionPgColumns() {
309
337
  };
310
338
  }
311
339
  __name(uploadSessionPgColumns, "uploadSessionPgColumns");
312
- var mediaPgTableWithoutTenant = (0, import_pg_core.pgTable)("media", mediaPgColumns());
313
- var mediaUploadSessionsPgTableWithoutTenant = (0, import_pg_core.pgTable)("media_upload_sessions", uploadSessionPgColumns());
314
340
 
315
341
  // src/pg/schema.ts
316
- var import_pg_core2 = require("drizzle-orm/pg-core");
317
- var mediaPgTable = (0, import_pg_core2.pgTable)("media", {
318
- ...mediaPgColumns(),
319
- /** Tenancy only: read and written once the store's `enableTenancy()` ran. */
320
- tenantId: (0, import_pg_core2.text)("tenant_id")
321
- }, (table) => [
322
- (0, import_pg_core2.index)("idx_media_owner").on(table.ownerType, table.ownerId, table.collection),
323
- (0, import_pg_core2.index)("idx_media_collection").on(table.collection),
324
- (0, import_pg_core2.index)("idx_media_disk").on(table.disk),
325
- (0, import_pg_core2.index)("idx_media_created_at").on(table.createdAt),
326
- (0, import_pg_core2.index)("idx_media_collection_created_at").on(table.collection, table.createdAt, table.id),
327
- (0, import_pg_core2.index)("idx_media_tenant").on(table.tenantId, table.ownerType, table.ownerId, table.collection)
328
- ]);
329
- var mediaUploadSessionsPgTable = (0, import_pg_core2.pgTable)("media_upload_sessions", {
330
- ...uploadSessionPgColumns(),
331
- /** Tenancy only: read and written once the store's `enableTenancy()` ran. */
332
- tenantId: (0, import_pg_core2.text)("tenant_id")
333
- }, (table) => [
334
- (0, import_pg_core2.index)("idx_media_upload_sessions_tenant").on(table.tenantId)
335
- ]);
336
- var mediaUploadPartsPgTable = (0, import_pg_core2.pgTable)("media_upload_parts", {
337
- sessionId: (0, import_pg_core2.text)("session_id").notNull().references(() => mediaUploadSessionsPgTable.id, {
338
- onDelete: "cascade"
339
- }),
340
- partNumber: (0, import_pg_core2.integer)("part_number").notNull(),
341
- etag: (0, import_pg_core2.text)("etag").notNull()
342
- }, (table) => [
343
- (0, import_pg_core2.primaryKey)({
344
- columns: [
345
- table.sessionId,
346
- table.partNumber
347
- ]
348
- })
349
- ]);
342
+ function tableIn(schema) {
343
+ if (schema === void 0) return import_pg_core2.pgTable;
344
+ assertSchemaName(schema);
345
+ if (schema === "public") {
346
+ throw new TypeError("[nestjs-media] `schema: 'public'` is Postgres' default schema: leave `schema` unset to use it.");
347
+ }
348
+ return (0, import_pg_core2.pgSchema)(schema).table;
349
+ }
350
+ __name(tableIn, "tableIn");
351
+ function mediaPgTables(schema) {
352
+ const table = tableIn(schema);
353
+ const media = table("media", {
354
+ ...mediaPgColumns(),
355
+ /** Tenancy only: read and written once the store's `enableTenancy()` ran. */
356
+ tenantId: (0, import_pg_core2.text)("tenant_id")
357
+ }, (t) => [
358
+ (0, import_pg_core2.index)("idx_media_owner").on(t.ownerType, t.ownerId, t.collection),
359
+ (0, import_pg_core2.index)("idx_media_collection").on(t.collection),
360
+ (0, import_pg_core2.index)("idx_media_disk").on(t.disk),
361
+ (0, import_pg_core2.index)("idx_media_created_at").on(t.createdAt),
362
+ (0, import_pg_core2.index)("idx_media_collection_created_at").on(t.collection, t.createdAt, t.id),
363
+ (0, import_pg_core2.index)("idx_media_tenant").on(t.tenantId, t.ownerType, t.ownerId, t.collection)
364
+ ]);
365
+ const uploadSessions = table("media_upload_sessions", {
366
+ ...uploadSessionPgColumns(),
367
+ /** Tenancy only: read and written once the store's `enableTenancy()` ran. */
368
+ tenantId: (0, import_pg_core2.text)("tenant_id")
369
+ }, (t) => [
370
+ (0, import_pg_core2.index)("idx_media_upload_sessions_tenant").on(t.tenantId)
371
+ ]);
372
+ const uploadParts = table("media_upload_parts", {
373
+ sessionId: (0, import_pg_core2.text)("session_id").notNull().references(() => uploadSessions.id, {
374
+ onDelete: "cascade"
375
+ }),
376
+ partNumber: (0, import_pg_core2.integer)("part_number").notNull(),
377
+ etag: (0, import_pg_core2.text)("etag").notNull()
378
+ }, (t) => [
379
+ (0, import_pg_core2.primaryKey)({
380
+ columns: [
381
+ t.sessionId,
382
+ t.partNumber
383
+ ]
384
+ })
385
+ ]);
386
+ return {
387
+ media,
388
+ uploadSessions,
389
+ uploadParts,
390
+ /**
391
+ * Internal: the tables without `tenant_id`. The stores insert through them while tenancy is
392
+ * off, because Drizzle lists every column of a table in an `INSERT`.
393
+ */
394
+ mediaWithoutTenant: table("media", mediaPgColumns()),
395
+ uploadSessionsWithoutTenant: table("media_upload_sessions", uploadSessionPgColumns())
396
+ };
397
+ }
398
+ __name(mediaPgTables, "mediaPgTables");
399
+ var unqualified = mediaPgTables();
400
+ var mediaPgTable = unqualified.media;
401
+ var mediaUploadSessionsPgTable = unqualified.uploadSessions;
402
+ var mediaUploadPartsPgTable = unqualified.uploadParts;
350
403
 
351
404
  // src/pg/drizzle-pg-media-store.ts
352
405
  function toRecord(row) {
@@ -370,12 +423,18 @@ var DrizzlePgMediaStore = class {
370
423
  }
371
424
  db;
372
425
  tenancy = false;
373
- constructor(db) {
426
+ t;
427
+ /**
428
+ * `schema`: the Postgres schema the `media` table lives in. Every statement names it
429
+ * (`search_path` is never used). Default: unqualified, the connection's current schema.
430
+ */
431
+ constructor(db, options = {}) {
374
432
  this.db = db;
433
+ this.t = mediaPgTables(options.schema);
375
434
  }
376
435
  /** The columns to read: `tenant_id` only once tenancy is on. */
377
436
  get columns() {
378
- return selectable((0, import_drizzle_orm3.getTableColumns)(mediaPgTable), this.tenancy);
437
+ return selectable((0, import_drizzle_orm3.getTableColumns)(this.t.media), this.tenancy);
379
438
  }
380
439
  /**
381
440
  * Tenancy: checks the `tenant_id` column exists, then reads and writes it. Called by the
@@ -383,66 +442,66 @@ var DrizzlePgMediaStore = class {
383
442
  */
384
443
  async enableTenancy() {
385
444
  await assertTenantColumn("media", () => this.db.select({
386
- tenantId: mediaPgTable.tenantId
387
- }).from(mediaPgTable).limit(1), "Generate a migration with drizzle-kit from `mediaPgTable` (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);`.");
445
+ tenantId: this.t.media.tenantId
446
+ }).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);`.");
388
447
  this.tenancy = true;
389
448
  }
390
449
  async save(record) {
391
450
  const row = toRow2(record, this.tenancy);
392
451
  const { id: _id, ...rest } = row;
393
- await this.db.insert(this.tenancy ? mediaPgTable : mediaPgTableWithoutTenant).values(row).onConflictDoUpdate({
394
- target: mediaPgTable.id,
452
+ await this.db.insert(this.tenancy ? this.t.media : this.t.mediaWithoutTenant).values(row).onConflictDoUpdate({
453
+ target: this.t.media.id,
395
454
  set: rest
396
455
  });
397
456
  return row;
398
457
  }
399
458
  async find(id) {
400
- const rows = await this.db.select(this.columns).from(mediaPgTable).where((0, import_drizzle_orm3.eq)(mediaPgTable.id, id)).limit(1);
459
+ const rows = await this.db.select(this.columns).from(this.t.media).where((0, import_drizzle_orm3.eq)(this.t.media.id, id)).limit(1);
401
460
  const row = rows[0];
402
461
  return row ? toRecord(row) : null;
403
462
  }
404
463
  async listByOwner(ownerType, ownerId, collection) {
405
- const where = collection === void 0 ? (0, import_drizzle_orm3.and)((0, import_drizzle_orm3.eq)(mediaPgTable.ownerType, ownerType), (0, import_drizzle_orm3.eq)(mediaPgTable.ownerId, ownerId)) : (0, import_drizzle_orm3.and)((0, import_drizzle_orm3.eq)(mediaPgTable.ownerType, ownerType), (0, import_drizzle_orm3.eq)(mediaPgTable.ownerId, ownerId), (0, import_drizzle_orm3.eq)(mediaPgTable.collection, collection));
406
- const rows = await this.db.select(this.columns).from(mediaPgTable).where(where).orderBy((0, import_drizzle_orm3.asc)(mediaPgTable.order), (0, import_drizzle_orm3.asc)(mediaPgTable.id));
464
+ const where = collection === void 0 ? (0, import_drizzle_orm3.and)((0, import_drizzle_orm3.eq)(this.t.media.ownerType, ownerType), (0, import_drizzle_orm3.eq)(this.t.media.ownerId, ownerId)) : (0, import_drizzle_orm3.and)((0, import_drizzle_orm3.eq)(this.t.media.ownerType, ownerType), (0, import_drizzle_orm3.eq)(this.t.media.ownerId, ownerId), (0, import_drizzle_orm3.eq)(this.t.media.collection, collection));
465
+ const rows = await this.db.select(this.columns).from(this.t.media).where(where).orderBy((0, import_drizzle_orm3.asc)(this.t.media.order), (0, import_drizzle_orm3.asc)(this.t.media.id));
407
466
  return rows.map(toRecord);
408
467
  }
409
468
  async delete(id) {
410
- await this.db.delete(mediaPgTable).where((0, import_drizzle_orm3.eq)(mediaPgTable.id, id));
469
+ await this.db.delete(this.t.media).where((0, import_drizzle_orm3.eq)(this.t.media.id, id));
411
470
  }
412
471
  async nextOrder(ownerType, ownerId, collection) {
413
472
  const rows = await this.db.select({
414
- max: (0, import_drizzle_orm3.max)(mediaPgTable.order)
415
- }).from(mediaPgTable).where((0, import_drizzle_orm3.and)((0, import_drizzle_orm3.eq)(mediaPgTable.ownerType, ownerType), (0, import_drizzle_orm3.eq)(mediaPgTable.ownerId, ownerId), (0, import_drizzle_orm3.eq)(mediaPgTable.collection, collection)));
473
+ max: (0, import_drizzle_orm3.max)(this.t.media.order)
474
+ }).from(this.t.media).where((0, import_drizzle_orm3.and)((0, import_drizzle_orm3.eq)(this.t.media.ownerType, ownerType), (0, import_drizzle_orm3.eq)(this.t.media.ownerId, ownerId), (0, import_drizzle_orm3.eq)(this.t.media.collection, collection)));
416
475
  const top = rows[0]?.max;
417
476
  return top == null ? 0 : Number(top) + 1;
418
477
  }
419
478
  async count(filter = {}) {
420
479
  const conditions = [
421
480
  ...filter.ownerType !== void 0 ? [
422
- (0, import_drizzle_orm3.eq)(mediaPgTable.ownerType, filter.ownerType)
481
+ (0, import_drizzle_orm3.eq)(this.t.media.ownerType, filter.ownerType)
423
482
  ] : [],
424
483
  ...filter.collection !== void 0 ? [
425
- (0, import_drizzle_orm3.eq)(mediaPgTable.collection, filter.collection)
484
+ (0, import_drizzle_orm3.eq)(this.t.media.collection, filter.collection)
426
485
  ] : [],
427
486
  ...filter.disk !== void 0 ? [
428
- (0, import_drizzle_orm3.eq)(mediaPgTable.disk, filter.disk)
487
+ (0, import_drizzle_orm3.eq)(this.t.media.disk, filter.disk)
429
488
  ] : [],
430
489
  ...filter.tenant ? [
431
- tenantCondition(mediaPgTable.tenantId, filter.tenant)
490
+ tenantCondition(this.t.media.tenantId, filter.tenant)
432
491
  ] : []
433
492
  ];
434
493
  const rows = await this.db.select({
435
494
  value: (0, import_drizzle_orm3.count)()
436
- }).from(mediaPgTable).where(conditions.length ? (0, import_drizzle_orm3.and)(...conditions) : void 0);
495
+ }).from(this.t.media).where(conditions.length ? (0, import_drizzle_orm3.and)(...conditions) : void 0);
437
496
  return Number(rows[0]?.value ?? 0);
438
497
  }
439
498
  async aggregate(query) {
440
- const column = query.groupBy === "collection" ? mediaPgTable.collection : mediaPgTable.disk;
499
+ const column = query.groupBy === "collection" ? this.t.media.collection : this.t.media.disk;
441
500
  const rows = await this.db.select({
442
501
  key: column,
443
502
  count: (0, import_drizzle_orm3.count)(),
444
- sumSize: (0, import_drizzle_orm3.sum)(mediaPgTable.size)
445
- }).from(mediaPgTable).where(query.tenant ? tenantCondition(mediaPgTable.tenantId, query.tenant) : void 0).groupBy(column);
503
+ sumSize: (0, import_drizzle_orm3.sum)(this.t.media.size)
504
+ }).from(this.t.media).where(query.tenant ? tenantCondition(this.t.media.tenantId, query.tenant) : void 0).groupBy(column);
446
505
  return rows.map((row) => ({
447
506
  key: row.key,
448
507
  count: Number(row.count),
@@ -455,22 +514,25 @@ var DrizzlePgMediaStore = class {
455
514
  const cursor = page.cursor ? decodeListCursor(page.cursor) : null;
456
515
  const filterConditions = [
457
516
  ...filter.ownerType !== void 0 ? [
458
- (0, import_drizzle_orm3.eq)(mediaPgTable.ownerType, filter.ownerType)
517
+ (0, import_drizzle_orm3.eq)(this.t.media.ownerType, filter.ownerType)
459
518
  ] : [],
460
519
  ...filter.collection !== void 0 ? [
461
- (0, import_drizzle_orm3.eq)(mediaPgTable.collection, filter.collection)
520
+ (0, import_drizzle_orm3.eq)(this.t.media.collection, filter.collection)
462
521
  ] : [],
463
522
  ...filter.disk !== void 0 ? [
464
- (0, import_drizzle_orm3.eq)(mediaPgTable.disk, filter.disk)
523
+ (0, import_drizzle_orm3.eq)(this.t.media.disk, filter.disk)
524
+ ] : [],
525
+ ...filter.nameContains !== void 0 ? [
526
+ nameContainsCondition(this.t.media.name, this.t.media.fileName, filter.nameContains)
465
527
  ] : [],
466
528
  ...filter.tenant ? [
467
- tenantCondition(mediaPgTable.tenantId, filter.tenant)
529
+ tenantCondition(this.t.media.tenantId, filter.tenant)
468
530
  ] : [],
469
531
  ...cursor ? [
470
- (0, import_drizzle_orm3.or)((0, import_drizzle_orm3.gt)(mediaPgTable.createdAt, cursor.createdAt), (0, import_drizzle_orm3.and)((0, import_drizzle_orm3.eq)(mediaPgTable.createdAt, cursor.createdAt), (0, import_drizzle_orm3.gt)(mediaPgTable.id, cursor.id)))
532
+ (0, import_drizzle_orm3.or)((0, import_drizzle_orm3.gt)(this.t.media.createdAt, cursor.createdAt), (0, import_drizzle_orm3.and)((0, import_drizzle_orm3.eq)(this.t.media.createdAt, cursor.createdAt), (0, import_drizzle_orm3.gt)(this.t.media.id, cursor.id)))
471
533
  ] : []
472
534
  ];
473
- const rows = await this.db.select(this.columns).from(mediaPgTable).where(filterConditions.length ? (0, import_drizzle_orm3.and)(...filterConditions) : void 0).orderBy((0, import_drizzle_orm3.asc)(mediaPgTable.createdAt), (0, import_drizzle_orm3.asc)(mediaPgTable.id)).limit(limit + 1);
535
+ const rows = await this.db.select(this.columns).from(this.t.media).where(filterConditions.length ? (0, import_drizzle_orm3.and)(...filterConditions) : void 0).orderBy((0, import_drizzle_orm3.asc)(this.t.media.createdAt), (0, import_drizzle_orm3.asc)(this.t.media.id)).limit(limit + 1);
474
536
  const hasMore = rows.length > limit;
475
537
  const records = rows.slice(0, limit).map(toRecord);
476
538
  const result = {
@@ -529,16 +591,22 @@ var DrizzlePgUploadSessionStore = class {
529
591
  }
530
592
  db;
531
593
  tenancy = false;
532
- constructor(db) {
594
+ t;
595
+ /**
596
+ * `schema`: the Postgres schema of `media_upload_sessions` / `media_upload_parts`. Every
597
+ * statement names it (`search_path` is never used). Default: unqualified.
598
+ */
599
+ constructor(db, options = {}) {
533
600
  this.db = db;
601
+ this.t = mediaPgTables(options.schema);
534
602
  }
535
603
  /** Off: the table without `tenant_id`, which Drizzle would otherwise list in an INSERT. */
536
604
  get sessionsTable() {
537
- return this.tenancy ? mediaUploadSessionsPgTable : mediaUploadSessionsPgTableWithoutTenant;
605
+ return this.tenancy ? this.t.uploadSessions : this.t.uploadSessionsWithoutTenant;
538
606
  }
539
607
  /** The columns to read: `tenant_id` only once tenancy is on. */
540
608
  get columns() {
541
- return selectable((0, import_drizzle_orm4.getTableColumns)(mediaUploadSessionsPgTable), this.tenancy);
609
+ return selectable((0, import_drizzle_orm4.getTableColumns)(this.t.uploadSessions), this.tenancy);
542
610
  }
543
611
  /**
544
612
  * Tenancy: checks the `tenant_id` column exists, then reads and writes it. Called by the upload
@@ -546,8 +614,8 @@ var DrizzlePgUploadSessionStore = class {
546
614
  */
547
615
  async enableTenancy() {
548
616
  await assertTenantColumn("media_upload_sessions", () => this.db.select({
549
- tenantId: mediaUploadSessionsPgTable.tenantId
550
- }).from(mediaUploadSessionsPgTable).limit(1), "Generate a migration with drizzle-kit from `mediaUploadSessionsPgTable` (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);`.");
617
+ tenantId: this.t.uploadSessions.tenantId
618
+ }).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);`.");
551
619
  this.tenancy = true;
552
620
  }
553
621
  /** The session as persisted: off, no `tenantId`; on, `tenantId` or `null`. */
@@ -572,7 +640,7 @@ var DrizzlePgUploadSessionStore = class {
572
640
  };
573
641
  }
574
642
  async get(id) {
575
- const rows = await this.db.select(this.columns).from(mediaUploadSessionsPgTable).where((0, import_drizzle_orm4.eq)(mediaUploadSessionsPgTable.id, id)).limit(1);
643
+ const rows = await this.db.select(this.columns).from(this.t.uploadSessions).where((0, import_drizzle_orm4.eq)(this.t.uploadSessions.id, id)).limit(1);
576
644
  const row = rows[0];
577
645
  return row ? toSession(row) : null;
578
646
  }
@@ -588,23 +656,23 @@ var DrizzlePgUploadSessionStore = class {
588
656
  id: session.id,
589
657
  ...columns
590
658
  }).onConflictDoUpdate({
591
- target: mediaUploadSessionsPgTable.id,
659
+ target: this.t.uploadSessions.id,
592
660
  set: columns
593
661
  });
594
662
  return this.persisted(session);
595
663
  }
596
664
  async delete(id) {
597
- await this.db.delete(mediaUploadSessionsPgTable).where((0, import_drizzle_orm4.eq)(mediaUploadSessionsPgTable.id, id));
665
+ await this.db.delete(this.t.uploadSessions).where((0, import_drizzle_orm4.eq)(this.t.uploadSessions.id, id));
598
666
  }
599
667
  async addPart(id, part) {
600
- await this.db.insert(mediaUploadPartsPgTable).values({
668
+ await this.db.insert(this.t.uploadParts).values({
601
669
  sessionId: id,
602
670
  partNumber: part.partNumber,
603
671
  etag: part.etag
604
672
  }).onConflictDoUpdate({
605
673
  target: [
606
- mediaUploadPartsPgTable.sessionId,
607
- mediaUploadPartsPgTable.partNumber
674
+ this.t.uploadParts.sessionId,
675
+ this.t.uploadParts.partNumber
608
676
  ],
609
677
  set: {
610
678
  etag: import_drizzle_orm4.sql`excluded.etag`
@@ -613,9 +681,9 @@ var DrizzlePgUploadSessionStore = class {
613
681
  }
614
682
  async listParts(id) {
615
683
  const rows = await this.db.select({
616
- partNumber: mediaUploadPartsPgTable.partNumber,
617
- etag: mediaUploadPartsPgTable.etag
618
- }).from(mediaUploadPartsPgTable).where((0, import_drizzle_orm4.eq)(mediaUploadPartsPgTable.sessionId, id)).orderBy((0, import_drizzle_orm4.asc)(mediaUploadPartsPgTable.partNumber));
684
+ partNumber: this.t.uploadParts.partNumber,
685
+ etag: this.t.uploadParts.etag
686
+ }).from(this.t.uploadParts).where((0, import_drizzle_orm4.eq)(this.t.uploadParts.sessionId, id)).orderBy((0, import_drizzle_orm4.asc)(this.t.uploadParts.partNumber));
619
687
  return rows.map((row) => ({
620
688
  partNumber: Number(row.partNumber),
621
689
  etag: row.etag
@@ -624,21 +692,29 @@ var DrizzlePgUploadSessionStore = class {
624
692
  async list(filter = {}) {
625
693
  const conditions = [
626
694
  ...filter.disk !== void 0 ? [
627
- (0, import_drizzle_orm4.eq)(mediaUploadSessionsPgTable.disk, filter.disk)
695
+ (0, import_drizzle_orm4.eq)(this.t.uploadSessions.disk, filter.disk)
628
696
  ] : [],
629
697
  ...filter.keyPrefix !== void 0 ? [
630
- (0, import_drizzle_orm4.like)(mediaUploadSessionsPgTable.key, `${escapeLike(filter.keyPrefix)}%`)
698
+ (0, import_drizzle_orm4.like)(this.t.uploadSessions.key, `${escapeLike(filter.keyPrefix)}%`)
631
699
  ] : [],
632
700
  ...filter.tenant ? [
633
- tenantCondition(mediaUploadSessionsPgTable.tenantId, filter.tenant)
701
+ tenantCondition(this.t.uploadSessions.tenantId, filter.tenant)
634
702
  ] : []
635
703
  ];
636
- const rows = await this.db.select(this.columns).from(mediaUploadSessionsPgTable).where(conditions.length ? (0, import_drizzle_orm4.and)(...conditions) : void 0).orderBy((0, import_drizzle_orm4.asc)(mediaUploadSessionsPgTable.createdAt), (0, import_drizzle_orm4.asc)(mediaUploadSessionsPgTable.id));
704
+ const rows = await this.db.select(this.columns).from(this.t.uploadSessions).where(conditions.length ? (0, import_drizzle_orm4.and)(...conditions) : void 0).orderBy((0, import_drizzle_orm4.asc)(this.t.uploadSessions.createdAt), (0, import_drizzle_orm4.asc)(this.t.uploadSessions.id));
637
705
  return rows.map(toSession);
638
706
  }
639
707
  };
640
708
 
641
709
  // src/nest/index.ts
710
+ function storeOptions(options) {
711
+ if (options.schema === void 0) return {};
712
+ mediaPgTables(options.schema);
713
+ return {
714
+ schema: options.schema
715
+ };
716
+ }
717
+ __name(storeOptions, "storeOptions");
642
718
  function factory(options, build) {
643
719
  return {
644
720
  inject: [
@@ -649,15 +725,18 @@ function factory(options, build) {
649
725
  }
650
726
  __name(factory, "factory");
651
727
  function drizzleMediaStore(options = {}) {
728
+ rejectSqliteSchema(options.schema);
652
729
  return factory(options, (db) => new DrizzleMediaStore(db));
653
730
  }
654
731
  __name(drizzleMediaStore, "drizzleMediaStore");
655
732
  function drizzlePgMediaStore(options = {}) {
656
- return factory(options, (db) => new DrizzlePgMediaStore(db));
733
+ const store = storeOptions(options);
734
+ return factory(options, (db) => new DrizzlePgMediaStore(db, store));
657
735
  }
658
736
  __name(drizzlePgMediaStore, "drizzlePgMediaStore");
659
737
  function drizzlePgUploadSessions(options = {}) {
660
- return factory(options, (db) => new DrizzlePgUploadSessionStore(db));
738
+ const store = storeOptions(options);
739
+ return factory(options, (db) => new DrizzlePgUploadSessionStore(db, store));
661
740
  }
662
741
  __name(drizzlePgUploadSessions, "drizzlePgUploadSessions");
663
742
  // Annotate the CommonJS export names for ESM import in node: