@dudousxd/nestjs-media-database-drizzle 0.5.5 → 0.5.7
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/index.cjs +92 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -1
- package/dist/index.d.ts +4 -1
- package/dist/index.js +94 -3
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
package/dist/index.cjs
CHANGED
|
@@ -54,10 +54,33 @@ var mediaTable = (0, import_sqlite_core.sqliteTable)("media", {
|
|
|
54
54
|
updatedAt: (0, import_sqlite_core.integer)("updated_at", {
|
|
55
55
|
mode: "timestamp_ms"
|
|
56
56
|
}).notNull()
|
|
57
|
-
})
|
|
57
|
+
}, (table) => ({
|
|
58
|
+
ownerIdx: (0, import_sqlite_core.index)("idx_media_owner").on(table.ownerType, table.ownerId, table.collection),
|
|
59
|
+
collectionIdx: (0, import_sqlite_core.index)("idx_media_collection").on(table.collection),
|
|
60
|
+
diskIdx: (0, import_sqlite_core.index)("idx_media_disk").on(table.disk),
|
|
61
|
+
createdAtIdx: (0, import_sqlite_core.index)("idx_media_created_at").on(table.createdAt),
|
|
62
|
+
collectionCreatedAtIdx: (0, import_sqlite_core.index)("idx_media_collection_created_at").on(table.collection, table.createdAt, table.id)
|
|
63
|
+
}));
|
|
58
64
|
|
|
59
65
|
// src/drizzle-media-store.ts
|
|
60
66
|
var import_drizzle_orm = require("drizzle-orm");
|
|
67
|
+
function encodeListCursor(record) {
|
|
68
|
+
return Buffer.from(`${record.createdAt.toISOString()}|${record.id}`, "utf8").toString("base64");
|
|
69
|
+
}
|
|
70
|
+
__name(encodeListCursor, "encodeListCursor");
|
|
71
|
+
function decodeListCursor(cursor) {
|
|
72
|
+
const decoded = Buffer.from(cursor, "base64").toString("utf8");
|
|
73
|
+
const separator = decoded.indexOf("|");
|
|
74
|
+
if (separator === -1) return null;
|
|
75
|
+
const createdAt = new Date(decoded.slice(0, separator));
|
|
76
|
+
if (Number.isNaN(createdAt.getTime())) return null;
|
|
77
|
+
const id = decoded.slice(separator + 1);
|
|
78
|
+
return {
|
|
79
|
+
createdAt,
|
|
80
|
+
id
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
__name(decodeListCursor, "decodeListCursor");
|
|
61
84
|
function createMediaTable(db) {
|
|
62
85
|
db.run(import_drizzle_orm.sql`
|
|
63
86
|
CREATE TABLE IF NOT EXISTS media (
|
|
@@ -78,6 +101,13 @@ function createMediaTable(db) {
|
|
|
78
101
|
updated_at integer NOT NULL
|
|
79
102
|
)
|
|
80
103
|
`);
|
|
104
|
+
db.run(import_drizzle_orm.sql`CREATE INDEX IF NOT EXISTS idx_media_collection ON media (collection)`);
|
|
105
|
+
db.run(import_drizzle_orm.sql`CREATE INDEX IF NOT EXISTS idx_media_disk ON media (disk)`);
|
|
106
|
+
db.run(import_drizzle_orm.sql`CREATE INDEX IF NOT EXISTS idx_media_created_at ON media (created_at)`);
|
|
107
|
+
db.run(import_drizzle_orm.sql`
|
|
108
|
+
CREATE INDEX IF NOT EXISTS idx_media_collection_created_at
|
|
109
|
+
ON media (collection, created_at, id)
|
|
110
|
+
`);
|
|
81
111
|
}
|
|
82
112
|
__name(createMediaTable, "createMediaTable");
|
|
83
113
|
var DrizzleMediaStore = class {
|
|
@@ -114,6 +144,67 @@ var DrizzleMediaStore = class {
|
|
|
114
144
|
const top = rows[0]?.max;
|
|
115
145
|
return top == null ? 0 : Number(top) + 1;
|
|
116
146
|
}
|
|
147
|
+
async count(filter = {}) {
|
|
148
|
+
const conditions = [
|
|
149
|
+
...filter.ownerType !== void 0 ? [
|
|
150
|
+
(0, import_drizzle_orm.eq)(mediaTable.ownerType, filter.ownerType)
|
|
151
|
+
] : [],
|
|
152
|
+
...filter.collection !== void 0 ? [
|
|
153
|
+
(0, import_drizzle_orm.eq)(mediaTable.collection, filter.collection)
|
|
154
|
+
] : [],
|
|
155
|
+
...filter.disk !== void 0 ? [
|
|
156
|
+
(0, import_drizzle_orm.eq)(mediaTable.disk, filter.disk)
|
|
157
|
+
] : []
|
|
158
|
+
];
|
|
159
|
+
const rows = await this.db.select({
|
|
160
|
+
value: (0, import_drizzle_orm.count)()
|
|
161
|
+
}).from(mediaTable).where(conditions.length ? (0, import_drizzle_orm.and)(...conditions) : void 0);
|
|
162
|
+
return Number(rows[0]?.value ?? 0);
|
|
163
|
+
}
|
|
164
|
+
async aggregate(query) {
|
|
165
|
+
const column = query.groupBy === "collection" ? mediaTable.collection : mediaTable.disk;
|
|
166
|
+
const rows = await this.db.select({
|
|
167
|
+
key: column,
|
|
168
|
+
count: (0, import_drizzle_orm.count)(),
|
|
169
|
+
sumSize: (0, import_drizzle_orm.sum)(mediaTable.size)
|
|
170
|
+
}).from(mediaTable).groupBy(column);
|
|
171
|
+
return rows.map((row) => ({
|
|
172
|
+
key: row.key,
|
|
173
|
+
count: Number(row.count),
|
|
174
|
+
sumSize: query.sum === "size" ? Number(row.sumSize ?? 0) : 0
|
|
175
|
+
}));
|
|
176
|
+
}
|
|
177
|
+
async list(filter = {}, page = {}) {
|
|
178
|
+
const limit = page.limit ?? 50;
|
|
179
|
+
const cursor = page.cursor ? decodeListCursor(page.cursor) : null;
|
|
180
|
+
const filterConditions = [
|
|
181
|
+
...filter.ownerType !== void 0 ? [
|
|
182
|
+
(0, import_drizzle_orm.eq)(mediaTable.ownerType, filter.ownerType)
|
|
183
|
+
] : [],
|
|
184
|
+
...filter.collection !== void 0 ? [
|
|
185
|
+
(0, import_drizzle_orm.eq)(mediaTable.collection, filter.collection)
|
|
186
|
+
] : [],
|
|
187
|
+
...filter.disk !== void 0 ? [
|
|
188
|
+
(0, import_drizzle_orm.eq)(mediaTable.disk, filter.disk)
|
|
189
|
+
] : [],
|
|
190
|
+
...cursor ? [
|
|
191
|
+
(0, import_drizzle_orm.or)((0, import_drizzle_orm.gt)(mediaTable.createdAt, cursor.createdAt), (0, import_drizzle_orm.and)((0, import_drizzle_orm.eq)(mediaTable.createdAt, cursor.createdAt), (0, import_drizzle_orm.gt)(mediaTable.id, cursor.id)))
|
|
192
|
+
] : []
|
|
193
|
+
];
|
|
194
|
+
const rows = await this.db.select().from(mediaTable).where(filterConditions.length ? (0, import_drizzle_orm.and)(...filterConditions) : void 0).orderBy((0, import_drizzle_orm.asc)(mediaTable.createdAt), (0, import_drizzle_orm.asc)(mediaTable.id)).limit(limit + 1);
|
|
195
|
+
const hasMore = rows.length > limit;
|
|
196
|
+
const records = rows.slice(0, limit).map((record) => ({
|
|
197
|
+
...record
|
|
198
|
+
}));
|
|
199
|
+
const result = {
|
|
200
|
+
records
|
|
201
|
+
};
|
|
202
|
+
const lastRecord = records.at(-1);
|
|
203
|
+
if (hasMore && lastRecord !== void 0) {
|
|
204
|
+
result.cursor = encodeListCursor(lastRecord);
|
|
205
|
+
}
|
|
206
|
+
return result;
|
|
207
|
+
}
|
|
117
208
|
};
|
|
118
209
|
|
|
119
210
|
// src/attachment.column.ts
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/media.schema.ts","../src/drizzle-media-store.ts","../src/attachment.column.ts"],"sourcesContent":["export * from './media.schema';\nexport * from './drizzle-media-store';\nexport * from './attachment.column';\n","import { integer, sqliteTable, text } from 'drizzle-orm/sqlite-core';\n\n/**\n * Drizzle (sqlite) media table. `order` maps to the `position` column. JSON columns\n * use text(mode:'json'). Postgres/MySQL variants follow the same shape via their\n * respective drizzle cores.\n */\nexport const mediaTable = sqliteTable('media', {\n id: text('id').primaryKey(),\n ownerType: text('owner_type').notNull(),\n ownerId: text('owner_id').notNull(),\n collection: text('collection').notNull(),\n name: text('name').notNull(),\n fileName: text('file_name').notNull(),\n mimeType: text('mime_type').notNull(),\n size: integer('size').notNull(),\n disk: text('disk').notNull(),\n path: text('path').notNull(),\n order: integer('position').notNull(),\n customProperties: text('custom_properties', { mode: 'json' }).notNull(),\n conversions: text('conversions', { mode: 'json' }).notNull(),\n createdAt: integer('created_at', { mode: 'timestamp_ms' }).notNull(),\n updatedAt: integer('updated_at', { mode: 'timestamp_ms' }).notNull(),\n});\n","import type { MediaRecord, MediaStore } from '@dudousxd/nestjs-media-core';\nimport { and, asc, eq, max, sql } from 'drizzle-orm';\nimport type { BetterSQLite3Database } from 'drizzle-orm/better-sqlite3';\nimport { mediaTable } from './media.schema';\n\ntype DB = BetterSQLite3Database<Record<string, never>>;\n\n/**\n * Migration-first (§3.10): Drizzle has no auto-ensure. Run migrations with\n * drizzle-kit in production; this helper creates the table for tests/dev.\n */\nexport function createMediaTable(db: DB): void {\n db.run(sql`\n CREATE TABLE IF NOT EXISTS media (\n id text PRIMARY KEY NOT NULL,\n owner_type text NOT NULL,\n owner_id text NOT NULL,\n collection text NOT NULL,\n name text NOT NULL,\n file_name text NOT NULL,\n mime_type text NOT NULL,\n size integer NOT NULL,\n disk text NOT NULL,\n path text NOT NULL,\n position integer NOT NULL,\n custom_properties text NOT NULL,\n conversions text NOT NULL,\n created_at integer NOT NULL,\n updated_at integer NOT NULL\n )\n `);\n}\n\n/** MediaStore backed by Drizzle (better-sqlite3). POJO receiving the drizzle db. */\nexport class DrizzleMediaStore implements MediaStore {\n constructor(private readonly db: DB) {}\n\n async save(record: MediaRecord): Promise<MediaRecord> {\n await this.db\n .insert(mediaTable)\n .values(record)\n .onConflictDoUpdate({ target: mediaTable.id, set: record });\n return record;\n }\n\n async find(id: string): Promise<MediaRecord | null> {\n const rows = await this.db.select().from(mediaTable).where(eq(mediaTable.id, id)).limit(1);\n return (rows[0] as MediaRecord | undefined) ?? null;\n }\n\n async listByOwner(\n ownerType: string,\n ownerId: string,\n collection?: string,\n ): Promise<MediaRecord[]> {\n const where =\n collection === undefined\n ? and(eq(mediaTable.ownerType, ownerType), eq(mediaTable.ownerId, ownerId))\n : and(\n eq(mediaTable.ownerType, ownerType),\n eq(mediaTable.ownerId, ownerId),\n eq(mediaTable.collection, collection),\n );\n const rows = await this.db\n .select()\n .from(mediaTable)\n .where(where)\n .orderBy(asc(mediaTable.order));\n return rows as MediaRecord[];\n }\n\n async delete(id: string): Promise<void> {\n await this.db.delete(mediaTable).where(eq(mediaTable.id, id));\n }\n\n async nextOrder(ownerType: string, ownerId: string, collection: string): Promise<number> {\n const rows = await this.db\n .select({ max: max(mediaTable.order) })\n .from(mediaTable)\n .where(\n and(\n eq(mediaTable.ownerType, ownerType),\n eq(mediaTable.ownerId, ownerId),\n eq(mediaTable.collection, collection),\n ),\n );\n const top = rows[0]?.max;\n return top == null ? 0 : Number(top) + 1;\n }\n}\n","import { Attachment, type AttachmentData } from '@dudousxd/nestjs-media-core';\nimport { customType } from 'drizzle-orm/sqlite-core';\n\n/**\n * Drizzle (sqlite) custom column for an Attachment value:\n * `avatar: attachment('avatar')` → stored as JSON text, rehydrated to an `Attachment`.\n */\nexport const attachment = customType<{ data: Attachment | null; driverData: string }>({\n dataType() {\n return 'text';\n },\n toDriver(value: Attachment | null): string {\n return JSON.stringify(value ? value.toJSON() : null);\n },\n fromDriver(value: string): Attachment | null {\n return Attachment.fromJSON(value ? (JSON.parse(value) as AttachmentData) : null);\n },\n});\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;ACAA,yBAA2C;AAOpC,IAAMA,iBAAaC,gCAAY,SAAS;EAC7CC,QAAIC,yBAAK,IAAA,EAAMC,WAAU;EACzBC,eAAWF,yBAAK,YAAA,EAAcG,QAAO;EACrCC,aAASJ,yBAAK,UAAA,EAAYG,QAAO;EACjCE,gBAAYL,yBAAK,YAAA,EAAcG,QAAO;EACtCG,UAAMN,yBAAK,MAAA,EAAQG,QAAO;EAC1BI,cAAUP,yBAAK,WAAA,EAAaG,QAAO;EACnCK,cAAUR,yBAAK,WAAA,EAAaG,QAAO;EACnCM,UAAMC,4BAAQ,MAAA,EAAQP,QAAO;EAC7BQ,UAAMX,yBAAK,MAAA,EAAQG,QAAO;EAC1BS,UAAMZ,yBAAK,MAAA,EAAQG,QAAO;EAC1BU,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C,KAAKT,UAAAA,EACLU,MAAMA,KAAAA,EACNS,YAAQC,wBAAIpB,WAAWqB,KAAK,CAAA;AAC/B,WAAOd;EACT;EAEA,MAAMe,OAAOlB,IAA2B;AACtC,UAAM,KAAKX,GAAG6B,OAAOtB,UAAAA,EAAYU,UAAMC,uBAAGX,WAAWI,IAAIA,EAAAA,CAAAA;EAC3D;EAEA,MAAMmB,UAAUT,WAAmBC,SAAiBC,YAAqC;AACvF,UAAMT,OAAO,MAAM,KAAKd,GACrBe,OAAO;MAAEgB,SAAKA,wBAAIxB,WAAWqB,KAAK;IAAE,CAAA,EACpCZ,KAAKT,UAAAA,EACLU,UACCQ,4BACEP,uBAAGX,WAAWc,WAAWA,SAAAA,OACzBH,uBAAGX,WAAWe,SAASA,OAAAA,OACvBJ,uBAAGX,WAAWgB,YAAYA,UAAAA,CAAAA,CAAAA;AAGhC,UAAMS,MAAMlB,KAAK,CAAA,GAAIiB;AACrB,WAAOC,OAAO,OAAO,IAAIC,OAAOD,GAAAA,IAAO;EACzC;AACF;;;ACzFA,+BAAgD;AAChD,IAAAE,sBAA2B;AAMpB,IAAMC,iBAAaC,gCAA4D;EACpFC,WAAAA;AACE,WAAO;EACT;EACAC,SAASC,OAAwB;AAC/B,WAAOC,KAAKC,UAAUF,QAAQA,MAAMG,OAAM,IAAK,IAAA;EACjD;EACAC,WAAWJ,OAAa;AACtB,WAAOK,oCAAWC,SAASN,QAASC,KAAKM,MAAMP,KAAAA,IAA4B,IAAA;EAC7E;AACF,CAAA;","names":["mediaTable","sqliteTable","id","text","primaryKey","ownerType","notNull","ownerId","collection","name","fileName","mimeType","size","integer","disk","path","order","customProperties","mode","conversions","createdAt","updatedAt","createMediaTable","db","run","sql","DrizzleMediaStore","save","record","insert","mediaTable","values","onConflictDoUpdate","target","id","set","find","rows","select","from","where","eq","limit","listByOwner","ownerType","ownerId","collection","undefined","and","orderBy","asc","order","delete","nextOrder","max","top","Number","import_sqlite_core","attachment","customType","dataType","toDriver","value","JSON","stringify","toJSON","fromDriver","Attachment","fromJSON","parse"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/media.schema.ts","../src/drizzle-media-store.ts","../src/attachment.column.ts"],"sourcesContent":["export * from './media.schema';\nexport * from './drizzle-media-store';\nexport * from './attachment.column';\n","import { index, integer, sqliteTable, text } from 'drizzle-orm/sqlite-core';\n\n/**\n * Drizzle (sqlite) media table. `order` maps to the `position` column. JSON columns\n * use text(mode:'json'). Postgres/MySQL variants follow the same shape via their\n * respective drizzle cores.\n */\nexport const mediaTable = sqliteTable(\n 'media',\n {\n id: text('id').primaryKey(),\n ownerType: text('owner_type').notNull(),\n ownerId: text('owner_id').notNull(),\n collection: text('collection').notNull(),\n name: text('name').notNull(),\n fileName: text('file_name').notNull(),\n mimeType: text('mime_type').notNull(),\n size: integer('size').notNull(),\n disk: text('disk').notNull(),\n path: text('path').notNull(),\n order: integer('position').notNull(),\n customProperties: text('custom_properties', { mode: 'json' }).notNull(),\n conversions: text('conversions', { mode: 'json' }).notNull(),\n createdAt: integer('created_at', { mode: 'timestamp_ms' }).notNull(),\n updatedAt: integer('updated_at', { mode: 'timestamp_ms' }).notNull(),\n },\n (table) => ({\n ownerIdx: index('idx_media_owner').on(table.ownerType, table.ownerId, table.collection),\n collectionIdx: index('idx_media_collection').on(table.collection),\n diskIdx: index('idx_media_disk').on(table.disk),\n createdAtIdx: index('idx_media_created_at').on(table.createdAt),\n collectionCreatedAtIdx: index('idx_media_collection_created_at').on(\n table.collection,\n table.createdAt,\n table.id,\n ),\n }),\n);\n","import type {\n MediaAggregateQuery,\n MediaAggregateResult,\n MediaCountFilter,\n MediaListFilter,\n MediaListPage,\n MediaListResult,\n MediaRecord,\n MediaStore,\n} from '@dudousxd/nestjs-media-core';\nimport { and, asc, count, eq, gt, max, or, sql, sum } from 'drizzle-orm';\nimport type { BetterSQLite3Database } from 'drizzle-orm/better-sqlite3';\nimport { mediaTable } from './media.schema';\n\ntype DB = BetterSQLite3Database<Record<string, never>>;\n\n/** Opaque keyset cursor over `(createdAt, id)`. Mirrors the in-memory store's encoding. */\nfunction encodeListCursor(record: MediaRecord): string {\n return Buffer.from(`${record.createdAt.toISOString()}|${record.id}`, 'utf8').toString('base64');\n}\n\ninterface DecodedListCursor {\n createdAt: Date;\n id: string;\n}\n\nfunction decodeListCursor(cursor: string): DecodedListCursor | null {\n const decoded = Buffer.from(cursor, 'base64').toString('utf8');\n const separator = decoded.indexOf('|');\n if (separator === -1) return null;\n const createdAt = new Date(decoded.slice(0, separator));\n if (Number.isNaN(createdAt.getTime())) return null;\n const id = decoded.slice(separator + 1);\n return { createdAt, id };\n}\n\n/**\n * Migration-first (§3.10): Drizzle has no auto-ensure. Run migrations with\n * drizzle-kit in production; this helper creates the table for tests/dev.\n */\nexport function createMediaTable(db: DB): void {\n db.run(sql`\n CREATE TABLE IF NOT EXISTS media (\n id text PRIMARY KEY NOT NULL,\n owner_type text NOT NULL,\n owner_id text NOT NULL,\n collection text NOT NULL,\n name text NOT NULL,\n file_name text NOT NULL,\n mime_type text NOT NULL,\n size integer NOT NULL,\n disk text NOT NULL,\n path text NOT NULL,\n position integer NOT NULL,\n custom_properties text NOT NULL,\n conversions text NOT NULL,\n created_at integer NOT NULL,\n updated_at integer NOT NULL\n )\n `);\n db.run(sql`CREATE INDEX IF NOT EXISTS idx_media_collection ON media (collection)`);\n db.run(sql`CREATE INDEX IF NOT EXISTS idx_media_disk ON media (disk)`);\n db.run(sql`CREATE INDEX IF NOT EXISTS idx_media_created_at ON media (created_at)`);\n db.run(sql`\n CREATE INDEX IF NOT EXISTS idx_media_collection_created_at\n ON media (collection, created_at, id)\n `);\n}\n\n/** MediaStore backed by Drizzle (better-sqlite3). POJO receiving the drizzle db. */\nexport class DrizzleMediaStore implements MediaStore {\n constructor(private readonly db: DB) {}\n\n async save(record: MediaRecord): Promise<MediaRecord> {\n await this.db\n .insert(mediaTable)\n .values(record)\n .onConflictDoUpdate({ target: mediaTable.id, set: record });\n return record;\n }\n\n async find(id: string): Promise<MediaRecord | null> {\n const rows = await this.db.select().from(mediaTable).where(eq(mediaTable.id, id)).limit(1);\n return (rows[0] as MediaRecord | undefined) ?? null;\n }\n\n async listByOwner(\n ownerType: string,\n ownerId: string,\n collection?: string,\n ): Promise<MediaRecord[]> {\n const where =\n collection === undefined\n ? and(eq(mediaTable.ownerType, ownerType), eq(mediaTable.ownerId, ownerId))\n : and(\n eq(mediaTable.ownerType, ownerType),\n eq(mediaTable.ownerId, ownerId),\n eq(mediaTable.collection, collection),\n );\n const rows = await this.db\n .select()\n .from(mediaTable)\n .where(where)\n .orderBy(asc(mediaTable.order));\n return rows as MediaRecord[];\n }\n\n async delete(id: string): Promise<void> {\n await this.db.delete(mediaTable).where(eq(mediaTable.id, id));\n }\n\n async nextOrder(ownerType: string, ownerId: string, collection: string): Promise<number> {\n const rows = await this.db\n .select({ max: max(mediaTable.order) })\n .from(mediaTable)\n .where(\n and(\n eq(mediaTable.ownerType, ownerType),\n eq(mediaTable.ownerId, ownerId),\n eq(mediaTable.collection, collection),\n ),\n );\n const top = rows[0]?.max;\n return top == null ? 0 : Number(top) + 1;\n }\n\n async count(filter: MediaCountFilter = {}): Promise<number> {\n const conditions = [\n ...(filter.ownerType !== undefined ? [eq(mediaTable.ownerType, filter.ownerType)] : []),\n ...(filter.collection !== undefined ? [eq(mediaTable.collection, filter.collection)] : []),\n ...(filter.disk !== undefined ? [eq(mediaTable.disk, filter.disk)] : []),\n ];\n const rows = await this.db\n .select({ value: count() })\n .from(mediaTable)\n .where(conditions.length ? and(...conditions) : undefined);\n return Number(rows[0]?.value ?? 0);\n }\n\n async aggregate(query: MediaAggregateQuery): Promise<MediaAggregateResult> {\n const column = query.groupBy === 'collection' ? mediaTable.collection : mediaTable.disk;\n const rows = await this.db\n .select({ key: column, count: count(), sumSize: sum(mediaTable.size) })\n .from(mediaTable)\n .groupBy(column);\n return rows.map((row) => ({\n key: row.key,\n count: Number(row.count),\n sumSize: query.sum === 'size' ? Number(row.sumSize ?? 0) : 0,\n }));\n }\n\n async list(filter: MediaListFilter = {}, page: MediaListPage = {}): Promise<MediaListResult> {\n const limit = page.limit ?? 50;\n const cursor = page.cursor ? decodeListCursor(page.cursor) : null;\n\n const filterConditions = [\n ...(filter.ownerType !== undefined ? [eq(mediaTable.ownerType, filter.ownerType)] : []),\n ...(filter.collection !== undefined ? [eq(mediaTable.collection, filter.collection)] : []),\n ...(filter.disk !== undefined ? [eq(mediaTable.disk, filter.disk)] : []),\n ...(cursor\n ? [\n or(\n gt(mediaTable.createdAt, cursor.createdAt),\n and(eq(mediaTable.createdAt, cursor.createdAt), gt(mediaTable.id, cursor.id)),\n ),\n ]\n : []),\n ];\n\n const rows = await this.db\n .select()\n .from(mediaTable)\n .where(filterConditions.length ? and(...filterConditions) : undefined)\n .orderBy(asc(mediaTable.createdAt), asc(mediaTable.id))\n .limit(limit + 1);\n\n const hasMore = rows.length > limit;\n const records = (rows.slice(0, limit) as MediaRecord[]).map((record) => ({ ...record }));\n const result: MediaListResult = { records };\n const lastRecord = records.at(-1);\n if (hasMore && lastRecord !== undefined) {\n result.cursor = encodeListCursor(lastRecord);\n }\n return result;\n }\n}\n","import { Attachment, type AttachmentData } from '@dudousxd/nestjs-media-core';\nimport { customType } from 'drizzle-orm/sqlite-core';\n\n/**\n * Drizzle (sqlite) custom column for an Attachment value:\n * `avatar: attachment('avatar')` → stored as JSON text, rehydrated to an `Attachment`.\n */\nexport const attachment = customType<{ data: Attachment | null; driverData: string }>({\n dataType() {\n return 'text';\n },\n toDriver(value: Attachment | null): string {\n return JSON.stringify(value ? value.toJSON() : null);\n },\n fromDriver(value: string): Attachment | null {\n return Attachment.fromJSON(value ? (JSON.parse(value) as AttachmentData) : null);\n },\n});\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;ACAA,yBAAkD;AAO3C,IAAMA,iBAAaC,gCACxB,SACA;EACEC,QAAIC,yBAAK,IAAA,EAAMC,WAAU;EACzBC,eAAWF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AE,CAAA,CAAA;UAG/E,CAAA;;AAGN,UAAMyB,OAAO,MAAM,KAAKZ,GACrBa,OAAM,EACN7B,KAAKsB,UAAAA,EACLQ,MAAMkC,iBAAiBb,aAASb,wBAAAA,GAAO0B,gBAAAA,IAAoB3B,MAAAA,EAC3DE,YAAQC,wBAAIlB,WAAWrB,SAAS,OAAGuC,wBAAIlB,WAAWnB,EAAE,CAAA,EACpD6B,MAAMA,QAAQ,CAAA;AAEjB,UAAMmC,UAAUvC,KAAKuB,SAASnB;AAC9B,UAAMoC,UAAWxC,KAAKjB,MAAM,GAAGqB,KAAAA,EAAyB4B,IAAI,CAAC9D,YAAY;MAAE,GAAGA;IAAO,EAAA;AACrF,UAAMuE,SAA0B;MAAED;IAAQ;AAC1C,UAAME,aAAaF,QAAQG,GAAG,EAAC;AAC/B,QAAIJ,WAAWG,eAAejC,QAAW;AACvCgC,aAAO/D,SAAST,iBAAiByE,UAAAA;IACnC;AACA,WAAOD;EACT;AACF;;;AC1LA,+BAAgD;AAChD,IAAAG,sBAA2B;AAMpB,IAAMC,iBAAaC,gCAA4D;EACpFC,WAAAA;AACE,WAAO;EACT;EACAC,SAASC,OAAwB;AAC/B,WAAOC,KAAKC,UAAUF,QAAQA,MAAMG,OAAM,IAAK,IAAA;EACjD;EACAC,WAAWJ,OAAa;AACtB,WAAOK,oCAAWC,SAASN,QAASC,KAAKM,MAAMP,KAAAA,IAA4B,IAAA;EAC7E;AACF,CAAA;","names":["mediaTable","sqliteTable","id","text","primaryKey","ownerType","notNull","ownerId","collection","name","fileName","mimeType","size","integer","disk","path","order","customProperties","mode","conversions","createdAt","updatedAt","table","ownerIdx","index","on","collectionIdx","diskIdx","createdAtIdx","collectionCreatedAtIdx","encodeListCursor","record","Buffer","from","createdAt","toISOString","id","toString","decodeListCursor","cursor","decoded","separator","indexOf","Date","slice","Number","isNaN","getTime","createMediaTable","db","run","sql","DrizzleMediaStore","save","insert","mediaTable","values","onConflictDoUpdate","target","set","find","rows","select","where","eq","limit","listByOwner","ownerType","ownerId","collection","undefined","and","orderBy","asc","order","delete","nextOrder","max","top","count","filter","conditions","disk","value","length","aggregate","query","column","groupBy","key","sumSize","sum","size","map","row","list","page","filterConditions","or","gt","hasMore","records","result","lastRecord","at","import_sqlite_core","attachment","customType","dataType","toDriver","value","JSON","stringify","toJSON","fromDriver","Attachment","fromJSON","parse"]}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as drizzle_orm_sqlite_core from 'drizzle-orm/sqlite-core';
|
|
2
|
-
import { MediaStore, MediaRecord, Attachment } from '@dudousxd/nestjs-media-core';
|
|
2
|
+
import { MediaStore, MediaRecord, MediaCountFilter, MediaAggregateQuery, MediaAggregateResult, MediaListFilter, MediaListPage, MediaListResult, Attachment } from '@dudousxd/nestjs-media-core';
|
|
3
3
|
import { BetterSQLite3Database } from 'drizzle-orm/better-sqlite3';
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -303,6 +303,9 @@ declare class DrizzleMediaStore implements MediaStore {
|
|
|
303
303
|
listByOwner(ownerType: string, ownerId: string, collection?: string): Promise<MediaRecord[]>;
|
|
304
304
|
delete(id: string): Promise<void>;
|
|
305
305
|
nextOrder(ownerType: string, ownerId: string, collection: string): Promise<number>;
|
|
306
|
+
count(filter?: MediaCountFilter): Promise<number>;
|
|
307
|
+
aggregate(query: MediaAggregateQuery): Promise<MediaAggregateResult>;
|
|
308
|
+
list(filter?: MediaListFilter, page?: MediaListPage): Promise<MediaListResult>;
|
|
306
309
|
}
|
|
307
310
|
|
|
308
311
|
/**
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as drizzle_orm_sqlite_core from 'drizzle-orm/sqlite-core';
|
|
2
|
-
import { MediaStore, MediaRecord, Attachment } from '@dudousxd/nestjs-media-core';
|
|
2
|
+
import { MediaStore, MediaRecord, MediaCountFilter, MediaAggregateQuery, MediaAggregateResult, MediaListFilter, MediaListPage, MediaListResult, Attachment } from '@dudousxd/nestjs-media-core';
|
|
3
3
|
import { BetterSQLite3Database } from 'drizzle-orm/better-sqlite3';
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -303,6 +303,9 @@ declare class DrizzleMediaStore implements MediaStore {
|
|
|
303
303
|
listByOwner(ownerType: string, ownerId: string, collection?: string): Promise<MediaRecord[]>;
|
|
304
304
|
delete(id: string): Promise<void>;
|
|
305
305
|
nextOrder(ownerType: string, ownerId: string, collection: string): Promise<number>;
|
|
306
|
+
count(filter?: MediaCountFilter): Promise<number>;
|
|
307
|
+
aggregate(query: MediaAggregateQuery): Promise<MediaAggregateResult>;
|
|
308
|
+
list(filter?: MediaListFilter, page?: MediaListPage): Promise<MediaListResult>;
|
|
306
309
|
}
|
|
307
310
|
|
|
308
311
|
/**
|
package/dist/index.js
CHANGED
|
@@ -2,7 +2,7 @@ var __defProp = Object.defineProperty;
|
|
|
2
2
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
3
|
|
|
4
4
|
// src/media.schema.ts
|
|
5
|
-
import { integer, sqliteTable, text } from "drizzle-orm/sqlite-core";
|
|
5
|
+
import { index, integer, sqliteTable, text } from "drizzle-orm/sqlite-core";
|
|
6
6
|
var mediaTable = sqliteTable("media", {
|
|
7
7
|
id: text("id").primaryKey(),
|
|
8
8
|
ownerType: text("owner_type").notNull(),
|
|
@@ -27,10 +27,33 @@ var mediaTable = sqliteTable("media", {
|
|
|
27
27
|
updatedAt: integer("updated_at", {
|
|
28
28
|
mode: "timestamp_ms"
|
|
29
29
|
}).notNull()
|
|
30
|
-
})
|
|
30
|
+
}, (table) => ({
|
|
31
|
+
ownerIdx: index("idx_media_owner").on(table.ownerType, table.ownerId, table.collection),
|
|
32
|
+
collectionIdx: index("idx_media_collection").on(table.collection),
|
|
33
|
+
diskIdx: index("idx_media_disk").on(table.disk),
|
|
34
|
+
createdAtIdx: index("idx_media_created_at").on(table.createdAt),
|
|
35
|
+
collectionCreatedAtIdx: index("idx_media_collection_created_at").on(table.collection, table.createdAt, table.id)
|
|
36
|
+
}));
|
|
31
37
|
|
|
32
38
|
// src/drizzle-media-store.ts
|
|
33
|
-
import { and, asc, eq, max, sql } from "drizzle-orm";
|
|
39
|
+
import { and, asc, count, eq, gt, max, or, sql, sum } from "drizzle-orm";
|
|
40
|
+
function encodeListCursor(record) {
|
|
41
|
+
return Buffer.from(`${record.createdAt.toISOString()}|${record.id}`, "utf8").toString("base64");
|
|
42
|
+
}
|
|
43
|
+
__name(encodeListCursor, "encodeListCursor");
|
|
44
|
+
function decodeListCursor(cursor) {
|
|
45
|
+
const decoded = Buffer.from(cursor, "base64").toString("utf8");
|
|
46
|
+
const separator = decoded.indexOf("|");
|
|
47
|
+
if (separator === -1) return null;
|
|
48
|
+
const createdAt = new Date(decoded.slice(0, separator));
|
|
49
|
+
if (Number.isNaN(createdAt.getTime())) return null;
|
|
50
|
+
const id = decoded.slice(separator + 1);
|
|
51
|
+
return {
|
|
52
|
+
createdAt,
|
|
53
|
+
id
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
__name(decodeListCursor, "decodeListCursor");
|
|
34
57
|
function createMediaTable(db) {
|
|
35
58
|
db.run(sql`
|
|
36
59
|
CREATE TABLE IF NOT EXISTS media (
|
|
@@ -51,6 +74,13 @@ function createMediaTable(db) {
|
|
|
51
74
|
updated_at integer NOT NULL
|
|
52
75
|
)
|
|
53
76
|
`);
|
|
77
|
+
db.run(sql`CREATE INDEX IF NOT EXISTS idx_media_collection ON media (collection)`);
|
|
78
|
+
db.run(sql`CREATE INDEX IF NOT EXISTS idx_media_disk ON media (disk)`);
|
|
79
|
+
db.run(sql`CREATE INDEX IF NOT EXISTS idx_media_created_at ON media (created_at)`);
|
|
80
|
+
db.run(sql`
|
|
81
|
+
CREATE INDEX IF NOT EXISTS idx_media_collection_created_at
|
|
82
|
+
ON media (collection, created_at, id)
|
|
83
|
+
`);
|
|
54
84
|
}
|
|
55
85
|
__name(createMediaTable, "createMediaTable");
|
|
56
86
|
var DrizzleMediaStore = class {
|
|
@@ -87,6 +117,67 @@ var DrizzleMediaStore = class {
|
|
|
87
117
|
const top = rows[0]?.max;
|
|
88
118
|
return top == null ? 0 : Number(top) + 1;
|
|
89
119
|
}
|
|
120
|
+
async count(filter = {}) {
|
|
121
|
+
const conditions = [
|
|
122
|
+
...filter.ownerType !== void 0 ? [
|
|
123
|
+
eq(mediaTable.ownerType, filter.ownerType)
|
|
124
|
+
] : [],
|
|
125
|
+
...filter.collection !== void 0 ? [
|
|
126
|
+
eq(mediaTable.collection, filter.collection)
|
|
127
|
+
] : [],
|
|
128
|
+
...filter.disk !== void 0 ? [
|
|
129
|
+
eq(mediaTable.disk, filter.disk)
|
|
130
|
+
] : []
|
|
131
|
+
];
|
|
132
|
+
const rows = await this.db.select({
|
|
133
|
+
value: count()
|
|
134
|
+
}).from(mediaTable).where(conditions.length ? and(...conditions) : void 0);
|
|
135
|
+
return Number(rows[0]?.value ?? 0);
|
|
136
|
+
}
|
|
137
|
+
async aggregate(query) {
|
|
138
|
+
const column = query.groupBy === "collection" ? mediaTable.collection : mediaTable.disk;
|
|
139
|
+
const rows = await this.db.select({
|
|
140
|
+
key: column,
|
|
141
|
+
count: count(),
|
|
142
|
+
sumSize: sum(mediaTable.size)
|
|
143
|
+
}).from(mediaTable).groupBy(column);
|
|
144
|
+
return rows.map((row) => ({
|
|
145
|
+
key: row.key,
|
|
146
|
+
count: Number(row.count),
|
|
147
|
+
sumSize: query.sum === "size" ? Number(row.sumSize ?? 0) : 0
|
|
148
|
+
}));
|
|
149
|
+
}
|
|
150
|
+
async list(filter = {}, page = {}) {
|
|
151
|
+
const limit = page.limit ?? 50;
|
|
152
|
+
const cursor = page.cursor ? decodeListCursor(page.cursor) : null;
|
|
153
|
+
const filterConditions = [
|
|
154
|
+
...filter.ownerType !== void 0 ? [
|
|
155
|
+
eq(mediaTable.ownerType, filter.ownerType)
|
|
156
|
+
] : [],
|
|
157
|
+
...filter.collection !== void 0 ? [
|
|
158
|
+
eq(mediaTable.collection, filter.collection)
|
|
159
|
+
] : [],
|
|
160
|
+
...filter.disk !== void 0 ? [
|
|
161
|
+
eq(mediaTable.disk, filter.disk)
|
|
162
|
+
] : [],
|
|
163
|
+
...cursor ? [
|
|
164
|
+
or(gt(mediaTable.createdAt, cursor.createdAt), and(eq(mediaTable.createdAt, cursor.createdAt), gt(mediaTable.id, cursor.id)))
|
|
165
|
+
] : []
|
|
166
|
+
];
|
|
167
|
+
const rows = await this.db.select().from(mediaTable).where(filterConditions.length ? and(...filterConditions) : void 0).orderBy(asc(mediaTable.createdAt), asc(mediaTable.id)).limit(limit + 1);
|
|
168
|
+
const hasMore = rows.length > limit;
|
|
169
|
+
const records = rows.slice(0, limit).map((record) => ({
|
|
170
|
+
...record
|
|
171
|
+
}));
|
|
172
|
+
const result = {
|
|
173
|
+
records
|
|
174
|
+
};
|
|
175
|
+
const lastRecord = records.at(-1);
|
|
176
|
+
if (hasMore && lastRecord !== void 0) {
|
|
177
|
+
result.cursor = encodeListCursor(lastRecord);
|
|
178
|
+
}
|
|
179
|
+
return result;
|
|
180
|
+
}
|
|
90
181
|
};
|
|
91
182
|
|
|
92
183
|
// src/attachment.column.ts
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/media.schema.ts","../src/drizzle-media-store.ts","../src/attachment.column.ts"],"sourcesContent":["import { integer, sqliteTable, text } from 'drizzle-orm/sqlite-core';\n\n/**\n * Drizzle (sqlite) media table. `order` maps to the `position` column. JSON columns\n * use text(mode:'json'). Postgres/MySQL variants follow the same shape via their\n * respective drizzle cores.\n */\nexport const mediaTable = sqliteTable('media', {\n id: text('id').primaryKey(),\n ownerType: text('owner_type').notNull(),\n ownerId: text('owner_id').notNull(),\n collection: text('collection').notNull(),\n name: text('name').notNull(),\n fileName: text('file_name').notNull(),\n mimeType: text('mime_type').notNull(),\n size: integer('size').notNull(),\n disk: text('disk').notNull(),\n path: text('path').notNull(),\n order: integer('position').notNull(),\n customProperties: text('custom_properties', { mode: 'json' }).notNull(),\n conversions: text('conversions', { mode: 'json' }).notNull(),\n createdAt: integer('created_at', { mode: 'timestamp_ms' }).notNull(),\n updatedAt: integer('updated_at', { mode: 'timestamp_ms' }).notNull(),\n});\n","import type { MediaRecord, MediaStore } from '@dudousxd/nestjs-media-core';\nimport { and, asc, eq, max, sql } from 'drizzle-orm';\nimport type { BetterSQLite3Database } from 'drizzle-orm/better-sqlite3';\nimport { mediaTable } from './media.schema';\n\ntype DB = BetterSQLite3Database<Record<string, never>>;\n\n/**\n * Migration-first (§3.10): Drizzle has no auto-ensure. Run migrations with\n * drizzle-kit in production; this helper creates the table for tests/dev.\n */\nexport function createMediaTable(db: DB): void {\n db.run(sql`\n CREATE TABLE IF NOT EXISTS media (\n id text PRIMARY KEY NOT NULL,\n owner_type text NOT NULL,\n owner_id text NOT NULL,\n collection text NOT NULL,\n name text NOT NULL,\n file_name text NOT NULL,\n mime_type text NOT NULL,\n size integer NOT NULL,\n disk text NOT NULL,\n path text NOT NULL,\n position integer NOT NULL,\n custom_properties text NOT NULL,\n conversions text NOT NULL,\n created_at integer NOT NULL,\n updated_at integer NOT NULL\n )\n `);\n}\n\n/** MediaStore backed by Drizzle (better-sqlite3). POJO receiving the drizzle db. */\nexport class DrizzleMediaStore implements MediaStore {\n constructor(private readonly db: DB) {}\n\n async save(record: MediaRecord): Promise<MediaRecord> {\n await this.db\n .insert(mediaTable)\n .values(record)\n .onConflictDoUpdate({ target: mediaTable.id, set: record });\n return record;\n }\n\n async find(id: string): Promise<MediaRecord | null> {\n const rows = await this.db.select().from(mediaTable).where(eq(mediaTable.id, id)).limit(1);\n return (rows[0] as MediaRecord | undefined) ?? null;\n }\n\n async listByOwner(\n ownerType: string,\n ownerId: string,\n collection?: string,\n ): Promise<MediaRecord[]> {\n const where =\n collection === undefined\n ? and(eq(mediaTable.ownerType, ownerType), eq(mediaTable.ownerId, ownerId))\n : and(\n eq(mediaTable.ownerType, ownerType),\n eq(mediaTable.ownerId, ownerId),\n eq(mediaTable.collection, collection),\n );\n const rows = await this.db\n .select()\n .from(mediaTable)\n .where(where)\n .orderBy(asc(mediaTable.order));\n return rows as MediaRecord[];\n }\n\n async delete(id: string): Promise<void> {\n await this.db.delete(mediaTable).where(eq(mediaTable.id, id));\n }\n\n async nextOrder(ownerType: string, ownerId: string, collection: string): Promise<number> {\n const rows = await this.db\n .select({ max: max(mediaTable.order) })\n .from(mediaTable)\n .where(\n and(\n eq(mediaTable.ownerType, ownerType),\n eq(mediaTable.ownerId, ownerId),\n eq(mediaTable.collection, collection),\n ),\n );\n const top = rows[0]?.max;\n return top == null ? 0 : Number(top) + 1;\n }\n}\n","import { Attachment, type AttachmentData } from '@dudousxd/nestjs-media-core';\nimport { customType } from 'drizzle-orm/sqlite-core';\n\n/**\n * Drizzle (sqlite) custom column for an Attachment value:\n * `avatar: attachment('avatar')` → stored as JSON text, rehydrated to an `Attachment`.\n */\nexport const attachment = customType<{ data: Attachment | null; driverData: string }>({\n dataType() {\n return 'text';\n },\n toDriver(value: Attachment | null): string {\n return JSON.stringify(value ? value.toJSON() : null);\n },\n fromDriver(value: string): Attachment | null {\n return Attachment.fromJSON(value ? (JSON.parse(value) as AttachmentData) : null);\n },\n});\n"],"mappings":";;;;AAAA,SAASA,SAASC,aAAaC,YAAY;AAOpC,IAAMC,aAAaF,YAAY,SAAS;EAC7CG,IAAIF,KAAK,IAAA,EAAMG,WAAU;EACzBC,WAAWJ,KAAK,YAAA,EAAcK,QAAO;EACrCC,SAASN,KAAK,UAAA,EAAYK,QAAO;EACjCE,YAAYP,KAAK,YAAA,EAAcK,QAAO;EACtCG,MAAMR,KAAK,MAAA,EAAQK,QAAO;EAC1BI,UAAUT,KAAK,WAAA,EAAaK,QAAO;EACnCK,UAAUV,KAAK,WAAA,EAAaK,QAAO;EACnCM,MAAMb,QAAQ,MAAA,EAAQO,QAAO;EAC7BO,MAAMZ,KAAK,MAAA,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,WAAWgB,YAAYA,UAAAA,CAAAA;AAElC,UAAMT,OAAO,MAAM,KAAKd,GACrBe,OAAM,EACNC,KAAKT,UAAAA,EACLU,MAAMA,KAAAA,EACNS,QAAQC,IAAIpB,WAAWqB,KAAK,CAAA;AAC/B,WAAOd;EACT;EAEA,MAAMe,OAAOlB,IAA2B;AACtC,UAAM,KAAKX,GAAG6B,OAAOtB,UAAAA,EAAYU,MAAMC,GAAGX,WAAWI,IAAIA,EAAAA,CAAAA;EAC3D;EAEA,MAAMmB,UAAUT,WAAmBC,SAAiBC,YAAqC;AACvF,UAAMT,OAAO,MAAM,KAAKd,GACrBe,OAAO;MAAEgB,KAAKA,IAAIxB,WAAWqB,KAAK;IAAE,CAAA,EACpCZ,KAAKT,UAAAA,EACLU,MACCQ,IACEP,GAAGX,WAAWc,WAAWA,SAAAA,GACzBH,GAAGX,WAAWe,SAASA,OAAAA,GACvBJ,GAAGX,WAAWgB,YAAYA,UAAAA,CAAAA,CAAAA;AAGhC,UAAMS,MAAMlB,KAAK,CAAA,GAAIiB;AACrB,WAAOC,OAAO,OAAO,IAAIC,OAAOD,GAAAA,IAAO;EACzC;AACF;;;ACzFA,SAASE,kBAAuC;AAChD,SAASC,kBAAkB;AAMpB,IAAMC,aAAaD,WAA4D;EACpFE,WAAAA;AACE,WAAO;EACT;EACAC,SAASC,OAAwB;AAC/B,WAAOC,KAAKC,UAAUF,QAAQA,MAAMG,OAAM,IAAK,IAAA;EACjD;EACAC,WAAWJ,OAAa;AACtB,WAAOL,WAAWU,SAASL,QAASC,KAAKK,MAAMN,KAAAA,IAA4B,IAAA;EAC7E;AACF,CAAA;","names":["integer","sqliteTable","text","mediaTable","id","primaryKey","ownerType","notNull","ownerId","collection","name","fileName","mimeType","size","disk","path","order","customProperties","mode","conversions","createdAt","updatedAt","and","asc","eq","max","sql","createMediaTable","db","run","sql","DrizzleMediaStore","save","record","insert","mediaTable","values","onConflictDoUpdate","target","id","set","find","rows","select","from","where","eq","limit","listByOwner","ownerType","ownerId","collection","undefined","and","orderBy","asc","order","delete","nextOrder","max","top","Number","Attachment","customType","attachment","dataType","toDriver","value","JSON","stringify","toJSON","fromDriver","fromJSON","parse"]}
|
|
1
|
+
{"version":3,"sources":["../src/media.schema.ts","../src/drizzle-media-store.ts","../src/attachment.column.ts"],"sourcesContent":["import { index, integer, sqliteTable, text } from 'drizzle-orm/sqlite-core';\n\n/**\n * Drizzle (sqlite) media table. `order` maps to the `position` column. JSON columns\n * use text(mode:'json'). Postgres/MySQL variants follow the same shape via their\n * respective drizzle cores.\n */\nexport const mediaTable = sqliteTable(\n 'media',\n {\n id: text('id').primaryKey(),\n ownerType: text('owner_type').notNull(),\n ownerId: text('owner_id').notNull(),\n collection: text('collection').notNull(),\n name: text('name').notNull(),\n fileName: text('file_name').notNull(),\n mimeType: text('mime_type').notNull(),\n size: integer('size').notNull(),\n disk: text('disk').notNull(),\n path: text('path').notNull(),\n order: integer('position').notNull(),\n customProperties: text('custom_properties', { mode: 'json' }).notNull(),\n conversions: text('conversions', { mode: 'json' }).notNull(),\n createdAt: integer('created_at', { mode: 'timestamp_ms' }).notNull(),\n updatedAt: integer('updated_at', { mode: 'timestamp_ms' }).notNull(),\n },\n (table) => ({\n ownerIdx: index('idx_media_owner').on(table.ownerType, table.ownerId, table.collection),\n collectionIdx: index('idx_media_collection').on(table.collection),\n diskIdx: index('idx_media_disk').on(table.disk),\n createdAtIdx: index('idx_media_created_at').on(table.createdAt),\n collectionCreatedAtIdx: index('idx_media_collection_created_at').on(\n table.collection,\n table.createdAt,\n table.id,\n ),\n }),\n);\n","import type {\n MediaAggregateQuery,\n MediaAggregateResult,\n MediaCountFilter,\n MediaListFilter,\n MediaListPage,\n MediaListResult,\n MediaRecord,\n MediaStore,\n} from '@dudousxd/nestjs-media-core';\nimport { and, asc, count, eq, gt, max, or, sql, sum } from 'drizzle-orm';\nimport type { BetterSQLite3Database } from 'drizzle-orm/better-sqlite3';\nimport { mediaTable } from './media.schema';\n\ntype DB = BetterSQLite3Database<Record<string, never>>;\n\n/** Opaque keyset cursor over `(createdAt, id)`. Mirrors the in-memory store's encoding. */\nfunction encodeListCursor(record: MediaRecord): string {\n return Buffer.from(`${record.createdAt.toISOString()}|${record.id}`, 'utf8').toString('base64');\n}\n\ninterface DecodedListCursor {\n createdAt: Date;\n id: string;\n}\n\nfunction decodeListCursor(cursor: string): DecodedListCursor | null {\n const decoded = Buffer.from(cursor, 'base64').toString('utf8');\n const separator = decoded.indexOf('|');\n if (separator === -1) return null;\n const createdAt = new Date(decoded.slice(0, separator));\n if (Number.isNaN(createdAt.getTime())) return null;\n const id = decoded.slice(separator + 1);\n return { createdAt, id };\n}\n\n/**\n * Migration-first (§3.10): Drizzle has no auto-ensure. Run migrations with\n * drizzle-kit in production; this helper creates the table for tests/dev.\n */\nexport function createMediaTable(db: DB): void {\n db.run(sql`\n CREATE TABLE IF NOT EXISTS media (\n id text PRIMARY KEY NOT NULL,\n owner_type text NOT NULL,\n owner_id text NOT NULL,\n collection text NOT NULL,\n name text NOT NULL,\n file_name text NOT NULL,\n mime_type text NOT NULL,\n size integer NOT NULL,\n disk text NOT NULL,\n path text NOT NULL,\n position integer NOT NULL,\n custom_properties text NOT NULL,\n conversions text NOT NULL,\n created_at integer NOT NULL,\n updated_at integer NOT NULL\n )\n `);\n db.run(sql`CREATE INDEX IF NOT EXISTS idx_media_collection ON media (collection)`);\n db.run(sql`CREATE INDEX IF NOT EXISTS idx_media_disk ON media (disk)`);\n db.run(sql`CREATE INDEX IF NOT EXISTS idx_media_created_at ON media (created_at)`);\n db.run(sql`\n CREATE INDEX IF NOT EXISTS idx_media_collection_created_at\n ON media (collection, created_at, id)\n `);\n}\n\n/** MediaStore backed by Drizzle (better-sqlite3). POJO receiving the drizzle db. */\nexport class DrizzleMediaStore implements MediaStore {\n constructor(private readonly db: DB) {}\n\n async save(record: MediaRecord): Promise<MediaRecord> {\n await this.db\n .insert(mediaTable)\n .values(record)\n .onConflictDoUpdate({ target: mediaTable.id, set: record });\n return record;\n }\n\n async find(id: string): Promise<MediaRecord | null> {\n const rows = await this.db.select().from(mediaTable).where(eq(mediaTable.id, id)).limit(1);\n return (rows[0] as MediaRecord | undefined) ?? null;\n }\n\n async listByOwner(\n ownerType: string,\n ownerId: string,\n collection?: string,\n ): Promise<MediaRecord[]> {\n const where =\n collection === undefined\n ? and(eq(mediaTable.ownerType, ownerType), eq(mediaTable.ownerId, ownerId))\n : and(\n eq(mediaTable.ownerType, ownerType),\n eq(mediaTable.ownerId, ownerId),\n eq(mediaTable.collection, collection),\n );\n const rows = await this.db\n .select()\n .from(mediaTable)\n .where(where)\n .orderBy(asc(mediaTable.order));\n return rows as MediaRecord[];\n }\n\n async delete(id: string): Promise<void> {\n await this.db.delete(mediaTable).where(eq(mediaTable.id, id));\n }\n\n async nextOrder(ownerType: string, ownerId: string, collection: string): Promise<number> {\n const rows = await this.db\n .select({ max: max(mediaTable.order) })\n .from(mediaTable)\n .where(\n and(\n eq(mediaTable.ownerType, ownerType),\n eq(mediaTable.ownerId, ownerId),\n eq(mediaTable.collection, collection),\n ),\n );\n const top = rows[0]?.max;\n return top == null ? 0 : Number(top) + 1;\n }\n\n async count(filter: MediaCountFilter = {}): Promise<number> {\n const conditions = [\n ...(filter.ownerType !== undefined ? [eq(mediaTable.ownerType, filter.ownerType)] : []),\n ...(filter.collection !== undefined ? [eq(mediaTable.collection, filter.collection)] : []),\n ...(filter.disk !== undefined ? [eq(mediaTable.disk, filter.disk)] : []),\n ];\n const rows = await this.db\n .select({ value: count() })\n .from(mediaTable)\n .where(conditions.length ? and(...conditions) : undefined);\n return Number(rows[0]?.value ?? 0);\n }\n\n async aggregate(query: MediaAggregateQuery): Promise<MediaAggregateResult> {\n const column = query.groupBy === 'collection' ? mediaTable.collection : mediaTable.disk;\n const rows = await this.db\n .select({ key: column, count: count(), sumSize: sum(mediaTable.size) })\n .from(mediaTable)\n .groupBy(column);\n return rows.map((row) => ({\n key: row.key,\n count: Number(row.count),\n sumSize: query.sum === 'size' ? Number(row.sumSize ?? 0) : 0,\n }));\n }\n\n async list(filter: MediaListFilter = {}, page: MediaListPage = {}): Promise<MediaListResult> {\n const limit = page.limit ?? 50;\n const cursor = page.cursor ? decodeListCursor(page.cursor) : null;\n\n const filterConditions = [\n ...(filter.ownerType !== undefined ? [eq(mediaTable.ownerType, filter.ownerType)] : []),\n ...(filter.collection !== undefined ? [eq(mediaTable.collection, filter.collection)] : []),\n ...(filter.disk !== undefined ? [eq(mediaTable.disk, filter.disk)] : []),\n ...(cursor\n ? [\n or(\n gt(mediaTable.createdAt, cursor.createdAt),\n and(eq(mediaTable.createdAt, cursor.createdAt), gt(mediaTable.id, cursor.id)),\n ),\n ]\n : []),\n ];\n\n const rows = await this.db\n .select()\n .from(mediaTable)\n .where(filterConditions.length ? and(...filterConditions) : undefined)\n .orderBy(asc(mediaTable.createdAt), asc(mediaTable.id))\n .limit(limit + 1);\n\n const hasMore = rows.length > limit;\n const records = (rows.slice(0, limit) as MediaRecord[]).map((record) => ({ ...record }));\n const result: MediaListResult = { records };\n const lastRecord = records.at(-1);\n if (hasMore && lastRecord !== undefined) {\n result.cursor = encodeListCursor(lastRecord);\n }\n return result;\n }\n}\n","import { Attachment, type AttachmentData } from '@dudousxd/nestjs-media-core';\nimport { customType } from 'drizzle-orm/sqlite-core';\n\n/**\n * Drizzle (sqlite) custom column for an Attachment value:\n * `avatar: attachment('avatar')` → stored as JSON text, rehydrated to an `Attachment`.\n */\nexport const attachment = customType<{ data: Attachment | null; driverData: string }>({\n dataType() {\n return 'text';\n },\n toDriver(value: Attachment | null): string {\n return JSON.stringify(value ? value.toJSON() : null);\n },\n fromDriver(value: string): Attachment | null {\n return Attachment.fromJSON(value ? (JSON.parse(value) as AttachmentData) : null);\n },\n});\n"],"mappings":";;;;AAAA,SAASA,OAAOC,SAASC,aAAaC,YAAY;AAO3C,IAAMC,aAAaF,YACxB,SACA;EACEG,IAAIF,KAAK,IAAA,EAAMG,WAAU;EACzBC,WAA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AAOH,EAAE,CAAA,CAAA;UAG/E,CAAA;;AAGN,UAAMyB,OAAO,MAAM,KAAKZ,GACrBa,OAAM,EACN7B,KAAKsB,UAAAA,EACLQ,MAAMkC,iBAAiBb,SAASb,IAAAA,GAAO0B,gBAAAA,IAAoB3B,MAAAA,EAC3DE,QAAQC,IAAIlB,WAAWrB,SAAS,GAAGuC,IAAIlB,WAAWnB,EAAE,CAAA,EACpD6B,MAAMA,QAAQ,CAAA;AAEjB,UAAMmC,UAAUvC,KAAKuB,SAASnB;AAC9B,UAAMoC,UAAWxC,KAAKjB,MAAM,GAAGqB,KAAAA,EAAyB4B,IAAI,CAAC9D,YAAY;MAAE,GAAGA;IAAO,EAAA;AACrF,UAAMuE,SAA0B;MAAED;IAAQ;AAC1C,UAAME,aAAaF,QAAQG,GAAG,EAAC;AAC/B,QAAIJ,WAAWG,eAAejC,QAAW;AACvCgC,aAAO/D,SAAST,iBAAiByE,UAAAA;IACnC;AACA,WAAOD;EACT;AACF;;;AC1LA,SAASG,kBAAuC;AAChD,SAASC,kBAAkB;AAMpB,IAAMC,aAAaD,WAA4D;EACpFE,WAAAA;AACE,WAAO;EACT;EACAC,SAASC,OAAwB;AAC/B,WAAOC,KAAKC,UAAUF,QAAQA,MAAMG,OAAM,IAAK,IAAA;EACjD;EACAC,WAAWJ,OAAa;AACtB,WAAOL,WAAWU,SAASL,QAASC,KAAKK,MAAMN,KAAAA,IAA4B,IAAA;EAC7E;AACF,CAAA;","names":["index","integer","sqliteTable","text","mediaTable","id","primaryKey","ownerType","notNull","ownerId","collection","name","fileName","mimeType","size","disk","path","order","customProperties","mode","conversions","createdAt","updatedAt","table","ownerIdx","on","collectionIdx","diskIdx","createdAtIdx","collectionCreatedAtIdx","and","asc","count","eq","gt","max","or","sql","sum","encodeListCursor","record","Buffer","from","createdAt","toISOString","id","toString","decodeListCursor","cursor","decoded","separator","indexOf","Date","slice","Number","isNaN","getTime","createMediaTable","db","run","sql","DrizzleMediaStore","save","insert","mediaTable","values","onConflictDoUpdate","target","set","find","rows","select","where","eq","limit","listByOwner","ownerType","ownerId","collection","undefined","and","orderBy","asc","order","delete","nextOrder","max","top","count","filter","conditions","disk","value","length","aggregate","query","column","groupBy","key","sumSize","sum","size","map","row","list","page","filterConditions","or","gt","hasMore","records","result","lastRecord","at","Attachment","customType","attachment","dataType","toDriver","value","JSON","stringify","toJSON","fromDriver","fromJSON","parse"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dudousxd/nestjs-media-database-drizzle",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.7",
|
|
4
4
|
"description": "nestjs-media Drizzle media store (sqlite)",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -24,15 +24,15 @@
|
|
|
24
24
|
],
|
|
25
25
|
"peerDependencies": {
|
|
26
26
|
"drizzle-orm": "^0.38.0",
|
|
27
|
-
"@dudousxd/nestjs-media-core": "0.6.
|
|
27
|
+
"@dudousxd/nestjs-media-core": "0.6.7"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"better-sqlite3": "11.8.1",
|
|
31
31
|
"drizzle-orm": "0.38.4",
|
|
32
32
|
"tsup": "8.3.5",
|
|
33
33
|
"typescript": "5.9.3",
|
|
34
|
-
"@dudousxd/nestjs-media-core": "0.6.
|
|
35
|
-
"@dudousxd/nestjs-media-testing": "0.5.
|
|
34
|
+
"@dudousxd/nestjs-media-core": "0.6.7",
|
|
35
|
+
"@dudousxd/nestjs-media-testing": "0.5.7"
|
|
36
36
|
},
|
|
37
37
|
"scripts": {
|
|
38
38
|
"build": "tsup",
|