Controllers
📝 Generics Controllers
Generic controllers is a library that allows you to create controllers for your models in a simple way, it is compatible with Sequelize and Typegoose using express as a server. soon it will be compatible with other servers and ORMs/ODMs.
📦 Installation
npm install @gnx-utilities/service @gnx-utilities/models @gnx-utilities/controller sequelize
pnpm add @gnx-utilities/service @gnx-utilities/models @gnx-utilities/controller sequelize
yarn add @gnx-utilities/service @gnx-utilities/models @gnx-utilities/controller sequelize
bun add @gnx-utilities/service @gnx-utilities/models @gnx-utilities/controller sequelize
📖 Usage
import { SequelizeBaseEntity } from '@gnx-utilities/models'import { SequelizeService } from '@gnx-utilities/services'import { DataTypes, Sequelize } from 'sequelize'import { GenericControllerService } from '@gnx-utilities/controllers'import { Router } from 'express'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) }}
class UserController extends GenericControllerService<User, UserService> { constructor () { super(new UserService()) }}const controller = new UserController()const router: Router = Router()
router.get('/list', controller.getAll)router.get('/get/:id', controller.getById)router.get('/paginate', controller.getAllPaginated)router.get('/all', controller.getAllWithDeleted)router.get('/deleted', controller.getAllDeleted)router.post('/create', controller.create)router.post('/createMany', controller.bulkCreate)router.patch('/update/:id', controller.update)router.delete('/hide/:id', controller.softDelete)router.patch('/restore/:id', controller.restore)router.delete('/delete/:id', controller.hardDelete)router.delete('/deleteAll', controller.bulkDelete)
export { router, sequelize }
import { TypegooseBaseEntity } from '@gnx-utilities/models'import { TypegooseService } from '@gnx-utilities/services'import { getModelForClass, prop } from '@typegoose/typegoose'import { Router } from 'express'import { connect } from 'mongoose'import { GenericControllerService } from '@gnx-utilities/controllers'
const uri ='mongodb://localhost:27017/?readPreference=primary&ssl=false&directConnection=true'
const connection = async (): Promise<void> => { await connect(uri, { dbName: 'test' })}
export class User extends TypegooseBaseEntity { @prop({ type: String }) declare firstName: string
@prop({ type: String }) declare lastName: string
@prop({ type: String }) declare email: string}
export const UserModel = getModelForClass(User)
export class UserService extends TypegooseService<User> { constructor() { super(UserModel) }}
export class UserController extends GenericControllerService<User, UserService> { constructor() { super(new UserService()) }}
const controller = new UserController()
const router: Router = Router()
router.get('/list', controller.getAll)router.get('/get/:id', controller.getById)router.get('/paginate', controller.getAllPaginated)router.get('/all', controller.getAllWithDeleted)router.get('/deleted', controller.getAllDeleted)router.post('/create', controller.create)router.post('/createMany', controller.bulkCreate)router.patch('/update/:id', controller.update)router.delete('/hide/:id', controller.softDelete)router.patch('/restore/:id', controller.restore)router.delete('/delete/:id', controller.hardDelete)router.delete('/deleteAll', controller.bulkDelete)
export { router, connection }
import express, { json, urlencoded } from 'express'import type { Application } from 'express'import { sequelizeRouter, sequelize } from './sequelize'import { typegooseRouter, connection } from './typegoose'
const app: Application = express()
app.use(json())app.use(urlencoded({ extended: true }))
app.get('/', (req, res) => { res.send('Hello World')})app.use('/api/sequelize', sequelizeRouter)app.use('/api/typegoose', typegooseRouter)
async function handleConnection(): Promise<void> { try { await Promise.all([connection(), sequelize.sync({ alter: true })]) } catch (err) { console.error(`Unable to connect to the database: ${err}`) }}
await handleConnection()
const port = 4000app.listen(port, () => { console.log(`Example app listening at http://localhost:${port}`)})export { app, handleConnection }