{"id":20604822,"url":"https://github.com/themost-framework/query","last_synced_at":"2025-04-15T02:26:31.056Z","repository":{"id":38107660,"uuid":"256459015","full_name":"themost-framework/query","owner":"themost-framework","description":"@themost/query is a query builder for SQL. It includes a wide variety of helper functions for building complex SQL queries under node.js","archived":false,"fork":false,"pushed_at":"2025-03-15T05:55:44.000Z","size":1225,"stargazers_count":3,"open_issues_count":6,"forks_count":1,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-28T14:11:31.820Z","etag":null,"topics":["agnostic","closures","database","orm","query","query-builder","rdbms","sql","sql-language","sql-query"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/themost-framework.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-04-17T09:25:27.000Z","updated_at":"2025-03-15T05:54:41.000Z","dependencies_parsed_at":"2023-11-07T10:27:33.406Z","dependency_job_id":"be3aee61-7020-4e2e-9f3d-5ecf02535910","html_url":"https://github.com/themost-framework/query","commit_stats":null,"previous_names":[],"tags_count":76,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/themost-framework%2Fquery","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/themost-framework%2Fquery/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/themost-framework%2Fquery/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/themost-framework%2Fquery/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/themost-framework","download_url":"https://codeload.github.com/themost-framework/query/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248993059,"owners_count":21195134,"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":["agnostic","closures","database","orm","query","query-builder","rdbms","sql","sql-language","sql-query"],"created_at":"2024-11-16T09:24:58.107Z","updated_at":"2025-04-15T02:26:31.034Z","avatar_url":"https://github.com/themost-framework.png","language":"JavaScript","readme":"[![npm](https://img.shields.io/npm/v/@themost%2Fquery.svg)](https://www.npmjs.com/package/@themost%2Fquery)\n![Dependency status for latest release](https://img.shields.io/librariesio/release/npm/@themost/query)\n![GitHub top language](https://img.shields.io/github/languages/top/themost-framework/query)\n[![License](https://img.shields.io/npm/l/@themost/query)](https://github.com/themost-framework/themost/blob/master/LICENSE)\n![GitHub last commit](https://img.shields.io/github/last-commit/themost-framework/query)\n![GitHub Release Date](https://img.shields.io/github/release-date/themost-framework/query)\n[![npm](https://img.shields.io/npm/dw/@themost/query)](https://www.npmjs.com/package/@themost%2Fquery)\n![Snyk Vulnerabilities for npm package](https://img.shields.io/snyk/vulnerabilities/npm/@themost/query)\n\n![MOST Web Framework Logo](https://github.com/themost-framework/common/raw/master/docs/img/themost_framework_v3_128.png)\n\n# @themost/query\n[MOST Web Framework](https://github.com/themost-framework) database-agnostic query module and query language parser of [@themost/data](https://github.com/themost-framework/data)\n\n`@themost/query` introduces javascript closures-to-SQL prototype. \n\nWrite javascript closures and let `@themost/query` extract the equivalent SQL statements for querying data:\n\n\tnew QueryExpression().select((x) =\u003e {\n\t\treturn {\n\t\t\tid: x.id,\n\t\t\tname: x.name,\n\t\t\tcategory: x.category,\n\t\t\tmodel: x.model,\n\t\t\tprice: x.price\n\t\t}\n\t}).from(Products)\n\t.where((x) =\u003e {\n\t\treturn x.price \u003e 500;\n\t}).take(10);\n\n\nLicense: [BSD-3-Clause](https://github.com/themost-framework/themost-query/blob/master/LICENSE)\n\n## Usage\n\nInstall @themost/query\n\n    npm i @themost/query\n\n## Data adapters\n\nUse [MOST Web Framework](https://github.com/themost-framework) data adapters and connect with any of the available database engines:\n\n- [MySQL](https://github.com/themost-framework/mysql)\n- [MariaDB](https://github.com/themost-framework/mysql)\n- [Microsoft Sql Server](https://github.com/themost-framework/mssql)\n- [PostgreSQL](https://github.com/themost-framework/pg)\n- [SQlite](https://github.com/themost-framework/sqlite)\n- [Oracle](https://github.com/themost-framework/oracle)\n\nAn example of using `@themost/sqlite#SqliteAdapter` to connect with a SQlite database and querying data:\n\n\t\timport { QueryField } from '@themost/query';\n\t\timport { QueryEntity, QueryExpression } from '@themost/query';\n\t\timport { SqliteAdapter } from '@themost/sqlite';\n\n\t\t// initialize data connection\n\t\tconst db = new SqliteAdapter({\n            name: 'local',\n            database: './db/local.db'\n        });\n\t\t// get query entity\n\t\tconst Products = new QueryEntity('ProductData');\n\t\t// build query\n\t\tconst query = new QueryExpression()\n\t\t\t.select((x) =\u003e {\n\t\t\t\tx.id,\n\t\t\t\tx.name,\n\t\t\t\tx.category,\n\t\t\t\tx.price\n\t\t\t})\n\t\t\t.from(Products)\n\t\t\t.take(25);\n\t\t// execute query\n\t\tconst results = await db.executeAsync(query);\n\t\t// and close connection\n\t\tawait db.close();\n\nFeatures: [SQL Select](#sql-select), [SQL Select From](#sql-select-from),\n[SQL Logical Operators](#sql-and-or), [SQL Select Top](#sql-select-top),\n[SQL Order By](#sql-order-by), [SQL Group By](#sql-group-by),\n[SQL Where](#sql-where), [SQL Insert](#sql-insert), [SQL Update](#sql-update),\n[SQL Delete](#sql-delete), [SQL Functions](#sql-functions),\n[SQL Date Functions](#sql-date-functions), [SQL String Functions](#sql-string-functions)\n\n## SQL Select\n\n### SQL Select From\n\nUse `QueryExpression` to build SQL select statements:\n\n\tconst q = new QueryExpression().select('id', 'name',  'category', 'model', 'price')\n        .from('ProductData').orderBy('name');\n\tconst SQL = new SqlFormatter().format(q);\n\nSQL \u003e `SELECT ProductData.id, ProductData.name, ProductData.category, ProductData.model, ProductData.price FROM ProductData ORDER BY name ASC`\n\n### SQL And, Or\n\nUse `QueryExpression.and()` and `QueryExpression.or()` to build logical SQL statements\n\n    const q = new QueryExpression().select('id', 'name',  'category', 'model', 'price')\n        .where('price').greaterThan(500).and('price').lowerOrEqual(1000)\n        .from('ProductData').orderBy('name');\n\tconst SQL = new SqlFormatter().format(q);\n\nSQL \u003e `SELECT ProductData.id, ProductData.name, ProductData.category, ProductData.model, ProductData.price FROM ProductData WHERE ((price\u003e500) AND (price\u003c=1000)) ORDER BY name ASC`\n\n    const q = new QueryExpression().select('id', 'name',  'category', 'model', 'price')\n        .where('category').equal('Laptops').or('category').equal('Desktops')\n        .from('ProductData').orderBy('name');\n\tconst SQL = new SqlFormatter().format(q);\n\nSQL \u003e `SELECT ProductData.id, ProductData.name, ProductData.category, ProductData.model, ProductData.price FROM ProductData WHERE ((category='Laptops') OR (category='Desktops')) ORDER BY name ASC`\n\nor use javascript closures and build your SQL statement:\n\n\tconst Products = new QueryEntity('ProductData');\n\tconst q = new QueryExpression().select(function(x) {\n            return {\n                id: x.id,\n                name: x.name,\n                category: x.category,\n                price: x.price\n            }\n        }).where(function(x) {\n            return x.price \u003e 500 \u0026\u0026 x.price \u003c= 1000;\n        }).from(Products).orderBy(function(x) {\n            return {\n                price: x.price\n            };\n        });\n\tconst SQL = new SqlFormatter().format(q);\n\n### SQL Select Top\n\nUse `QueryExpression.take()` and `QueryExpression.skip()` to specify the number of records to return.\n\n\tconst q = new QueryExpression().select('id', 'name', 'category', 'model','price')\n\t    .from('ProductData').orderBy('name').take(25).skip(25);\n\tconst SQL = new SqlFormatter().format(q);\n\nSQL \u003e `SELECT ProductData.id, ProductData.name, ProductData.category, ProductData.model, ProductData.price FROM ProductData ORDER BY name ASC LIMIT 25, 25`\n\n### SQL Order By\n\nUse `QueryExpression.orderBy()`, `QueryExpression.thenBy()`, `QueryExpression.thenByDescending()` to sort the result-set in ascending or descending order.\n\n\tconst q = new QueryExpression().select('id', 'name', 'category', 'model','price')\n\t    .from('ProductData').orderBy('category').thenByDescending('price');\n\tconst SQL = new SqlFormatter().format(q);\n\nSQL \u003e `SELECT ProductData.id, ProductData.name, ProductData.category, ProductData.model, ProductData.price FROM ProductData ORDER BY category ASC, price DESC`\n\n### SQL Group By\n\nUse `QueryExpression.groupBy()` to group rows that have the same values into summary rows.\n\n\tconst Orders = new QueryEntity('OrderData');\n\tconst Products = new QueryEntity('ProductData').as('orderedItem');\n\tlet query = new QueryExpression()\n\t\t.select((x) =\u003e {\n\t\t\treturn {\n\t\t\t\ttotal: count(x.id),\n\t\t\t\tproduct: x.orderedItem.name,\n\t\t\t\torderYear: x.orderDate.getFullYear()\n\t\t\t}\n\t\t})\n\t\t.from(Orders)\n\t\t.join(Products).with((x, y) =\u003e {\n\t\t\treturn x.orderedItem === y.id;\n\t\t})\n\t\t.groupBy(x =\u003e x.orderedItem.name, x =\u003e x.orderDate.getFullYear());\n\tconst SQL = new SqlFormatter().format(query);\n\nSQL \u003e `SELECT COUNT(OrderData.id) AS total, orderedItem.name AS product, YEAR(OrderData.orderDate) AS orderYear FROM OrderData INNER JOIN ProductData AS orderedItem ON OrderData.orderedItem = orderedItem.id GROUP BY orderedItem.name, YEAR(OrderData.orderDate)`\n\n### SQL Where\n\nFilter records by using comparison operators:\n\n- equal()\n\n\t\tconst q = new QueryExpression().select('id', 'name', 'category', 'model','price')\n\t    \t.from('ProductData').where('category').equal('Laptops');\n\n- notEqual()\n\n\t\tconst q = new QueryExpression().select('id', 'name', 'category', 'model','price')\n\t    \t.from('ProductData').where('category').notEqual('Desktops');\n\n- greaterThan()\n\n\t\tconst q = new QueryExpression().select('id', 'name', 'category', 'model','price')\n\t    \t.from('ProductData').where('price').greaterThan(500);\n\n- lowerThan()\n\n\t\tconst q = new QueryExpression().select('id', 'name', 'category', 'model','price')\n\t    \t.from('ProductData').where('price').lowerThan(1000);\n\n- greaterOrEqual()\n\n\t\tconst q = new QueryExpression().select('id', 'name', 'category', 'model','price')\n\t    \t.from('ProductData').where('price').greaterOrEqual(500);\n\n- lowerOrEqual()\n\n\t\tconst q = new QueryExpression().select('id', 'name', 'category', 'model','price')\n\t    \t.from('ProductData').where('price').lowerOrEqual(1000);\n\n- in()\n\n\t\tconst q = new QueryExpression().select('id', 'name', 'category', 'model','price')\n\t    \t.from('ProductData').where('category').in([\n\t\t\t\t'Laptops',\n\t\t\t\t'Desktops'\n\t\t\t]);\n\tSQL \u003e `SELECT ProductData.id, ProductData.name, ProductData.category, ProductData.model, ProductData.price FROM ProductData WHERE (category IN ('Laptops', 'Desktops'))`\n\n- notIn()\n\n\t\tconst q = new QueryExpression().select('id', 'name', 'category', 'model','price')\n\t    \t.from('ProductData').where('category').notIn([\n\t\t\t\t'Laptops',\n\t\t\t\t'Desktops'\n\t\t\t]);\n\tSQL \u003e `SELECT ProductData.id, ProductData.name, ProductData.category, ProductData.model, ProductData.price FROM ProductData WHERE (NOT category IN ('Laptops', 'Desktops'))`\n\n## SQL Insert\n\nUse `QueryExpression.insert()` to build SQL insert statements\n\n\tconst q = new QueryExpression().insert({\n\t\tname: 'Laptop XLG 15 16GB',\n\t\tcategory: 'Laptops',\n\t\tmodel: 'XLG15',\n\t\tprice: 1199.99\n\t}).into('ProductBase');\n\tconst SQL = new SqlFormatter().format(q);\n\nSQL \u003e `INSERT INTO ProductBase(name, category, model, price) VALUES ('Laptop XLG 15 16GB', 'Laptops', 'XLG15', 1199.99)`\n\n## SQL Update\n\nUse `QueryExpression.update()` to build SQL update statements\n\n\tconst q = new QueryExpression().update('OrderBase').set({\n\t\tstatus: 2\n\t}).where('orderDate').lowerThan('2022-10-25');\n\tconst SQL = new SqlFormatter().format(q);\n\nSQL \u003e `UPDATE OrderBase SET status=2 WHERE (orderDate\u003c'2022-10-25')`\n\n## SQL Delete\n\nUse `QueryExpression.delete()` to build SQL delete statements\n\n\tconst q = new QueryExpression().delete('OrderBase').where('id').equal(12020);\n\tconst SQL = new SqlFormatter().format(q);\n\nSQL \u003e `DELETE FROM OrderBase WHERE (id=12020)`\n\n## SQL Join\n\nUse `QueryExpression.join()`, `QueryExpression.leftJoin()`, `QueryExpression.rightJoin()` to combine rows from two or more tables, based on a related column between them.\n\n\tconst q = new QueryExpression().select(\n\t\t'id',\n\t\t'orderDate', \n\t\t'orderStatus',\n\t\tnew QueryField('name').from('ProductData').as('product')\n\t).from('OrderData').join('ProductData').with(\n\t\tnew QueryExpression().where(new QueryField('orderedItem').from('OrderData'))\n\t\t.equal(new QueryField('id').from('ProductData'))\n\t);\n\tconst SQL = new SqlFormatter().format(q);\n\nSQL \u003e `SELECT OrderData.id, OrderData.orderDate, OrderData.orderStatus, ProductData.name AS product FROM OrderData INNER JOIN ProductData ON (OrderData.orderedItem=ProductData.id)`\n\nor use closures:\n\n\tconst Orders = new QueryEntity('OrderData').as('Orders');\n\tconst Customers = new QueryEntity('PersonData').as('customer');\n\tconst familyName = 'Thomas'\n\tconst q = new QueryExpression().from(Orders).select((x) =\u003e {\n\t\treturn {\n\t\t\tid: x.id,\n\t\t\torderedItem: x.orderedItem,\n\t\t\tcustomerFamilyName: x.customer.familyName,\n\t\t\tcustomerGivenName: x.customer.givenName,\n\t\t\torderStatus: x.orderStatus\n\t\t}\n\t}).leftJoin(Customers).with((x, y) =\u003e {\n\t\treturn x.customer === y.id;\n\t}).where((x, familyName) =\u003e {\n\t\treturn x.customer.familyName === familyName;\n\t}, {\n\t\tfamilyName\n\t}).orderByDescending((x) =\u003e x.orderDate).take(25);\n\tconst SQL = new SqlFormatter().format(q);\n\nSQL \u003e `SELECT Orders.id AS id, Orders.orderedItem AS orderedItem, customer.familyName AS customerFamilyName, customer.givenName AS customerGivenName, Orders.orderStatus AS orderStatus FROM OrderData AS Orders LEFT JOIN PersonData AS customer ON Orders.customer = customer.id WHERE customer.familyName = 'Thomas' ORDER BY Orders.orderDate DESC LIMIT 25`\n\n## SQL Functions\n\n`@themost/query` prototype offers a set of common SQL functions for filtering records.\n\n### add()\n\n\tconst Products = new QueryEntity('ProductData');\n\tlet query = new QueryExpression()\n\t\t.select(function(x) {\n\t\t\treturn {\n\t\t\t\tid: x.id,\n\t\t\t\tname: x.name,\n\t\t\t\tcategory: x.category,\n\t\t\t\tmodel: x.model,\n\t\t\t\tprice: x.price,\n\t\t\t\tnewPrice: x.price + 50\n\t\t\t}\n\t\t})\n\t\t.from(Products)\n\t\t.where(function(x) {\n\t\t\treturn (x.price + 50) \u003e 500;\n\t\t}).take(10)\n\t.orderByDescending(function(x) { \n\t\treturn {orderDate: x.orderDate }; \n\t}).take(25);\n\tconst SQL = new SqlFormatter().format(query);\n\t\nSQL \u003e `SELECT ProductData.id AS id, ProductData.name AS name, ProductData.category AS category, ProductData.model AS model, ProductData.price AS price, (ProductData.price + 50) AS newPrice FROM ProductData WHERE (ProductData.price + 50) \u003e 500 ORDER BY ProductData.orderDate DESC LIMIT 25`\n\n### subtract()\n\n\tconst Products = new QueryEntity('ProductData');\n\tlet query = new QueryExpression()\n\t\t.select(function(x) {\n\t\t\treturn {\n\t\t\t\tid: x.id,\n\t\t\t\tname: x.name,\n\t\t\t\tcategory: x.category,\n\t\t\t\tmodel: x.model,\n\t\t\t\tprice: x.price,\n\t\t\t\tnewPrice: x.price - 100\n\t\t\t}\n\t\t})\n\t\t.from(Products)\n\t\t.where(function(x) {\n\t\t\treturn (x.price - 100) \u003e 500;\n\t\t}).take(10)\n\t.orderByDescending(function(x) { \n\t\treturn {orderDate: x.orderDate }; \n\t}).take(25);\n\tconst SQL = new SqlFormatter().format(query);\n\nSQL \u003e `SELECT ProductData.id AS id, ProductData.name AS name, ProductData.category AS category, ProductData.model AS model, ProductData.price AS price, (ProductData.price - 100) AS newPrice FROM ProductData WHERE (ProductData.price - 100) \u003e 500 ORDER BY ProductData.orderDate DESC LIMIT 25`\n\n### multiply()\n\n\tconst Products = new QueryEntity('ProductData');\n\tlet query = new QueryExpression()\n\t\t.select((x) =\u003e {\n\t\t\treturn {\n\t\t\t\tid: x.id,\n\t\t\t\tname: x.name,\n\t\t\t\tcategory: x.category,\n\t\t\t\tmodel: x.model,\n\t\t\t\tprice: x.price,\n\t\t\t\tnewPrice: x.price * 0.75\n\t\t\t}\n\t\t})\n\t\t.from(Products)\n\t\t.where((x) =\u003e {\n\t\t\treturn x.price \u003e 500;\n\t\t})\n\t\t.take(10);\n\t\tconst SQL = new SqlFormatter().format(query);\n\nSQL \u003e `SELECT ProductData.id AS id, ProductData.name AS name, ProductData.category AS category, ProductData.model AS model, ProductData.price AS price, (ProductData.price * 0.75) AS newPrice FROM ProductData WHERE ProductData.price \u003e 500 LIMIT 10`\n\n### divide()\n\n\tconst Products = new QueryEntity('ProductData');\n\tlet query = new QueryExpression()\n    .select((x) =\u003e {\n        return {\n            id: x.id,\n            name: x.name,\n            category: x.category,\n            model: x.model,\n            price: x.price,\n            newPrice: x.price / 1.5\n        }\n    })\n    .from(Products)\n    .where((x) =\u003e {\n        return x.price \u003e 500;\n    })\n    .take(10);\n\tconst SQL = new SqlFormatter().format(query);\n\nSQL \u003e `SELECT ProductData.id AS id, ProductData.name AS name, ProductData.category AS category, ProductData.model AS model, ProductData.price AS price, (ProductData.price / 1.5) AS newPrice FROM ProductData WHERE ProductData.price \u003e 500 LIMIT 10`\n\n### floor()\n\nThe floor() function always rounds down and returns the largest integer less than or equal to a given number.\n\n\tconst Products = new QueryEntity('ProductData');\n\tlet query = new QueryExpression()\n    .select((x) =\u003e {\n        x.id,\n        x.name,\n        x.category,\n        x.model,\n        x.price\n    })\n    .from(Products)\n    .where((x) =\u003e {\n        return Math.floor(x.price) === 877;\n    })\n    .orderBy((x) =\u003e x.price)\n    .take(10);\n\tconst SQL = new SqlFormatter().format(query);\n\nSQL \u003e `SELECT ProductData.id, ProductData.name, ProductData.category, ProductData.model, ProductData.price FROM ProductData WHERE FLOOR(ProductData.price) = 877 ORDER BY ProductData.price ASC LIMIT 10`\n\n### ceil()\n\nThe ceil() function always rounds up and returns the smaller integer greater than or equal to a given number.\n\n\tconst Products = new QueryEntity('ProductData');\n\tlet query = new QueryExpression()\n    .select((x) =\u003e {\n        return {\n            id: x.id,\n            name: x.name,\n            category: x.category,\n            model: x.model,\n            price: x.price,\n            alternatePrice: Math.ceil(x.price)\n        }\n    })\n    .from(Products)\n    .where((x) =\u003e {\n        return Math.ceil(x.price) === 878;\n    })\n    .orderBy((x) =\u003e x.price)\n    .take(10);\n\tconst SQL = new SqlFormatter().format(query);\n\nSQL \u003e `SELECT ProductData.id AS id, ProductData.name AS name, ProductData.category AS category, ProductData.model AS model, ProductData.price AS price, CEILING(ProductData.price) AS alternatePrice FROM ProductData WHERE CEILING(ProductData.price) = 878 ORDER BY ProductData.price ASC LIMIT 10`\n\n### round()\n\nThe round() function returns the value of a number rounded to the nearest number.\n\n\tconst {QueryExpression, SqlFormatter, QueryEntity, QueryField, round} = require(\"@themost/query\");\n\tconst Products = new QueryEntity('ProductData');\n\tlet query = new QueryExpression()\n\t\t.select((x) =\u003e {\n\t\t\treturn {\n\t\t\t\tid: x.id,\n\t\t\t\tname: x.name,\n\t\t\t\tcategory: x.category,\n\t\t\t\tmodel: x.model,\n\t\t\t\tprice: x.price,\n\t\t\t\tnewPrice: round(x.price, 1)\n\t\t\t}\n\t\t})\n\t\t.from(Products)\n\t\t.where((x) =\u003e {\n\t\t\treturn x.price \u003e 500;\n\t\t})\n\t\t.take(10);\n\tconst SQL = new SqlFormatter().format(query);\n\nSQL \u003e `SELECT ProductData.id AS id, ProductData.name AS name, ProductData.category AS category, ProductData.model AS model, ProductData.price AS price, ROUND(ProductData.price,1) AS newPrice FROM ProductData WHERE ProductData.price \u003e 500 LIMIT 10`\n\n### max()\n\nThe max() function returns the largest value of the selected column.\n\n\tconst {QueryExpression, SqlFormatter, QueryEntity, QueryField, round, max} = require(\"@themost/query\");\n\tconst Products = new QueryEntity('ProductData');\n\tlet query = new QueryExpression()\n\t\t.select((x) =\u003e {\n\t\t\treturn {\n\t\t\t\tmaxPrice: max(x.price)\n\t\t\t}\n\t\t})\n\t\t.from(Products)\n\t\t.where((x) =\u003e {\n\t\t\treturn x.category === 'Laptops';\n\t\t})\n\t\t.take(1);\n\nSQL \u003e `SELECT MAX(ProductData.price) AS maxPrice FROM ProductData WHERE ProductData.category = 'Laptops' LIMIT 1`\n\n### min()\n\nThe min() function returns the smallest value of the selected column.\n\n\tonst {QueryExpression, SqlFormatter, QueryEntity, QueryField, min} = require(\"@themost/query\");\n\tconst Products = new QueryEntity('ProductData');\n\tlet query = new QueryExpression()\n\t\t.select((x) =\u003e {\n\t\t\treturn {\n\t\t\t\tminPrice: min(x.price)\n\t\t\t}\n\t\t})\n\t\t.from(Products)\n\t\t.where((x) =\u003e {\n\t\t\treturn x.category === 'Laptops';\n\t\t})\n\t\t.take(1);\n\tconst SQL = new SqlFormatter().format(query);\n\nSQL \u003e `SELECT MIN(ProductData.price) AS minPrice FROM ProductData WHERE ProductData.category = 'Laptops' LIMIT 1`\n\n### count()\n\nThe count() function returns the number of rows that matches a specified criteria.\n\n\tconst {QueryExpression, SqlFormatter, QueryEntity, QueryField, count} = require(\"@themost/query\");\n\tconst Products = new QueryEntity('ProductData');\n\tlet query = new QueryExpression()\n\t\t.select((x) =\u003e {\n\t\t\treturn {\n\t\t\t\tcategory: x.category,\n\t\t\t\ttotal: count(x.id)\n\t\t\t}\n\t\t})\n\t\t.from(Products)\n\t\t.groupBy((x) =\u003e x.category);\n\tconst SQL = new SqlFormatter().format(query);\n\nSQL \u003e `SELECT ProductData.category AS category, COUNT(ProductData.id) AS total FROM ProductData GROUP BY ProductData.category`\n\n### avg()\n\nThe avg() function returns the average value of a numeric column. \n\n\tconst {QueryExpression, SqlFormatter, QueryEntity, QueryField, round, avg} = require(\"@themost/query\");\n\tconst Products = new QueryEntity('ProductData');\n\tlet query = new QueryExpression()\n\t\t.select((x) =\u003e {\n\t\t\treturn {\n\t\t\t\tavgPrice: round(avg(x.price),2)\n\t\t\t}\n\t\t})\n\t\t.from(Products)\n\t\t.where((x) =\u003e {\n\t\t\treturn x.category === 'Laptops';\n\t\t})\n\t\t.take(1);\n\tconst SQL = new SqlFormatter().format(query);\n\nSQL \u003e `SELECT ROUND(AVG(ProductData.price),2) AS avgPrice FROM ProductData WHERE ProductData.category = 'Laptops' LIMIT 1`\n\n### sum()\n\nThe sum() function returns the total sum of a numeric column. \n\n\tconst {QueryExpression, SqlFormatter, QueryEntity, QueryField, round, sum} = require(\"@themost/query\");\n\tconst Orders = new QueryEntity('OrderData');\n\tconst Products = new QueryEntity('ProductData').as('orderedItem');\n\tlet query = new QueryExpression()\n\t\t.select((x) =\u003e {\n\t\t\treturn {\n\t\t\t\ttotal: round(sum(x.orderedItem.price),2)\n\t\t\t}\n\t\t})\n\t\t.from(Orders)\n\t\t.join(Products).with((x, y) =\u003e {\n\t\t\treturn x.orderedItem === y.id;\n\t\t})\n\t\t.where((x) =\u003e {\n\t\t\treturn x.orderedItem.category === 'Laptops';\n\t\t})\n\t\t.take(1);\n\tconst SQL = new SqlFormatter().format(query);\n\nSQL \u003e `SELECT ROUND(SUM(orderedItem.price),2) AS total FROM OrderData INNER JOIN ProductData AS orderedItem ON OrderData.orderedItem = orderedItem.id WHERE orderedItem.category = 'Laptops' LIMIT 1`\n\n## SQL Date Functions\n\n### getFullYear()\n\nThe getFullYear() method returns the year of the specified date according to local time.\n\n\tconst {QueryExpression, SqlFormatter, QueryEntity, QueryField, round, sum} = require(\"@themost/query\");\n\tconst Orders = new QueryEntity('OrderData');\n\tconst Products = new QueryEntity('ProductData').as('orderedItem');\n\tlet query = new QueryExpression()\n\t\t.select((x) =\u003e {\n\t\t\treturn {\n\t\t\t\ttotal: round(sum(x.orderedItem.price),2)\n\t\t\t}\n\t\t})\n\t\t.from(Orders)\n\t\t.join(Products).with((x, y) =\u003e {\n\t\t\treturn x.orderedItem === y.id;\n\t\t})\n\t\t.where((x) =\u003e {\n\t\t\treturn x.orderedItem.category === 'Laptops' \u0026\u0026\n\t\t\t\tx.orderDate.getFullYear() === 2019\n\t\t});\n\tconst SQL = new SqlFormatter().format(query);\n\nSQL \u003e `SELECT ROUND(SUM(orderedItem.price),2) AS total FROM OrderData INNER JOIN ProductData AS orderedItem ON OrderData.orderedItem = orderedItem.id WHERE (orderedItem.category = 'Laptops' AND YEAR(OrderData.orderDate) = 2019)`\n\n### getMonth()\n\nThe getMonth() method returns the month in the specified date according to local time\n\n\tconst {QueryExpression, SqlFormatter, QueryEntity, QueryField, count} = require(\"@themost/query\");\n\tconst Orders = new QueryEntity('OrderData');\n\tconst Products = new QueryEntity('ProductData').as('orderedItem');\n\tlet query = new QueryExpression()\n\t\t.select((x) =\u003e {\n\t\t\treturn {\n\t\t\t\tmonth: x.orderDate.getMonth(),\n\t\t\t\ttotal: count(x.id)\n\t\t\t}\n\t\t})\n\t\t.from(Orders)\n\t\t.groupBy((x) =\u003e x.orderDate.getMonth())\n\t\t.where((x) =\u003e {\n\t\t\treturn x.orderDate.getFullYear() === 2019;\n\t\t});\n\tconst SQL = new SqlFormatter().format(query);\n\nSQL \u003e `SELECT MONTH(OrderData.orderDate) AS month, COUNT(OrderData.id) AS total FROM OrderData WHERE YEAR(OrderData.orderDate) = 2019 GROUP BY MONTH(OrderData.orderDate)`\n\n### getDate()\n\nThe getDate() method returns the day of the month for the specified date according to local time.\n\n\tconst {QueryExpression, SqlFormatter, QueryEntity, QueryField, count} = require(\"@themost/query\");\n\tconst Orders = new QueryEntity('OrderData');\n\tconst Products = new QueryEntity('ProductData').as('orderedItem');\n\tlet query = new QueryExpression()\n\t\t.select((x) =\u003e {\n\t\t\treturn {\n\t\t\t\tdayOfMonth: x.orderDate.getDate(),\n\t\t\t\ttotal: count(x.id)\n\t\t\t}\n\t\t})\n\t\t.from(Orders)\n\t\t.groupBy((x) =\u003e x.orderDate.getDate())\n\t\t.where((x) =\u003e {\n\t\t\treturn x.orderDate.getFullYear() === 2019 \u0026\u0026 x.orderDate.getMonth() === 1;\n\t\t});\n\tconst SQL = new SqlFormatter().format(query);\n\nSQL \u003e `SELECT DAY(OrderData.orderDate) AS dayOfMonth, COUNT(OrderData.id) AS total FROM OrderData WHERE (YEAR(OrderData.orderDate) = 2019 AND MONTH(OrderData.orderDate) = 1) GROUP BY DAY(OrderData.orderDate)`\n\n### getHours()\n\nThe getHours() method returns the hour for the specified date, according to local time.\n\n\tconst {QueryExpression, SqlFormatter, QueryEntity, QueryField, count} = require(\"@themost/query\");\n\tconst Orders = new QueryEntity('OrderData');\n\tlet query = new QueryExpression()\n\t\t.select((x) =\u003e {\n\t\t\treturn {\n\t\t\t\thours: x.orderDate.getHours(),\n\t\t\t\tminutes: x.orderDate.getMinutes(),\n\t\t\t\tseconds: x.orderDate.getSeconds(),\n\t\t\t}\n\t\t})\n\t\t.from(Orders)\n\t\t.where((x) =\u003e {\n\t\t\treturn x.orderDate.getFullYear() === 2019 \u0026\u0026 x.orderDate.getMonth() === 1;\n\t\t});\n\tconst SQL = new SqlFormatter().format(query);\n\nSQL \u003e `SELECT HOUR(OrderData.orderDate) AS hours, MINUTE(OrderData.orderDate) AS minutes, SECOND(OrderData.orderDate) AS seconds FROM OrderData WHERE (YEAR(OrderData.orderDate) = 2019 AND MONTH(OrderData.orderDate) = 1)`\n\n### getMinutes()\n\nThe getMinutes() method returns the minutes in the specified date according to local time.\n\n\tconst {QueryExpression, SqlFormatter, QueryEntity, QueryField, count} = require(\"@themost/query\");\n\tconst Orders = new QueryEntity('OrderData');\n\tlet query = new QueryExpression()\n\t\t.select((x) =\u003e {\n\t\t\treturn {\n\t\t\t\thours: x.orderDate.getHours(),\n\t\t\t\tminutes: x.orderDate.getMinutes(),\n\t\t\t\tseconds: x.orderDate.getSeconds(),\n\t\t\t}\n\t\t})\n\t\t.from(Orders)\n\t\t.where((x) =\u003e {\n\t\t\treturn x.orderDate.getFullYear() === 2019 \u0026\u0026 x.orderDate.getMonth() === 1;\n\t\t});\n\tconst SQL = new SqlFormatter().format(query);\n\nSQL \u003e `SELECT HOUR(OrderData.orderDate) AS hours, MINUTE(OrderData.orderDate) AS minutes, SECOND(OrderData.orderDate) AS seconds FROM OrderData WHERE (YEAR(OrderData.orderDate) = 2019 AND MONTH(OrderData.orderDate) = 1)`\n\n### getSeconds()\n\nThe getSeconds() method returns the seconds in the specified date according to local time.\n\n\tconst {QueryExpression, SqlFormatter, QueryEntity, QueryField, count} = require(\"@themost/query\");\n\tconst Orders = new QueryEntity('OrderData');\n\tlet query = new QueryExpression()\n\t\t.select((x) =\u003e {\n\t\t\treturn {\n\t\t\t\thours: x.orderDate.getHours(),\n\t\t\t\tminutes: x.orderDate.getMinutes(),\n\t\t\t\tseconds: x.orderDate.getSeconds(),\n\t\t\t}\n\t\t})\n\t\t.from(Orders)\n\t\t.where((x) =\u003e {\n\t\t\treturn x.orderDate.getFullYear() === 2019 \u0026\u0026 x.orderDate.getMonth() === 1;\n\t\t});\n\tconst SQL = new SqlFormatter().format(query);\n\nSQL \u003e `SELECT HOUR(OrderData.orderDate) AS hours, MINUTE(OrderData.orderDate) AS minutes, SECOND(OrderData.orderDate) AS seconds FROM OrderData WHERE (YEAR(OrderData.orderDate) = 2019 AND MONTH(OrderData.orderDate) = 1)`\n\n## SQL String Functions\n\n### startsWith()\n\nThe startsWith() method determines whether a string begins with the characters of a specified string, returning true or false as appropriate.\n\n\tconst {QueryExpression, SqlFormatter, QueryEntity} = require(\"@themost/query\");\n\tconst People = new QueryEntity('PersonData');\n\tlet query = new QueryExpression()\n\t\t.select((x) =\u003e {\n\t\t\tx.id,\n\t\t\tx.familyName,\n\t\t\tx.givenName,\n\t\t\tx.email,\n\t\t\tx.dateCreated\n\t\t})\n\t\t.from(People)\n\t\t.where((x) =\u003e {\n\t\t\treturn x.familyName.startsWith('Cam') === true;\n\t\t});\n\tconst SQL = new SqlFormatter().format(query);\n\nSQL \u003e `SELECT PersonData.id, PersonData.familyName, PersonData.givenName, PersonData.email, PersonData.dateCreated FROM PersonData WHERE (PersonData.familyName REGEXP '^Cam') = true`\n\n### endsWith()\n\nThe endsWith() method determines whether a string ends with the characters of a specified string, returning true or false as appropriate.\n\n\tconst {QueryExpression, SqlFormatter, QueryEntity} = require(\"@themost/query\");\n\tconst People = new QueryEntity('PersonData');\n\tlet query = new QueryExpression()\n\t\t.select((x) =\u003e {\n\t\t\tx.id,\n\t\t\tx.familyName,\n\t\t\tx.givenName,\n\t\t\tx.email,\n\t\t\tx.dateCreated\n\t\t})\n\t\t.from(People)\n\t\t.where((x) =\u003e {\n\t\t\treturn x.familyName.endsWith('er') === true;\n\t\t});\n\tconst SQL = new SqlFormatter().format(query);\n\nSQL \u003e `SELECT PersonData.id, PersonData.familyName, PersonData.givenName, PersonData.email, PersonData.dateCreated FROM PersonData WHERE (PersonData.familyName REGEXP 'er$$') = true`\n\n### includes()\n\nThe includes() method performs a search to determine whether one string may be found within another string, returning true or false as appropriate.\n\n\tconst {QueryExpression, SqlFormatter, QueryEntity} = require(\"@themost/query\");\n\tconst People = new QueryEntity('PersonData');\n\tlet query = new QueryExpression()\n\t\t.select((x) =\u003e {\n\t\t\tx.id,\n\t\t\tx.familyName,\n\t\t\tx.givenName,\n\t\t\tx.email,\n\t\t\tx.dateCreated\n\t\t})\n\t\t.from(People)\n\t\t.where((x) =\u003e {\n\t\t\treturn x.givenName.includes('Chris') === true;\n\t\t});\n\tconst SQL = new SqlFormatter().format(query);\n\nSQL \u003e `SELECT PersonData.id, PersonData.familyName, PersonData.givenName, PersonData.email, PersonData.dateCreated FROM PersonData WHERE (PersonData.givenName REGEXP 'Chris') = true`\n\n### toUpperCase()\n\n\tconst {QueryExpression, SqlFormatter, QueryEntity} = require(\"@themost/query\");\n\tconst People = new QueryEntity('PersonData');\n\tlet query = new QueryExpression()\n\t\t.select((x) =\u003e {\n\t\t\treturn {\n\t\t\t\tid: x.id,\n\t\t\t\tfamilyName: x.familyName,\n\t\t\t\tgivenName1: x.givenName.toUpperCase(),\n\t\t\t\tgivenName: x.givenName\n\t\t\t}\n\t\t})\n\t\t.from(People)\n\t\t.where((x) =\u003e {\n\t\t\treturn x.givenName.toUpperCase() === 'CHRISTOPHER';\n\t\t});\n\tconst SQL = new SqlFormatter().format(query);\n\nSQL \u003e `SELECT PersonData.id AS id, PersonData.familyName AS familyName, UPPER(PersonData.givenName) AS givenName1, PersonData.givenName AS givenName FROM PersonData WHERE UPPER(PersonData.givenName) = 'CHRISTOPHER'`\n\n### substr()\n\n\tconst {QueryExpression, SqlFormatter, QueryEntity} = require(\"@themost/query\");\n\tconst People = new QueryEntity('PersonData');\n\tlet query = new QueryExpression()\n\t.select((x) =\u003e {\n\t\treturn {\n\t\t\tid: x.id,\n\t\t\tfamilyName: x.familyName,\n\t\t\tgivenName1: x.givenName.substr(0,4),\n\t\t\tgivenName: x.givenName\n\t\t}\n\t})\n\t.from(People)\n\t.where((x) =\u003e {\n\t\treturn x.givenName.substr(0,4) === 'Chri';\n\t});\n\tconst SQL = new SqlFormatter().format(query);\n\nSQL \u003e `SELECT PersonData.id AS id, PersonData.familyName AS familyName, SUBSTRING(PersonData.givenName,1,4) AS givenName1, PersonData.givenName AS givenName FROM PersonData WHERE SUBSTRING(PersonData.givenName,1,4) = 'Chri'`\n\n### concat()\n\nThe concat() method concatenates the string arguments to the calling string and returns a new string.\n\n\tconst {QueryExpression, SqlFormatter, QueryEntity} = require(\"@themost/query\");\n\tconst People = new QueryEntity('PersonData');\n\tlet query = new QueryExpression()\n\t\t.select((x) =\u003e {\n\t\t\treturn {\n\t\t\t\tid: x.id,\n\t\t\t\tfamilyName: x.familyName,\n\t\t\t\tgivenName: x.givenName,\n\t\t\t\tname: x.givenName.concat(' ', x.familyName)\n\t\t\t}\n\t\t})\n\t\t.from(People)\n\t\t.where((x) =\u003e {\n\t\t\treturn x.givenName.indexOf('Chri') \u003e= 0;\n\t\t});\n\tconst SQL = new SqlFormatter().format(query);\n\nSQL \u003e `SELECT PersonData.id AS id, PersonData.familyName AS familyName, PersonData.givenName AS givenName, CONCAT(PersonData.givenName, ' ', PersonData.familyName) AS name FROM PersonData WHERE (LOCATE('Chri',PersonData.givenName)-1) \u003e= 0`\n\n### indexOf()\n\nThe indexOf() method, given one argument: a substring to search for, searches the entire calling string, and returns the index of the first occurrence of the specified substring.\n\n\tconst {QueryExpression, SqlFormatter, QueryEntity} = require(\"@themost/query\");\n\tconst People = new QueryEntity('PersonData');\n\tlet query = new QueryExpression()\n\t\t.select((x) =\u003e {\n\t\t\treturn {\n\t\t\t\tid: x.id,\n\t\t\t\tfamilyName: x.familyName,\n\t\t\t\tgivenName: x.givenName\n\t\t\t}\n\t\t})\n\t\t.from(People)\n\t\t.where((x) =\u003e {\n\t\t\treturn x.givenName.indexOf('Chri') \u003e= 0;\n\t\t});\n\tconst SQL = new SqlFormatter().format(query);\n\nSQL \u003e `SELECT PersonData.id AS id, PersonData.familyName AS familyName, PersonData.givenName AS givenName FROM PersonData WHERE (LOCATE('Chri',PersonData.givenName)-1) \u003e= 0`\n\n\n\n## Development\n\n    git clone http://github.com/themost-framework/query.git\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthemost-framework%2Fquery","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthemost-framework%2Fquery","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthemost-framework%2Fquery/lists"}