Skip to content

Services

Services Api contain the Generics repository of our ODM/ORM based on our models API gnx-utilities/models it has many methods which will help you to implement your applications quickly

✏️ Methods

MethodParamsReturn
getAll-Promise<T[]>
getAllPaginated{ page, limit }Promise<Pagination<T>>
getAllDeleted-Promise<T[]>
getAllWithDeleted-Promise<T[]>
getById{ id }Promise<T>
create{ entity }Promise<T>
bulkCreate{ entities }Promise<T[]>
update{ entity, id }Promise<T>
hardDelete{ id }Promise<void>
softDelete{ id }Promise<boolean>
restore{ id }Promise<boolean>
bulkDelete-Promise<void>
getSchema{ exclude }Schema[]

📝 Declaration

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()

getAll

Get all documents from a collection

const users = await userService.getAll();
console.log(users); // [ { firstName: 'John', lastName: 'Doe' } ]

getAllPaginated

Get all documents from a collection with pagination

const users = await userService.getAllPaginated({ page: 1, limit: 10 });
console.log(users); // { docs: [ { firstName: 'John', lastName: 'Doe' } ], totalDocs: 1, limit: 10, totalPages: 1, page: 1, pagingCounter: 1, hasPrevPage: false, hasNextPage: false, prevPage: null, nextPage: null }

getAllDeleted

Get all deleted documents from a collection

const users = await userService.getAllDeleted();
console.log(users); // [ { firstName: 'John', lastName: 'Doe' } ]

getAllWithDeleted

Get all documents from a collection with deleted documents

const users = await userService.getAllWithDeleted();
console.log(users); // [ { firstName: 'John', lastName: 'Doe' } ]

getById

Get a document by id

const user = await userService.getById({ id: 1 });
console.log(user); // { firstName: 'John', lastName: 'Doe' }

create

Create a document

const user = await userService.create({
entity: { firstName: 'John', lastName: 'Doe' },
});
console.log(user.firstName); // John

bulkCreate

Create many documents

const entity = { firstName: 'John', lastName: 'Doe' }
const created = await userService.bulkCreate({
entities: Array(10).fill(entity),
});
console.log(created); // [ { firstName: 'John', lastName: 'Doe' } ]

update

Update a document

const user = await userService.update({
id: 1,
entity: { firstName: 'Jane', lastName: 'Doe' },
});
console.log(user.firstName); // Jane

hardDelete

Hard delete a document

await userService.hardDelete({ id: 1 });

on the database the document will be deleted

softDelete

Soft delete a document

await userService.softDelete({ id: 1 });

on the database the document will be updated with the isDeleted field

idfirstNamelastNamecreatedAtupdatedAtisDeleted
1JohnDoe2021-01-01 00:00:002021-01-01 00:00:001 or true

restore

Restore a document

await userService.restore({ id: 1 });

on the database the document will be updated with the isDeleted field

idfirstNamelastNamecreatedAtupdatedAtisDeleted
1JohnDoe2021-01-01 00:00:002021-01-01 00:00:000 or false

bulkDelete

Delete all documents from a collection

await userService.bulkDelete();
idfirstNamelastNamecreatedAtupdatedAtisDeleted

getSchema

Get the properties that are mandatory for the model

const schema = userService.getSchema();
console.log(schema); // [ { name: 'firstName', type: 'string' }, { name: 'lastName', type: 'string' } ]
const schema = userService.getSchema({ exclude: ['firstName'] });
console.log(schema); // [ { name: 'lastName', type: 'string' } ]