Giter Club home page Giter Club logo

nap-db's Introduction

nap-logo

nap-db

Overview

nap-db provides a robust database interaction layer for PostgreSQL using JavaScript, leveraging the pg-promise library. It includes classes to manage database connections, build and execute SQL queries, and perform CRUD operations efficiently.

Features

  • CRUD Operations: Perform create, read, update, and delete operations on database tables.
  • Table Management: Create and manage tables with support for all PostgreSQL field types, constraints, references, and indexes.
  • Advanced Querying: Use the SelectQueryBuilder class to build dynamic queries, including all PostgreSQL aggregate functions.
  • Utility Methods: Supports additional operations like drop, truncate, and more.
  • Extendable: Easily extend base classes to create custom models and queries.

Installation

Install the library using npm:

npm i nap-db

Usage

Initialize the Database

require('dotenv').config();
const { DB, Model } = require('nap-db');

// Define a User model
class Users extends Model {
  constructor(db, pgp) {
    const schema = {
      tableName: 'users',
      dbSchema: 'public',
      timeStamps: 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();
  }
}

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,
};

const repositories = { users: Users };
const db = DB.init(connection, repositories);

// Server setup code...

Documentation

For detailed documentation, visit the nap-db documentation.

Classes and Methods

DB

The DB class initializes and manages the database connection.

Methods

  • DB.init(connection, repositories): Initializes the database with the provided connection parameters and repositories.

Model

The Model class extends SelectQueryBuilder and provides a base for defining database models.

Constructor

  • Model(db, pgp, schema): Initializes a new instance of the Model class with the given schema.

SelectQueryBuilder

The SelectQueryBuilder class is used to dynamically build SQL SELECT queries.

Methods

  • buildQuery(): Builds the query based on the specified table, aggregates, and values.
  • addJoins(query): Adds joins to the query.
  • addWhereClause(query): Adds a WHERE clause based on specified conditions.

Errors

Custom error classes for specific database-related errors.

Classes

  • DBError: Base class for custom database errors.
  • ConnectionParameterError: Thrown when the connection parameter is missing.
  • RepositoriesParameterError: Thrown when the repositories parameter is missing or invalid.

Change Log

v1.0.4 - Beta 1

  • Added missing default value handling for boolean types.
  • Replaced the doc-dash template with the clean-jsdoc-theme template.
  • Added SelectBuildQuery and QueryOptions classes to enhance query creation flexibility
  • v1.0.4 - Beta 1 is a breaking change to previous versions

License

This project is licensed under the MIT License. See the LICENSE file for details.

nap-db's People

Contributors

silverstone-i avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.