crud-node is an agnostic database client for Nodejs that allows you to perform CRUD operations to a database in a simple way.
Motivation
Out of the box, database clients such as pg, mysql, knex, and more do not come with an opinionated way of querying or mutating data from your database so developers end up building their own ways of performing CRUD operations.
This usually means that most of the time developer has to build himself the logic for querying a database and also make the implementation reusable across the entire system.
Crud Node is hands down one of the best packages for managing CRUD operations to a database. It works amazingly well out-of-the-box, with zero-config, and can be extended to your liking as your application grows.
Crud Node allows you to defeat and overcome the tricky challenges and hurdles of querying a database and controlling your app data before it starts to control you.
You can use a controller to organize better your application and to easily import/export anywhere you need.
// employeeController.{ts|js}import { MySQL, CRUDMySQL, IAppWithDatabase } from'crud-node';import { employeeSchema, EmployeeProps } from'./schemas/employee';exportclassEmployeeControllerextendsCRUDMySQL<EmployeeProps> {constructor(app:IAppWithDatabase<MySQL>) {super(app.db, employeeSchema); }}// This can be placed in a middleware that will leave all the controllers or can be called inside a route where you have access to app object.
exportconstemployeeController=newEmployeeController(app);
Create record
// employeeRouter.{ts|js}import { CRUDMySQL } from'crud-node';import { employeeSchema } from'./schemas/employee';// Executes operations in a single transactionconsttransacted=true;constemployeeController=newCRUDMySQL(db, employeeSchema);awaitdb.usingSession(async (session) => {constpayload= { email:'leslie46@24mailin.com', firstName:'Leslie', lastName:'Brett', };constdata=awaitemployeeController.createDocument(session, payload);}, transacted);
import { IDocumentSchema, IDocumentValidation, IDocument, getDocument, generateId } from'crud-node';exportenumEmployeeProps { _id ='_id', createdAt ='createdAt', email ='email', lastName ='lastName', firstName ='firstName', responsibilities ='responsibilities', officeId ='officeId', fired ='fired',}exportconstvalidation:IDocumentValidation<EmployeeProps> = { level:'strict', schema: { type:'object', description:'Employee', properties: { _id: { type:'string' }, createdAt: { type:'string', description:'Timestamp when the record was created' }, email: { type:'string', description:'The email of an employee, used as unique identifier for account registration', }, lastName: { type:'string', description:'Last name of an employee' }, firstName: { type:'string', description:'First name of an employee' }, responsibilities: { type:'array', items: { type:'string' }, uniqueItems:true, description:'The responsibilities of an employee', }, officeId: { type:'string', description:'The id of office, employee works at' }, fired: { type:'boolean', description:'' }, }, required: [EmployeeProps._id,EmployeeProps.email], },};exportconstemployeeSchema:IDocumentSchema<EmployeeProps> = { name:'employee', alias:'emp', validation, generatedId:false, unique: [[EmployeeProps.email]],getDocument: (data:Partial<IDocument<EmployeeProps>>):IDocument<EmployeeProps> => {constcreatedAt=Date.now().toString();constdefaults:Partial<IDocument<EmployeeProps>> = { _id:generateId(employeeSchema.alias), createdAt, };returngetDocument(EmployeeProps, data, defaults); },toString: (data:IDocument<EmployeeProps>) => {return`${data.firstName}${data.lastName}`; },};
Controller(!optional)
You can use a controller to organize better your application and to easily import/export anywhere you need.
// employeeController.{ts|js}import { MySQLX, CRUDMySQLX, IAppWithDatabase } from'crud-node';import { employeeSchema, EmployeeProps } from'./schemas/employee';exportclassEmployeeControllerextendsCRUDMySQLX<EmployeeProps> {constructor(app:IAppWithDatabase<MySQLX>) {super(app.db, employeeSchema); }}// This can be placed in a middleware that will leave all the controllers or can be called inside a route where you have access to app object.
exportconstemployeeController=newEmployeeController(app);
Create record
// employeeRouter.{ts|js}import { CRUDMySQLX } from'crud-node';import { employeeSchema } from'./schemas/employee';// Executes operations in a single transactionconsttransacted=true;constemployeeController=newCRUDMySQLX(db, employeeSchema);awaitdb.usingSession(async (session) => {constpayload= { email:'leslie46@24mailin.com', firstName:'Leslie', lastName:'Brett', };constdata=awaitemployeeController.createDocument(session, payload);}, transacted);
Congratulations! You successfully created a record in the database.