{"id":16330997,"url":"https://github.com/justsml/sqlite-factory","last_synced_at":"2026-05-16T08:05:16.595Z","repository":{"id":40934362,"uuid":"388291471","full_name":"justsml/sqlite-factory","owner":"justsml","description":"A sqlite helper to simplify using it in your apps!","archived":false,"fork":false,"pushed_at":"2023-01-07T09:27:12.000Z","size":403,"stargazers_count":1,"open_issues_count":6,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-02-09T21:13:33.096Z","etag":null,"topics":["api","sqlite","sqlite3","wrapper"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/justsml.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}},"created_at":"2021-07-22T01:23:31.000Z","updated_at":"2023-03-04T05:37:41.000Z","dependencies_parsed_at":"2023-02-06T19:16:09.871Z","dependency_job_id":null,"html_url":"https://github.com/justsml/sqlite-factory","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/justsml%2Fsqlite-factory","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/justsml%2Fsqlite-factory/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/justsml%2Fsqlite-factory/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/justsml%2Fsqlite-factory/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/justsml","download_url":"https://codeload.github.com/justsml/sqlite-factory/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247166159,"owners_count":20894653,"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":["api","sqlite","sqlite3","wrapper"],"created_at":"2024-10-10T23:25:12.421Z","updated_at":"2026-05-16T08:05:11.549Z","avatar_url":"https://github.com/justsml.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Sqlite Factory\n\n`sqlite-factory` is an easy way to add Sqlite based storage to most NodeJS apps. \n\nDesigned for command line and terminal-style apps!\n\n## Use Cases\n\n* Restartable tasks!\n* Logging progress or stats of long running processes.\n* Other stuff with structured data.\n\n## Notes\n\nThe library could be extended to support web based sqlite libraries. PRs welcome :).\n\nIt's not intended for use in multi-threaded apps (express server, cluster/worker module, PM2, etc.)\n\n## Examples\n\n### [JavaScript Example](examples/javascript/README.md)\n\n### [TypeScript Example](examples/typescript/README.md)\n\n**Folder structure**\n\n```\n- src/index.ts\n- src/models/logs.ts\n```\n\n#### `src/index.ts`\n\n**Important notes:**\n\n* Remember to close the database when done.\n  * In this example we listen for exit events on the `process`.\n\n```ts\nimport Logs from \"./models/logs\";\n\nLogs.insert({action: \"script_started\"});\n\n// Cleanup \u0026 Final Save handler!\nprocess.on('SIGTERM', async () =\u003e await Logs.close());\nprocess.on('SIGINT', async () =\u003e await Logs.close());\n```\n\n#### `src/models/logs.ts`\n\n**Important notes:**\n\n* The Model exported here is effectively a singleton.\n* It includes the typescript type \u0026 SQL `CREATE TABLE` script.\n\n```ts\nimport sqliteFactory from \"sqlite-factory\";\n\ninterface LogRecord {\n  id: number; // INTEGER PRIMARY KEY AUTOINCREMENT,\n  timestamp: number; // TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n  action: string; // VARCHAR(50),\n  error_message: string; // TEXT,\n  error_stack: string; // TEXT,\n  source_file_name: string; // VARCHAR(100),\n  source_line_number: number; // INTEGER,\n  data: string; // TEXT\n}\n\nlogService = sqliteFactory\u003cLogRecord\u003e({\n  tableName: \"logs\",\n  filePath: \"./db.sqlite\",\n  createTableSql: `CREATE TABLE IF NOT EXISTS logs (\n    id INTEGER PRIMARY KEY AUTOINCREMENT,\n    timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n    action VARCHAR(50),\n    error_message TEXT,\n    error_stack TEXT,\n    source_file_name VARCHAR(100),\n    source_line_number INTEGER,\n    data TEXT\n  );`,\n});\n\nexport default logService;\n```\n\n\n## API\n\nFor the following API examples, most use this example interface:\n\n```ts\nconst customerModel = sqliteFactory\u003c{ name: string }\u003e({\n  tableName: \"customers\",\n  createTableSql: `CREATE TABLE IF NOT EXISTS customers ( name VARCHAR(50) )`,\n});\n```\n\n- [`**sqliteFactory()**`](#sqliteFactoryoptions)\n\n- [`Model#close()`](#close)\n- [`Model#get(query, params)`](#getquery-params)\n- [`Model#getAll(query, params)`](#getallquery-params)\n- [`Model#insert(sql, params)`](#insertquery-params)*\n- [`Model#update(sql, params)`](#update-sql-params)*\n- [`Model#remove(sql, params)`](#update-sql-params)*\n  \u003c!--\n  - [ ] TODO: [Model#configure(option, value)](#configureoption-value)\n  - [ ] TODO: [Model#run(sql, [param, ...])](#runsql-param)\n  - [ ] TODO: [Model#each(sql, [param, ...], [complete])](#eachsql-param-complete) --\u003e\n\n### `sqliteFactory(options)`\n\n**`options`**\n\n**filePath?: string**\nValid values are filenames, `\":memory:\"` for an anonymous in-memory database and an empty string for an anonymous disk-based database. Anonymous databases are not persisted and when closing the database handle, their contents are lost.\n\n**tableName: string**\nThe table name to use for insert, update and delete commands.\n\n**createTableSql: string**\nMust be a complete SQL DML Statement. Should start with `CREATE TABLE IF NOT EXISTS table_name ...`\n\n#### In-memory example\n\n```ts\nconst customers = sqliteFactory({\n  tableName: \"customers\",\n  createTableSql: `CREATE TABLE IF NOT EXISTS customers ( name VARCHAR(50) )`,\n});\n```\n\n#### Disk-based example\n\n```js\nconst customers = sqliteFactory({\n  tableName: \"customers\",\n  createTableSql: `CREATE TABLE IF NOT EXISTS customers ( name VARCHAR(50) )`,\n  filePath: \"./customers.sqlite\"\n});\n```\n\n### `.close()`\n\nCloses the database.\nData could be lost if you forget to call this!\n\n### `.get(query, params)`\n\nRuns the SQL query with the specified parameters and resolves with the first result row afterwards. If the result set is empty, returns undefined.\n\nThe property names correspond to the column names of the result set.\n\nIt is impossible to access them by column index; the only supported way is by column name.\n\n* @param {string} query The SQL query to run.\n\n* @param {any} [params, ...] When the SQL statement contains placeholders, you can pass them in here. They will be bound to the statement before it is executed. There are three ways of passing bind parameters: directly in the function's arguments, as an array, and as an object for named parameters. This automatically sanitizes inputs.\n\n[See related method from node-sqlite3](https://github.com/mapbox/node-sqlite3/wiki/API#databasegetsql-param--callback)\n\n\n### `.getAll(query, params)`\n\nRuns the SQL query with the specified parameters. The parameters are the same as the Model#run function, with the following differences:\n\nIf the result set is empty, it will be an empty array, otherwise it will have an object for each result row which in turn contains the values of that row, like the Model#get function.\n\n\u003e Note: `getAll` first retrieves all result rows and stores them in memory.\n\n* @param {string} `query` The SQL query to run.\n* @param {array | object} params When the SQL statement contains placeholders, you can pass them in here. They will be bound to the statement before it is executed. \n\nThere are three ways of passing bind parameters:\n\n1. directly in the function's arguments,\n2. as an array, \n3. and as an object for named parameters.\n\nInput is automatically sanitized.\n\n```ts\nconst customerModel = sqliteFactory\u003c{ name: string }\u003e({\n  tableName: \"customers\",\n  createTableSql: `CREATE TABLE IF NOT EXISTS customers ( name VARCHAR(50) )`,\n});\nawait customerModel.insert({ name: \"Dan\" });\nawait customerModel.insert({ name: \"Rosie\" });\nawait customerModel.insert({ name: \"Sunflower\" });\n\n// 1 match found:\ncustomerModel.getAll(\"SELECT * FROM customers WHERE name = :name\",{ name: \"Dan\" });\n// no matches found:\ncustomerModel.getAll(\"SELECT * FROM customers WHERE name = :name\",{ name: \"not in the table!\" });\n\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjustsml%2Fsqlite-factory","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjustsml%2Fsqlite-factory","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjustsml%2Fsqlite-factory/lists"}