zumito-db
Version:
Multi-driver database abstraction layer with decorator-based models
23 lines (22 loc) • 1.06 kB
JavaScript
import 'reflect-metadata';
import { MetadataStore } from '../metadata/MetadataStore.js';
import { resolveFieldType } from '../metadata/ModelMetadata.js';
export function Field(options = {}) {
return function (target, propertyKey) {
const designType = Reflect.getMetadata('design:type', target, propertyKey);
const resolvedType = options.type || resolveFieldType(designType);
if (!options.type && !designType) {
throw new Error(`Could not determine type for field "${String(propertyKey)}" on "${target.constructor.name}". ` +
`Please specify 'type' explicitly in @Field({ type: 'string' }) or ensure emitDecoratorMetadata is enabled.`);
}
MetadataStore.addFieldMetadata(target.constructor, {
name: propertyKey,
type: resolvedType,
primary: options.primary || false,
unique: options.unique || false,
nullable: options.nullable ?? true,
default: options.default,
propertyKey: propertyKey,
});
};
}