Red Entities
(Logo attribution)
What it is?
Red Entities is a simple but flexible and fast object-mapper & sql query builder.
Red Entities is focused on minimal typing when accesing data and the definition of database models using schemas (simple json objects).
A model is defined in a json object, called schema.
Data access (inserts, selects, removes and updates), are done by selectors for a fast an simple writing of the sentences.
Database engines supported
Tested with:
- Mysql 5.x
- Mysql 8.x
- Amazon Aurora
- AWS RDS databases based on Mysql
- Sqlite3
- (future integration of Posgresql, MariaDB and Sql Server)
Thus, has been fully tested with:
- Node.js 10.x
- Node.js 12.x
- Node.js 13.x
- Node.js 14.x
Install
$ npm i redentities --save
Test
Change database configuration providers in file located at:
/test/providersconfig.json
Testing will create many databases and tables.
Then, just run (mocha required):
$ npm test
Basic sample
Consider this self-explained schema:
const sampleSchema = {
entities: [
{
name : "users",
fields: [
{ name : "mail", type : "string" },
{ name : "password", type : "string" },
{ name : "created", type : "datetime"}
],
indexes: [ ["mail"], ["created"] ]
}
]
}
Load this schema in an Red Entities object with:
const RedEntities = require("redentities")({
provider: "mysql",
host: "localhost",
user: "myuser",
password: "mypassword"
});
Create once schema in database with:
await RedEntities.Entities(sampleSchema).CreateSchema();
From now on, you can use Red Entities powers with fast sentences like this:
const db = await RedEntities.Entities(sampleSchema);
const newUserId = await db.users.I().V( {
mail: "re@redentities.com",
password: "12345" ).R();
Retrieve an entity with simple sentences like:
let userEntity = await db.users.S().SingleById(userId);
Documentation
Demos
You can find some demos at /demos folders.
To run them, just
node /demos/<demo name.js>
01-inserts-sample : Insert and retrieve basic values
02-insert-and-iterate : Insert a bulk of values and iterates over them
03-insert-and-deleteall : Insert a bulk of values and removed them
04-json-type : Insert a bulk of json objects and retrieve them
Credits
RedEntities
has been fully written by Rafael Gómez Blanes
Professional site at Rafablanes.com
Have a look to my books at Rafa G. Blanes books
About
This project is part of the libraries developed for projects like Hub de Libros and others.
License
Licensed under MIT
Copyright (c) 2019-2020 Rafael Gómez Blanes