{"id":20514052,"url":"https://github.com/webreflection/sqlite-tag","last_synced_at":"2025-04-14T00:05:02.436Z","repository":{"id":65993274,"uuid":"236301823","full_name":"WebReflection/sqlite-tag","owner":"WebReflection","description":"Template literal tag based sqlite3 queries.","archived":false,"fork":false,"pushed_at":"2022-02-19T17:03:11.000Z","size":25,"stargazers_count":56,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-14T00:04:59.174Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"isc","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/WebReflection.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-01-26T11:19:40.000Z","updated_at":"2024-06-30T17:11:22.000Z","dependencies_parsed_at":"2023-03-10T23:27:06.558Z","dependency_job_id":null,"html_url":"https://github.com/WebReflection/sqlite-tag","commit_stats":null,"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WebReflection%2Fsqlite-tag","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WebReflection%2Fsqlite-tag/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WebReflection%2Fsqlite-tag/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WebReflection%2Fsqlite-tag/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/WebReflection","download_url":"https://codeload.github.com/WebReflection/sqlite-tag/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248799954,"owners_count":21163404,"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":"2024-11-15T21:14:33.412Z","updated_at":"2025-04-14T00:05:02.409Z","avatar_url":"https://github.com/WebReflection.png","language":"JavaScript","readme":"# sqlite-tag\n\n[![Build Status](https://travis-ci.com/WebReflection/sqlite-tag.svg?branch=master)](https://travis-ci.com/WebReflection/sqlite-tag) [![Coverage Status](https://coveralls.io/repos/github/WebReflection/sqlite-tag/badge.svg?branch=master)](https://coveralls.io/github/WebReflection/sqlite-tag?branch=master)\n\n\u003csup\u003e**Social Media Photo by [Alexander Sinn](https://unsplash.com/@swimstaralex) on [Unsplash](https://unsplash.com/)**\u003c/sup\u003e\n\nTemplate literal tag based sqlite3 queries.\n\nAvailable for [PostgreSQL](https://github.com/WebReflection/pg-tag/#readme) too.\n\n```js\nconst sqlite3 = require('sqlite3').verbose();\nconst SQLiteTag = require('sqlite-tag');\n\nconst db = new sqlite3.Database(':memory:');\nconst {all, get, query, raw, transaction} = SQLiteTag(db);\n\n(async () =\u003e {\n  console.log('✔', 'table creation');\n  await query`CREATE TABLE ${raw`lorem`} (info TEXT)`;\n\n  console.log('✔', 'multiple inserts (transaction)');\n  const insert = transaction();\n  for (let i = 0; i \u003c 10; i++)\n    insert`INSERT INTO lorem VALUES (${'Ipsum ' + i})`;\n  await insert.commit();\n\n  console.log('✔', 'Single row');\n  const row = await get`\n    SELECT rowid AS id, info\n    FROM ${raw`lorem`}\n    WHERE info = ${'Ipsum 5'}\n  `;\n  console.log(' ', row.id + \": \" + row.info);\n\n  console.log('✔', 'Multiple rows');\n  const TABLE = 'lorem';\n  const rows = await all`SELECT rowid AS id, info FROM ${raw`${TABLE}`}`;\n  for (const row of rows)\n    console.log(' ', row.id + \": \" + row.info);\n\n  // automatically and safely expanded as ?, ?\n  const list = ['Ipsum 2', 'Ipsum 3'];\n  console.log('✔', 'IN clause');\n  console.log(' ', await all`SELECT * FROM lorem WHERE info IN (${list})`);\n\n  console.log('✔', 'Error handling');\n  try {\n    await query`INSERT INTO shenanigans VALUES (1, 2, 3)`;\n  }\n  catch ({code}) {\n    console.log(' ', code);\n  }\n\n  db.close();\n})();\n\n```\n\n## API\n\nEvery exported method can be used either as function or as template literal tag.\n\n  * `all` to retrieve all rows that match the query\n  * `get` to retrieve one row that matches the query\n  * `query` to simply query the database\n  * `raw` to enable raw interpolations within the query string (see next)\n\n### The `raw` utility\n\nEvery hole within the template literal will be passed as query parameter.\nIn some case though, we might need to define at runtime some part of the query, without it being necessarily a parameter.\n\nThis utility aim is to provide a mechanism that would not affect the query itself, or its parameters.\n\n```js\n// will insert into table_0, table_1, and table_2 respective `i` values\nfor (let i = 0; i \u003c 3; i++)\n  await query`INSERT INTO ${raw`table_${i}`} VALUES (${i})`;\n```\n\nThe resulting operation, behind the scene, would be the following one:\n```js\ndb.run('INSERT INTO table_0 VALUES (?)', [0]);\ndb.run('INSERT INTO table_1 VALUES (?)', [1]);\ndb.run('INSERT INTO table_2 VALUES (?)', [2]);\n```\n\nThis also should explain how this library works: it's safe by default, as every hole becomes automatically a parameter, so that SQL injections are implicitly avoided.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebreflection%2Fsqlite-tag","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwebreflection%2Fsqlite-tag","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebreflection%2Fsqlite-tag/lists"}