Decorators
📝 Generics Decorators
Generic decorators is a extended library from @gnx-utilities/core
that allows you to create services with a generic repository, this library is based on the Sequelizer library and also in Typegoose but based on typescript decorators.
📦 Installation
npm install @gnx-utilities/models @gnx-utilities/decorators
pnpm add @gnx-utilities/models @gnx-utilities/decorators
yarn add @gnx-utilities/models @gnx-utilities/decorators
bun add @gnx-utilities/models @gnx-utilities/decorators
📖 Usage
import { SequelizeBaseEntity } from '@gnx-utilities/models'import { DataTypes, Sequelize } from 'sequelize'import { sequelizeRepository, getRepository } from '@gnx-utilities/decorators'import type { UUID } from 'node:crypto'
const sequelize = new Sequelize({ dialect: 'sqlite', storage: './db/test.sqlite'})
class User extends SequelizeBaseEntity { declare id: UUID declare firstName: string declare lastName: string declare email: string}
User.init( { id: { type: DataTypes.UUID, primaryKey: true, defaultValue: DataTypes.UUIDV4 }, firstName: { type: DataTypes.STRING }, lastName: { type: DataTypes.STRING }, email: { type: DataTypes.STRING }, isDeleted: { type: DataTypes.BOOLEAN, defaultValue: false } }, { sequelize, modelName: 'person' })
@sequelizeRepository({ model: SequelizeUser })class UserService { greeting(): string { return 'Hello, world!' }}
const userService = getRepository<User>({ repository: UserService })
const user = await userService.create({ entity: { firstName: 'John', lastName: 'Doe' }})
console.log(user.firstName) // John
import { TypegooseBaseEntity } from '@gnx-utilities/models'import { getModelForClass, prop } from '@typegoose/typegoose'import { typegooseRepository, getRepository } from '@gnx-utilities/decorators'
const uri = 'mongodb://localhost:27017/?readPreference=primary&ssl=false&directConnection=true'
const connection = async (): Promise<void> => { await connect(uri, { dbName: 'test' })}
class User extends TypegooseBaseEntity { @prop({ type: String }) declare firstName: string
@prop({ type: String }) declare lastName: string
@prop({ type: String }) declare email: string}
@typegooseRepository({ model: UserModel })class UserService { greeting(): string { return 'Hello, world!' }}
const userService = getRepository<User>({ repository: UserService })
const user = await userService.create({ entity: { firstName: 'John', lastName: 'Doe' }})
console.log(user.firstName) // John