Model database tables based on a user provided data schema to map the model to the database classes
The promise library pg-promise offers simple and flexible methods to efficiently interact with Postgres database. As such we chose to use the pg-promise functionality into a base class that can be easily extended to provide CRUD operations and map the class to a database table.
The current library only supports parametirizedQuery operations.
Provide future support for
- [ ] parametized Statements
- [ ] cursor operations for negotiating through large datasets
npm i nap-db
This module offers 2 classes
class DB - initialize database
class Model - CRUD and table mapping
Define a User class:
require('dotenv').config();
const { DB, Model } = require('nap-db');
// Define a model to map the users table
class Users extends Model {
constructor(db, pgp) {
const schema = {
tableName: 'users',
dbSchema: 'public',
timeStamps: true, // Add time stamps to table - default is true
columns: {
email: { type: 'varchar(255)', primaryKey: true },
password: { type: 'varchar(255)', nullable: false },
employee_id: { type: 'int4', nullable: false },
full_name: { type: 'varchar(50)', nullable: false },
role: { type: 'varchar(25)', nullable: false, default: 'user' },
active: { type: 'bool', nullable: false, default: true },
},
};
super(db, pgp, schema);
this.createColumnSet();
}
}
// setup database
// repositories
// Get DB connetion info from your .env file
try {
const connection = {
host: process.env.DB_HOST,
port: process.env.DB_PORT,
database: process.env.DB_NAME,
user: process.env.DB_USER,
password: process.env.DB_PASS,
};
// repositories
const repositories = { users: Users };
const db = DB.init(connection, repositories);
//Setup server code...
} catch(error) {
console.log("Internal server error". error);
}
This project is licensed under the MIT License - see the LICENSE file for details.
copyright © 2023 to present Ian Silverstone ian@isilverstone.com