{"id":24367176,"url":"https://github.com/vajahath/sqlify","last_synced_at":"2025-04-10T13:53:56.421Z","repository":{"id":19193045,"uuid":"86440116","full_name":"vajahath/sqlify","owner":"vajahath","description":"Yet another sql query builder.","archived":false,"fork":false,"pushed_at":"2022-12-30T19:32:23.000Z","size":874,"stargazers_count":14,"open_issues_count":10,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-24T12:39:27.678Z","etag":null,"topics":["mysql","node-module","nodejs","npm","npm-package","postgres","postgresql","query","query-builder","sql","sql-query","sql-query-builder"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/sqlify","language":"TypeScript","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/vajahath.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-03-28T09:20:51.000Z","updated_at":"2024-05-10T11:54:56.000Z","dependencies_parsed_at":"2023-01-13T20:13:34.025Z","dependency_job_id":null,"html_url":"https://github.com/vajahath/sqlify","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vajahath%2Fsqlify","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vajahath%2Fsqlify/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vajahath%2Fsqlify/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vajahath%2Fsqlify/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vajahath","download_url":"https://codeload.github.com/vajahath/sqlify/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247801155,"owners_count":20998338,"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":["mysql","node-module","nodejs","npm","npm-package","postgres","postgresql","query","query-builder","sql","sql-query","sql-query-builder"],"created_at":"2025-01-19T01:42:09.356Z","updated_at":"2025-04-10T13:53:56.402Z","avatar_url":"https://github.com/vajahath.png","language":"TypeScript","readme":"# Sqlify\n\nYet another SQL query builder.\n\n[![npm](https://img.shields.io/npm/v/sqlify.svg)](https://www.npmjs.com/package/sqlify)\n![Build Status](https://github.com/vajahath/sqlify/workflows/Build/badge.svg) \n[![T](https://img.shields.io/badge/TypeScript%20Ready-.d.ts%20included-blue.svg)]()\n[![npm](https://img.shields.io/npm/dt/sqlify.svg)](https://www.npmjs.com/package/sqlify)\n[![Greenkeeper badge](https://badges.greenkeeper.io/vajahath/sqlify.svg)](https://greenkeeper.io/)\n\n\u003e There are many sql query builders out there. But this one makes more sense to me :wink:.\n\n![](media/sqlify.png)\n\n## Install\n\n```bash\nnpm install --save sqlify\n```\n\n## Why\n\n* This package is a wrapper around [squel](https://hiddentao.com/squel) module to make it more friendly. (Check that package to know its maintenance status)\n* Helps you to build dynamic sql queries.\n* **Example use case:** suppose, you are getting a POST request to insert some data to your SQL database.\n  You'll get the data in `req.body` as `{name: \"Swat\", age: 22, address: \"ND\"}`.\n  Now make the query like:\n\n    ```js\n    const resource = {\n      set: req.body\n      where: {\n        id: 5\n      }\n    }\n\n    sqlify(chain, resource); // done!\n    ```\n\n\u003e Warning ⚠️: Do not ever pass queries generated on the client side to your web server for execution. The above example is only a use case. Do NOT copy paste as such.\n\n## Examples\n\n#### SELECT\n\n```js\nconst { squel, sqlify } = require('sqlify');\n\nconst resource = {\n  field: ['name', 'age', 'address'],\n  where: {\n    name: 'Swat',\n    age: 22,\n  },\n};\n\nconst chain = squel.select().from('users');\n\nsqlify(chain, resource);\n\nchain.toString();\n// =\u003e SELECT name, age, address FROM users WHERE (name=Swat) AND (age=22)\n```\n\n##### Starter Guide For TypeScript\n\n```ts\nimport { squel, sqlify, Resource } from 'sqlify'\n\n// `Resource` is type.\nconst resource :Resource = {\n  field: ['name', 'age', 'address'],\n  where: {\n    name: 'Swat',\n    age: 22,\n  },\n};\n\n// ...\n```\n\n#### SELECT with a simple JOIN\n\n```js\n// ...\n\nconst resource = {\n  field: ['user.*', 'hobbies.hobby', 'colors.favorite'],\n  where: {\n    name: 'Swat',\n    age: 22,\n  },\n  join: [\n    ['hobbies', null, 'hobbies.id = user.id'],\n    ['colors', null, 'colors.user_id = user.id'],\n  ];\n}\nconst chain = squel.select().from('Hero');\n\nsqlify(chain, resource);\n\nchain.toString();\n\n/*\nSELECT \n  user.*,\n  hobbies.hobby,\n  colors.favorite \nFROM Hero \n  INNER JOIN hobbies \n    ON (hobbies.id = user.id) \n  INNER JOIN colors \n    ON (colors.user_id = user.id) \nWHERE (name='Swat') AND (age=22)\n*/\n```\n\nRead the JOIN section of [squel docs](https://hiddentao.com/squel/#select) for more.\n\n#### INSERT\n\n```js\nconst { squel, sqlify } = require('sqlify');\n\nconst resource = {\n  set: {\n    name: 'Swat',\n    age: 22,\n  },\n};\n\nconst chain = sql.insert().into('users');\nsqlify(chain, resource);\n\nchain.toString();\n// =\u003e INSERT INTO users (name, age) VALUES ('Swat', 22)\n```\n\n## How?\n\n`sqlify` exposes a **function**, **module** ([squel](https://www.npmjs.com/package/squel)) and a `Resource` type (for using with TypeScript).\n\nThe function receives 2 arguments. They are:\n\n* `chain`\n* `resource`\n\n#### Step 1: Require the package\n\n```js\nconst { squel, sqlify } = require('sqlify');\n```\n\n#### Step 2: Initialize `chain` and `resource`\n\n`chain` is an instance of [squel](https://www.npmjs.com/package/squel).\nFor example,\n\n```js\n// ...\n\nconst chain = squel.select().from('users');\n\n// ...\n```\n\n`resource` is an object which contains the data to build the query.\n\nExample:\n\n```js\n// ...\n\nconst resource = {\n    field: ['name', 'age', 'address'],\n    where: {\n        name: 'Swa',\n        age: 22\n    }\n};\n\n// ...\n```\n\nWhere, the properties of `resource` object (in the above case, `field` and `where`) are taken from the chain function names of the [squel](https://www.npmjs.com/package/squel). There are more. Refer their docs and use them accordingly.\n\n\u003e When used with TypeScript, you should mark type of `resource` with the `import`ed `Resource` class.\n\u003e Like `const resource:Resource = {...}`.\n\n#### Step 3: Sqlify\n\n```js\n// ...\n\nsqlify(chain, resource);\n\n// ...\n```\n\n`sqlify` function wont return anything. It simply do things in in-place.\n\n#### Step 4: Watch stuff\n\n```js\n// ...\n\n// parse query\nconst query = chain.toString();\n// see it\nconsole.log(query);\n// =\u003e SELECT name, age, address FROM users WHERE (name='Swa') AND (age=22)\n\n// ...\n```\n\n_Unclear about something here? Feel free to rise an issue.._\n\n## Also,\n\nSince `sqlify` takes in and out chain functions, you can modify it **even after** `sqlify`ing it.\n\nExample:\n\n```js\n// ...\n\nconst chain = squel.select().from('users');\n\nsqlify(chain, resource);\n\nchain.limit(10);\n\nchain.toString(); // Voila!\n```\n\n### Supported Squel Functions\n\nThe following fields can be used inside the `resource` object. Logic behind the usage of these functions can be found at [squel docs](https://hiddentao.com/squel).\n\n|            |            |      |           |            |\n| ---------- | ---------- | ---- | --------- | ---------- |\n| `cross_join` | `field`      | `join` | `left_join` | `outer_join` |\n| `returning`  | `right_join` | `set`  | `where`     | `group`      |\n| `order`      |            |      |           |            |\n|            |            |      |           |            |\n\n## Contributors\n\n* [Lakshmipriya](https://github.com/lakshmipriyamukundan)\n\n## v1 to v2 migration guide\n\n* **change the way you `require` the package:**\n  * in v1, you required `sqlify` along with `squel` as:\n  ```js\n  const sqlify = require('sqlify');\n  const squel = require('squel');\n  // ...\n  ```\n  * in v2 you've to change that code into:\n  ```js\n  const { sqlify, squel } = require('sqlify');\n  // ...\n  ```\n\n* **change in function name:** change `fields:[]` to `field:[]` in the `resource` object.\n  \u003cbr\u003e\u003cbr\u003e*Oh yes! it's that simple.*\n  \u003cbr\u003e\u003cbr\u003e\n\n## Change log\n\n* v2.5.0, v2.5.1, v2.5.2\n  * Security Update\n* v2.4.0\n  * TypeScript support and definitions\n  * Better docs\n* v2.3.1\n  * enabling Greeenkeeper, better docs\n* v2.3.0\n  * adds better error handling: (if an unsupported method is used, sqlify throws an err)\n* v2.2.0\n  * adds `order` function from [squel-order](https://hiddentao.com/squel/api.html#select_order)\n  * better docs\n* v2.1.1\n  * adds `group` function from [squel-group](https://hiddentao.com/squel/api.html#select_group)\n  * better docs\n* v2.0.0\n  * fixing [#5](https://github.com/vajahath/sqlify/issues/5) and [#2](https://github.com/vajahath/sqlify/issues/2).\n  * more squel functions\n* v1.0.4\n  * bug fix with 's in select queries\n* v1.0.1, 1.0.2, 1.0.3\n  * bug fix (in `package.json`)\n  * better docs\n* v1.0.0\n  * initial release\n\n## Licence\n\nMIT © [Vajahath Ahmed](https://twitter.com/vajahath7)\n\n[badge_paypal_donate]: https://cdn.rawgit.com/vajahath/cloud-codes/a01f087f/badges/paypal_donate.svg\n[paypal-donations]: https://paypal.me/vajahath\n","funding_links":["https://paypal.me/vajahath"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvajahath%2Fsqlify","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvajahath%2Fsqlify","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvajahath%2Fsqlify/lists"}