Skip to content

Models

📝 Generics Models

Generic models is a library that allows you to create models to manage generics repository, this library is based on the Sequelizer library and also in Typegoose.

📦 Installation

Terminal window
npm install @gnx-utilities/models

📖 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