Skip to content

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

Terminal window
npm install @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