Info
Magical MySql query builder that takes as input a json object and makes it blossom into real data
First steps
- Install the 'wish granter' (Obviously)
npm install --save djin
const Djin = require('djin')
const djin = new Djin({
host: <database_server>
user: <database_user>
password: <database_password>
database: <database_name>
})
Initialize the djin
djin.initialize()
.then(() => {
})
.catch((error) => {
})
or you can try
(async () => {
await djin.initialize()
})()
ooooor
async function initializeDjin() {
await djin.initialize()
}
initializeDjin()
Let the games begin
In order to explain how this query builder works, a small database diagram will be used as an example. (eyes down)
So, we can easily say that a user can have multiple roles, a role can be used for
multiple users and a user can have assigned multiple messages.
Now, let's say that we only want to have the users. We'll use djin for the getting all the users in the following way... (Just hold on)
async function getUsers() {
const queryAsJson = {
users: {}
}
const users = await djin.select(queryAsJson)
}
Custom select
If we want to retreave only some specific fields from the database, we can use the following methods
const queryAsJson = {
users: '*'
}
const queryAsJson = {
users: ['name', 'email']
}
const queryAsJson = {
users: {
select: ['name', 'email']
}
}
const queryAsJson = {
users: {
select: 'email'
}
}
Select from multiple tables (JOINS)
When using Djin, you don't have to think about making the jois between the tables manually, for it will figure out on
its own, the path from a table to another (if it exists).
Let's have an example...
const queryAsJson = {
users: {
roles: ['name']
}
}
const queryAsJson = {
users: {
select: 'name',
roles: {},
messages: ['message']
}
}
Conditions
If you want to apply a 'WHERE' clause on one of the queries, you can do it like in the following example
const queryAsJson = {
users: {
where: 'id = 125'
}
}
const queryAsJson = {
users: {
roles: {},
where: 'users.id = 125'
}
}
Many others will come...