@dudousxd/nestjs-media-database-typeorm
TypeORM media store with non-destructive auto-schema.
A MediaStore backed by TypeORM. A POJO that receives your DataSource — no @Injectable, no internal token. With @nestjs/typeorm, the /nest entry builds it on a TypeOrmModule connection:
import { typeOrmMediaStore } from '@dudousxd/nestjs-media-database-typeorm/nest';
MediaModule.forRoot({ store: typeOrmMediaStore() }); // default connection
MediaModule.forRoot({ store: typeOrmMediaStore({ connectionName: 'media' }) }); // TypeOrmModule.forRoot({ name: 'media' })
MediaModule.forRootAsync({ store: typeOrmMediaStore({ connectionName: 'media' }), useFactory: () => ({ /* … */ }) });typeOrmMediaStore() returns { inject: [getDataSourceToken(connectionName)], useFactory }, which MediaModule resolves; it takes the store options too (autoCreateSchema). @nestjs/typeorm is an optional peer, loaded only by the /nest entry. Without it, construct the store yourself:
import { TypeOrmMediaStore } from '@dudousxd/nestjs-media-database-typeorm';
MediaModule.forRoot({ store: new TypeOrmMediaStore(dataSource) });Auto-schema
new TypeOrmMediaStore(ds) creates the media table on first use, non-destructively: it creates the table if missing and adds any missing columns, but never drops, alters types, or renames. Opt out to manage it yourself:
new TypeOrmMediaStore(ds, { autoCreateSchema: false }); // you own migrations
await ensureMediaSchema(ds); // or call it explicitly at bootThe entity is declared via EntitySchema (not decorators), so the package carries no decorator-metadata build requirement, and order maps to a position column to avoid the reserved word.
The entity also declares a nullable tenant_id (and the idx_media_tenant index), used only for tenancy. Auto-schema adds it to an existing table only once tenancy is on (ensureMediaSchema(ds, { tenancy: true }) does the same by hand); while tenancy is off the store never reads or writes it.
Portable timestamps
Timestamps are stored as ISO strings (varchar) rather than a dialect-specific date type — so the same schema works on sqlite, mysql, and postgres alike. This is verified by an integration suite running the store against real Postgres (testcontainers).
Database schema
The store can keep the media table in a schema of its own ("media"."media" on Postgres), or on MySQL in a database of its own (`media`.`media`), while your app stays where it is. Register the matching entity, and pass the same schema to the store:
import { mediaEntity } from '@dudousxd/nestjs-media-database-typeorm';
import { typeOrmMediaStore } from '@dudousxd/nestjs-media-database-typeorm/nest';
TypeOrmModule.forRoot({ /* … */ entities: [mediaEntity({ schema: 'media' })] });
MediaModule.forRoot({ store: typeOrmMediaStore({ schema: 'media' }) });
// or: new TypeOrmMediaStore(dataSource, { schema: 'media' })Auto-schema (and ensureMediaSchema(ds, { schema: 'media' })) creates the schema, or the MySQL database, when it is missing, then the table and its indexes in it.
- Names: a letter or
_, then letters, digits,_or$; at most 63 characters. - Permissions: a role that may not create the schema (Postgres:
CREATEon the database; MySQL:CREATEon the database) fails with the grant it needs. Create it yourself, or turnautoCreateSchemaoff and migrate. - MySQL: the schema is a database on the same server. The tenant index uses 191-character column prefixes there, so it fits InnoDB's key limit in
utf8mb4. - SQLite has no schemas:
schemaon a SQLiteDataSourcethrows. - Without
schema, nothing changes:MediaEntityand the table 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.
Named connections
typeOrmMediaStore({ connectionName: 'media' }) above; or pass any DataSource to new TypeOrmMediaStore(ds).
Attachment column
Beyond the media table, this package also wires the attachment column model — a single file stored as a JSON value object directly on your entity. Use the @AttachmentColumn() decorator:
import { Entity, PrimaryColumn } from 'typeorm';
import type { Attachment } from '@dudousxd/nestjs-media-core';
import { AttachmentColumn } from '@dudousxd/nestjs-media-database-typeorm';
@Entity()
export class User {
@PrimaryColumn() id!: string;
@AttachmentColumn() avatar!: Attachment | null;
}It stores a portable simple-json column, nullable by default, and rehydrates the value into an Attachment on read. Pass any ColumnOptions to override (@AttachmentColumn({ name: 'avatar_data' })).
Using EntitySchema instead of decorators? Apply the underlying attachmentTransformer to a simple-json column directly:
import { attachmentTransformer } from '@dudousxd/nestjs-media-database-typeorm';
columns: {
avatar: { type: 'simple-json', nullable: true, transformer: attachmentTransformer },
}The round-trip is verified against real sqlite in the package's test suite.
Peer dependencies
typeorm (^0.3) and @dudousxd/nestjs-media-core; optionally @nestjs/typeorm (^11 or ^12) for /nest. See Concepts → Persistence for the shared store contract, and Concepts → Attachments for the column model.