Aviary
Packages

@dudousxd/nestjs-media-database-drizzle

Drizzle media store (sqlite, and Postgres via /pg), migration-first.

A MediaStore backed by Drizzle ORM. The store is a POJO that receives your drizzle db handle.

import { DrizzleMediaStore, createMediaTable, mediaTable } from '@dudousxd/nestjs-media-database-drizzle';

createMediaTable(db);   // dev/test helper — see below
MediaModule.forRoot({ store: new DrizzleMediaStore(db) });

Migration-first

Drizzle is migration-first by design — there's no runtime auto-schema. In production you manage the table with drizzle-kit using the exported mediaTable schema; createMediaTable(db) is a convenience for tests and quick dev setups that issues the CREATE TABLE IF NOT EXISTS.

The tables declare a nullable tenant_id and its index, used only for tenancy: your next drizzle-kit migration adds them. While tenancy is off, the stores never read or write the column, so an existing table without it keeps working.

Postgres (/pg)

Drizzle's schema is dialect-specific, so the package root ships the sqlite/libSQL schema and the Postgres flavour lives on its own subpath, @dudousxd/nestjs-media-database-drizzle/pg (built on drizzle-orm/pg-core). Its stores accept any drizzle Postgres database (PgDatabase — node-postgres, postgres-js, neon, pglite, …).

import { drizzle } from 'drizzle-orm/node-postgres';
import {
  DrizzlePgMediaStore,
  DrizzlePgUploadSessionStore,
  createMediaPgTables,
} from '@dudousxd/nestjs-media-database-drizzle/pg';

const db = drizzle(process.env.DATABASE_URL!);
await createMediaPgTables(db); // dev/test helper — use drizzle-kit migrations in production

MediaModule.forRoot({
  store: new DrizzlePgMediaStore(db),
  // resumable uploads: persist sessions + multipart ETags in Postgres
  uploadSessions: new DrizzlePgUploadSessionStore(db),
});
ExportWhat it is
mediaPgTablemedia table (jsonb properties/conversions, bigint size, timestamptz dates) — same logical shape as mediaTable
DrizzlePgMediaStoreMediaStore over mediaPgTable (count / aggregate / keyset-cursor list included)
mediaUploadSessionsPgTable, mediaUploadPartsPgTablemedia_upload_sessions + media_upload_parts (FK ON DELETE CASCADE, PK (session_id, part_number))
DrizzlePgUploadSessionStoreUploadSessionStore with every optional method: atomic addPart (INSERT … ON CONFLICT DO UPDATE), listParts, list({ disk, keyPrefix })
createMediaPgTables(db) / MEDIA_PG_DDLidempotent CREATE TABLE/INDEX IF NOT EXISTS for tests and dev
DrizzlePgDatabasethe accepted db type (PgDatabase<any, any, any>)

For drizzle-kit, re-export mediaPgTable, mediaUploadSessionsPgTable and mediaUploadPartsPgTable from your schema file and generate migrations as usual.

Database schema

The Postgres stores can keep their tables in a schema of their own ("media"."media"), while your app stays in public:

import {
  DrizzlePgMediaStore,
  DrizzlePgUploadSessionStore,
  createMediaPgTables,
  mediaPgTables,
} from '@dudousxd/nestjs-media-database-drizzle/pg';

await createMediaPgTables(db, { schema: 'media' }); // creates the schema too (dev/test)

MediaModule.forRoot({
  store: new DrizzlePgMediaStore(db, { schema: 'media' }),
  uploadSessions: new DrizzlePgUploadSessionStore(db, { schema: 'media' }),
});

With drizzlePgMediaStore / drizzlePgUploadSessions (below), pass schema next to connectionName.

For drizzle-kit, export the tables of mediaPgTables('media') from your schema file (media, uploadSessions, uploadParts) instead of the unqualified mediaPgTable set. mediaPgDdl('media') gives the matching SQL.

  • Names: a letter or _, then letters, digits, _ or $; at most 63 characters. 'public' is refused: leave schema unset to use it.
  • Permissions: createMediaPgTables runs CREATE SCHEMA IF NOT EXISTS. A role without CREATE on the database fails with the grant it needs; create the schema yourself in that case.
  • SQLite has no schemas: schema on DrizzleMediaStore, createMediaTable or drizzleMediaStore throws.
  • Without schema, nothing changes: the tables are unqualified, as before.

Don't use search_path

Don't point the connection at the schema with search_path (or a connection option that sets it). Behind PgBouncer in transaction mode, SET search_path leaks to other clients' connections. With schema, every statement the store issues names the schema itself.

On a @nestjs/drizzle connection (/nest)

With @nestjs/drizzle, let Nest inject the database instead of building it yourself:

import {
  drizzleMediaStore,        // sqlite/libSQL store
  drizzlePgMediaStore,      // Postgres store
  drizzlePgUploadSessions,  // Postgres upload sessions
} from '@dudousxd/nestjs-media-database-drizzle/nest';

MediaModule.forRoot({
  store: drizzlePgMediaStore({ connectionName: 'media' }),       // DrizzleModule.forRoot({ name: 'media', ... })
  uploadSessions: drizzlePgUploadSessions({ connectionName: 'media' }),
});

Each returns { inject: [getDrizzleToken(connectionName)], useFactory }; omit connectionName for the default connection. @nestjs/drizzle is an optional peer, loaded only by the /nest entry.

Attachment column

For the attachment column model — a single file as a JSON value object on your own table — use the attachment() custom column:

import { sqliteTable, text } from 'drizzle-orm/sqlite-core';
import { attachment } from '@dudousxd/nestjs-media-database-drizzle';

export const users = sqliteTable('users', {
  id: text('id').primaryKey(),
  avatar: attachment('avatar'), // Attachment | null, stored as JSON text
});

On write it JSON.stringifys the Attachment (or stores null); on read it rehydrates an Attachment. The column is sqlite/libSQL — for Postgres/MySQL define the equivalent JSON column in that dialect and (de)serialize with the value object's toJSON() / Attachment.fromJSON().

Peer dependencies

drizzle-orm (>=0.38 <1, tested on 0.45) and @dudousxd/nestjs-media-core, plus a driver (better-sqlite3, libsql, … — or pg / postgres for /pg); optionally @nestjs/drizzle for /nest. See Concepts → Persistence and Concepts → Attachments.

On this page