npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@gnx-utilities/controllers

v0.0.2

Published

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.

Downloads

1

Readme

📝 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

[!Note] You need to have one of the ORM or ODM to manage the data before. The supported ORMs/ODMs are Sequelize and Typegoose wich needs moongose, so you need to install it too if you want the whole experience of the library install it.

npm install @gnx-utilities/service @gnx-utilities/models @gnx-utilities/controller
pnpm add @gnx-utilities/service @gnx-utilities/models @gnx-utilities/controller
yarn add @gnx-utilities/service @gnx-utilities/models @gnx-utilities/controller
bun add @gnx-utilities/service @gnx-utilities/models @gnx-utilities/controller

📖 Usage

🔷 Sequelize

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'
})

export 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 }

🍃 Typegoose

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 }

Your server

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 = 4000
app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
export { app, handleConnection }

🛠️ Tools

Typescript Sequelize Typegoose NodeJS MongoDB

📝 Documentation

Documentation

Authors

ImRLopezAG

🔗 Links

portfolio linkedin twitter