git-sqlite-vfs 0.0.9 → 0.0.10
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/README.md +26 -26
- package/index.js +8 -0
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1,42 +1,42 @@
|
|
|
1
1
|
# git-sqlite-vfs
|
|
2
2
|
|
|
3
|
-
A Git-
|
|
3
|
+
A Git-versioned SQLite Database powered by a custom Virtual File System (VFS).
|
|
4
4
|
|
|
5
|
-
By
|
|
5
|
+
By integrating native SQLite, Drizzle ORM, and libSQL with Git's version control capabilities, `git-sqlite-vfs` allows you to version, diff, and merge your application's database in a manner similar to source code. It is compatible with both **Node.js** and **Deno**.
|
|
6
6
|
|
|
7
7
|
## Architecture
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
Standard SQLite databases are stored as a single flat file. This structure presents challenges for version control, as a minor insertion can result in widespread byte shifts across the file, reducing the effectiveness of delta-compression and making binary merge conflicts difficult to resolve.
|
|
10
10
|
|
|
11
|
-
This package
|
|
11
|
+
This package provides a loadable SQLite C Extension that overrides the default VFS behavior. It shards the database into 4KB deterministic binary pages within a specified directory (e.g. `.my-db`).
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
During a Git merge, a provided `git-merge-sqlitevfs` driver integrates with Git's conflict resolution pipeline to reconcile binary B-Tree page conflicts, maintaining data integrity.
|
|
14
14
|
|
|
15
15
|
## Installation
|
|
16
16
|
|
|
17
|
-
Install the
|
|
17
|
+
Install the package alongside your libSQL and Drizzle ORM dependencies:
|
|
18
18
|
|
|
19
19
|
```bash
|
|
20
20
|
npm install git-sqlite-vfs @libsql/client drizzle-orm
|
|
21
21
|
```
|
|
22
22
|
|
|
23
|
-
## Git
|
|
23
|
+
## Git Configuration
|
|
24
24
|
|
|
25
|
-
To enable Git versioning and binary merging,
|
|
25
|
+
To enable Git versioning and binary merging, the repository must be configured to use the custom merge driver. This is done **automatically** when you call `bootstrapGitVFS()`:
|
|
26
26
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
27
|
+
1. It registers `git-merge-sqlitevfs` as a custom Git merge driver in the local `.git/config`.
|
|
28
|
+
2. It adds or appends to a `.gitattributes` file in the repository root to route all files matching your configured directory (e.g., `.my-db/*`) through the custom merge driver.
|
|
29
|
+
3. It creates or updates a `.gitignore` to ignore SQLite transient files (`*-journal`, `*-wal`, `*-shm`).
|
|
30
|
+
|
|
31
|
+
### Safe alongside Source Code
|
|
30
32
|
|
|
31
|
-
|
|
32
|
-
1. Register `git-merge-sqlitevfs` as a custom Git merge driver in your local `.git/config`.
|
|
33
|
-
2. Create or append to a `.gitattributes` file in your repo root to route all files in `.my-db/*` through the custom merge driver.
|
|
33
|
+
The custom SQLite merge driver **will not interfere with the merging of standard source code or text files**. By utilizing the `.gitattributes` file, Git explicitly scopes the custom driver strictly to the files within your designated database directory (e.g., `.my-db/* merge=sqlitevfs`). All other files in your repository will continue to use Git's default text-based merge algorithms.
|
|
34
34
|
|
|
35
|
-
## Usage
|
|
35
|
+
## Usage
|
|
36
36
|
|
|
37
|
-
The VFS works
|
|
37
|
+
The VFS works in both Node.js and Deno environments. By calling the `bootstrapGitVFS()` method prior to initializing `@libsql/client`, the extension is loaded process-wide.
|
|
38
38
|
|
|
39
|
-
###
|
|
39
|
+
### Example with `@libsql/client` and Drizzle ORM
|
|
40
40
|
|
|
41
41
|
```typescript
|
|
42
42
|
import { createClient } from '@libsql/client'; // In Deno: 'npm:@libsql/client/node'
|
|
@@ -47,7 +47,7 @@ import { bootstrapGitVFS } from 'git-sqlite-vfs';
|
|
|
47
47
|
// 1. Load the native extension process-wide and set the VFS directory
|
|
48
48
|
await bootstrapGitVFS({ dir: '.my-db' });
|
|
49
49
|
|
|
50
|
-
// 2. Initialize
|
|
50
|
+
// 2. Initialize the database connection using a standard file: URL
|
|
51
51
|
const client = createClient({
|
|
52
52
|
url: 'file:.my-db/local.db'
|
|
53
53
|
});
|
|
@@ -55,36 +55,36 @@ const client = createClient({
|
|
|
55
55
|
// 3. Wrap with Drizzle ORM
|
|
56
56
|
const db = drizzle(client);
|
|
57
57
|
|
|
58
|
-
// 4. Define
|
|
58
|
+
// 4. Define the schema
|
|
59
59
|
const users = sqliteTable('users', {
|
|
60
60
|
id: integer('id').primaryKey(),
|
|
61
61
|
name: text('name')
|
|
62
62
|
});
|
|
63
63
|
|
|
64
|
-
// 5.
|
|
64
|
+
// 5. Execute queries. File I/O is intercepted by the Git VFS.
|
|
65
65
|
await db.insert(users).values({ id: 1, name: 'Alice' });
|
|
66
66
|
const allUsers = await db.select().from(users);
|
|
67
67
|
|
|
68
68
|
console.log(allUsers);
|
|
69
69
|
```
|
|
70
70
|
|
|
71
|
-
## Preventing
|
|
71
|
+
## Preventing Repository Bloat
|
|
72
72
|
|
|
73
|
-
Because SQLite
|
|
73
|
+
Because SQLite often zeroes out deleted data pages rather than shrinking the database file size, "zombie" `.bin` pages may accumulate in the repository over time. To allow the Git VFS to automatically garbage-collect these unused chunks, configure SQLite to use `FULL` auto-vacuuming and `DELETE` journaling.
|
|
74
74
|
|
|
75
|
-
|
|
75
|
+
Execute these PRAGMA statements when initializing the database connection:
|
|
76
76
|
|
|
77
77
|
```sql
|
|
78
78
|
PRAGMA auto_vacuum = FULL;
|
|
79
79
|
PRAGMA journal_mode = DELETE;
|
|
80
80
|
```
|
|
81
81
|
|
|
82
|
-
|
|
82
|
+
Alternatively, you can run `VACUUM;` periodically. When SQLite explicitly reduces the database file size, the underlying VFS `xTruncate` implementation will remove the out-of-bounds `.bin` shards, keeping the repository history compressed.
|
|
83
83
|
|
|
84
84
|
## Compatibility
|
|
85
85
|
|
|
86
|
-
- **Node.js**: v22.5+ (using the
|
|
87
|
-
- **Deno**: Supported
|
|
86
|
+
- **Node.js**: v22.5+ (using the internal `node:sqlite` API) or fallback to `better-sqlite3`.
|
|
87
|
+
- **Deno**: Supported natively (loads the extension dynamically via `jsr:@db/sqlite`).
|
|
88
88
|
|
|
89
89
|
## License
|
|
90
90
|
|
package/index.js
CHANGED
|
@@ -53,6 +53,14 @@ export async function bootstrapGitVFS(options = {}) {
|
|
|
53
53
|
const db = new Database(':memory:');
|
|
54
54
|
db.loadExtension(currentExtPath);
|
|
55
55
|
db.close();
|
|
56
|
+
|
|
57
|
+
try {
|
|
58
|
+
const repoDir = typeof Deno !== 'undefined' ? Deno.cwd() : process.cwd();
|
|
59
|
+
const vfsDir = options.dir || '.db';
|
|
60
|
+
await configureGitIntegration({ repoDir, vfsDir });
|
|
61
|
+
} catch (e) {
|
|
62
|
+
// Ignore errors if git is not available or not in a git repository
|
|
63
|
+
}
|
|
56
64
|
}
|
|
57
65
|
|
|
58
66
|
export async function configureGitIntegration({ repoDir, vfsDir }) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "git-sqlite-vfs",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.10",
|
|
4
4
|
"description": "A Git-Versioned SQLite Database via a Custom Virtual File System (VFS)",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -24,9 +24,9 @@
|
|
|
24
24
|
"scripts": {
|
|
25
25
|
"build": "cd c && make",
|
|
26
26
|
"postinstall": "node install.js",
|
|
27
|
-
"pretest": "npm run build && rm -rf .db test.db .test-db .compaction-db .git && git init --initial-branch=master",
|
|
27
|
+
"pretest": "npm run build && rm -rf .db test.db .test-db .compaction-db .git .gitattributes .gitignore && git init --initial-branch=master",
|
|
28
28
|
"test": "node --test test/*.node.test.js",
|
|
29
|
-
"test:deno": "npm run build && rm -rf .db test.db .test-db .git && git init --initial-branch=master && deno test -A test/*.deno.test.ts"
|
|
29
|
+
"test:deno": "npm run build && rm -rf .db test.db .test-db .git .gitattributes .gitignore && git init --initial-branch=master && deno test -A test/*.deno.test.ts"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
32
|
"@libsql/client": "^0.14.0",
|