UNPKG

@goatlab/fluent

Version:

Readable query Interface & API generator for TS and Node

105 lines (104 loc) 4.56 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.f = exports.DecoratorsClass = void 0; const typeorm_1 = require("typeorm"); const graphql_1 = require("graphql"); const types_1 = require("./core/types"); const applyDecorators_1 = require("./core/Nestjs/applyDecorators"); const field_decorator_1 = require("./core/Nestjs/types/decorators/field.decorator"); const js_utils_1 = require("@goatlab/js-utils"); class DecoratorsClass { id() { return (0, applyDecorators_1.applyDecorators)((0, typeorm_1.ObjectIdColumn)({ type: 'uuid' }), (0, typeorm_1.PrimaryGeneratedColumn)('uuid'), (0, types_1.ApiProperty)(), (0, field_decorator_1.Field)(() => graphql_1.GraphQLID)); } mongoId() { return (0, applyDecorators_1.applyDecorators)((0, typeorm_1.ObjectIdColumn)(), (0, types_1.ApiProperty)(), (0, field_decorator_1.Field)(() => graphql_1.GraphQLID)); } entity(name) { const PascalCase = name; return (0, applyDecorators_1.applyDecorators)((0, typeorm_1.Entity)({ name: PascalCase }), (0, types_1.ObjectType)()); } property(params) { if (params && params.hidden) { return (0, applyDecorators_1.applyDecorators)((0, types_1.HideField)(), (0, typeorm_1.Column)({ select: false, nullable: !params.required, type: params.type }), (0, types_1.ApiProperty)({ nullable: !params.required, required: !!params.required }), (0, types_1.ApiHideProperty)()); } return (0, applyDecorators_1.applyDecorators)((0, typeorm_1.Column)({ nullable: !params?.required }), (0, types_1.ApiProperty)({ nullable: !params?.required, required: !!params?.required }), (0, field_decorator_1.Field)()); } embed(e) { return (0, applyDecorators_1.applyDecorators)((0, typeorm_1.Column)(type => e), (0, types_1.ApiProperty)({ type: e })); } embedArray(e, params) { return (0, applyDecorators_1.applyDecorators)((0, typeorm_1.Column)(type => e), (0, types_1.ApiProperty)({ isArray: true, type: e, nullable: !params?.required, required: !!params?.required })); } stringArray(e, params) { return (0, applyDecorators_1.applyDecorators)((0, typeorm_1.Column)({ type: 'simple-array', nullable: !params?.required }), (0, types_1.ApiProperty)({ type: [e], nullable: !params?.required, required: !!params?.required })); } Enum(e, params) { return (0, applyDecorators_1.applyDecorators)((0, typeorm_1.Column)({ type: 'enum', enum: e.enum, default: e.default, nullable: !params?.required }), (0, types_1.ApiProperty)({ enum: e.enum, default: e.default, nullable: !params?.required, required: !!params?.required })); } created(e) { return (0, applyDecorators_1.applyDecorators)((0, typeorm_1.CreateDateColumn)(), (0, types_1.ApiProperty)()); } updated(e) { return (0, applyDecorators_1.applyDecorators)((0, typeorm_1.UpdateDateColumn)(), (0, types_1.ApiProperty)()); } deleted(e) { return (0, applyDecorators_1.applyDecorators)((0, typeorm_1.DeleteDateColumn)(), (0, types_1.ApiProperty)()); } version(e) { return (0, applyDecorators_1.applyDecorators)((0, typeorm_1.VersionColumn)(), (0, types_1.ApiProperty)()); } belongsToMany({ entity, joinTableName, foreignKey, inverseForeignKey }) { return (0, applyDecorators_1.applyDecorators)((0, typeorm_1.ManyToMany)(entity), (0, typeorm_1.JoinTable)({ name: joinTableName, joinColumns: [{ name: foreignKey }], inverseJoinColumns: [{ name: inverseForeignKey }] })); } belongsTo({ entity, inverse, pivotColumnName }) { return (0, applyDecorators_1.applyDecorators)((0, typeorm_1.ManyToOne)(entity, inverse), (0, typeorm_1.JoinColumn)({ name: pivotColumnName, referencedColumnName: 'id', foreignKeyConstraintName: `${pivotColumnName}_id_${js_utils_1.Ids.nanoId()}` })); } hasMany({ entity, inverse }) { return (0, applyDecorators_1.applyDecorators)((0, typeorm_1.OneToMany)(entity, inverse)); } } exports.DecoratorsClass = DecoratorsClass; exports.f = new DecoratorsClass();