{"_id":"@cai_jinghong/wrapper-qp","_rev":"1-2289c11b1fa6481d79bff5988a6c270c","name":"@cai_jinghong/wrapper-qp","dist-tags":{"latest":"1.0.2"},"versions":{"1.0.1":{"name":"@cai_jinghong/wrapper-qp","version":"1.0.1","description":"query processor as a wrapper on top of mysql, this version is continue from flexqp","main":"qp.js","scripts":{"test":"echo \"Error: no test specified\" && exit 1"},"repository":{"type":"git","url":"git+https://github.com/bundleOFSunlight/wrapper-qp.git"},"keywords":["flexqp"],"author":{"name":"Cai JingHong"},"license":"ISC","dependencies":{"core-util-is":"1.0.2","inherits":"2.0.3","moment":"^2.24.0","mysql":"^2.18.1","util-deprecate":"1.0.2","uuid":"^3.4.0"},"types":"./qp.d.ts","bugs":{"url":"https://github.com/bundleOFSunlight/wrapper-qp/issues"},"homepage":"https://github.com/bundleOFSunlight/wrapper-qp#readme","_id":"@cai_jinghong/wrapper-qp@1.0.1","gitHead":"65999171e3e954752ad836c6277ac2a0f2d81aef","_nodeVersion":"20.11.0","_npmVersion":"10.2.4","dist":{"integrity":"sha512-4FyHOJuWZB8j1pJFpPaCF9GpOpxkeXFeL0fYiPMlGHDJtIddfnmLmRzZYIMWv5vTvA73qQ806GHhShxr2wbbFg==","shasum":"d59934b023df00537bd98b6a5794add0f748c40a","tarball":"https://registry.npmjs.org/@cai_jinghong/wrapper-qp/-/wrapper-qp-1.0.1.tgz","fileCount":13,"unpackedSize":102475,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIA9X/z1Os5mJ1OEplMwP6wfZfj1cSY0X6K3BuWalOUJeAiAopVrl1E8ykVlhYcuNW8rBRUG3OuOtJsMgq5qLQOy+3Q=="}]},"_npmUser":{"name":"cai_jinghong","email":"raycjhcoding@gmail.com"},"directories":{},"maintainers":[{"name":"cai_jinghong","email":"raycjhcoding@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/wrapper-qp_1.0.1_1706844017360_0.22958524153388282"},"_hasShrinkwrap":false},"1.0.2":{"name":"@cai_jinghong/wrapper-qp","version":"1.0.2","description":"query processor as a wrapper on top of mysql, this version is continue from flexqp","main":"qp.js","scripts":{"test":"echo \"Error: no test specified\" && exit 1"},"repository":{"type":"git","url":"git+https://github.com/bundleOFSunlight/wrapper-qp.git"},"keywords":["flexqp"],"author":{"name":"Cai JingHong"},"license":"ISC","dependencies":{"core-util-is":"1.0.2","inherits":"2.0.3","moment":"^2.24.0","mysql2":"^3.9.1","util-deprecate":"1.0.2","uuid":"^3.4.0"},"types":"./qp.d.ts","bugs":{"url":"https://github.com/bundleOFSunlight/wrapper-qp/issues"},"homepage":"https://github.com/bundleOFSunlight/wrapper-qp#readme","_id":"@cai_jinghong/wrapper-qp@1.0.2","gitHead":"f0a6a581454025762ab71efead530964c81da779","_nodeVersion":"20.11.0","_npmVersion":"10.2.4","dist":{"integrity":"sha512-QhYL+aWRxCjgwwZZyHGyIKXxChHx6S/NBS5CZDsYtI2ejF11A7rpJ5WwR6p9DZ9tZHyfNjv/oDIVnkbyQB7X/Q==","shasum":"fd528ca8acf854fc29d4510e70a4725f550c16b5","tarball":"https://registry.npmjs.org/@cai_jinghong/wrapper-qp/-/wrapper-qp-1.0.2.tgz","fileCount":13,"unpackedSize":102478,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQD81alz8eQZf7rC/YrpHeuGEJUhIvFmyLUNHFElGKVIyQIhAPDuoH6XJXSEIFx0zMp+1ppRcubISxoU/C6u5EDEBGOY"}]},"_npmUser":{"name":"cai_jinghong","email":"raycjhcoding@gmail.com"},"directories":{},"maintainers":[{"name":"cai_jinghong","email":"raycjhcoding@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/wrapper-qp_1.0.2_1707209466703_0.3001398075041968"},"_hasShrinkwrap":false}},"time":{"created":"2024-02-02T03:20:17.294Z","1.0.1":"2024-02-02T03:20:17.513Z","modified":"2024-02-06T08:51:07.030Z","1.0.2":"2024-02-06T08:51:06.855Z"},"maintainers":[{"name":"cai_jinghong","email":"raycjhcoding@gmail.com"}],"description":"query processor as a wrapper on top of mysql, this version is continue from flexqp","homepage":"https://github.com/bundleOFSunlight/wrapper-qp#readme","keywords":["flexqp"],"repository":{"type":"git","url":"git+https://github.com/bundleOFSunlight/wrapper-qp.git"},"author":{"name":"Cai JingHong"},"bugs":{"url":"https://github.com/bundleOFSunlight/wrapper-qp/issues"},"license":"ISC","readme":"# Flex-Solver Query Processor Library\r\n\r\n## Table of Contents\r\n-  [Introduction](#introduction)\r\n-  [Important Notes](#important-notes)\r\n-  [Getting Started](#getting-started)\r\n-  [Basic Syntax](#basic-syntax)\r\n-  [Transactions](#transactions)\r\n\r\n\r\n\r\n## Introduction\r\n\r\n### Objective\r\nThis library was built on top of the [Official MySQL Library](https://github.com/mysqljs/mysql#introduction), to create a clean and standardized format for MySQL queries, so as to ease the process of database interactions.\r\n\r\nWe recommend that you try out the Official MySQL Library before you start with this wrapper.\r\n\r\n### Features\r\n- Simplified connection to MySQL Database.\r\n- Simplified queries to the database.\r\n- Parsing of data from database for use in backend or frontend code.\r\n\r\n\r\n\r\n## Important Notes\r\n\r\n### Important Notes #1 - About Promises (Latest 20/05/2018)\r\nMost, if not all, functions in this library return promises, hence it is important for developers be fluent it using [await-async (read me to find out why).](https://hackernoon.com/6-reasons-why-javascripts-async-await-blows-promises-away-tutorial-c7ec10518dd9)\r\n\r\n### Important Notes #2 - About Returns (Latest 20/05/2018)\r\nAs mentioned, the library is written on top of the offical library, minimal changes were made to the returned data for ease of data parsing.\r\n\r\nApart from that, you may refer to \r\n1. [JSON Data Type](#json-data-type)\r\n2. [Timestamp Data Type](#timestamp-data-type)\r\n\r\n\r\n## Getting Started\r\n\r\n### Installation\r\nThis is a Node.js modeule available through the [NPM registry](https://www.npmjs.com/).\r\n\r\nBefore installing, download and install Node.js. Node.js 8.0 or higher is required.\r\n\r\nInstallation is done using the npm install command:\r\n\r\n```\r\n$ npm install @flexsolver/flexqp2\r\n```\r\n\r\n## Defining a Connection\r\nThe recommended method of defining a connection is to create a JSON file:\r\n\r\n```JSON\r\n{\r\n    \"host\" : \"localhost\",\r\n    \"user\" : \"admin\",\r\n    \"password\" : \"p@55w0rd\",\r\n    \"database\" : \"world\"\r\n}\r\n```\r\n\r\nAlternatively, it can be defined as a Javascript Object:\r\n\r\n```Javascript\r\nlet config = {\r\n    \"host\" : \"localhost\",\r\n    \"user\" : \"admin\",\r\n    \"password\" : \"p@55w0rd\",\r\n    \"database\" : \"world\"\r\n}\r\n```\r\n\r\n## Connecting to the Database\r\nWhen using the flexqp library, a connection to the database must be preset upon running the server, after which the connection is no longer required to be defined for each individual SQL query. \r\n\r\n```Javascript\r\nconst qp = require('@flexsolver/flexqp2');\r\nqp.presetConnection(require('./dbConfig.json'));\r\n```\r\n\r\nNote that when importing the flexqp library, an instance of the class is created for the entire project, meaning that a connection defined in the entry file will persist throughout the project.\r\n\r\n### Connecting to multiple Schemas\r\nComing Soon.\r\n\r\n\r\n\r\n## Basic Syntax\r\n\r\n### MySQL Query Parameters\r\nMost, if not all, functions require at least an SQL statemend and parameters, if any. In order to pass parameters into the SQL statement, the following methods are used:\r\n\r\n1. Passing parameters as an Array\r\n\r\n   `?`, denoting an unescaped paramenter and `??`, denoting and escaped parameter, symbols are expected within the SQL statements, with the number of symbols matching the number of array elements.\r\n\r\n   ```Javascript\r\n   // Actual SQL query processed: SELECT * FROM `table` WHERE date > 2019-01-01\r\n   let params = ['table', '2019-01-01'];\r\n   let result = await qp.select(`SELECT * FROM ?? WHERE date > ?`, params);\r\n   ```\r\n\r\n\r\n2. Passing parameters as an Object\r\n\r\n   This method is useful if reusing the same parameter multiple times within a statement. In this case, the syntax used to denote the parameter in the query would be `:object_property`:\r\n\r\n   ```Javascript\r\n   // Actual SQL query processed: SELECT * FROM table WHERE name LIKE CONCAT('%', 'flex', '%') OR email LIKE CONCAT('%', 'flex', '%') or user LIKE CONCAT('%', 'flex', '%')\r\n   let params = {\r\n       search: 'flex'\r\n   }\r\n   let result = await qp.select(`SELECT * FROM table WHERE name LIKE CONCAT('%', :search, '%') OR email LIKE CONCAT('%', :search, '%') or user LIKE CONCAT('%', :search, '%')`, params);\r\n   ```\r\n\r\n### Data Retrieval\r\nFor simple data retrieval, use the following functions:\r\n\r\n```Javascript\r\n// returns an array of rowData, or empty array if no matches found\r\nlet arr = await qp.select(`SELECT * FROM table`);\r\n\r\n// returns a first result found as an object, or null if no matches found\r\nlet result = await qp.selectFirst(`SELECT * FROM table`);\r\n\r\n// returns a map from rowData, with specified column as key (key must be unique)\r\n// the resulting map has the following functions: get, set, delete, keys, values, toJSON, toValuesArray, toKeysArray\r\nlet map = await qp.selectMap(`column`, `SELECT * FROM table`);\r\n\r\n// returns a map from rowData, with specified column as key (results are grouped into arrays)\r\nlet arrMap = await qp.selectMapArray(`column`, `SELECT * FROM table`);\r\n\r\n// returns array of values from a specific column in table\r\nlet array = await qp.scalar(`SELECT column FROM table`); \r\n\r\n// returns single value from specific column in table (similar to selectFirst, returns first result)\r\nlet value = await qp.scalarFirst(`SELECT column FROM table`);\r\n```\r\n\r\n### Data Manipulation\r\nFor simple data manipulation, use the following function:\r\n\r\n```Javascript\r\nlet insert = await qp.run(`INSERT INTO table SET ?`, [dao]);\r\nlet update = await qp.run(`UPDATE table SET name = 'BLANK'`);\r\nlet delete = await qp.run(`DELETE FROM table WHERE name = 'BLANK`); \r\n\r\n// runs query and ignores error\r\nlet ignore = await qp.run(`INSERT INTO table SET ?`, [dao]);\r\n\r\n// inserts an array of rows in 1 statement, updates selected columns if specified in colArr, ignore errors if specified by ignore parameter\r\nlet cols = [`column1`, `column2`];\r\nlet ignore = true;\r\nlet bulkInsert = await qp.bulkInsert(`table`, daoArr, ignore, con, colArr);\r\n\r\n// insert or update of already exists\r\nlet upsert = await qp.upsert(`table`, dao, con, cols);\r\n\r\n// insert or update an array of rows\r\nlet bulkUpsert = await qp.bulkUpsert(`table`, daoArr, con, cols);\r\n```\r\n\r\n\r\n\r\n## Transactions\r\nFor handling of multiple MySQL queries within a single transaction, follow the example below\r\n\r\n```Javascript\r\nlet con;\r\ntry {\r\n\r\n    // Create a temporary connection for the transaction\r\n    con = await qp.connectWithTBegin();\r\n\r\n    /*\r\n    Multiple MySQL queries for the connection\r\n    .\r\n    .\r\n    .\r\n    */\r\n\r\n   // Commit and closing the connection\r\n   await qp.commitAndCloseConnection(con);\r\n\r\n} catch (err){\r\n    if (con) await qp.rollbackAndCloseConnection(con);\r\n}\r\n```\r\n\r\nNote that it is important to define `let con` outside of the `try {}` code block, in order to rollback the connection when an error is caught.","readmeFilename":"readme.md"}