{"_id":"@1-/sqlite","_rev":"2-f0f4f2ce99fa1a2655b15ab850b4b2dd","name":"@1-/sqlite","dist-tags":{"latest":"0.1.1"},"versions":{"0.1.0":{"name":"@1-/sqlite","version":"0.1.0","keywords":["bun","disposable","sqlite","transaction","using"],"author":{"name":"x-at-01@googlegroups.com"},"license":"MulanPSL-2.0","_id":"@1-/sqlite@0.1.0","maintainers":[{"name":"i18n-now","email":"i18n.site@gmail.com"}],"homepage":"https://github.com/webc-site/npm/tree/main/sqlite","bugs":{"url":"https://github.com/webc-site/npm/issues"},"dist":{"shasum":"b96b1ed65eda256e90e77dac49880b6733ef7fcb","tarball":"https://registry.npmjs.org/@1-/sqlite/-/sqlite-0.1.0.tgz","fileCount":4,"integrity":"sha512-WZdpPW5ncVmp2L1t8hV4BIeZN+KP2SbR75JMzL04ZUGjCMqx5BsyndCIGOkd2k4vbNnyR6VPTzGUZiJV7YrqxQ==","signatures":[{"sig":"MEUCIQDbhvRdrRhWLmfgShJfCcrWAwTLtKyTIR+owliw55S1AgIgchf3CMDRoqIDgp6X3BrPccsvhtKMzN6df1SzHVMX0mQ=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":5550},"type":"module","exports":{".":"./_.js","./*":"./*"},"_npmUser":{"name":"i18n-now","email":"i18n.site@gmail.com"},"repository":{"url":"git+https://github.com/webc-site/npm.git","type":"git"},"_npmVersion":"11.13.0","description":"Bun:SQLite wrapper supporting automatic resource management and transactions / 支持自动资源管理与事务的 Bun:SQLite 封装","directories":{},"_nodeVersion":"26.2.0","_hasShrinkwrap":false,"_npmOperationalInternal":{"tmp":"tmp/sqlite_0.1.0_1780991605833_0.9284991009414747","host":"s3://npm-registry-packages-npm-production"}},"0.1.1":{"name":"@1-/sqlite","version":"0.1.1","description":"Bun:SQLite wrapper supporting automatic resource management and transactions / 支持自动资源管理与事务的 Bun:SQLite 封装","keywords":["bun","disposable","sqlite","transaction","using"],"homepage":"https://github.com/webc-site/npm/tree/main/sqlite","license":"MulanPSL-2.0","author":{"name":"x-at-01@googlegroups.com"},"repository":{"type":"git","url":"git+https://github.com/webc-site/npm.git"},"type":"module","exports":{".":"./_.js","./*":"./*"},"_id":"@1-/sqlite@0.1.1","bugs":{"url":"https://github.com/webc-site/npm/issues"},"_nodeVersion":"26.2.0","_npmVersion":"11.13.0","dist":{"integrity":"sha512-uBhs1BtFTb75eHE97c+PRKGozcbbFaxOJzsG7+d4Zzf+GA1eEVh3/ZVkCvWV7LtEUQU50mccsNDwvZB7Ybmsdw==","shasum":"201588638e8c9122eb9efa6e936a9ab84bf5bf1d","tarball":"https://registry.npmjs.org/@1-/sqlite/-/sqlite-0.1.1.tgz","fileCount":4,"unpackedSize":7453,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEYCIQC355t1u69iZpgTZsBix6jdguJZj43XbWGznT5aeIDaHAIhAM4M/+/nRIdyAhFAwI9EtGQJj/4V+NAhuHVRYlG3c4FG"}]},"_npmUser":{"name":"i18n-now","email":"i18n.site@gmail.com"},"directories":{},"maintainers":[{"name":"i18n-now","email":"i18n.site@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/sqlite_0.1.1_1781021850688_0.36719295712403643"},"_hasShrinkwrap":false}},"time":{"created":"2026-06-09T07:53:25.664Z","modified":"2026-06-09T16:17:31.016Z","0.1.0":"2026-06-09T07:53:25.974Z","0.1.1":"2026-06-09T16:17:30.886Z"},"bugs":{"url":"https://github.com/webc-site/npm/issues"},"author":{"name":"x-at-01@googlegroups.com"},"license":"MulanPSL-2.0","homepage":"https://github.com/webc-site/npm/tree/main/sqlite","keywords":["bun","disposable","sqlite","transaction","using"],"repository":{"type":"git","url":"git+https://github.com/webc-site/npm.git"},"description":"Bun:SQLite wrapper supporting automatic resource management and transactions / 支持自动资源管理与事务的 Bun:SQLite 封装","maintainers":[{"name":"i18n-now","email":"i18n.site@gmail.com"}],"readme":"[English](#en) | [中文](#zh)\n\n---\n\n<a id=\"en\"></a>\n# @1-/sqlite : Automatic resource management and transaction wrapper for Bun:SQLite\n\n## 1. Features\n\nA database wrapper based on `bun:sqlite` providing the following core capabilities:\n\n- **Automatic Resource Cleanup**: Fully supports the JavaScript `using` declaration (TC39 Stage 3 Explicit Resource Management) to automatically close database connections upon exiting block scope.\n- **Automatic Directory Creation**: Automatically verifies and recursively creates parent directories if they do not exist when initializing with a file path.\n- **Declarative Transactions**: Simplifies database transactions with the `tx` function, executing automatic commits on success and automatic rollbacks on exception.\n\n## 2. Usage\n\n### Auto Directory Creation and Resource Cleanup\n\n```javascript\nimport sqlite from \"@1-/sqlite\";\n\n{\n  // Automatically creates the directory ./data/db/ and initializes the database\n  using db = sqlite(\"./data/db/local.db\");\n\n  db.query(\"SELECT 1\").all();\n  // Exiting the scope automatically closes db and releases the connection\n}\n```\n\n### Transaction Control\n\n```javascript\nimport sqlite from \"@1-/sqlite\";\nimport tx from \"@1-/sqlite/tx\";\n\nusing db = sqlite(\":memory:\");\ndb.exec(\"CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)\");\n\n// Auto-commit on success\ntx(db, () => {\n  db.prepare(\"INSERT INTO users (name) VALUES (?)\").run(\"Alice\");\n});\n\n// Auto-rollback on exception\ntry {\n  tx(db, () => {\n    db.prepare(\"INSERT INTO users (name) VALUES (?)\").run(\"Bob\");\n    throw new Error(\"failed\");\n  });\n} catch (e) {\n  // Insertion of Bob is rolled back, database remains unaffected\n}\n```\n\n## 3. Design\n\nBinds the `Symbol.dispose` hook to the database instance to integrate with the Explicit Resource Management protocol of modern JavaScript runtimes.\nTransactions wrap client callbacks in a try-catch pattern to guarantee cleanup and command safety.\n\n![](https://fastly.jsdelivr.net/gh/webc-fs/-@iD/Ryad74nRGFkRyFO37jsA.svg)\n\n## 4. Tech Stack\n\n- **Bun**: JavaScript runtime environment and package manager\n- **bun:sqlite**: High-performance SQLite engine built into Bun\n- **ES Module**: Standard JavaScript module system\n\n## 5. Code Structure\n\n```text\nsrc/\n├── _.js      # Database initialization and lifecycle binding\n└── tx.js     # Transaction wrapper\ntests/\n└── _.test.js # Integration test cases\n```\n\n## 6. History\n\nSQLite originated in 2000.\nD. Richard Hipp was designing software for US Navy guided-missile destroyers, where database servers frequently suffered downtime due to network drops or setup conflicts.\nTo build a database that required no installation, no administration, did not depend on any server process, and stored data in a single file, he developed SQLite.\nThis design revolutionized local data storage.\nToday, SQLite is the most widely deployed database engine, running on billions of devices, smartphones, and web browsers.\n\n## About\n\nThis library is developed by [WebC.site](https://webc.site).\n\n[WebC.site](https://webc.site): A new paradigm of web development for AI\n\n---\n\n<a id=\"zh\"></a>\n# @1-/sqlite : 支持自动资源管理与事务的 Bun:SQLite 封装\n\n## 1. 功能介绍\n\n基于 `bun:sqlite` 构建的数据库包装器，提供以下核心特性：\n\n- **自动资源释放**：支持 JS `using` 声明（TC39 Stage 3 显式资源管理规范），离开块级作用域时自动关闭数据库，防止资源泄漏。\n- **自动创建目录**：传入文件路径时，自动检测并递归创建不存在的父级目录，避免因路径不存在导致初始化失败。\n- **声明式事务**：封装 `tx` 函数，支持事务自动提交（COMMIT）与异常回滚（ROLLBACK）。\n\n## 2. 使用演示\n\n### 自动创建目录与释放资源\n\n```javascript\nimport sqlite from \"@1-/sqlite\";\n\n{\n  // 自动创建 ./data/db/ 目录并初始化数据库\n  using db = sqlite(\"./data/db/local.db\");\n\n  db.query(\"SELECT 1\").all();\n  // 离开作用域，db 自动关闭并释放连接\n}\n```\n\n### 事务控制\n\n```javascript\nimport sqlite from \"@1-/sqlite\";\nimport tx from \"@1-/sqlite/tx\";\n\nusing db = sqlite(\":memory:\");\ndb.exec(\"CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)\");\n\n// 正常执行自动提交\ntx(db, () => {\n  db.prepare(\"INSERT INTO users (name) VALUES (?)\").run(\"Alice\");\n});\n\n// 抛出异常自动回滚\ntry {\n  tx(db, () => {\n    db.prepare(\"INSERT INTO users (name) VALUES (?)\").run(\"Bob\");\n    throw new Error(\"failed\");\n  });\n} catch (e) {\n  // Bob 写入被回滚，数据库中无此记录\n}\n```\n\n## 3. 设计思路\n\n通过为数据库实例绑定 `Symbol.dispose` 钩子，对接现代 JS 运行时的显式资源管理协议。\n事务逻辑采用高阶函数包裹，捕获回调内的异常并执行回滚。\n\n```mermaid\ngraph TD\n    A[调用 sqlite 初始化] --> B{包含文件路径?}\n    B -- 是 --> C{父目录是否存在?}\n    C -- 否 --> D[递归创建目录]\n    C -- 是 --> E[实例化 Database]\n    B -- 否 --> E\n    D --> E\n    E --> F[绑定 Symbol.dispose]\n    F --> G[返回数据库实例]\n    G --> H[进入 block 作用域]\n    H --> I{退出作用域?}\n    I -- 是 --> J[触发 Symbol.dispose]\n    J --> K[执行 db.close]\n```\n\n## 4. 技术栈\n\n- **Bun**：JS 运行环境与包管理器\n- **bun:sqlite**：Bun 内置的原生 SQLite 高性能引擎\n- **ES Module**：标准 JavaScript 模块规范\n\n## 5. 代码结构\n\n```text\nsrc/\n├── _.js      # 数据库初始化与生命周期绑定\n└── tx.js     # 事务控制封装\ntests/\n└── _.test.js # 功能测试用例\n```\n\n## 6. 历史故事\n\nSQLite 的诞生源自于 2000 年。\n当时，D. Richard Hipp 在为美国海军导弹驱逐舰编写系统软件，其使用的数据库经常因为网络中断或配置错误导致不可用。\n为了实现一个无需安装、无需配置、不依赖独立服务器进程且能直接读写磁盘文件的本地数据库，他着手开发了 SQLite。\n这一设计彻底改变了嵌入式与本地存储的游戏规则。\n如今，SQLite 已经是全球部署量最大的数据库，运行在数十亿台设备、手机以及现代浏览器中。\n\n## 关于\n\n本库由 [WebC.site](https://webc.site) 开发。\n\n[WebC.site](https://webc.site) : 面向人工智能的网站开发新范式\n","readmeFilename":"README.md"}