{"id":27958314,"url":"https://github.com/91ahmed/nodeqr","last_synced_at":"2025-05-07T18:21:59.945Z","repository":{"id":152139543,"uuid":"625018033","full_name":"91ahmed/nodeQR","owner":"91ahmed","description":"elegant node.js query builder offering a unified interface for both MySQL and PostgreSQL drivers.","archived":false,"fork":false,"pushed_at":"2025-02-22T10:39:29.000Z","size":491,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-22T11:26:51.019Z","etag":null,"topics":[],"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/91ahmed.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-04-07T21:05:30.000Z","updated_at":"2025-02-22T10:40:05.000Z","dependencies_parsed_at":"2025-01-28T21:35:56.717Z","dependency_job_id":"edc29836-460b-4f15-9cea-99c51434f048","html_url":"https://github.com/91ahmed/nodeQR","commit_stats":null,"previous_names":["91ahmed/nodeqr"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/91ahmed%2FnodeQR","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/91ahmed%2FnodeQR/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/91ahmed%2FnodeQR/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/91ahmed%2FnodeQR/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/91ahmed","download_url":"https://codeload.github.com/91ahmed/nodeQR/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252931821,"owners_count":21827173,"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":[],"created_at":"2025-05-07T18:21:59.358Z","updated_at":"2025-05-07T18:21:59.923Z","avatar_url":"https://github.com/91ahmed.png","language":"JavaScript","readme":"## node establish sql\n\nAn elegant Node.js query builder offering a unified interface for both MySQL and PostgreSQL drivers, featuring intuitive methods to seamlessly construct complex SQL queries with flexibility and precision.\n\n### Content\n- [Features](#features)\n- [How to install](#install-via-npm)\n- [Full Example](#full-example)\n- [Establish Connection](#establish-connection)\n- [Specify the table name](#specify-the-table-name)\n- [Get the connection object](#get-the-connection-object)\n- [End Connection](#end-connection)\n- [Select Statement](#select)\n\t- [Select All Columns](#select-all-columns)\n\t- [Select Specific Columns](#select-specific-columns)\n\t- [Select Distinct Values](#select-distinct-values)\n  - [Select With Alias Name](#select-with-alias-name)\n  - [Select With Aggregate Functions](#select-with-aggregate-functions)\n- [Where Clause](#where-caluse)\n  - [And - Or - Not](#and-or-not)\n  - [Like - In - Between](#like-in-between)\n  - [Is Null - Is Not Null](#is-null-and-is-not-null)\n- [Order By - Limit](#orderby-and-limit)\n- [Group By - Having](#groupby-and-having)\n- [Joins (inner, left, right, full, cross)](#joins)\n- [Union - Union All](#union-and-union-all)\n- [Insert](#insert)\n- [Update](#update)\n- [Truncate](#truncate)\n- [Delete](#delete)\n\n### Features\n**Supports MySQL and PostgreSQL:**\nThe query builder is fully compatible with both MySQL and PostgreSQL databases, offering seamless integration for your projects.\n\n**Comprehensive SQL Coverage:**\nEquipped with a variety of methods to handle the majority of SQL statements, simplifying complex operations and reducing boilerplate code.\n\n**Chaining Style for Readability:**\nMethods are designed to be chainable, allowing you to write concise and easily readable queries.\n\n**Built-in Security Measures:**\nUtilizes placeholders and value filters to prevent SQL injection, ensuring your application remains secure and robust.\n\n### Install via npm\n``` bash\nnpm i node_establish_sql\n```\n\n### Full Example\n``` javascript\n// require the package\nconst connection = require('node_establish_sql')\n\n// Create connection\nconst database = new connection({\n    es_driver: 'mysql-pool', // specify the database driver\n    es_table: 'tablename', // specify the database table name\n\n    // provide your connection information\n    connect: \n    {\n      host: 'localhost',\n      user: 'user-name',\n      password: '12345',\n      database: 'database-name',\n      port: 3306\n    }\n})\n\n// build your SQL Query\ndatabase.query((sql) =\u003e \n{\n    sql.all()\n       .where('id')\n       .between(5, 30)\n       .orderBy(['name'], 'ASC')\n       .limit(5)\n       .get((result) =\u003e {\n          // Get the result\n          console.log(result)\n          // Close connection\n          sql.close()\n       })\n})\n```\n\n### Establish Connection\n\nTo establish a connection, simply pass an object containing the database details to the class. This object allows you to define the connection driver, specify the target table, and provide the database connection credentials.\n\nThe query builder supports both [MySQL](https://www.npmjs.com/package/mysql) and [PostgreSQL](https://node-postgres.com/) drivers. You can set the desired driver using the \"es_driver\" property by choosing one of the following options: mysql-client, mysql-pool, pg-client, or pg-pool.\n\n**Example**\n``` javascript\nconst database = new connection({\n    // database driver\n    // [mysql-client, mysql-pool, pg-client, pg-pool]\n    es_driver: 'mysql-pool',\n    // database table\n    es_table: 'tablename', \n\n    // Connection information\n    connect: \n    {\n      host: 'localhost',\n      user: 'user-name',\n      password: '1234',\n      database: 'database-name',\n      port: 3306\n    }\n})\n``` \n\nLearn about the differences between pool and client connection.\u003cbr/\u003e\n[https://node-postgres.com/features/pooling](https://node-postgres.com/features/pooling) \n\n### Specify the Table Name\n\nTo define the table name, you can include it directly in the connection object using the es_table property, as shown below:\n\n`{ es_table: \"tablename\" }`\n\nAlternatively, you can dynamically set the table name using the table() method.\n\n**Example**\n``` javascript\n// specify the table name within the connection object\nconst database = new connection({\n    es_driver: 'mysql-pool', // database driver\n    es_table: 'your-table-name', // table name\n\n    // Connection information\n    connect: \n    {\n      host: 'localhost',\n      user: 'user-name',\n      password: '1234',\n      database: 'database-name',\n      port: 3306\n    }\n})\n\n// specify the table name through table method.\ndatabase.table('your-table-name')\n```\n\n### Get the Connection Object\nThe ``connect`` property is a key feature that allows you to interact seamlessly with the client and pool connections of MySQL and PostgreSQL drivers. It provides access to the database driver's connection object, enabling you to execute SQL queries, manage connections, and perform other database operations efficiently.\n\nHere’s an example demonstrating how to use the ``connect`` property:\n\n**Example**\n``` javascript\ndatabase.query((sql) =\u003e \n{\n    // connection object\n    sql.connect\n\n    /** Some Examples **/\n\n    // Write sql query\n    sql.connect.query('SELECT * FROM users', (err, res) =\u003e {\n      console.log(res)\n    }) \n\n    // end connection\n    sql.connect.destroy()\n})\n```\n\n### Closing the Database Connection\n\nIt is advisable to close the database connection once you have finished executing queries. This helps free up system resources and prevents potential issues such as connection leaks.\n\nTo close the connection, use the `close()` method:\n\n**Example**\n``` javascript\ndatabase.query((sql) =\u003e \n{\n    // SELECT * FROM table\n    sql.all()\n       .get((result) =\u003e {\n           console.log(result) // result\n\n           sql.close() // close connection\n       })\n})\n```\n\n### Select\nThe query builder provide three methods to select table columns.\n\n**Method** | **Describe** | **Parameters** | **Output**\n:--- | :--- | :--- | :---\n``all()`` | This method selects all columns from the table. | no parameters needed | _SELECT *_\n``select()`` | This method will help you to select specific columns from the table. | _(array)_ the columns you need to select | _SELECT columns_\n``distinct()`` | This method will return distinct (different) results | _(array)_ the columns you need to select | _SELECT DISTINCT columns_\n\n#### Select all columns\n``` javascript\ndatabase.query((sql) =\u003e \n{\n    // SELECT * FROM table\n    sql.all()\n       .get((result) =\u003e {\n           console.log(result) // result\n       })\n})\n```\n\n#### Select specific columns\n``` javascript\ndatabase.query((sql) =\u003e \n{\n    // SELECT column1, column2 FROM table\n    sql.select(['column1', 'column2'])\n       .get((result) =\u003e {\n            console.log(result) // result\n       })\n})\n```\n\n#### Select distinct values\n``` javascript\ndatabase.query((sql) =\u003e \n{\n    // SELECT DISTINCT column1, column2 FROM table\n    sql.distinct(['column1', 'column2'])\n       .get((result) =\u003e {\n           console.log(result) // result\n       })\n})\n```\n\n#### Select with alias name\n``` javascript\ndatabase.query((sql) =\u003e \n{\n    // SELECT column1 AS col1, column2 AS col2 FROM table\n    sql.select([\n          'column1 AS col1', \n          'column2 AS col2'\n        ])\n       .get((result) =\u003e {\n            console.log(result) // result\n       })\n})\n```\n\n#### Select with aggregate functions\n``` javascript\ndatabase.query((sql) =\u003e \n{\n    sql.select([\n          'COUNT(id) AS id_count', \n          'MAX(price) AS max_price',\n          'MIN(price) AS min_price',\n          'SUM(price) AS total_price'\n        ])\n       .get((result) =\u003e {\n            console.log(result) // result\n       })\n})\n```\n\n### Where Clause\nAdding where clause is very important to filter the columns.\nHere are the available methods that will help you to build your **Where** condition.\n\n**Method** | **Describe** | **Parameters** | **Output**\n:--- | :--- | :--- | :---\n``where()`` | Allow you to filter rows using where clause | _(string)_ the column | _WHERE column_\n``value()`` | Used to specify the **operator** and the **value** after where statement.\u003cbr/\u003e``=``  ``\u003c``  ``\u003e``  ``\u003c=``  ``\u003e=``  ``\u003c\u003e``  ``!=`` | _(string)_ the operator \u003cbr/\u003e _(mixed)_ the value | _= value_\n\n**Example**\n``` javascript\ndatabase.query((sql) =\u003e \n{\n    // SELECT * FROM table WHERE column \u003e 5\n    sql.all()\n       .where('column').value('\u003e', 5)\n       .get((result) =\u003e {\n           console.log(result)\n       })\n})\n```\n\n### And Or Not\n\nThese operators are used to combine with **where** condition to get accurate results.\n\n**Method** | **Parameters** | **Output**\n:--- | :--- | :---\n``and()`` | _(string)_ column name | _AND column_\n``or()`` | _(string)_ column name | _OR column_\n``whereNot()`` | _(string)_ column name | _WHERE NOT column_\n\n**Example**\n``` javascript\ndatabase.query((sql) =\u003e \n{\n    // SELECT * FROM table WHERE column = 2 AND column2 = 'value'\n    sql.all()\n       .where('column').value('=', 2)\n       .and('column2').value('=', 'value')\n       .get((result) =\u003e {\n          console.log(result)\n       })\n\n    // SELECT * FROM table WHERE column = 2 OR column = 5\n    sql.all()\n       .where('column').value('=', 2)\n       .or('column').value('=', 5)\n       .get((result) =\u003e {\n          console.log(result)\n       })\n\n    // SELECT * FROM table WHERE NOT column = 20\n    sql.all()\n       .whereNot('column').value('=', 20)\n       .get((result) =\u003e {\n          console.log(result)\n       })\n})\n```\n\n### Like In Between\n\n**Method** | **Parameters** | **Output**\n:--- | :--- | :---\n``like()`` | _(string)_ pattern | _LIKE \"%%\"_\n``in()`` | _(array)_ values | _IN (1,2,3)_\n``between()`` | _(mixed)_ value1 \u003cbr/\u003e _(mixed)_ value2 | _BETWEEN value AND value_\n\n**Example**\n``` javascript\ndatabase.query((sql) =\u003e \n{\n    // SELECT * FROM table WHERE column LIKE '%pattern%'\n    sql.all()\n       .where('column').like('%pattern%')\n       .get((result) =\u003e {\n          console.log(result)\n       })\n\n    // SELECT * FROM table WHERE column IN (3,0,8)\n    sql.all()\n       .where('column').in([3, 0, 8])\n       .get((result) =\u003e {\n          console.log(result)\n       })\n\n    // SELECT * FROM table WHERE column BETWEEN 5 AND 10\n    sql.all()\n       .where('column').between(5, 10)\n       .get((result) =\u003e {\n          console.log(result)\n       })\n})\n```\n\n### Is Null and Is Not Null\n\n**Example**\n``` javascript\ndatabase.query((sql) =\u003e \n{\n    // SELECT * FROM table WHERE column IS NULL\n    sql.all()\n       .where('column').isNull()\n       .get((result) =\u003e {\n          console.log(result)\n       })\n\n    // SELECT * FROM table WHERE column IS NOT NULL\n    sql.all()\n       .where('column').isNotNull()\n       .get((result) =\u003e {\n          console.log(result)\n       })\n})\n```\n\n### Orderby and Limit\n\nYou can use `orderBy()` and `limit()` to sort data and retrieve limited records.\n\n**Method** | **Parameters** | **Output**\n:--- | :--- | :---\n``orderBy()`` | _(array)_ columns. \u003cbr/\u003e _(string)_ sort (DESC, ASC). | _ORDER BY columns DESC_\n``limit()`` | _(integer)_ records number. | _LIMIT value_\n\n\n**Example**\n``` javascript\ndatabase.query((sql) =\u003e \n{\n    // SELECT * FROM table ORDER BY id LIMIT 5\n    sql.all()\n       .orderBy(['id']) // default DESC\n       .limit(5)\n       .get((result) =\u003e {\n          console.log(result)\n       })\n})\n```\n\n### Groupby and Having\n\nUse `groupBy()` and `having()` to summarize the results and get statistical information.\n\n**Method** | **Parameters** | **Output**\n:--- | :--- | :---\n``groupBy()`` | _(array)_ columns. | _GROUP BY columns_\n``having()`` | _(string)_ the column. | _HAVING column_\n\n\n**Example**\n``` javascript\ndatabase.query((sql) =\u003e \n{\n    // SELECT COUNT(column) AS c FROM table GROUP BY column HAVING column \u003e 5\n    sql.select(['COUNT(column) AS c'])\n       .groupBy(['column'])\n       .having('column').value('\u003e', 5)\n       .get((result) =\u003e {\n          console.log(result)\n       })\n})\n```\n\n### Joins\n\n**Example**\n``` javascript\ndatabase.query((sql) =\u003e \n{\n    // SELECT * FROM table1 INNER JOIN table2 ON column1 = column2\n    sql.all()\n       .innerJoin('table2').on('column1', 'column2')\n       .get((result) =\u003e {\n           console.log(result)\n       })\n\n    // SELECT * FROM table1 LEFT JOIN table2 ON column1 = column2\n    sql.all()\n       .leftJoin('table2').on('column1', 'column2')\n       .get((result) =\u003e {\n           console.log(result)\n       })\n\n    // SELECT * FROM table1 RIGHT JOIN table2 ON column1 = column2\n    sql.all()\n       .rightJoin('table2').on('column1', 'column2')\n       .get((result) =\u003e {\n           console.log(result)\n       })\n\n    // SELECT * FROM table1 FULL OUTER JOIN table2 ON column1 = column2\n    sql.all()\n       .fullJoin('table2').on('column1', 'column2')\n       .get((result) =\u003e {\n           console.log(result)\n       })\n\n    // SELECT * FROM table1 CROSS JOIN table2\n    sql.all()\n       .crossJoin('table2')\n       .get((result) =\u003e {\n           console.log(result)\n       })\n})\n```\n\n### Union and Union All\n\nUse Union and Union All Operators two combine the result of two tables.\n\n**Method** | **Parameters** | **Output**\n:--- | :--- | :---\n``union()`` | _(array)_ columns. \u003cbr/\u003e _(string)_ table. | _UNION columns FROM table_\n``unionAll()`` | _(array)_ columns. \u003cbr/\u003e _(string)_ table. | _UNION ALL columns FROM table_\n\n**Example**\n``` javascript\ndatabase.query((sql) =\u003e \n{\n    // SELECT column1, column2 FROM table1 UNION column1, column2 FROM table2\n    sql.select(['column1', 'column2'])\n       .union(['column1', 'column2'], 'table2')\n       .get((result) =\u003e {\n           console.log(result)\n       })\n\n    // SELECT column1, column2 FROM table1 UNION ALL column1, column2 FROM table2\n    sql.select(['column1', 'column2'])\n       .unionAll(['column1', 'column2'], 'table2')\n       .get((result) =\u003e {\n           console.log(result)\n       })\n})\n```\n\n### Insert\n\nThe query builder provide ``insert()`` method to insert records into database table, The insert method accepts an object of column names and values.\n\n**Method** | **Describe** | **Parameters** | **Output**\n:--- | :--- | :--- | :---\n``insert()`` | Generate sql insert statement. | _(object)_ column and value | _INSERT INTO table (columns) VALUES (values)_\n\n**Example**\n``` javascript\ndatabase.query((sql) =\u003e \n{\n    // INSERT INTO table (id, name) VALUES (20, \"ahmed\")\n    sql.insert({id: 20, name: 'ahmed'})\n       .save()\n})\n```\n\n### Update\n\nTo update existing records use `update()` method, it accepts an object of column and value pairs indicating the columns to be updated.\n\n**Method** | **Describe** | **Parameters** | **Output**\n:--- | :--- | :--- | :---\n``update()`` | Generate sql update statement. | _(object)_ column and value | _UPDATE table SET column = value_\n\n**Example**\n``` javascript\ndatabase.query((sql) =\u003e \n{\n    // UPDATE table SET column1 = 'value1', column2 = 'value2' WHERE column = 'value'\n    sql.update({\n      column1: 'value1', \n      column2: 'value2'\n    })\n    .where('column').value('=', 'value') // condition\n    .save()\n\n    // UPDATE table SET column = 'value' WHERE column = 'value'\n    sql.update({column: 'value'})\n       .where('column').value('=', 'value')\n       .save()\n})\n```\n\n### Truncate\n\nThis method will truncate the selected table.\n\n**Example**\n``` javascript\ndatabase.query((sql) =\u003e \n{\n    // TRUNCATE TABLE tablename\n    sql.truncate().save()\n})\n```\n\n### Delete\n\nYou can use `delete()` method to delete single or multiple records.\n\n**Method** | **Describe** | **Parameters** | **Output**\n:--- | :--- | :--- | :---\n``delete()`` | Generate sql delete statement. | no parameters needed | _DELETE FROM table_\n\n**Example**\n``` javascript\ndatabase.query((sql) =\u003e \n{\n    // DELETE FROM table WHERE column = 'value'\n    sql.delete()\n       .where('column').value('=', 'value')\n       .save()\n\n    // DELETE FROM table WHERE column IN (9,7,8)\n    sql.delete().where('column').in([9,7,8]).save()\n})\n```\n\n\u003e Note: For insert, delete and update you must call the ``save()`` method at the end to execute the qurey.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F91ahmed%2Fnodeqr","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F91ahmed%2Fnodeqr","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F91ahmed%2Fnodeqr/lists"}