UNPKG

bun-sqlite-orm

Version:

A lightweight TypeScript ORM for Bun runtime with Bun SQLite, featuring Active Record pattern and decorator-based entities

37 lines (30 loc) 1.31 kB
import 'reflect-metadata'; import { getGlobalMetadataContainer } from '../container'; import type { ColumnMetadata } from '../types'; export function PrimaryColumn() { return (target: object, propertyKey: string) => { const metadataContainer = getGlobalMetadataContainer(); const type = Reflect.getMetadata('design:type', target, propertyKey); let sqliteType: 'text' | 'integer' | 'real' | 'blob' = 'text'; if (type === Number) { sqliteType = 'integer'; } else if (type === String) { sqliteType = 'text'; } const columnMetadata: ColumnMetadata = { propertyName: propertyKey, type: sqliteType, nullable: false, unique: true, isPrimary: true, isGenerated: false, }; const entityConstructor = target.constructor as new () => unknown; // Auto-register entity if not already registered (will be marked as non-explicit) if (!metadataContainer.hasEntity(entityConstructor)) { const tableName = entityConstructor.name.toLowerCase(); metadataContainer.addEntity(entityConstructor, tableName, false); } metadataContainer.addColumn(entityConstructor, propertyKey, columnMetadata); }; }