Services
📝 Generics Services
Generic services is a library that allows you to create services with a generic repository, this library is based on the Sequelizer library and also in Typegoose.
📦 Installation
npm install @gnx-utilities/models @gnx-utilities/services
pnpm add @gnx-utilities/models @gnx-utilities/services
yarn add @gnx-utilities/models @gnx-utilities/services
bun add @gnx-utilities/models @gnx-utilities/services
📖 Usage
import { SequelizeBaseEntity } from '@gnx-utilities/models'import { SequelizeService } from '@gnx-utilities/services'import { DataTypes, Sequelize } from 'sequelize'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' })
class UserService extends SequelizeService<User> { constructor () { super(User) }}
const userService = new UserService()
const user = await userService.create({ entity: { firstName: 'John', lastName: 'Doe' }})
console.log(user.firstName) // John
import { TypegooseBaseEntity } from '@gnx-utilities/models'import { TypegooseService } from '@gnx-utilities/services'import { getModelForClass, prop } from '@typegoose/typegoose'
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}
const UserModel = getModelForClass(User)
class UserService extends TypegooseService<User> { constructor () { super(UserModel) }}
const userService = new UserService()
const user = await userService.create({ entity: { firstName: 'John', lastName: 'Doe' }})
console.log(user.firstName) // John