Oraios Queries
Oraios Queries (formerly node-db-models) is a light-weighted project aims to provide class-based table representation and flexible query experience to help developers to avoid plain string queries that are error-prune.
Visit Documentation
Oraios Queries supports postgres and mysql2 packages.
Features
The package is consistently getting enhanced and updated. Your contributions are always welcome. Here are the functionality that are developed/being developed:
- CRUD Ops: Insert, select, update & delete Data from Postgresql and MySQL with flexible nested WHERE conditions.
- ORM: Create class-based models for your tables with built-in features.
- Flexible Queries: Designed to perform flexible, nested WHERE statements, ordering and grouping.
- Model Settings: Specify certain fields to be selectable, allow HTML tags to be stored in database for certain fields, add default values on insert and update, and more.
- Pre-defined Query Executers: Extract data in various ways: list, select one column, first item, slicing, chunking, pagination and more.
- Light Weighted: This package is light and can be added on APIs, web workers, .. etc.
Get Started
Install package using npm:
$ npm install --save oraios-queries
Connect to your database using pg or mysql2 package, then attach your connection with oraios-queries:
For Postgres:
const Pg = ;const Connection Model = ; let pgModConn = host: '127.0.0.1' user: 'admin' database: 'sampledb' password: '*******' port: 5432; let conn = connection: pgModConn type: 'pg';
For MySQL:
const mysql = ;const Connection Model = ; const mysqlConn = mysql; let conn = connection: mysqlConn type: 'mysql';
That's it. From now on everything will be the same across different connections.n rows in database.
Visit Documentation
Code Examples
- Create a Model:
const Model Util = ; tableName = 'posts'; allowHtml = 'body'; selectable = 'title' 'body' 'author_id' 'created_at::date'; //optional, default value is 'id' primaryKey = 'uuid'; //the object created above connection = conn; //optional default value setup defaultValue = onInsert: created_at: Util updated_at: Util onUpdate: updated_at: Util
- Inserting new row to database:
let insertedId = await post;ifinsertedId //success
- Inserting multiple rows to database:
let insertedRows = await post;ifinsertedRows > 0 //success
- Updating certain rows in database:
let affectedRows = await post;ifaffectedRows !== 0 //update successful
- Deleting a row in database:
let rowDeleted = await post;ifrowDeleted !== 0 //delete successful
- Find a row by id in database:
let row = await post;
- Perform a query with joins:
let userJoinQuery = user;let userEmails = await userJoinQuery;
- Select query with conditions using AND & OR with grouping:
let post = ;let conditions = nestedConditions = cond: ; conditionsrelation = 'AND';conditionscond;conditionscond; //include a nested conditionnestedConditionsrelation = 'OR';nestedConditionscond;nestedConditionscond; //add nested condition into the list of conditionsconditionscond;let postQuery = post ; let postRes = await postQuery;
The previous statement will produce a query like this:
SELECT created_at::date, count(*) as posts FROM posts WHERE ( created_at::date > "2019-01-01" AND author_id, "=", 25 AND ( created_at::date > "2019-05-01" OR created_at::date < "2019-10-01" )) GROUP BY created_at::date ORDER BY created_at::date desc;
Copyright (c) 2019-2020 Ahmed Saad Zaghloul (ahmedthegicoder@gmail.com) MIT License