1.0.2 • Published 2 months ago

@zamoula/crud-relational-generator-package v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

crud-relational-generator-package

crud-relational-generator-package is a Node.js package that facilitates the generation of CRUD (Create, Read, Update, Delete) functionalities for any data model within your Node.js application simply in minutes. It simplifies the process of interacting with a database by providing reusable functions for common database operations. (mention persistance with any rdbms)

Installation

To install crud-relational-generator-package , simply run:

npm i @zamoula/crud-relational-generator-package

Usage

Initializing the Database

Before using CRUD.js, you need to initialize your database connection using the initDatabase function provided by the package. Here's an example of how to initialize the database:

const { initDatabase } = require('@zamoula/crud-relational-generator-package');

  

const  sequelize = initDatabase('database_name', 'username', 'password', 'host', 'port', 'dialect', forceSync);

Defining Models

This package uses Sequelize behind the scenes , Here is the table with Sequelize data types and their corresponding SQL column types for MySQL, MariaDB, SQLite, PostgreSQL, MSSQL, and Oracle.

Sequelize Data TypeMySQL Column TypeMariaDB TypeSQLite TypePostgreSQL TypeMSSQL TypeOracle Type
STRINGVARCHARVARCHARTEXTVARCHARNVARCHAR(MAX)VARCHAR2
TEXTTEXTTEXTTEXTTEXTNVARCHAR(MAX)CLOB
INTEGERINTEGERINTEGERINTEGERINTEGERINTNUMBER
FLOATFLOATFLOATREALDOUBLE PRECISIONFLOATFLOAT
DOUBLEDOUBLEDOUBLEREALDOUBLE PRECISIONFLOATFLOAT
DECIMALDECIMALDECIMALNUMERICNUMERICDECIMALNUMBER
DATEDATETIMEDATETIMETEXTTIMESTAMPDATETIME2DATE
BOOLEANBOOLEANBOOLEANINTEGERBOOLEANBITNUMBER(1,0)
ENUMENUMENUMTEXTENUMNVARCHAR(MAX)VARCHAR2
JSONJSONJSONTEXTJSONNVARCHAR(MAX)CLOB
JSONBJSONJSONTEXTJSONBNVARCHAR(MAX)CLOB
UUIDCHAR(36)CHAR(36)TEXTUUIDUNIQUEIDENTIFIERRAW(16)
ARRAYTEXTTEXTTEXTARRAYNVARCHAR(MAX)VARRAY
RANGETEXTTEXTTEXTRANGENVARCHAR(MAX)VARRAY
GEOMETRYGEOMETRYGEOMETRYTEXTGEOMETRYVARBINARY(MAX)SDO_GEOMETRY

Once the database connection is established, you can define your data models using the defineModel function. Here's how you can define a model:

(you must add Sequelize orm to your own project dependencies)

const { defineModel } = require('@zamoula/crud-relational-generator-package');

  

defineModel(sequelize, 'ModelName', {

// Define model attributes here

});

CRUD Operations

crud-relational-generator-package provides functions for performing CRUD operations on your models:

  • getAll: Retrieve all records of a model.

  • getById: Retrieve a single record by its ID.

  • create: Create a new record.

  • update: Update an existing record.

  • remove: Delete a record.

Here's how you can use these functions:

const { getAll, getById, create, update, remove } = require('@zamoula/crud-relational-generator-package');

  

// Example usage of getAll

app.get('/models/:modelName', getAll);

  

// Example usage of getById

app.get('/models/:modelName/:id', getById);

  

// Example usage of create

app.post('/models/:modelName', create);

  

// Example usage of update

app.put('/models/:modelName/:id', update);

  

// Example usage of remove

app.delete('/models/:modelName/:id', remove);

Error Handling

crud-relational-generator-package handles errors gracefully. If a model is not found or if a record does not exist, appropriate error responses are sent.

Contributing

Contributions are welcome! If you have any suggestions, bug reports, or feature requests, please open an issue or submit a pull request on the GitHub repository.

License

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