UNPKG

@goatlab/fluent

Version:

Readable query Interface & API generator for TS and Node

156 lines 5.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.f = exports.DecoratorsClass = void 0; const js_utils_1 = require("@goatlab/js-utils"); const graphql_1 = require("graphql"); const typeorm_1 = require("typeorm"); const applyDecorators_1 = require("./core/Nestjs/applyDecorators"); const field_decorator_1 = require("./core/Nestjs/types/decorators/field.decorator"); const types_1 = require("./core/types"); class DecoratorsClass { /** * * @param spec */ 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)(), // PrimaryGeneratedColumn('uuid') as PropertyDecorator, (0, types_1.ApiProperty)(), (0, field_decorator_1.Field)(() => graphql_1.GraphQLID)); } /** * * @param name */ entity(name) { // TODO implement pascal case for this const PascalCase = name; return (0, applyDecorators_1.applyDecorators)((0, typeorm_1.Entity)({ name: PascalCase }), (0, types_1.ObjectType)()); } /** * * @param params */ property(params) { if (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, type: params?.type, }), (0, types_1.ApiProperty)({ nullable: !params?.required, required: !!params?.required, }), (0, field_decorator_1.Field)()); } /** * * @param e */ embed(e) { return (0, applyDecorators_1.applyDecorators)((0, typeorm_1.Column)(_type => e), (0, types_1.ApiProperty)({ type: e })); } /** * * @param 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, })); } /** * * @param params */ 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, })); } /** * * @param e * @param params */ Enum(e, params) { return (0, applyDecorators_1.applyDecorators)( // Attribute(), (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, })); } /** * * @param e */ created(_e) { return (0, applyDecorators_1.applyDecorators)((0, typeorm_1.CreateDateColumn)(), (0, types_1.ApiProperty)()); } /** * * @param e */ updated(_e) { return (0, applyDecorators_1.applyDecorators)((0, typeorm_1.UpdateDateColumn)(), (0, types_1.ApiProperty)()); } /** * * @param e */ deleted(_e) { return (0, applyDecorators_1.applyDecorators)((0, typeorm_1.DeleteDateColumn)(), (0, types_1.ApiProperty)()); } /** * * @param e */ 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(); //# sourceMappingURL=decorators.js.map