remult-sqlite-s3 0.1.0

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024
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,145 @@
1
+ # remult-sqlite-s3
2
+
3
+ A [Remult](https://remult.dev) data provider that wraps [better-sqlite3](https://github.com/WiseLibs/better-sqlite3) with automatic S3 synchronization.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install remult-sqlite-s3 better-sqlite3
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```typescript
14
+ import { remultApi } from 'remult/remult-express'
15
+ import { SqlDatabase } from 'remult'
16
+ import { createS3DataProvider } from 'remult-sqlite-s3'
17
+ import { Task } from './entities/Task'
18
+
19
+ export const api = remultApi({
20
+ dataProvider: createS3DataProvider({
21
+ file: './mydb.sqlite',
22
+ s3: {
23
+ bucket: 'my-bucket',
24
+ databaseName: 'my-database',
25
+ },
26
+ }),
27
+ entities: [Task],
28
+ })
29
+ ```
30
+
31
+ The `databaseName` is required and must be unique per database to prevent data conflicts in S3.
32
+
33
+ ## S3 Configuration
34
+
35
+ ### AWS S3
36
+
37
+ ```typescript
38
+ s3: {
39
+ bucket: 'my-bucket',
40
+ databaseName: 'my-database',
41
+ region: 'us-east-1', // optional, defaults to us-east-1
42
+ credentials: { // optional, uses AWS SDK default chain if not provided
43
+ accessKeyId: '...',
44
+ secretAccessKey: '...',
45
+ },
46
+ }
47
+ ```
48
+
49
+ ### Cloudflare R2
50
+
51
+ ```typescript
52
+ s3: {
53
+ bucket: 'my-bucket',
54
+ databaseName: 'my-database',
55
+ endpoint: 'https://<account-id>.r2.cloudflarestorage.com',
56
+ credentials: {
57
+ accessKeyId: process.env.R2_ACCESS_KEY_ID!,
58
+ secretAccessKey: process.env.R2_SECRET_ACCESS_KEY!,
59
+ },
60
+ }
61
+ ```
62
+
63
+ ### MinIO
64
+
65
+ ```typescript
66
+ s3: {
67
+ bucket: 'my-bucket',
68
+ databaseName: 'my-database',
69
+ endpoint: 'http://localhost:9000',
70
+ forcePathStyle: true,
71
+ credentials: {
72
+ accessKeyId: 'minioadmin',
73
+ secretAccessKey: 'minioadmin',
74
+ },
75
+ }
76
+ ```
77
+
78
+ ## Options
79
+
80
+ | Option | Description | Required | Default |
81
+ |--------|-------------|----------|---------|
82
+ | `file` | Path to SQLite database file | Yes | - |
83
+ | `s3.bucket` | S3 bucket name | Yes | - |
84
+ | `s3.databaseName` | Unique name for this database | Yes | - |
85
+ | `s3.keyPrefix` | Prefix for S3 keys (for organization) | No | - |
86
+ | `s3.region` | AWS region | No | `us-east-1` |
87
+ | `s3.endpoint` | Custom endpoint for S3-compatible storage | No | - |
88
+ | `s3.forcePathStyle` | Use path-style URLs (required for MinIO) | No | `false` |
89
+ | `s3.credentials` | AWS credentials | No | SDK default |
90
+ | `sync.mode` | `async` (fast) or `sync` (durable) | No | `async` |
91
+ | `sync.snapshotInterval` | Interval between snapshots (ms) | No | `300000` |
92
+ | `sync.lockTtl` | Lock TTL (ms) | No | `60000` |
93
+ | `sync.forceLock` | Force acquire lock (dev only) | No | `false` |
94
+ | `verbose` | Enable verbose logging | No | `false` |
95
+
96
+ ## How It Works
97
+
98
+ 1. **On startup**: Acquires a distributed lock, downloads the latest snapshot from S3 if local database is missing
99
+ 2. **On writes**: Syncs changes to S3 (immediately in `sync` mode, periodically in `async` mode)
100
+ 3. **On shutdown**: Takes a final snapshot and releases the lock
101
+
102
+ ### S3 Structure
103
+
104
+ ```
105
+ [keyPrefix/]<databaseName>/
106
+ ├── lock.json
107
+ └── generations/<id>/
108
+ ├── snapshot.db
109
+ └── snapshot.meta.json
110
+ ```
111
+
112
+ ## Framework Examples
113
+
114
+ ### SvelteKit
115
+
116
+ See the [examples/sveltekit](./examples/sveltekit) folder for a complete working example.
117
+
118
+ ```typescript
119
+ // src/server/api.ts
120
+ import { remultApi } from 'remult/remult-sveltekit'
121
+ import { createS3DataProvider } from 'remult-sqlite-s3'
122
+ import { building } from '$app/environment'
123
+ import { env } from '$env/dynamic/private'
124
+
125
+ export const api = remultApi({
126
+ dataProvider: building ? undefined : createS3DataProvider({
127
+ file: './mydb.sqlite',
128
+ s3: {
129
+ bucket: env.S3_BUCKET!,
130
+ databaseName: env.DATABASE_NAME!,
131
+ },
132
+ }),
133
+ entities: [...],
134
+ })
135
+ ```
136
+
137
+ ## Limitations
138
+
139
+ - **Single writer**: Only one instance can write at a time (by design)
140
+ - **Cold starts**: Initial recovery from S3 adds startup latency
141
+ - **Best for**: Low-to-medium write loads in serverless environments
142
+
143
+ ## License
144
+
145
+ MIT