{"id":15414596,"url":"https://github.com/ionutpasca/djin","last_synced_at":"2025-04-19T12:40:15.065Z","repository":{"id":57213329,"uuid":"94011873","full_name":"ionutpasca/djin","owner":"ionutpasca","description":"JSON based MySql query builder","archived":false,"fork":false,"pushed_at":"2017-09-28T10:10:15.000Z","size":77,"stargazers_count":5,"open_issues_count":5,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-23T22:40:38.000Z","etag":null,"topics":["database","mysql","query-builder"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ionutpasca.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-06-11T15:00:20.000Z","updated_at":"2023-04-19T12:06:38.000Z","dependencies_parsed_at":"2022-08-24T21:01:31.734Z","dependency_job_id":null,"html_url":"https://github.com/ionutpasca/djin","commit_stats":null,"previous_names":["ovidiuionut94/djin"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ionutpasca%2Fdjin","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ionutpasca%2Fdjin/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ionutpasca%2Fdjin/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ionutpasca%2Fdjin/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ionutpasca","download_url":"https://codeload.github.com/ionutpasca/djin/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241435175,"owners_count":19962400,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["database","mysql","query-builder"],"created_at":"2024-10-01T17:04:21.667Z","updated_at":"2025-03-01T22:32:26.257Z","avatar_url":"https://github.com/ionutpasca.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Info\n\n[![HitCount](https://hitt.herokuapp.com/ovidiuionut94/djin.svg)](https://github.com/ovidiuionut94/djin)\n[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/ovidiuionut94/djin/master/LICENSE) \n[![npm](https://img.shields.io/npm/dt/djin.svg)](https://www.npmjs.com/package/djin)\n[![NSP Status](https://nodesecurity.io/orgs/djin/projects/a1c8849c-bf3d-4cdc-9e6c-262e388ca165/badge)](https://nodesecurity.io/orgs/djin/projects/a1c8849c-bf3d-4cdc-9e6c-262e388ca165)\n[![Code Climate](https://codeclimate.com/github/ovidiuionut94/djin/badges/gpa.svg)](https://codeclimate.com/github/ovidiuionut94/djin)\n\nMagical MySql query builder that takes as input a json object and makes it blossom into real data\n\n# First steps\n- Install the 'wish granter' (Obviously)\n\n```\nnpm install --save djin\n```\n        \n- Easy peasy lemon squeezy\n\n```js\nconst Djin = require('djin')\n\nconst djin = new Djin({\n  host: \u003cdatabase_server\u003e\n  user: \u003cdatabase_user\u003e\n  password: \u003cdatabase_password\u003e\n  database: \u003cdatabase_name\u003e\n})\n\n```\n\n### Initialize the djin\n   \n```js\n// This operation must be done only once.\n// Shortly, under the hood, the djin will take the database structure, together with all the foreign keys \n// from the given database (so, be sure that all the related tables have the required foreign keys created) \n// and cache them in a JSON file, on the running server, so the queries can be created asap.\n\ndjin.initialize()\n  .then(() =\u003e {\n    // We have the djin initialized\n  })\n  .catch((error) =\u003e {\n    // Do something with this ^\n  })\n```\n\nor you can try\n\n```js\n(async () =\u003e {\n  await djin.initialize()\n})()\n```\n\nooooor\n\n```js\nasync function initializeDjin() {\n  await djin.initialize()\n}\ninitializeDjin()\n```\n\n### Let the games begin\nIn order to explain how this query builder works, a small database diagram will be used as an example. \u003cb\u003e (eyes down) \u003c/b\u003e\n![databasediagram](https://user-images.githubusercontent.com/11486739/27407362-ae69782c-56e0-11e7-92d7-e3690f09035d.jpg)\n\nSo, we can easily say that a user can have multiple roles, a role can be used for \nmultiple users and a user can have assigned multiple messages.\n\nNow, 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)\n```js\nasync function getUsers() {\n  const queryAsJson = {\n    users: {}\n  }\n  const users = await djin.select(queryAsJson)\n  // Aaaaaaand we have it\n}\n```\n### Custom select\nIf we want to retreave only some specific fields from the database, we can use the following methods\n```js\nconst queryAsJson = {\n  users: '*'\n}\n//This json forces the djin to select all the fields from each user (just like an empty object, as above)\n```\n\n```js\nconst queryAsJson = {\n  users: ['name', 'email']\n}\n```\n\n```js\nconst queryAsJson = {\n  users: {\n    select: ['name', 'email']\n  }\n}\n```\n\n```js\nconst queryAsJson = {\n  users: {\n    select: 'email'\n  }\n}\n```\n\n### Select from multiple tables \u003cb\u003e(JOINS)\u003c/b\u003e\nWhen using Djin, you don't have to think about making the jois between the tables manually, for it will figure out on\nits own, the path from a table to another (if it exists).\nLet's have an example...\n\n```js\nconst queryAsJson = {\n  users: {\n    roles: ['name']\n  }\n}\n// The result of this query will contain every user from the database, together with its role\n// The Djin will figure out that from 'users' table to the 'roles' table exists an intermediate, \n// and it will first join the intermediate one in the query, before including the 'roles' table \n// (Hoping this is clearer than dirt)\n```\n\n```js\nconst queryAsJson = {\n  users: {\n    select: 'name',\n    roles: {},\n    messages: ['message']\n  }\n}\n// Guess what this query does...\n// It will retreave all the users (only the name), together with its roles \n// (all the fields from the roles table) and messages (only the message field)\n```\n\n### Conditions\nIf you want to apply a 'WHERE' clause on one of the queries, you can do it like in the following example\n```js\nconst queryAsJson = {\n  users: {\n    where: 'id = 125'\n  }\n}\n// This query will provide all the information about the user with the id = 125\n```\n\n```js\nconst queryAsJson = {\n  users: {\n    roles: {},\n    where: 'users.id = 125'\n  }\n}\n// In the current version, if you want to retreave data from multiple tables, the query must\n// be more specific... for now\n```\n\n### Inserts\nJust like when you're making a select, you can easily insert data into your database, using a json object\n\n```js\nconst roleToInsert = {\n    roles: {\n        name: 'New Role Name'\n    }\n}\nconst insertedRole = await djin.insert(roleToInsert)\n```\n\nMultiple inserts at once \u003e\u003e\u003e\n```js\nconst dataToInsert = {\n    roles: {\n        name: 'New Role Name'\n    },\n    messages: {\n        message: 'New message',\n        user_id: 1\n    }\n}\nconst insertResult = await djin.insert(dataToInsert)\n```\n\n### Many others will come...\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fionutpasca%2Fdjin","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fionutpasca%2Fdjin","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fionutpasca%2Fdjin/lists"}