{"id":22382363,"url":"https://github.com/jcoreio/sequelize-sql-tag","last_synced_at":"2025-07-01T05:03:37.546Z","repository":{"id":33153755,"uuid":"153009440","full_name":"jcoreio/sequelize-sql-tag","owner":"jcoreio","description":"a template tag for Sequelize that quotes Models' table names, attribute names, and puts other expressions into bind parameters","archived":false,"fork":false,"pushed_at":"2023-07-25T19:34:06.000Z","size":2094,"stargazers_count":1,"open_issues_count":18,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-06-05T14:50:28.005Z","etag":null,"topics":["parameterized-queries","parameterized-query","raw-sql","sequelize","sql","tagged-template-literal"],"latest_commit_sha":null,"homepage":"","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/jcoreio.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","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":"2018-10-14T19:13:15.000Z","updated_at":"2023-07-25T19:32:59.000Z","dependencies_parsed_at":"2025-02-01T01:44:13.700Z","dependency_job_id":"8d5709e7-b9a3-4131-b252-5cca85efd78d","html_url":"https://github.com/jcoreio/sequelize-sql-tag","commit_stats":null,"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"purl":"pkg:github/jcoreio/sequelize-sql-tag","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jcoreio%2Fsequelize-sql-tag","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jcoreio%2Fsequelize-sql-tag/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jcoreio%2Fsequelize-sql-tag/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jcoreio%2Fsequelize-sql-tag/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jcoreio","download_url":"https://codeload.github.com/jcoreio/sequelize-sql-tag/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jcoreio%2Fsequelize-sql-tag/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260409159,"owners_count":23004637,"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":["parameterized-queries","parameterized-query","raw-sql","sequelize","sql","tagged-template-literal"],"created_at":"2024-12-05T00:12:42.341Z","updated_at":"2025-07-01T05:03:37.475Z","avatar_url":"https://github.com/jcoreio.png","language":"TypeScript","readme":"# @jcoreio/sequelize-sql-tag\n\n[![CircleCI](https://circleci.com/gh/jcoreio/sequelize-sql-tag)](https://circleci.com/gh/jcoreio/sequelize-sql-tag)\n[![Build Status](https://travis-ci.org/jcoreio/sequelize-sql-tag.svg?branch=master)](https://travis-ci.org/jcoreio/sequelize-sql-tag)\n[![Coverage Status](https://codecov.io/gh/jcoreio/sequelize-sql-tag/branch/master/graph/badge.svg)](https://codecov.io/gh/jcoreio/sequelize-sql-tag)\n[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)\n[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)\n\na template tag for [Sequelize](docs.sequelizejs.com) that quotes `Model`s' table\nnames, attribute names, and puts other expressions into bind parameters\n\nUsing the table and attribute names from your Sequelize `Model`s is much more\nrefactor-proof in raw queries than embedding raw identifiers.\n\n# Installation\n\n```sh\npnpm install --save @jcoreio/sequelize-sql-tag\n```\n\n# Compatibility\n\nRequires `sequelize@^4.0.0`. Once v5 is released I'll check if it's still\ncompatible. Not making any effort to support versions \u003c 4, but you're welcome\nto make a PR.\n\n# Examples\n\n```js\nconst Sequelize = require('sequelize')\nconst sql = require('@jcoreio/sequelize-sql-tag')\nconst sequelize = new Sequelize('test', 'test', 'test', { dialect: 'postgres', logging: false })\n\nconst User = sequelize.define('User', {\n  name: {type: Sequelize.STRING},\n  birthday: {type: Sequelize.STRING},\n  active: {type: Sequelize.BOOLEAN},\n})\n\nconst lock = true\n\nsequelize.query(...sql`SELECT ${User.rawAttributes.name} FROM ${User}\nWHERE ${User.rawAttributes.birthday} = ${new Date('2346-7-11')} AND\n  ${User.rawAttributes.active} = ${true}\n  ${lock ? sql`FOR UPDATE` : sql``}then(console.log);\n// =\u003e [ [ { name: 'Jimbob' } ], Statement { sql: 'SELECT \"name\" FROM \"Users\" WHERE \"birthday\" = $1 AND \"active\" = $2 FOR UPDATE' } ]\n```\n\nSometimes custom subqueries within a Sequelize `where` clause can be useful.\nIn this case, there is no way to use query parameters. You can use\n`sql.escape` in this context to inline the escaped values rather than using\nquery parameters:\n\n```js\nconst { Op } = Sequelize\n\nconst User = sequelize.define('User', {\n  name: { type: Sequelize.STRING },\n})\nconst Organization = sequelize.define('Organization', {\n  name: { type: Sequelize.STRING },\n})\nconst OrganizationMember = sequelize.define('OrganizationMember', {\n  userId: { type: Sequelize.INTEGER },\n  organizationId: { type: Sequelize.INTEGER },\n})\nUser.belongsToMany(Organization, { through: OrganizationMember })\nOrganization.belongsToMany(User, { through: OrganizationMember })\n\nasync function getUsersInOrganization(organizationId, where = {}) {\n  return await User.findAll({\n    where: {\n      ...where,\n      // Using a sequelize include clause to do this kind of sucks tbh\n      id: {\n        [Op.in]: Sequelize.literal(sql.escape`\n        SELECT ${OrganizationMember.rawAttributes.userId}\n        FROM ${OrganizationMember}\n        WHERE ${OrganizationMember.rawAttributes.organizationId} = ${organizationId}\n      `),\n      },\n      // SELECT \"userId\" FROM \"OrganizationMembers\" WHERE \"organizationId\" = 2\n    },\n  })\n}\n```\n\n# API\n\n## `` sql`query` ``\n\nCreates arguments for `sequelize.query`.\n\n### Expressions you can embed in the template\n\n#### Sequelize `Model` class\n\nWill be interpolated to the model's `tableName`.\n\n#### Model attribute (e.g. `User.rawAttributes.id`)\n\nWill be interpolated to the column name for the attribute\n\n#### `` sql`nested` ``\n\nGood for conditionally including a SQL clause (see examples above)\n\n#### `Sequelize.literal(...)`\n\nText will be included as-is\n\n#### Arrays of `values` tagged template literals\n\nWill be included as-is joined by commas.\n\n#### All other values\n\nWill be added to bind parameters.\n\n### Returns (`[string, {bind: Array\u003cstring\u003e}]`)\n\nThe `sql, options` arguments to pass to `sequelize.query`.\n\n## `` sql.escape`query` ``\n\nCreates a raw SQL string with all expressions in the template escaped.\n\n### Expressions you can embed in the template\n\n#### Sequelize `Model` class\n\nWill be interpolated to the model's `tableName`.\n\n#### Model attribute (e.g. `User.rawAttributes.id`)\n\nWill be interpolated to the column name for the attribute\n\n#### `` sql`nested` ``\n\nGood for conditionally including a SQL clause (see examples above)\n\n#### `Sequelize.literal(...)`\n\nText will be included as-is\n\n#### Arrays of `values` tagged template literals\n\nWill be included as-is joined by commas.\n\n#### All other values\n\nWill be escaped with `QueryGenerator.escape(...)`. If none of the expressions\nis a Sequelize `Model` class, attribute, `Sequelize` instance, or nested `` sql`query` `` containing\nsuch, then an error will be thrown.\n\n### Returns (`string`)\n\nThe raw SQL.\n\n## `sql.with(sequelize)`\n\nReturns an interface using the `QueryGenerator` from the given `Sequelize` instance.\nThe returned interface has the following tagged template literals:\n\n#### `` escape`query` ``\n\nJust like `sql.escape`, but doesn't require any of the expressions to be a Sequelize `Model` class\nor attribute.\n\n#### `` values`sql` ``\n\nUsed for building `VALUES` lists. Only works inside an array expression.\nThe items will be included as-is joined by commas. For example:\n\n```js\nconst users = [\n  { name: 'Jim', birthday: 'Jan 1 2020' },\n  { name: 'Bob', birthday: 'Jan 2 1986' },\n]\nconst { escape, values } = sql.with(sequelize)\nescape`\nINSERT INTO ${User}\n  ${User.rawAttributes.name}, ${User.rawAttributes.birthday}\n  VALUES ${users.map(({ name, birthday }) =\u003e values`(${name}, ${birthday})`)}\n`\n// returns `INSERT INTO \"Users\" \"name\", \"birthday\" VALUES ('Jim', 'Jan 1 2020'), ('Bob', 'Jan 2 1986')`\n```\n\n#### `` literal`sql` ``\n\nLike `sql.escape`, but wraps the escaped SQL in `Sequelize.literal`.\n\n#### `` query`sql` ``\n\nReturns a function that executes the query. Example:\n\n```js\nconst Sequelize = require('sequelize')\nconst sql = require('@jcoreio/sequelize-sql-tag')\nconst sequelize = new Sequelize('test', 'test', 'test', {\n  dialect: 'postgres',\n  logging: false,\n})\n\nconst User = sequelize.define('User', {\n  name: { type: Sequelize.STRING },\n})\n\nasync function insertUser(user) {\n  const { query } = sql.with(sequelize)\n  await query`\n    INSERT INTO ${User} ${User.rawAttributes.name} VALUES (${user.name});\n  `({ transaction })\n}\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjcoreio%2Fsequelize-sql-tag","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjcoreio%2Fsequelize-sql-tag","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjcoreio%2Fsequelize-sql-tag/lists"}