import { TClassDecoratorReturn, TPropertyDecoratorReturn, Type } from "@simplysm/sd-core-common"; import { DbDefUtils } from "./utils/DbDefUtils"; import { DbContext } from "./DbContext"; import { Queryable } from "./query/queryable/Queryable"; import { TSdOrmDataType } from "./types"; export function Table(def: { description: string; database?: string; schema?: string; name?: string; view?: (db: any) => Queryable; procedure?: string; }): TClassDecoratorReturn { return (classType: Type): void => { DbDefUtils.mergeTableDef(classType, { name: classType.name, ...def, }); }; } export function Column(columnDef: { description: string; name?: string; dataType?: TSdOrmDataType; nullable?: boolean; autoIncrement?: boolean; primaryKey?: number; }): TPropertyDecoratorReturn { return (object: T, propertyKey: string): void => { const classType = object.constructor as Type; DbDefUtils.addColumnDef(classType, { propertyKey, name: columnDef.name ?? propertyKey, dataType: columnDef.dataType, nullable: columnDef.nullable, autoIncrement: columnDef.autoIncrement, primaryKey: columnDef.primaryKey, description: columnDef.description, typeFwd: () => Reflect.getMetadata("design:type", object, propertyKey), }); }; } export function ForeignKey( columnNames: (keyof T)[], targetTypeFwd: () => Type, description: string, ): TPropertyDecoratorReturn> { return (object: Partial, propertyKey: string): void => { const classType = object.constructor as Type; DbDefUtils.addForeignKeyDef(classType, { propertyKey, name: propertyKey, columnPropertyKeys: columnNames as string[], description, targetTypeFwd, }); }; } export function ForeignKeyTarget( sourceTypeFwd: () => Type

, foreignKeyPropertyKey: keyof P, description: string, multiplicity?: "single", ): TPropertyDecoratorReturn { return (object: T, propertyKey: string): void => { const classType = object.constructor as Type; DbDefUtils.addForeignKeyTargetDef(classType, { propertyKey, name: propertyKey, sourceTypeFwd, description, sourceKeyPropertyKey: foreignKeyPropertyKey as string, isSingle: multiplicity === "single", }); }; } export function Index(def?: { name?: string; order?: number; orderBy?: "ASC" | "DESC"; unique?: boolean; }): TPropertyDecoratorReturn { return (object, propertyKey) => { const classType = object.constructor as Type; DbDefUtils.addIndexDef(classType, { name: def?.name ?? propertyKey, columns: [ { columnPropertyKey: propertyKey, order: def?.order ?? 1, orderBy: def?.orderBy ?? "ASC", unique: def?.unique ?? false, }, ], }); }; } export function ReferenceKey( columnNames: (keyof T)[], targetTypeFwd: () => Type, description: string, ): TPropertyDecoratorReturn> { return (object: Partial, propertyKey: string): void => { const classType = object.constructor as Type; DbDefUtils.addReferenceKeyDef(classType, { propertyKey, name: propertyKey, columnPropertyKeys: columnNames as string[], description, targetTypeFwd, }); }; } export function ReferenceKeyTarget( sourceTypeFwd: () => Type

, referenceKeyPropertyKey: keyof P, description: string, multiplicity?: "single", ): TPropertyDecoratorReturn { return (object: T, propertyKey: string): void => { const classType = object.constructor as Type; DbDefUtils.addReferenceKeyTargetDef(classType, { propertyKey, name: propertyKey, sourceTypeFwd, description, sourceKeyPropertyKey: referenceKeyPropertyKey as string, isSingle: multiplicity === "single", }); }; }