@dudousxd/nestjs-media-database-drizzle 0.6.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-BHoJjohg.d.cts +32 -0
- package/dist/drizzle-media-store-BHoJjohg.d.ts +32 -0
- package/dist/drizzle-pg-upload-session-store-DWBmRKMi.d.cts +67 -0
- package/dist/drizzle-pg-upload-session-store-DWBmRKMi.d.ts +67 -0
- package/dist/index.cjs +141 -62
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +23 -23
- package/dist/index.d.ts +23 -23
- package/dist/index.js +124 -45
- package/dist/index.js.map +1 -1
- package/dist/nest/index.cjs +669 -0
- package/dist/nest/index.cjs.map +1 -0
- package/dist/nest/index.d.cts +32 -0
- package/dist/nest/index.d.ts +32 -0
- package/dist/nest/index.js +644 -0
- package/dist/nest/index.js.map +1 -0
- package/dist/pg/index.cjs +217 -100
- package/dist/pg/index.cjs.map +1 -1
- package/dist/pg/index.d.cts +40 -48
- package/dist/pg/index.d.ts +40 -48
- package/dist/pg/index.js +205 -88
- package/dist/pg/index.js.map +1 -1
- package/package.json +30 -9
|
@@ -0,0 +1,669 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
|
|
21
|
+
// src/nest/index.ts
|
|
22
|
+
var nest_exports = {};
|
|
23
|
+
__export(nest_exports, {
|
|
24
|
+
drizzleMediaStore: () => drizzleMediaStore,
|
|
25
|
+
drizzlePgMediaStore: () => drizzlePgMediaStore,
|
|
26
|
+
drizzlePgUploadSessions: () => drizzlePgUploadSessions
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(nest_exports);
|
|
29
|
+
var import_drizzle = require("@nestjs/drizzle");
|
|
30
|
+
|
|
31
|
+
// src/drizzle-media-store.ts
|
|
32
|
+
var import_drizzle_orm2 = require("drizzle-orm");
|
|
33
|
+
|
|
34
|
+
// src/list-cursor.ts
|
|
35
|
+
function encodeListCursor(record) {
|
|
36
|
+
return Buffer.from(`${record.createdAt.toISOString()}|${record.id}`, "utf8").toString("base64");
|
|
37
|
+
}
|
|
38
|
+
__name(encodeListCursor, "encodeListCursor");
|
|
39
|
+
function decodeListCursor(cursor) {
|
|
40
|
+
const decoded = Buffer.from(cursor, "base64").toString("utf8");
|
|
41
|
+
const separator = decoded.indexOf("|");
|
|
42
|
+
if (separator === -1) return null;
|
|
43
|
+
const createdAt = new Date(decoded.slice(0, separator));
|
|
44
|
+
if (Number.isNaN(createdAt.getTime())) return null;
|
|
45
|
+
const id = decoded.slice(separator + 1);
|
|
46
|
+
return {
|
|
47
|
+
createdAt,
|
|
48
|
+
id
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
__name(decodeListCursor, "decodeListCursor");
|
|
52
|
+
|
|
53
|
+
// src/media-columns.ts
|
|
54
|
+
var import_sqlite_core = require("drizzle-orm/sqlite-core");
|
|
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")
|
|
91
|
+
}, (table) => ({
|
|
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)
|
|
98
|
+
}));
|
|
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
|
+
|
|
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");
|
|
134
|
+
var DrizzleMediaStore = class {
|
|
135
|
+
static {
|
|
136
|
+
__name(this, "DrizzleMediaStore");
|
|
137
|
+
}
|
|
138
|
+
db;
|
|
139
|
+
tenancy = false;
|
|
140
|
+
constructor(db) {
|
|
141
|
+
this.db = db;
|
|
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
|
+
}
|
|
157
|
+
async save(record) {
|
|
158
|
+
const row = toRow(record, this.tenancy);
|
|
159
|
+
await this.db.insert(this.tenancy ? mediaTable : mediaTableWithoutTenant).values(row).onConflictDoUpdate({
|
|
160
|
+
target: mediaTable.id,
|
|
161
|
+
set: row
|
|
162
|
+
});
|
|
163
|
+
return row;
|
|
164
|
+
}
|
|
165
|
+
async find(id) {
|
|
166
|
+
const rows = await this.db.select(this.columns).from(mediaTable).where((0, import_drizzle_orm2.eq)(mediaTable.id, id)).limit(1);
|
|
167
|
+
return rows[0] ?? null;
|
|
168
|
+
}
|
|
169
|
+
async listByOwner(ownerType, ownerId, collection) {
|
|
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));
|
|
172
|
+
return rows;
|
|
173
|
+
}
|
|
174
|
+
async delete(id) {
|
|
175
|
+
await this.db.delete(mediaTable).where((0, import_drizzle_orm2.eq)(mediaTable.id, id));
|
|
176
|
+
}
|
|
177
|
+
async nextOrder(ownerType, ownerId, collection) {
|
|
178
|
+
const rows = await this.db.select({
|
|
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)));
|
|
181
|
+
const top = rows[0]?.max;
|
|
182
|
+
return top == null ? 0 : Number(top) + 1;
|
|
183
|
+
}
|
|
184
|
+
async count(filter = {}) {
|
|
185
|
+
const conditions = [
|
|
186
|
+
...filter.ownerType !== void 0 ? [
|
|
187
|
+
(0, import_drizzle_orm2.eq)(mediaTable.ownerType, filter.ownerType)
|
|
188
|
+
] : [],
|
|
189
|
+
...filter.collection !== void 0 ? [
|
|
190
|
+
(0, import_drizzle_orm2.eq)(mediaTable.collection, filter.collection)
|
|
191
|
+
] : [],
|
|
192
|
+
...filter.disk !== void 0 ? [
|
|
193
|
+
(0, import_drizzle_orm2.eq)(mediaTable.disk, filter.disk)
|
|
194
|
+
] : [],
|
|
195
|
+
...filter.tenant ? [
|
|
196
|
+
tenantCondition(mediaTable.tenantId, filter.tenant)
|
|
197
|
+
] : []
|
|
198
|
+
];
|
|
199
|
+
const rows = await this.db.select({
|
|
200
|
+
value: (0, import_drizzle_orm2.count)()
|
|
201
|
+
}).from(mediaTable).where(conditions.length ? (0, import_drizzle_orm2.and)(...conditions) : void 0);
|
|
202
|
+
return Number(rows[0]?.value ?? 0);
|
|
203
|
+
}
|
|
204
|
+
async aggregate(query) {
|
|
205
|
+
const column = query.groupBy === "collection" ? mediaTable.collection : mediaTable.disk;
|
|
206
|
+
const rows = await this.db.select({
|
|
207
|
+
key: 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);
|
|
211
|
+
return rows.map((row) => ({
|
|
212
|
+
key: row.key,
|
|
213
|
+
count: Number(row.count),
|
|
214
|
+
sumSize: query.sum === "size" ? Number(row.sumSize ?? 0) : 0
|
|
215
|
+
}));
|
|
216
|
+
}
|
|
217
|
+
async list(filter = {}, page = {}) {
|
|
218
|
+
const limit = page.limit ?? 50;
|
|
219
|
+
const cursor = page.cursor ? decodeListCursor(page.cursor) : null;
|
|
220
|
+
const filterConditions = [
|
|
221
|
+
...filter.ownerType !== void 0 ? [
|
|
222
|
+
(0, import_drizzle_orm2.eq)(mediaTable.ownerType, filter.ownerType)
|
|
223
|
+
] : [],
|
|
224
|
+
...filter.collection !== void 0 ? [
|
|
225
|
+
(0, import_drizzle_orm2.eq)(mediaTable.collection, filter.collection)
|
|
226
|
+
] : [],
|
|
227
|
+
...filter.disk !== void 0 ? [
|
|
228
|
+
(0, import_drizzle_orm2.eq)(mediaTable.disk, filter.disk)
|
|
229
|
+
] : [],
|
|
230
|
+
...filter.tenant ? [
|
|
231
|
+
tenantCondition(mediaTable.tenantId, filter.tenant)
|
|
232
|
+
] : [],
|
|
233
|
+
...cursor ? [
|
|
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)))
|
|
235
|
+
] : []
|
|
236
|
+
];
|
|
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);
|
|
238
|
+
const hasMore = rows.length > limit;
|
|
239
|
+
const records = rows.slice(0, limit).map((record) => ({
|
|
240
|
+
...record
|
|
241
|
+
}));
|
|
242
|
+
const result = {
|
|
243
|
+
records
|
|
244
|
+
};
|
|
245
|
+
const lastRecord = records.at(-1);
|
|
246
|
+
if (hasMore && lastRecord !== void 0) {
|
|
247
|
+
result.cursor = encodeListCursor(lastRecord);
|
|
248
|
+
}
|
|
249
|
+
return result;
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
// src/pg/drizzle-pg-media-store.ts
|
|
254
|
+
var import_drizzle_orm3 = require("drizzle-orm");
|
|
255
|
+
|
|
256
|
+
// src/pg/columns.ts
|
|
257
|
+
var import_pg_core = require("drizzle-orm/pg-core");
|
|
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")
|
|
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
|
+
]);
|
|
350
|
+
|
|
351
|
+
// src/pg/drizzle-pg-media-store.ts
|
|
352
|
+
function toRecord(row) {
|
|
353
|
+
return {
|
|
354
|
+
...row,
|
|
355
|
+
size: Number(row.size)
|
|
356
|
+
};
|
|
357
|
+
}
|
|
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");
|
|
367
|
+
var DrizzlePgMediaStore = class {
|
|
368
|
+
static {
|
|
369
|
+
__name(this, "DrizzlePgMediaStore");
|
|
370
|
+
}
|
|
371
|
+
db;
|
|
372
|
+
tenancy = false;
|
|
373
|
+
constructor(db) {
|
|
374
|
+
this.db = db;
|
|
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
|
+
}
|
|
390
|
+
async save(record) {
|
|
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({
|
|
394
|
+
target: mediaPgTable.id,
|
|
395
|
+
set: rest
|
|
396
|
+
});
|
|
397
|
+
return row;
|
|
398
|
+
}
|
|
399
|
+
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);
|
|
401
|
+
const row = rows[0];
|
|
402
|
+
return row ? toRecord(row) : null;
|
|
403
|
+
}
|
|
404
|
+
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));
|
|
407
|
+
return rows.map(toRecord);
|
|
408
|
+
}
|
|
409
|
+
async delete(id) {
|
|
410
|
+
await this.db.delete(mediaPgTable).where((0, import_drizzle_orm3.eq)(mediaPgTable.id, id));
|
|
411
|
+
}
|
|
412
|
+
async nextOrder(ownerType, ownerId, collection) {
|
|
413
|
+
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)));
|
|
416
|
+
const top = rows[0]?.max;
|
|
417
|
+
return top == null ? 0 : Number(top) + 1;
|
|
418
|
+
}
|
|
419
|
+
async count(filter = {}) {
|
|
420
|
+
const conditions = [
|
|
421
|
+
...filter.ownerType !== void 0 ? [
|
|
422
|
+
(0, import_drizzle_orm3.eq)(mediaPgTable.ownerType, filter.ownerType)
|
|
423
|
+
] : [],
|
|
424
|
+
...filter.collection !== void 0 ? [
|
|
425
|
+
(0, import_drizzle_orm3.eq)(mediaPgTable.collection, filter.collection)
|
|
426
|
+
] : [],
|
|
427
|
+
...filter.disk !== void 0 ? [
|
|
428
|
+
(0, import_drizzle_orm3.eq)(mediaPgTable.disk, filter.disk)
|
|
429
|
+
] : [],
|
|
430
|
+
...filter.tenant ? [
|
|
431
|
+
tenantCondition(mediaPgTable.tenantId, filter.tenant)
|
|
432
|
+
] : []
|
|
433
|
+
];
|
|
434
|
+
const rows = await this.db.select({
|
|
435
|
+
value: (0, import_drizzle_orm3.count)()
|
|
436
|
+
}).from(mediaPgTable).where(conditions.length ? (0, import_drizzle_orm3.and)(...conditions) : void 0);
|
|
437
|
+
return Number(rows[0]?.value ?? 0);
|
|
438
|
+
}
|
|
439
|
+
async aggregate(query) {
|
|
440
|
+
const column = query.groupBy === "collection" ? mediaPgTable.collection : mediaPgTable.disk;
|
|
441
|
+
const rows = await this.db.select({
|
|
442
|
+
key: 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);
|
|
446
|
+
return rows.map((row) => ({
|
|
447
|
+
key: row.key,
|
|
448
|
+
count: Number(row.count),
|
|
449
|
+
// `sum(bigint)` is `numeric` in Postgres — drivers hand it back as a string.
|
|
450
|
+
sumSize: query.sum === "size" ? Number(row.sumSize ?? 0) : 0
|
|
451
|
+
}));
|
|
452
|
+
}
|
|
453
|
+
async list(filter = {}, page = {}) {
|
|
454
|
+
const limit = page.limit ?? 50;
|
|
455
|
+
const cursor = page.cursor ? decodeListCursor(page.cursor) : null;
|
|
456
|
+
const filterConditions = [
|
|
457
|
+
...filter.ownerType !== void 0 ? [
|
|
458
|
+
(0, import_drizzle_orm3.eq)(mediaPgTable.ownerType, filter.ownerType)
|
|
459
|
+
] : [],
|
|
460
|
+
...filter.collection !== void 0 ? [
|
|
461
|
+
(0, import_drizzle_orm3.eq)(mediaPgTable.collection, filter.collection)
|
|
462
|
+
] : [],
|
|
463
|
+
...filter.disk !== void 0 ? [
|
|
464
|
+
(0, import_drizzle_orm3.eq)(mediaPgTable.disk, filter.disk)
|
|
465
|
+
] : [],
|
|
466
|
+
...filter.tenant ? [
|
|
467
|
+
tenantCondition(mediaPgTable.tenantId, filter.tenant)
|
|
468
|
+
] : [],
|
|
469
|
+
...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)))
|
|
471
|
+
] : []
|
|
472
|
+
];
|
|
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);
|
|
474
|
+
const hasMore = rows.length > limit;
|
|
475
|
+
const records = rows.slice(0, limit).map(toRecord);
|
|
476
|
+
const result = {
|
|
477
|
+
records
|
|
478
|
+
};
|
|
479
|
+
const lastRecord = records.at(-1);
|
|
480
|
+
if (hasMore && lastRecord !== void 0) {
|
|
481
|
+
result.cursor = encodeListCursor(lastRecord);
|
|
482
|
+
}
|
|
483
|
+
return result;
|
|
484
|
+
}
|
|
485
|
+
};
|
|
486
|
+
|
|
487
|
+
// src/pg/drizzle-pg-upload-session-store.ts
|
|
488
|
+
var import_drizzle_orm4 = require("drizzle-orm");
|
|
489
|
+
function escapeLike(value) {
|
|
490
|
+
return value.replace(/[\\%_]/g, (char) => `\\${char}`);
|
|
491
|
+
}
|
|
492
|
+
__name(escapeLike, "escapeLike");
|
|
493
|
+
function toSession(row) {
|
|
494
|
+
const session = {
|
|
495
|
+
id: row.id,
|
|
496
|
+
disk: row.disk,
|
|
497
|
+
key: row.key,
|
|
498
|
+
contentType: row.contentType ?? void 0,
|
|
499
|
+
size: row.size == null ? void 0 : Number(row.size),
|
|
500
|
+
offset: Number(row.offset),
|
|
501
|
+
parts: Number(row.parts),
|
|
502
|
+
createdAt: row.createdAt instanceof Date ? row.createdAt : new Date(row.createdAt)
|
|
503
|
+
};
|
|
504
|
+
if (row.multipartUploadId != null) session.multipartUploadId = row.multipartUploadId;
|
|
505
|
+
if (row.metadata != null) session.metadata = row.metadata;
|
|
506
|
+
if (row.tenantId !== void 0) session.tenantId = row.tenantId;
|
|
507
|
+
return session;
|
|
508
|
+
}
|
|
509
|
+
__name(toSession, "toSession");
|
|
510
|
+
function toColumns(session, tenancy) {
|
|
511
|
+
return {
|
|
512
|
+
...tenancy ? {
|
|
513
|
+
tenantId: session.tenantId ?? null
|
|
514
|
+
} : {},
|
|
515
|
+
disk: session.disk,
|
|
516
|
+
key: session.key,
|
|
517
|
+
contentType: session.contentType ?? null,
|
|
518
|
+
size: session.size ?? null,
|
|
519
|
+
offset: session.offset,
|
|
520
|
+
parts: session.parts,
|
|
521
|
+
multipartUploadId: session.multipartUploadId ?? null,
|
|
522
|
+
metadata: session.metadata ?? null
|
|
523
|
+
};
|
|
524
|
+
}
|
|
525
|
+
__name(toColumns, "toColumns");
|
|
526
|
+
var DrizzlePgUploadSessionStore = class {
|
|
527
|
+
static {
|
|
528
|
+
__name(this, "DrizzlePgUploadSessionStore");
|
|
529
|
+
}
|
|
530
|
+
db;
|
|
531
|
+
tenancy = false;
|
|
532
|
+
constructor(db) {
|
|
533
|
+
this.db = db;
|
|
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
|
+
}
|
|
561
|
+
async create(session) {
|
|
562
|
+
const createdAt = session.createdAt ?? /* @__PURE__ */ new Date();
|
|
563
|
+
await this.db.insert(this.sessionsTable).values({
|
|
564
|
+
id: session.id,
|
|
565
|
+
...toColumns(session, this.tenancy),
|
|
566
|
+
createdAt,
|
|
567
|
+
updatedAt: /* @__PURE__ */ new Date()
|
|
568
|
+
});
|
|
569
|
+
return {
|
|
570
|
+
...this.persisted(session),
|
|
571
|
+
createdAt
|
|
572
|
+
};
|
|
573
|
+
}
|
|
574
|
+
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);
|
|
576
|
+
const row = rows[0];
|
|
577
|
+
return row ? toSession(row) : null;
|
|
578
|
+
}
|
|
579
|
+
async update(session) {
|
|
580
|
+
const columns = {
|
|
581
|
+
...toColumns(session, this.tenancy),
|
|
582
|
+
...session.createdAt !== void 0 ? {
|
|
583
|
+
createdAt: session.createdAt
|
|
584
|
+
} : {},
|
|
585
|
+
updatedAt: /* @__PURE__ */ new Date()
|
|
586
|
+
};
|
|
587
|
+
await this.db.insert(this.sessionsTable).values({
|
|
588
|
+
id: session.id,
|
|
589
|
+
...columns
|
|
590
|
+
}).onConflictDoUpdate({
|
|
591
|
+
target: mediaUploadSessionsPgTable.id,
|
|
592
|
+
set: columns
|
|
593
|
+
});
|
|
594
|
+
return this.persisted(session);
|
|
595
|
+
}
|
|
596
|
+
async delete(id) {
|
|
597
|
+
await this.db.delete(mediaUploadSessionsPgTable).where((0, import_drizzle_orm4.eq)(mediaUploadSessionsPgTable.id, id));
|
|
598
|
+
}
|
|
599
|
+
async addPart(id, part) {
|
|
600
|
+
await this.db.insert(mediaUploadPartsPgTable).values({
|
|
601
|
+
sessionId: id,
|
|
602
|
+
partNumber: part.partNumber,
|
|
603
|
+
etag: part.etag
|
|
604
|
+
}).onConflictDoUpdate({
|
|
605
|
+
target: [
|
|
606
|
+
mediaUploadPartsPgTable.sessionId,
|
|
607
|
+
mediaUploadPartsPgTable.partNumber
|
|
608
|
+
],
|
|
609
|
+
set: {
|
|
610
|
+
etag: import_drizzle_orm4.sql`excluded.etag`
|
|
611
|
+
}
|
|
612
|
+
});
|
|
613
|
+
}
|
|
614
|
+
async listParts(id) {
|
|
615
|
+
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));
|
|
619
|
+
return rows.map((row) => ({
|
|
620
|
+
partNumber: Number(row.partNumber),
|
|
621
|
+
etag: row.etag
|
|
622
|
+
}));
|
|
623
|
+
}
|
|
624
|
+
async list(filter = {}) {
|
|
625
|
+
const conditions = [
|
|
626
|
+
...filter.disk !== void 0 ? [
|
|
627
|
+
(0, import_drizzle_orm4.eq)(mediaUploadSessionsPgTable.disk, filter.disk)
|
|
628
|
+
] : [],
|
|
629
|
+
...filter.keyPrefix !== void 0 ? [
|
|
630
|
+
(0, import_drizzle_orm4.like)(mediaUploadSessionsPgTable.key, `${escapeLike(filter.keyPrefix)}%`)
|
|
631
|
+
] : [],
|
|
632
|
+
...filter.tenant ? [
|
|
633
|
+
tenantCondition(mediaUploadSessionsPgTable.tenantId, filter.tenant)
|
|
634
|
+
] : []
|
|
635
|
+
];
|
|
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));
|
|
637
|
+
return rows.map(toSession);
|
|
638
|
+
}
|
|
639
|
+
};
|
|
640
|
+
|
|
641
|
+
// src/nest/index.ts
|
|
642
|
+
function factory(options, build) {
|
|
643
|
+
return {
|
|
644
|
+
inject: [
|
|
645
|
+
(0, import_drizzle.getDrizzleToken)(options.connectionName)
|
|
646
|
+
],
|
|
647
|
+
useFactory: build
|
|
648
|
+
};
|
|
649
|
+
}
|
|
650
|
+
__name(factory, "factory");
|
|
651
|
+
function drizzleMediaStore(options = {}) {
|
|
652
|
+
return factory(options, (db) => new DrizzleMediaStore(db));
|
|
653
|
+
}
|
|
654
|
+
__name(drizzleMediaStore, "drizzleMediaStore");
|
|
655
|
+
function drizzlePgMediaStore(options = {}) {
|
|
656
|
+
return factory(options, (db) => new DrizzlePgMediaStore(db));
|
|
657
|
+
}
|
|
658
|
+
__name(drizzlePgMediaStore, "drizzlePgMediaStore");
|
|
659
|
+
function drizzlePgUploadSessions(options = {}) {
|
|
660
|
+
return factory(options, (db) => new DrizzlePgUploadSessionStore(db));
|
|
661
|
+
}
|
|
662
|
+
__name(drizzlePgUploadSessions, "drizzlePgUploadSessions");
|
|
663
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
664
|
+
0 && (module.exports = {
|
|
665
|
+
drizzleMediaStore,
|
|
666
|
+
drizzlePgMediaStore,
|
|
667
|
+
drizzlePgUploadSessions
|
|
668
|
+
});
|
|
669
|
+
//# sourceMappingURL=index.cjs.map
|