@dudousxd/nestjs-media-database-drizzle 0.7.0 → 0.8.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-B_rsYG0g.d.cts → drizzle-media-store-BHoJjohg.d.cts} +8 -0
- package/dist/{drizzle-media-store-B_rsYG0g.d.ts → drizzle-media-store-BHoJjohg.d.ts} +8 -0
- package/dist/{drizzle-pg-upload-session-store-DO2rMDNN.d.cts → drizzle-pg-upload-session-store-DWBmRKMi.d.cts} +20 -0
- package/dist/{drizzle-pg-upload-session-store-DO2rMDNN.d.ts → drizzle-pg-upload-session-store-DWBmRKMi.d.ts} +20 -0
- package/dist/index.cjs +141 -62
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +20 -1
- package/dist/index.d.ts +20 -1
- package/dist/index.js +124 -45
- package/dist/index.js.map +1 -1
- package/dist/nest/index.cjs +305 -151
- package/dist/nest/index.cjs.map +1 -1
- package/dist/nest/index.d.cts +2 -2
- package/dist/nest/index.d.ts +2 -2
- package/dist/nest/index.js +284 -130
- package/dist/nest/index.js.map +1 -1
- package/dist/pg/index.cjs +217 -100
- package/dist/pg/index.cjs.map +1 -1
- package/dist/pg/index.d.cts +39 -5
- package/dist/pg/index.d.ts +39 -5
- package/dist/pg/index.js +205 -88
- package/dist/pg/index.js.map +1 -1
- package/package.json +4 -4
package/dist/nest/index.cjs
CHANGED
|
@@ -29,7 +29,7 @@ module.exports = __toCommonJS(nest_exports);
|
|
|
29
29
|
var import_drizzle = require("@nestjs/drizzle");
|
|
30
30
|
|
|
31
31
|
// src/drizzle-media-store.ts
|
|
32
|
-
var
|
|
32
|
+
var import_drizzle_orm2 = require("drizzle-orm");
|
|
33
33
|
|
|
34
34
|
// src/list-cursor.ts
|
|
35
35
|
function encodeListCursor(record) {
|
|
@@ -50,99 +50,164 @@ function decodeListCursor(cursor) {
|
|
|
50
50
|
}
|
|
51
51
|
__name(decodeListCursor, "decodeListCursor");
|
|
52
52
|
|
|
53
|
-
// src/media.
|
|
53
|
+
// src/media-columns.ts
|
|
54
54
|
var import_sqlite_core = require("drizzle-orm/sqlite-core");
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
55
|
+
function mediaColumns() {
|
|
56
|
+
return {
|
|
57
|
+
id: (0, import_sqlite_core.text)("id").primaryKey(),
|
|
58
|
+
ownerType: (0, import_sqlite_core.text)("owner_type").notNull(),
|
|
59
|
+
ownerId: (0, import_sqlite_core.text)("owner_id").notNull(),
|
|
60
|
+
collection: (0, import_sqlite_core.text)("collection").notNull(),
|
|
61
|
+
name: (0, import_sqlite_core.text)("name").notNull(),
|
|
62
|
+
fileName: (0, import_sqlite_core.text)("file_name").notNull(),
|
|
63
|
+
mimeType: (0, import_sqlite_core.text)("mime_type").notNull(),
|
|
64
|
+
size: (0, import_sqlite_core.integer)("size").notNull(),
|
|
65
|
+
disk: (0, import_sqlite_core.text)("disk").notNull(),
|
|
66
|
+
path: (0, import_sqlite_core.text)("path").notNull(),
|
|
67
|
+
order: (0, import_sqlite_core.integer)("position").notNull(),
|
|
68
|
+
customProperties: (0, import_sqlite_core.text)("custom_properties", {
|
|
69
|
+
mode: "json"
|
|
70
|
+
}).notNull(),
|
|
71
|
+
conversions: (0, import_sqlite_core.text)("conversions", {
|
|
72
|
+
mode: "json"
|
|
73
|
+
}).notNull(),
|
|
74
|
+
createdAt: (0, import_sqlite_core.integer)("created_at", {
|
|
75
|
+
mode: "timestamp_ms"
|
|
76
|
+
}).notNull(),
|
|
77
|
+
updatedAt: (0, import_sqlite_core.integer)("updated_at", {
|
|
78
|
+
mode: "timestamp_ms"
|
|
79
|
+
}).notNull()
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
__name(mediaColumns, "mediaColumns");
|
|
83
|
+
var mediaTableWithoutTenant = (0, import_sqlite_core.sqliteTable)("media", mediaColumns());
|
|
84
|
+
|
|
85
|
+
// src/media.schema.ts
|
|
86
|
+
var import_sqlite_core2 = require("drizzle-orm/sqlite-core");
|
|
87
|
+
var mediaTable = (0, import_sqlite_core2.sqliteTable)("media", {
|
|
88
|
+
...mediaColumns(),
|
|
89
|
+
/** Tenancy only: read and written once the store's `enableTenancy()` ran. */
|
|
90
|
+
tenantId: (0, import_sqlite_core2.text)("tenant_id")
|
|
79
91
|
}, (table) => ({
|
|
80
|
-
ownerIdx: (0,
|
|
81
|
-
collectionIdx: (0,
|
|
82
|
-
diskIdx: (0,
|
|
83
|
-
createdAtIdx: (0,
|
|
84
|
-
collectionCreatedAtIdx: (0,
|
|
92
|
+
ownerIdx: (0, import_sqlite_core2.index)("idx_media_owner").on(table.ownerType, table.ownerId, table.collection),
|
|
93
|
+
collectionIdx: (0, import_sqlite_core2.index)("idx_media_collection").on(table.collection),
|
|
94
|
+
diskIdx: (0, import_sqlite_core2.index)("idx_media_disk").on(table.disk),
|
|
95
|
+
createdAtIdx: (0, import_sqlite_core2.index)("idx_media_created_at").on(table.createdAt),
|
|
96
|
+
collectionCreatedAtIdx: (0, import_sqlite_core2.index)("idx_media_collection_created_at").on(table.collection, table.createdAt, table.id),
|
|
97
|
+
tenantIdx: (0, import_sqlite_core2.index)("idx_media_tenant").on(table.tenantId, table.ownerType, table.ownerId, table.collection)
|
|
85
98
|
}));
|
|
86
99
|
|
|
100
|
+
// src/tenancy.ts
|
|
101
|
+
var import_nestjs_media_core = require("@dudousxd/nestjs-media-core");
|
|
102
|
+
var import_drizzle_orm = require("drizzle-orm");
|
|
103
|
+
function tenantCondition(column, match) {
|
|
104
|
+
if (match.id === null) return (0, import_drizzle_orm.isNull)(column);
|
|
105
|
+
return match.orNull ? (0, import_drizzle_orm.or)((0, import_drizzle_orm.eq)(column, match.id), (0, import_drizzle_orm.isNull)(column)) : (0, import_drizzle_orm.eq)(column, match.id);
|
|
106
|
+
}
|
|
107
|
+
__name(tenantCondition, "tenantCondition");
|
|
108
|
+
function selectable(columns, tenancy) {
|
|
109
|
+
if (tenancy) return columns;
|
|
110
|
+
const { tenantId: _tenantId, ...rest } = columns;
|
|
111
|
+
return rest;
|
|
112
|
+
}
|
|
113
|
+
__name(selectable, "selectable");
|
|
114
|
+
async function assertTenantColumn(table, probe, fix) {
|
|
115
|
+
try {
|
|
116
|
+
await probe();
|
|
117
|
+
} catch (error) {
|
|
118
|
+
throw Object.assign(new import_nestjs_media_core.MediaTenancyUnavailableError(`the \`${table}\` table has no \`tenant_id\` column`, fix), {
|
|
119
|
+
cause: error
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
__name(assertTenantColumn, "assertTenantColumn");
|
|
124
|
+
|
|
87
125
|
// src/drizzle-media-store.ts
|
|
126
|
+
function toRow(record, tenancy) {
|
|
127
|
+
const { tenantId, ...rest } = record;
|
|
128
|
+
return tenancy ? {
|
|
129
|
+
...rest,
|
|
130
|
+
tenantId: tenantId ?? null
|
|
131
|
+
} : rest;
|
|
132
|
+
}
|
|
133
|
+
__name(toRow, "toRow");
|
|
88
134
|
var DrizzleMediaStore = class {
|
|
89
135
|
static {
|
|
90
136
|
__name(this, "DrizzleMediaStore");
|
|
91
137
|
}
|
|
92
138
|
db;
|
|
139
|
+
tenancy = false;
|
|
93
140
|
constructor(db) {
|
|
94
141
|
this.db = db;
|
|
95
142
|
}
|
|
143
|
+
/** The columns to read: `tenant_id` only once tenancy is on. */
|
|
144
|
+
get columns() {
|
|
145
|
+
return selectable((0, import_drizzle_orm2.getTableColumns)(mediaTable), this.tenancy);
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Tenancy: checks the `tenant_id` column exists, then reads and writes it. Called by the
|
|
149
|
+
* library when tenancy is on; until then the column is never touched.
|
|
150
|
+
*/
|
|
151
|
+
async enableTenancy() {
|
|
152
|
+
await assertTenantColumn("media", () => this.db.select({
|
|
153
|
+
tenantId: mediaTable.tenantId
|
|
154
|
+
}).from(mediaTable).limit(1), "Generate a migration with drizzle-kit from `mediaTable` (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);`.");
|
|
155
|
+
this.tenancy = true;
|
|
156
|
+
}
|
|
96
157
|
async save(record) {
|
|
97
|
-
|
|
158
|
+
const row = toRow(record, this.tenancy);
|
|
159
|
+
await this.db.insert(this.tenancy ? mediaTable : mediaTableWithoutTenant).values(row).onConflictDoUpdate({
|
|
98
160
|
target: mediaTable.id,
|
|
99
|
-
set:
|
|
161
|
+
set: row
|
|
100
162
|
});
|
|
101
|
-
return
|
|
163
|
+
return row;
|
|
102
164
|
}
|
|
103
165
|
async find(id) {
|
|
104
|
-
const rows = await this.db.select().from(mediaTable).where((0,
|
|
166
|
+
const rows = await this.db.select(this.columns).from(mediaTable).where((0, import_drizzle_orm2.eq)(mediaTable.id, id)).limit(1);
|
|
105
167
|
return rows[0] ?? null;
|
|
106
168
|
}
|
|
107
169
|
async listByOwner(ownerType, ownerId, collection) {
|
|
108
|
-
const where = collection === void 0 ? (0,
|
|
109
|
-
const rows = await this.db.select().from(mediaTable).where(where).orderBy((0,
|
|
170
|
+
const where = collection === void 0 ? (0, import_drizzle_orm2.and)((0, import_drizzle_orm2.eq)(mediaTable.ownerType, ownerType), (0, import_drizzle_orm2.eq)(mediaTable.ownerId, ownerId)) : (0, import_drizzle_orm2.and)((0, import_drizzle_orm2.eq)(mediaTable.ownerType, ownerType), (0, import_drizzle_orm2.eq)(mediaTable.ownerId, ownerId), (0, import_drizzle_orm2.eq)(mediaTable.collection, collection));
|
|
171
|
+
const rows = await this.db.select(this.columns).from(mediaTable).where(where).orderBy((0, import_drizzle_orm2.asc)(mediaTable.order));
|
|
110
172
|
return rows;
|
|
111
173
|
}
|
|
112
174
|
async delete(id) {
|
|
113
|
-
await this.db.delete(mediaTable).where((0,
|
|
175
|
+
await this.db.delete(mediaTable).where((0, import_drizzle_orm2.eq)(mediaTable.id, id));
|
|
114
176
|
}
|
|
115
177
|
async nextOrder(ownerType, ownerId, collection) {
|
|
116
178
|
const rows = await this.db.select({
|
|
117
|
-
max: (0,
|
|
118
|
-
}).from(mediaTable).where((0,
|
|
179
|
+
max: (0, import_drizzle_orm2.max)(mediaTable.order)
|
|
180
|
+
}).from(mediaTable).where((0, import_drizzle_orm2.and)((0, import_drizzle_orm2.eq)(mediaTable.ownerType, ownerType), (0, import_drizzle_orm2.eq)(mediaTable.ownerId, ownerId), (0, import_drizzle_orm2.eq)(mediaTable.collection, collection)));
|
|
119
181
|
const top = rows[0]?.max;
|
|
120
182
|
return top == null ? 0 : Number(top) + 1;
|
|
121
183
|
}
|
|
122
184
|
async count(filter = {}) {
|
|
123
185
|
const conditions = [
|
|
124
186
|
...filter.ownerType !== void 0 ? [
|
|
125
|
-
(0,
|
|
187
|
+
(0, import_drizzle_orm2.eq)(mediaTable.ownerType, filter.ownerType)
|
|
126
188
|
] : [],
|
|
127
189
|
...filter.collection !== void 0 ? [
|
|
128
|
-
(0,
|
|
190
|
+
(0, import_drizzle_orm2.eq)(mediaTable.collection, filter.collection)
|
|
129
191
|
] : [],
|
|
130
192
|
...filter.disk !== void 0 ? [
|
|
131
|
-
(0,
|
|
193
|
+
(0, import_drizzle_orm2.eq)(mediaTable.disk, filter.disk)
|
|
194
|
+
] : [],
|
|
195
|
+
...filter.tenant ? [
|
|
196
|
+
tenantCondition(mediaTable.tenantId, filter.tenant)
|
|
132
197
|
] : []
|
|
133
198
|
];
|
|
134
199
|
const rows = await this.db.select({
|
|
135
|
-
value: (0,
|
|
136
|
-
}).from(mediaTable).where(conditions.length ? (0,
|
|
200
|
+
value: (0, import_drizzle_orm2.count)()
|
|
201
|
+
}).from(mediaTable).where(conditions.length ? (0, import_drizzle_orm2.and)(...conditions) : void 0);
|
|
137
202
|
return Number(rows[0]?.value ?? 0);
|
|
138
203
|
}
|
|
139
204
|
async aggregate(query) {
|
|
140
205
|
const column = query.groupBy === "collection" ? mediaTable.collection : mediaTable.disk;
|
|
141
206
|
const rows = await this.db.select({
|
|
142
207
|
key: column,
|
|
143
|
-
count: (0,
|
|
144
|
-
sumSize: (0,
|
|
145
|
-
}).from(mediaTable).groupBy(column);
|
|
208
|
+
count: (0, import_drizzle_orm2.count)(),
|
|
209
|
+
sumSize: (0, import_drizzle_orm2.sum)(mediaTable.size)
|
|
210
|
+
}).from(mediaTable).where(query.tenant ? tenantCondition(mediaTable.tenantId, query.tenant) : void 0).groupBy(column);
|
|
146
211
|
return rows.map((row) => ({
|
|
147
212
|
key: row.key,
|
|
148
213
|
count: Number(row.count),
|
|
@@ -154,19 +219,22 @@ var DrizzleMediaStore = class {
|
|
|
154
219
|
const cursor = page.cursor ? decodeListCursor(page.cursor) : null;
|
|
155
220
|
const filterConditions = [
|
|
156
221
|
...filter.ownerType !== void 0 ? [
|
|
157
|
-
(0,
|
|
222
|
+
(0, import_drizzle_orm2.eq)(mediaTable.ownerType, filter.ownerType)
|
|
158
223
|
] : [],
|
|
159
224
|
...filter.collection !== void 0 ? [
|
|
160
|
-
(0,
|
|
225
|
+
(0, import_drizzle_orm2.eq)(mediaTable.collection, filter.collection)
|
|
161
226
|
] : [],
|
|
162
227
|
...filter.disk !== void 0 ? [
|
|
163
|
-
(0,
|
|
228
|
+
(0, import_drizzle_orm2.eq)(mediaTable.disk, filter.disk)
|
|
229
|
+
] : [],
|
|
230
|
+
...filter.tenant ? [
|
|
231
|
+
tenantCondition(mediaTable.tenantId, filter.tenant)
|
|
164
232
|
] : [],
|
|
165
233
|
...cursor ? [
|
|
166
|
-
(0,
|
|
234
|
+
(0, import_drizzle_orm2.or)((0, import_drizzle_orm2.gt)(mediaTable.createdAt, cursor.createdAt), (0, import_drizzle_orm2.and)((0, import_drizzle_orm2.eq)(mediaTable.createdAt, cursor.createdAt), (0, import_drizzle_orm2.gt)(mediaTable.id, cursor.id)))
|
|
167
235
|
] : []
|
|
168
236
|
];
|
|
169
|
-
const rows = await this.db.select().from(mediaTable).where(filterConditions.length ? (0,
|
|
237
|
+
const rows = await this.db.select(this.columns).from(mediaTable).where(filterConditions.length ? (0, import_drizzle_orm2.and)(...filterConditions) : void 0).orderBy((0, import_drizzle_orm2.asc)(mediaTable.createdAt), (0, import_drizzle_orm2.asc)(mediaTable.id)).limit(limit + 1);
|
|
170
238
|
const hasMore = rows.length > limit;
|
|
171
239
|
const records = rows.slice(0, limit).map((record) => ({
|
|
172
240
|
...record
|
|
@@ -183,72 +251,96 @@ var DrizzleMediaStore = class {
|
|
|
183
251
|
};
|
|
184
252
|
|
|
185
253
|
// src/pg/drizzle-pg-media-store.ts
|
|
186
|
-
var
|
|
254
|
+
var import_drizzle_orm3 = require("drizzle-orm");
|
|
187
255
|
|
|
188
|
-
// src/pg/
|
|
256
|
+
// src/pg/columns.ts
|
|
189
257
|
var import_pg_core = require("drizzle-orm/pg-core");
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
258
|
+
function mediaPgColumns() {
|
|
259
|
+
return {
|
|
260
|
+
id: (0, import_pg_core.text)("id").primaryKey(),
|
|
261
|
+
ownerType: (0, import_pg_core.text)("owner_type").notNull(),
|
|
262
|
+
ownerId: (0, import_pg_core.text)("owner_id").notNull(),
|
|
263
|
+
collection: (0, import_pg_core.text)("collection").notNull(),
|
|
264
|
+
name: (0, import_pg_core.text)("name").notNull(),
|
|
265
|
+
fileName: (0, import_pg_core.text)("file_name").notNull(),
|
|
266
|
+
mimeType: (0, import_pg_core.text)("mime_type").notNull(),
|
|
267
|
+
size: (0, import_pg_core.bigint)("size", {
|
|
268
|
+
mode: "number"
|
|
269
|
+
}).notNull(),
|
|
270
|
+
disk: (0, import_pg_core.text)("disk").notNull(),
|
|
271
|
+
path: (0, import_pg_core.text)("path").notNull(),
|
|
272
|
+
order: (0, import_pg_core.integer)("position").notNull(),
|
|
273
|
+
customProperties: (0, import_pg_core.jsonb)("custom_properties").$type().notNull(),
|
|
274
|
+
conversions: (0, import_pg_core.jsonb)("conversions").$type().notNull(),
|
|
275
|
+
createdAt: (0, import_pg_core.timestamp)("created_at", {
|
|
276
|
+
withTimezone: true,
|
|
277
|
+
mode: "date"
|
|
278
|
+
}).notNull(),
|
|
279
|
+
updatedAt: (0, import_pg_core.timestamp)("updated_at", {
|
|
280
|
+
withTimezone: true,
|
|
281
|
+
mode: "date"
|
|
282
|
+
}).notNull()
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
__name(mediaPgColumns, "mediaPgColumns");
|
|
286
|
+
function uploadSessionPgColumns() {
|
|
287
|
+
return {
|
|
288
|
+
id: (0, import_pg_core.text)("id").primaryKey(),
|
|
289
|
+
disk: (0, import_pg_core.text)("disk").notNull(),
|
|
290
|
+
key: (0, import_pg_core.text)("key").notNull(),
|
|
291
|
+
contentType: (0, import_pg_core.text)("content_type"),
|
|
292
|
+
size: (0, import_pg_core.bigint)("size", {
|
|
293
|
+
mode: "number"
|
|
294
|
+
}),
|
|
295
|
+
offset: (0, import_pg_core.bigint)("offset", {
|
|
296
|
+
mode: "number"
|
|
297
|
+
}).notNull().default(0),
|
|
298
|
+
parts: (0, import_pg_core.integer)("parts").notNull().default(0),
|
|
299
|
+
multipartUploadId: (0, import_pg_core.text)("multipart_upload_id"),
|
|
300
|
+
metadata: (0, import_pg_core.jsonb)("metadata").$type(),
|
|
301
|
+
createdAt: (0, import_pg_core.timestamp)("created_at", {
|
|
302
|
+
withTimezone: true,
|
|
303
|
+
mode: "date"
|
|
304
|
+
}).notNull().defaultNow(),
|
|
305
|
+
updatedAt: (0, import_pg_core.timestamp)("updated_at", {
|
|
306
|
+
withTimezone: true,
|
|
307
|
+
mode: "date"
|
|
308
|
+
}).notNull().defaultNow()
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
__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
|
+
|
|
315
|
+
// 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")
|
|
214
321
|
}, (table) => [
|
|
215
|
-
(0,
|
|
216
|
-
(0,
|
|
217
|
-
(0,
|
|
218
|
-
(0,
|
|
219
|
-
(0,
|
|
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)
|
|
220
328
|
]);
|
|
221
|
-
var mediaUploadSessionsPgTable = (0,
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
mode: "number"
|
|
231
|
-
}).notNull().default(0),
|
|
232
|
-
parts: (0, import_pg_core.integer)("parts").notNull().default(0),
|
|
233
|
-
multipartUploadId: (0, import_pg_core.text)("multipart_upload_id"),
|
|
234
|
-
metadata: (0, import_pg_core.jsonb)("metadata").$type(),
|
|
235
|
-
createdAt: (0, import_pg_core.timestamp)("created_at", {
|
|
236
|
-
withTimezone: true,
|
|
237
|
-
mode: "date"
|
|
238
|
-
}).notNull().defaultNow(),
|
|
239
|
-
updatedAt: (0, import_pg_core.timestamp)("updated_at", {
|
|
240
|
-
withTimezone: true,
|
|
241
|
-
mode: "date"
|
|
242
|
-
}).notNull().defaultNow()
|
|
243
|
-
});
|
|
244
|
-
var mediaUploadPartsPgTable = (0, import_pg_core.pgTable)("media_upload_parts", {
|
|
245
|
-
sessionId: (0, import_pg_core.text)("session_id").notNull().references(() => mediaUploadSessionsPgTable.id, {
|
|
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, {
|
|
246
338
|
onDelete: "cascade"
|
|
247
339
|
}),
|
|
248
|
-
partNumber: (0,
|
|
249
|
-
etag: (0,
|
|
340
|
+
partNumber: (0, import_pg_core2.integer)("part_number").notNull(),
|
|
341
|
+
etag: (0, import_pg_core2.text)("etag").notNull()
|
|
250
342
|
}, (table) => [
|
|
251
|
-
(0,
|
|
343
|
+
(0, import_pg_core2.primaryKey)({
|
|
252
344
|
columns: [
|
|
253
345
|
table.sessionId,
|
|
254
346
|
table.partNumber
|
|
@@ -264,66 +356,93 @@ function toRecord(row) {
|
|
|
264
356
|
};
|
|
265
357
|
}
|
|
266
358
|
__name(toRecord, "toRecord");
|
|
359
|
+
function toRow2(record, tenancy) {
|
|
360
|
+
const { tenantId, ...rest } = record;
|
|
361
|
+
return tenancy ? {
|
|
362
|
+
...rest,
|
|
363
|
+
tenantId: tenantId ?? null
|
|
364
|
+
} : rest;
|
|
365
|
+
}
|
|
366
|
+
__name(toRow2, "toRow");
|
|
267
367
|
var DrizzlePgMediaStore = class {
|
|
268
368
|
static {
|
|
269
369
|
__name(this, "DrizzlePgMediaStore");
|
|
270
370
|
}
|
|
271
371
|
db;
|
|
372
|
+
tenancy = false;
|
|
272
373
|
constructor(db) {
|
|
273
374
|
this.db = db;
|
|
274
375
|
}
|
|
376
|
+
/** The columns to read: `tenant_id` only once tenancy is on. */
|
|
377
|
+
get columns() {
|
|
378
|
+
return selectable((0, import_drizzle_orm3.getTableColumns)(mediaPgTable), this.tenancy);
|
|
379
|
+
}
|
|
380
|
+
/**
|
|
381
|
+
* Tenancy: checks the `tenant_id` column exists, then reads and writes it. Called by the
|
|
382
|
+
* library when tenancy is on; until then the column is never touched.
|
|
383
|
+
*/
|
|
384
|
+
async enableTenancy() {
|
|
385
|
+
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);`.");
|
|
388
|
+
this.tenancy = true;
|
|
389
|
+
}
|
|
275
390
|
async save(record) {
|
|
276
|
-
const
|
|
277
|
-
|
|
391
|
+
const row = toRow2(record, this.tenancy);
|
|
392
|
+
const { id: _id, ...rest } = row;
|
|
393
|
+
await this.db.insert(this.tenancy ? mediaPgTable : mediaPgTableWithoutTenant).values(row).onConflictDoUpdate({
|
|
278
394
|
target: mediaPgTable.id,
|
|
279
395
|
set: rest
|
|
280
396
|
});
|
|
281
|
-
return
|
|
397
|
+
return row;
|
|
282
398
|
}
|
|
283
399
|
async find(id) {
|
|
284
|
-
const rows = await this.db.select().from(mediaPgTable).where((0,
|
|
400
|
+
const rows = await this.db.select(this.columns).from(mediaPgTable).where((0, import_drizzle_orm3.eq)(mediaPgTable.id, id)).limit(1);
|
|
285
401
|
const row = rows[0];
|
|
286
402
|
return row ? toRecord(row) : null;
|
|
287
403
|
}
|
|
288
404
|
async listByOwner(ownerType, ownerId, collection) {
|
|
289
|
-
const where = collection === void 0 ? (0,
|
|
290
|
-
const rows = await this.db.select().from(mediaPgTable).where(where).orderBy((0,
|
|
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));
|
|
291
407
|
return rows.map(toRecord);
|
|
292
408
|
}
|
|
293
409
|
async delete(id) {
|
|
294
|
-
await this.db.delete(mediaPgTable).where((0,
|
|
410
|
+
await this.db.delete(mediaPgTable).where((0, import_drizzle_orm3.eq)(mediaPgTable.id, id));
|
|
295
411
|
}
|
|
296
412
|
async nextOrder(ownerType, ownerId, collection) {
|
|
297
413
|
const rows = await this.db.select({
|
|
298
|
-
max: (0,
|
|
299
|
-
}).from(mediaPgTable).where((0,
|
|
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)));
|
|
300
416
|
const top = rows[0]?.max;
|
|
301
417
|
return top == null ? 0 : Number(top) + 1;
|
|
302
418
|
}
|
|
303
419
|
async count(filter = {}) {
|
|
304
420
|
const conditions = [
|
|
305
421
|
...filter.ownerType !== void 0 ? [
|
|
306
|
-
(0,
|
|
422
|
+
(0, import_drizzle_orm3.eq)(mediaPgTable.ownerType, filter.ownerType)
|
|
307
423
|
] : [],
|
|
308
424
|
...filter.collection !== void 0 ? [
|
|
309
|
-
(0,
|
|
425
|
+
(0, import_drizzle_orm3.eq)(mediaPgTable.collection, filter.collection)
|
|
310
426
|
] : [],
|
|
311
427
|
...filter.disk !== void 0 ? [
|
|
312
|
-
(0,
|
|
428
|
+
(0, import_drizzle_orm3.eq)(mediaPgTable.disk, filter.disk)
|
|
429
|
+
] : [],
|
|
430
|
+
...filter.tenant ? [
|
|
431
|
+
tenantCondition(mediaPgTable.tenantId, filter.tenant)
|
|
313
432
|
] : []
|
|
314
433
|
];
|
|
315
434
|
const rows = await this.db.select({
|
|
316
|
-
value: (0,
|
|
317
|
-
}).from(mediaPgTable).where(conditions.length ? (0,
|
|
435
|
+
value: (0, import_drizzle_orm3.count)()
|
|
436
|
+
}).from(mediaPgTable).where(conditions.length ? (0, import_drizzle_orm3.and)(...conditions) : void 0);
|
|
318
437
|
return Number(rows[0]?.value ?? 0);
|
|
319
438
|
}
|
|
320
439
|
async aggregate(query) {
|
|
321
440
|
const column = query.groupBy === "collection" ? mediaPgTable.collection : mediaPgTable.disk;
|
|
322
441
|
const rows = await this.db.select({
|
|
323
442
|
key: column,
|
|
324
|
-
count: (0,
|
|
325
|
-
sumSize: (0,
|
|
326
|
-
}).from(mediaPgTable).groupBy(column);
|
|
443
|
+
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);
|
|
327
446
|
return rows.map((row) => ({
|
|
328
447
|
key: row.key,
|
|
329
448
|
count: Number(row.count),
|
|
@@ -336,19 +455,22 @@ var DrizzlePgMediaStore = class {
|
|
|
336
455
|
const cursor = page.cursor ? decodeListCursor(page.cursor) : null;
|
|
337
456
|
const filterConditions = [
|
|
338
457
|
...filter.ownerType !== void 0 ? [
|
|
339
|
-
(0,
|
|
458
|
+
(0, import_drizzle_orm3.eq)(mediaPgTable.ownerType, filter.ownerType)
|
|
340
459
|
] : [],
|
|
341
460
|
...filter.collection !== void 0 ? [
|
|
342
|
-
(0,
|
|
461
|
+
(0, import_drizzle_orm3.eq)(mediaPgTable.collection, filter.collection)
|
|
343
462
|
] : [],
|
|
344
463
|
...filter.disk !== void 0 ? [
|
|
345
|
-
(0,
|
|
464
|
+
(0, import_drizzle_orm3.eq)(mediaPgTable.disk, filter.disk)
|
|
465
|
+
] : [],
|
|
466
|
+
...filter.tenant ? [
|
|
467
|
+
tenantCondition(mediaPgTable.tenantId, filter.tenant)
|
|
346
468
|
] : [],
|
|
347
469
|
...cursor ? [
|
|
348
|
-
(0,
|
|
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)))
|
|
349
471
|
] : []
|
|
350
472
|
];
|
|
351
|
-
const rows = await this.db.select().from(mediaPgTable).where(filterConditions.length ? (0,
|
|
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);
|
|
352
474
|
const hasMore = rows.length > limit;
|
|
353
475
|
const records = rows.slice(0, limit).map(toRecord);
|
|
354
476
|
const result = {
|
|
@@ -363,7 +485,7 @@ var DrizzlePgMediaStore = class {
|
|
|
363
485
|
};
|
|
364
486
|
|
|
365
487
|
// src/pg/drizzle-pg-upload-session-store.ts
|
|
366
|
-
var
|
|
488
|
+
var import_drizzle_orm4 = require("drizzle-orm");
|
|
367
489
|
function escapeLike(value) {
|
|
368
490
|
return value.replace(/[\\%_]/g, (char) => `\\${char}`);
|
|
369
491
|
}
|
|
@@ -381,11 +503,15 @@ function toSession(row) {
|
|
|
381
503
|
};
|
|
382
504
|
if (row.multipartUploadId != null) session.multipartUploadId = row.multipartUploadId;
|
|
383
505
|
if (row.metadata != null) session.metadata = row.metadata;
|
|
506
|
+
if (row.tenantId !== void 0) session.tenantId = row.tenantId;
|
|
384
507
|
return session;
|
|
385
508
|
}
|
|
386
509
|
__name(toSession, "toSession");
|
|
387
|
-
function toColumns(session) {
|
|
510
|
+
function toColumns(session, tenancy) {
|
|
388
511
|
return {
|
|
512
|
+
...tenancy ? {
|
|
513
|
+
tenantId: session.tenantId ?? null
|
|
514
|
+
} : {},
|
|
389
515
|
disk: session.disk,
|
|
390
516
|
key: session.key,
|
|
391
517
|
contentType: session.contentType ?? null,
|
|
@@ -402,48 +528,73 @@ var DrizzlePgUploadSessionStore = class {
|
|
|
402
528
|
__name(this, "DrizzlePgUploadSessionStore");
|
|
403
529
|
}
|
|
404
530
|
db;
|
|
531
|
+
tenancy = false;
|
|
405
532
|
constructor(db) {
|
|
406
533
|
this.db = db;
|
|
407
534
|
}
|
|
535
|
+
/** Off: the table without `tenant_id`, which Drizzle would otherwise list in an INSERT. */
|
|
536
|
+
get sessionsTable() {
|
|
537
|
+
return this.tenancy ? mediaUploadSessionsPgTable : mediaUploadSessionsPgTableWithoutTenant;
|
|
538
|
+
}
|
|
539
|
+
/** The columns to read: `tenant_id` only once tenancy is on. */
|
|
540
|
+
get columns() {
|
|
541
|
+
return selectable((0, import_drizzle_orm4.getTableColumns)(mediaUploadSessionsPgTable), this.tenancy);
|
|
542
|
+
}
|
|
543
|
+
/**
|
|
544
|
+
* Tenancy: checks the `tenant_id` column exists, then reads and writes it. Called by the upload
|
|
545
|
+
* manager when tenancy is on; until then the column is never touched.
|
|
546
|
+
*/
|
|
547
|
+
async enableTenancy() {
|
|
548
|
+
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);`.");
|
|
551
|
+
this.tenancy = true;
|
|
552
|
+
}
|
|
553
|
+
/** The session as persisted: off, no `tenantId`; on, `tenantId` or `null`. */
|
|
554
|
+
persisted(session) {
|
|
555
|
+
const { tenantId, ...rest } = session;
|
|
556
|
+
return this.tenancy ? {
|
|
557
|
+
...rest,
|
|
558
|
+
tenantId: tenantId ?? null
|
|
559
|
+
} : rest;
|
|
560
|
+
}
|
|
408
561
|
async create(session) {
|
|
409
562
|
const createdAt = session.createdAt ?? /* @__PURE__ */ new Date();
|
|
410
|
-
await this.db.insert(
|
|
563
|
+
await this.db.insert(this.sessionsTable).values({
|
|
411
564
|
id: session.id,
|
|
412
|
-
...toColumns(session),
|
|
565
|
+
...toColumns(session, this.tenancy),
|
|
413
566
|
createdAt,
|
|
414
567
|
updatedAt: /* @__PURE__ */ new Date()
|
|
415
568
|
});
|
|
416
569
|
return {
|
|
417
|
-
...session,
|
|
570
|
+
...this.persisted(session),
|
|
418
571
|
createdAt
|
|
419
572
|
};
|
|
420
573
|
}
|
|
421
574
|
async get(id) {
|
|
422
|
-
const rows = await this.db.select().from(mediaUploadSessionsPgTable).where((0,
|
|
575
|
+
const rows = await this.db.select(this.columns).from(mediaUploadSessionsPgTable).where((0, import_drizzle_orm4.eq)(mediaUploadSessionsPgTable.id, id)).limit(1);
|
|
423
576
|
const row = rows[0];
|
|
424
577
|
return row ? toSession(row) : null;
|
|
425
578
|
}
|
|
426
579
|
async update(session) {
|
|
427
580
|
const columns = {
|
|
428
|
-
...toColumns(session),
|
|
581
|
+
...toColumns(session, this.tenancy),
|
|
429
582
|
...session.createdAt !== void 0 ? {
|
|
430
583
|
createdAt: session.createdAt
|
|
431
584
|
} : {},
|
|
432
585
|
updatedAt: /* @__PURE__ */ new Date()
|
|
433
586
|
};
|
|
434
|
-
await this.db.insert(
|
|
587
|
+
await this.db.insert(this.sessionsTable).values({
|
|
435
588
|
id: session.id,
|
|
436
589
|
...columns
|
|
437
590
|
}).onConflictDoUpdate({
|
|
438
591
|
target: mediaUploadSessionsPgTable.id,
|
|
439
592
|
set: columns
|
|
440
593
|
});
|
|
441
|
-
return
|
|
442
|
-
...session
|
|
443
|
-
};
|
|
594
|
+
return this.persisted(session);
|
|
444
595
|
}
|
|
445
596
|
async delete(id) {
|
|
446
|
-
await this.db.delete(mediaUploadSessionsPgTable).where((0,
|
|
597
|
+
await this.db.delete(mediaUploadSessionsPgTable).where((0, import_drizzle_orm4.eq)(mediaUploadSessionsPgTable.id, id));
|
|
447
598
|
}
|
|
448
599
|
async addPart(id, part) {
|
|
449
600
|
await this.db.insert(mediaUploadPartsPgTable).values({
|
|
@@ -456,7 +607,7 @@ var DrizzlePgUploadSessionStore = class {
|
|
|
456
607
|
mediaUploadPartsPgTable.partNumber
|
|
457
608
|
],
|
|
458
609
|
set: {
|
|
459
|
-
etag:
|
|
610
|
+
etag: import_drizzle_orm4.sql`excluded.etag`
|
|
460
611
|
}
|
|
461
612
|
});
|
|
462
613
|
}
|
|
@@ -464,7 +615,7 @@ var DrizzlePgUploadSessionStore = class {
|
|
|
464
615
|
const rows = await this.db.select({
|
|
465
616
|
partNumber: mediaUploadPartsPgTable.partNumber,
|
|
466
617
|
etag: mediaUploadPartsPgTable.etag
|
|
467
|
-
}).from(mediaUploadPartsPgTable).where((0,
|
|
618
|
+
}).from(mediaUploadPartsPgTable).where((0, import_drizzle_orm4.eq)(mediaUploadPartsPgTable.sessionId, id)).orderBy((0, import_drizzle_orm4.asc)(mediaUploadPartsPgTable.partNumber));
|
|
468
619
|
return rows.map((row) => ({
|
|
469
620
|
partNumber: Number(row.partNumber),
|
|
470
621
|
etag: row.etag
|
|
@@ -473,13 +624,16 @@ var DrizzlePgUploadSessionStore = class {
|
|
|
473
624
|
async list(filter = {}) {
|
|
474
625
|
const conditions = [
|
|
475
626
|
...filter.disk !== void 0 ? [
|
|
476
|
-
(0,
|
|
627
|
+
(0, import_drizzle_orm4.eq)(mediaUploadSessionsPgTable.disk, filter.disk)
|
|
477
628
|
] : [],
|
|
478
629
|
...filter.keyPrefix !== void 0 ? [
|
|
479
|
-
(0,
|
|
630
|
+
(0, import_drizzle_orm4.like)(mediaUploadSessionsPgTable.key, `${escapeLike(filter.keyPrefix)}%`)
|
|
631
|
+
] : [],
|
|
632
|
+
...filter.tenant ? [
|
|
633
|
+
tenantCondition(mediaUploadSessionsPgTable.tenantId, filter.tenant)
|
|
480
634
|
] : []
|
|
481
635
|
];
|
|
482
|
-
const rows = await this.db.select().from(mediaUploadSessionsPgTable).where(conditions.length ? (0,
|
|
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));
|
|
483
637
|
return rows.map(toSession);
|
|
484
638
|
}
|
|
485
639
|
};
|