web-sqlite-js 0.0.1 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +212 -0
- package/dist/index.js +1 -2
- package/package.json +28 -10
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 wuchuheng
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
<h1 align="center">web-sqlite-js</h1>
|
|
2
|
+
<p align="center"> <img src="docs/public/web-sqlite-js.gif" width="400px" />
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
`web-sqlite-js` gives web developers an out-of-the-box way to run SQLite directly in the browser and keep data persisted on the client via OPFS (Origin Private File System).
|
|
6
|
+
|
|
7
|
+
Developers only need to install and configure the http request header to use it directly, which is quite friendly.
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- **Persistent Storage**: Uses OPFS for high-performance, persistent file storage.
|
|
12
|
+
- **Non-Blocking**: Runs in a Web Worker, keeping your UI responsive.
|
|
13
|
+
- **Concurrency Safe**: Built-in mutex ensures safe, sequential execution of commands.
|
|
14
|
+
- **Type-Safe**: Written in TypeScript with full type definitions.
|
|
15
|
+
- **Transactions**: Supports atomic transactions with automatic rollback on error.
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install web-sqlite-js
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Setup http headers
|
|
24
|
+
|
|
25
|
+
This library high-performance dependencies `SharedArrayBuffer`, which requires your server to send the following HTTP headers:
|
|
26
|
+
|
|
27
|
+
```http
|
|
28
|
+
Cross-Origin-Opener-Policy: same-origin
|
|
29
|
+
Cross-Origin-Embedder-Policy: require-corp
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
<details>
|
|
33
|
+
<summary><strong>Vite</strong></summary>
|
|
34
|
+
|
|
35
|
+
Update your `vite.config.ts`:
|
|
36
|
+
|
|
37
|
+
```typescript
|
|
38
|
+
import { defineConfig } from "vite";
|
|
39
|
+
|
|
40
|
+
export default defineConfig({
|
|
41
|
+
server: {
|
|
42
|
+
headers: {
|
|
43
|
+
"Cross-Origin-Opener-Policy": "same-origin",
|
|
44
|
+
"Cross-Origin-Embedder-Policy": "require-corp",
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
preview: {
|
|
48
|
+
headers: {
|
|
49
|
+
"Cross-Origin-Opener-Policy": "same-origin",
|
|
50
|
+
"Cross-Origin-Embedder-Policy": "require-corp",
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
</details>
|
|
57
|
+
|
|
58
|
+
<details>
|
|
59
|
+
<summary><strong>Next.js</strong></summary>
|
|
60
|
+
|
|
61
|
+
Update your `next.config.js`:
|
|
62
|
+
|
|
63
|
+
```javascript
|
|
64
|
+
/** @type {import('next').NextConfig} */
|
|
65
|
+
const nextConfig = {
|
|
66
|
+
async headers() {
|
|
67
|
+
return [
|
|
68
|
+
{
|
|
69
|
+
source: "/(.*)",
|
|
70
|
+
headers: [
|
|
71
|
+
{
|
|
72
|
+
key: "Cross-Origin-Opener-Policy",
|
|
73
|
+
value: "same-origin",
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
key: "Cross-Origin-Embedder-Policy",
|
|
77
|
+
value: "require-corp",
|
|
78
|
+
},
|
|
79
|
+
],
|
|
80
|
+
},
|
|
81
|
+
];
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
module.exports = nextConfig;
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
</details>
|
|
89
|
+
|
|
90
|
+
<details>
|
|
91
|
+
<summary><strong>Webpack (Dev Server)</strong></summary>
|
|
92
|
+
|
|
93
|
+
Update your `webpack.config.js`:
|
|
94
|
+
|
|
95
|
+
```javascript
|
|
96
|
+
module.exports = {
|
|
97
|
+
// ...
|
|
98
|
+
devServer: {
|
|
99
|
+
headers: {
|
|
100
|
+
"Cross-Origin-Opener-Policy": "same-origin",
|
|
101
|
+
"Cross-Origin-Embedder-Policy": "require-corp",
|
|
102
|
+
},
|
|
103
|
+
},
|
|
104
|
+
};
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
</details>
|
|
108
|
+
|
|
109
|
+
<details>
|
|
110
|
+
<summary><strong>Nginx</strong></summary>
|
|
111
|
+
|
|
112
|
+
Add the headers to your server block:
|
|
113
|
+
|
|
114
|
+
```nginx
|
|
115
|
+
server {
|
|
116
|
+
# ...
|
|
117
|
+
add_header Cross-Origin-Opener-Policy "same-origin";
|
|
118
|
+
add_header Cross-Origin-Embedder-Policy "require-corp";
|
|
119
|
+
# ...
|
|
120
|
+
}
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
</details>
|
|
124
|
+
|
|
125
|
+
<details>
|
|
126
|
+
<summary><strong>Express.js</strong></summary>
|
|
127
|
+
|
|
128
|
+
Use a middleware:
|
|
129
|
+
|
|
130
|
+
```javascript
|
|
131
|
+
const express = require("express");
|
|
132
|
+
const app = express();
|
|
133
|
+
|
|
134
|
+
app.use((req, res, next) => {
|
|
135
|
+
res.setHeader("Cross-Origin-Opener-Policy", "same-origin");
|
|
136
|
+
res.setHeader("Cross-Origin-Embedder-Policy", "require-corp");
|
|
137
|
+
next();
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
// ...
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
</details>
|
|
144
|
+
|
|
145
|
+
<details>
|
|
146
|
+
<summary><strong>React / Vue (Create React App / Vue CLI)</strong></summary>
|
|
147
|
+
|
|
148
|
+
Most modern React/Vue setups use **Vite**. Please refer to the **Vite** section above.
|
|
149
|
+
|
|
150
|
+
If you are using an older webpack-based setup (like CRA `react-scripts`), you technically need to configure the underlying `webpack-dev-server`, but CRA doesn't expose this easily without ejecting or using tools like `craco` or `react-app-rewired` to modify the dev server configuration as shown in the **Webpack** section.
|
|
151
|
+
|
|
152
|
+
</details>
|
|
153
|
+
|
|
154
|
+
## Usage
|
|
155
|
+
|
|
156
|
+
#### Basic Usage
|
|
157
|
+
|
|
158
|
+
```typescript
|
|
159
|
+
import openDB from "web-sqlite-js";
|
|
160
|
+
|
|
161
|
+
// 1. Open the database (creates 'my-database.sqlite3' in OPFS)
|
|
162
|
+
const db = await openDB("my-database");
|
|
163
|
+
|
|
164
|
+
// 2. Initialize schema
|
|
165
|
+
await db.exec(`
|
|
166
|
+
CREATE TABLE IF NOT EXISTS users (
|
|
167
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
168
|
+
name TEXT,
|
|
169
|
+
email TEXT
|
|
170
|
+
);
|
|
171
|
+
`);
|
|
172
|
+
|
|
173
|
+
// 3. Insert data (Parameterized)
|
|
174
|
+
await db.exec("INSERT INTO users (name, email) VALUES (?, ?)", [
|
|
175
|
+
"Alice",
|
|
176
|
+
"alice@example.com",
|
|
177
|
+
]);
|
|
178
|
+
await db.exec("INSERT INTO users (name, email) VALUES ($name, $email)", {
|
|
179
|
+
$name: "Bob",
|
|
180
|
+
$email: "bob@example.com",
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
// 4. Query data
|
|
184
|
+
interface User {
|
|
185
|
+
id: number;
|
|
186
|
+
name: string;
|
|
187
|
+
email: string;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const users = await db.query<User>("SELECT * FROM users");
|
|
191
|
+
console.log(users);
|
|
192
|
+
// Output: [{ id: 1, name: 'Alice', ... }, { id: 2, name: 'Bob', ... }]
|
|
193
|
+
|
|
194
|
+
// 5. Close when done
|
|
195
|
+
await db.close();
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
#### Transactions
|
|
199
|
+
|
|
200
|
+
Transactions are atomic. If any command inside the callback fails, the entire transaction is rolled back.
|
|
201
|
+
|
|
202
|
+
```typescript
|
|
203
|
+
await db.transaction(async (tx) => {
|
|
204
|
+
await tx.exec("INSERT INTO users (name) VALUES (?)", ["Charlie"]);
|
|
205
|
+
|
|
206
|
+
// You can perform multiple operations safely
|
|
207
|
+
await tx.exec("INSERT INTO logs (action) VALUES (?)", ["User Created"]);
|
|
208
|
+
|
|
209
|
+
// If you throw an error here, both INSERTs will be rolled back!
|
|
210
|
+
// throw new Error('Something went wrong');
|
|
211
|
+
});
|
|
212
|
+
```
|
package/dist/index.js
CHANGED
|
@@ -12743,7 +12743,6 @@ if(!globalThis.SharedArrayBuffer){
|
|
|
12743
12743
|
return sIM;
|
|
12744
12744
|
})();
|
|
12745
12745
|
sqlite3InitModule = toExportForESM;
|
|
12746
|
-
var sqlite3InitModule$1 = sqlite3InitModule;
|
|
12747
12746
|
var SqliteEvent = /* @__PURE__ */ ((SqliteEvent2) => {
|
|
12748
12747
|
SqliteEvent2["OPEN"] = "open";
|
|
12749
12748
|
SqliteEvent2["CLOSE"] = "close";
|
|
@@ -12865,7 +12864,7 @@ if(!globalThis.SharedArrayBuffer){
|
|
|
12865
12864
|
if (typeof payload.filename !== "string") {
|
|
12866
12865
|
throw new Error("Invalid payload for OPEN event: expected filename string");
|
|
12867
12866
|
}
|
|
12868
|
-
sqlite3 = await sqlite3InitModule
|
|
12867
|
+
sqlite3 = await sqlite3InitModule();
|
|
12869
12868
|
console.debug(\`Initialized sqlite3 module in worker.\`);
|
|
12870
12869
|
let { filename } = payload;
|
|
12871
12870
|
if (!filename.endsWith(".sqlite3")) {
|
package/package.json
CHANGED
|
@@ -1,10 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "web-sqlite-js",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"description": "",
|
|
3
|
+
"version": "0.0.3",
|
|
4
|
+
"description": "web-sqlite-js gives web developers an out-of-the-box way to run SQLite directly in the browser and keep data persisted on the client via OPFS (Origin Private File System).",
|
|
5
|
+
"workspaces": [
|
|
6
|
+
"docs"
|
|
7
|
+
],
|
|
5
8
|
"type": "module",
|
|
6
9
|
"main": "./dist/index.js",
|
|
7
10
|
"types": "./dist/index.d.ts",
|
|
11
|
+
"author": "wuchuheng <root@wuchuheng.com>",
|
|
12
|
+
"keywords": [
|
|
13
|
+
"sqlite",
|
|
14
|
+
"wasm",
|
|
15
|
+
"opfs",
|
|
16
|
+
"web-worker",
|
|
17
|
+
"database",
|
|
18
|
+
"typescript",
|
|
19
|
+
"web-sqlite"
|
|
20
|
+
],
|
|
21
|
+
"sideEffects": false,
|
|
8
22
|
"exports": {
|
|
9
23
|
".": {
|
|
10
24
|
"types": "./dist/index.d.ts",
|
|
@@ -14,7 +28,7 @@
|
|
|
14
28
|
"scripts": {
|
|
15
29
|
"test": "npm run build && npm run lint && npm run test:unit && npm run test:e2e ",
|
|
16
30
|
"test:unit": "vitest run --config vitest.unit.config.ts --coverage",
|
|
17
|
-
"http": "tsx scripts/http-service.ts ./ --port 8399",
|
|
31
|
+
"http": "tsx scripts/http-service.ts ./docs/.vitepress/dist --port 8399",
|
|
18
32
|
"test:e2e": "vitest run --config vitest.e2e.config.ts",
|
|
19
33
|
"test:debug": "vitest run --config vitest.e2e.config.ts --no-file-parallelism",
|
|
20
34
|
"build": "vite build",
|
|
@@ -24,7 +38,12 @@
|
|
|
24
38
|
"typecheck": "tsc --noEmit",
|
|
25
39
|
"format": "prettier --ignore-path .gitignore --write .",
|
|
26
40
|
"package:compress": "npm pack",
|
|
27
|
-
"package:publish": "npm run test && npm run build && npm publish --access public"
|
|
41
|
+
"package:publish": "npm run test && npm run build && npm publish --access public",
|
|
42
|
+
"docs:dev": "npm run dev --workspace=@web-sqlite-js/docs",
|
|
43
|
+
"docs:build": "npm run build --workspace=@web-sqlite-js/docs",
|
|
44
|
+
"docs:preview": "npm run preview --workspace=@web-sqlite-js/docs",
|
|
45
|
+
"docs:icons": "npm run icons --workspace=@web-sqlite-js/docs",
|
|
46
|
+
"docs:deploy": "cd docs && ./deploy.sh"
|
|
28
47
|
},
|
|
29
48
|
"files": [
|
|
30
49
|
"dist"
|
|
@@ -53,12 +72,11 @@
|
|
|
53
72
|
},
|
|
54
73
|
"repository": {
|
|
55
74
|
"type": "git",
|
|
56
|
-
"url": "git+https://github.com/wuchuheng/web-sqlite-
|
|
75
|
+
"url": "git+https://github.com/wuchuheng/web-sqlite-js.git"
|
|
57
76
|
},
|
|
58
|
-
"
|
|
59
|
-
"license": "ISC",
|
|
77
|
+
"license": "MIT",
|
|
60
78
|
"bugs": {
|
|
61
|
-
"url": "https://github.com/wuchuheng/web-sqlite-
|
|
79
|
+
"url": "https://github.com/wuchuheng/web-sqlite-js/issues"
|
|
62
80
|
},
|
|
63
|
-
"homepage": "https://github.com/wuchuheng/web-sqlite-
|
|
64
|
-
}
|
|
81
|
+
"homepage": "https://github.com/wuchuheng/web-sqlite-js#readme"
|
|
82
|
+
}
|