sqlite-cloud-backup 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 +21 -0
- package/README.md +282 -0
- package/dist/index.cjs +832 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +95 -0
- package/dist/index.d.ts +95 -0
- package/dist/index.js +795 -0
- package/dist/index.js.map +1 -0
- package/package.json +70 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 1291pravin
|
|
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,282 @@
|
|
|
1
|
+
# sqlite-cloud-backup
|
|
2
|
+
|
|
3
|
+
> Lightweight SQLite database synchronization to Google Drive with zero vendor lock-in
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/sqlite-cloud-backup)
|
|
6
|
+
[](https://github.com/1291pravin/sqlite-cloud-backup/blob/main/LICENSE)
|
|
7
|
+
|
|
8
|
+
## Features
|
|
9
|
+
|
|
10
|
+
- ✅ **Simple API** - Push, pull, or sync with 3 lines of code
|
|
11
|
+
- ✅ **Google Drive** - Use your personal or organization Drive for storage
|
|
12
|
+
- ✅ **Lightweight** - Minimal dependencies, <20KB minified
|
|
13
|
+
- ✅ **TypeScript** - Full type definitions included
|
|
14
|
+
- ✅ **Data Integrity** - SHA-256 checksums for verification
|
|
15
|
+
- ✅ **Zero Lock-in** - Your database, your cloud, your control
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install sqlite-cloud-backup
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Quick Start
|
|
24
|
+
|
|
25
|
+
```typescript
|
|
26
|
+
import SqliteCloudBackup from 'sqlite-cloud-backup';
|
|
27
|
+
|
|
28
|
+
const sync = new SqliteCloudBackup({
|
|
29
|
+
dbPath: './my-app.db',
|
|
30
|
+
provider: 'google-drive',
|
|
31
|
+
credentials: {
|
|
32
|
+
clientId: 'YOUR_CLIENT_ID',
|
|
33
|
+
clientSecret: 'YOUR_CLIENT_SECRET'
|
|
34
|
+
// No need to provide refreshToken!
|
|
35
|
+
// OAuth flow opens browser automatically on first sync
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
// Smart sync - automatically handles OAuth if not authenticated
|
|
40
|
+
await sync.sync();
|
|
41
|
+
|
|
42
|
+
// That's it! Subsequent syncs use stored tokens
|
|
43
|
+
await sync.sync();
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Getting Google Drive Credentials
|
|
47
|
+
|
|
48
|
+
### Step 1: Create Google Cloud Project
|
|
49
|
+
|
|
50
|
+
1. Go to [Google Cloud Console](https://console.cloud.google.com/)
|
|
51
|
+
2. Create a new project (or select an existing one)
|
|
52
|
+
3. Go to **APIs & Services** > **Library**
|
|
53
|
+
4. Search for "Google Drive API"
|
|
54
|
+
5. Click on it and press **Enable**
|
|
55
|
+
|
|
56
|
+
### Step 2: Create OAuth Credentials
|
|
57
|
+
|
|
58
|
+
1. Go to **APIs & Services** > **Credentials**
|
|
59
|
+
2. Click **Create Credentials** > **OAuth client ID**
|
|
60
|
+
3. Configure consent screen if prompted
|
|
61
|
+
4. Choose **Desktop app**
|
|
62
|
+
5. Download credentials JSON
|
|
63
|
+
|
|
64
|
+
### Step 3: Store Credentials
|
|
65
|
+
|
|
66
|
+
Create `.env` file:
|
|
67
|
+
|
|
68
|
+
```env
|
|
69
|
+
GOOGLE_CLIENT_ID=your_client_id
|
|
70
|
+
GOOGLE_CLIENT_SECRET=your_client_secret
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
**Never commit credentials to version control!**
|
|
74
|
+
|
|
75
|
+
### Step 4: First Sync (OAuth Flow)
|
|
76
|
+
|
|
77
|
+
When you call `sync()` for the first time:
|
|
78
|
+
1. Package detects no authentication
|
|
79
|
+
2. Opens your default browser automatically
|
|
80
|
+
3. Google OAuth consent screen appears
|
|
81
|
+
4. User approves access
|
|
82
|
+
5. Package receives and stores tokens locally
|
|
83
|
+
6. Sync proceeds automatically
|
|
84
|
+
|
|
85
|
+
All subsequent syncs use the stored tokens - no browser needed!
|
|
86
|
+
|
|
87
|
+
## API Reference
|
|
88
|
+
|
|
89
|
+
### Constructor
|
|
90
|
+
|
|
91
|
+
```typescript
|
|
92
|
+
new SqliteCloudBackup(config: SyncConfig)
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
**Config Options:**
|
|
96
|
+
|
|
97
|
+
```typescript
|
|
98
|
+
{
|
|
99
|
+
dbPath: string; // Path to SQLite database
|
|
100
|
+
provider: 'google-drive'; // Cloud provider
|
|
101
|
+
credentials: {
|
|
102
|
+
clientId: string;
|
|
103
|
+
clientSecret: string;
|
|
104
|
+
refreshToken?: string; // Optional - OAuth flow if not provided
|
|
105
|
+
};
|
|
106
|
+
options?: {
|
|
107
|
+
logLevel?: 'debug' | 'info' | 'warn' | 'error'; // Default: 'info'
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### Methods
|
|
113
|
+
|
|
114
|
+
#### `pushToCloud(): Promise<SyncResult>`
|
|
115
|
+
|
|
116
|
+
Upload local database to cloud.
|
|
117
|
+
|
|
118
|
+
```typescript
|
|
119
|
+
const result = await sync.pushToCloud();
|
|
120
|
+
console.log(`Uploaded ${result.bytesTransferred} bytes in ${result.duration}ms`);
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
#### `pullFromCloud(): Promise<SyncResult>`
|
|
124
|
+
|
|
125
|
+
Download database from cloud to local.
|
|
126
|
+
|
|
127
|
+
```typescript
|
|
128
|
+
const result = await sync.pullFromCloud();
|
|
129
|
+
console.log(`Downloaded ${result.bytesTransferred} bytes`);
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
#### `sync(): Promise<SyncResult>`
|
|
133
|
+
|
|
134
|
+
Smart bidirectional sync. Automatically determines if push or pull is needed based on modification times.
|
|
135
|
+
|
|
136
|
+
```typescript
|
|
137
|
+
const result = await sync.sync();
|
|
138
|
+
// result.type will be 'push', 'pull', or 'bidirectional'
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
#### `authenticate(): Promise<void>`
|
|
142
|
+
|
|
143
|
+
Manually trigger OAuth authentication flow.
|
|
144
|
+
|
|
145
|
+
```typescript
|
|
146
|
+
await sync.authenticate();
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
#### `isAuthenticated(): Promise<boolean>`
|
|
150
|
+
|
|
151
|
+
Check if user is authenticated.
|
|
152
|
+
|
|
153
|
+
```typescript
|
|
154
|
+
const isAuth = await sync.isAuthenticated();
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
#### `needsAuthentication(): Promise<boolean>`
|
|
158
|
+
|
|
159
|
+
Check if authentication is needed.
|
|
160
|
+
|
|
161
|
+
```typescript
|
|
162
|
+
const needsAuth = await sync.needsAuthentication();
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
#### `logout(): Promise<void>`
|
|
166
|
+
|
|
167
|
+
Logout and clear stored tokens.
|
|
168
|
+
|
|
169
|
+
```typescript
|
|
170
|
+
await sync.logout();
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
#### `shutdown(): Promise<void>`
|
|
174
|
+
|
|
175
|
+
Clean up and close connections.
|
|
176
|
+
|
|
177
|
+
```typescript
|
|
178
|
+
await sync.shutdown();
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
## Use Cases
|
|
182
|
+
|
|
183
|
+
### Electron Apps
|
|
184
|
+
|
|
185
|
+
```typescript
|
|
186
|
+
import SqliteCloudBackup from 'sqlite-cloud-backup';
|
|
187
|
+
import path from 'path';
|
|
188
|
+
import { app } from 'electron';
|
|
189
|
+
|
|
190
|
+
const dbPath = path.join(app.getPath('userData'), 'app.db');
|
|
191
|
+
const sync = new SqliteCloudBackup({
|
|
192
|
+
dbPath,
|
|
193
|
+
provider: 'google-drive',
|
|
194
|
+
credentials: {
|
|
195
|
+
clientId: 'YOUR_CLIENT_ID',
|
|
196
|
+
clientSecret: 'YOUR_CLIENT_SECRET'
|
|
197
|
+
// No refreshToken needed - OAuth flow opens browser automatically
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
// Sync on app start - OAuth flow triggers automatically if needed
|
|
202
|
+
app.on('ready', async () => {
|
|
203
|
+
await sync.sync();
|
|
204
|
+
});
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
### React Native
|
|
208
|
+
|
|
209
|
+
```typescript
|
|
210
|
+
import SqliteCloudBackup from 'sqlite-cloud-backup';
|
|
211
|
+
import RNFS from 'react-native-fs';
|
|
212
|
+
|
|
213
|
+
const dbPath = `${RNFS.DocumentDirectoryPath}/app.db`;
|
|
214
|
+
const sync = new SqliteCloudBackup({
|
|
215
|
+
dbPath,
|
|
216
|
+
provider: 'google-drive',
|
|
217
|
+
credentials: { /* ... */ }
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
// Sync when app comes to foreground
|
|
221
|
+
AppState.addEventListener('change', async (state) => {
|
|
222
|
+
if (state === 'active') {
|
|
223
|
+
await sync.sync();
|
|
224
|
+
}
|
|
225
|
+
});
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
### CLI Tools
|
|
229
|
+
|
|
230
|
+
```typescript
|
|
231
|
+
import SqliteCloudBackup from 'sqlite-cloud-backup';
|
|
232
|
+
|
|
233
|
+
const sync = new SqliteCloudBackup({
|
|
234
|
+
dbPath: './data.db',
|
|
235
|
+
provider: 'google-drive',
|
|
236
|
+
credentials: { /* ... */ }
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
// Backup on schedule (e.g., cron job)
|
|
240
|
+
await sync.pushToCloud();
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
## Current Status (v0.1)
|
|
244
|
+
|
|
245
|
+
**v0.1 includes:**
|
|
246
|
+
- ✅ Push/pull/sync operations
|
|
247
|
+
- ✅ Google Drive integration
|
|
248
|
+
- ✅ SHA-256 checksums
|
|
249
|
+
- ✅ Automatic conflict detection
|
|
250
|
+
- ✅ TypeScript support
|
|
251
|
+
|
|
252
|
+
**Coming in v0.2+:**
|
|
253
|
+
- ⏳ Additional providers (Dropbox, S3)
|
|
254
|
+
|
|
255
|
+
## Requirements
|
|
256
|
+
|
|
257
|
+
- Node.js 18+
|
|
258
|
+
- SQLite database file
|
|
259
|
+
- Google Drive account
|
|
260
|
+
|
|
261
|
+
## Examples
|
|
262
|
+
|
|
263
|
+
See [examples/](./examples/) directory for more use cases:
|
|
264
|
+
|
|
265
|
+
- `basic-usage.ts` - Simple push/pull/sync example
|
|
266
|
+
|
|
267
|
+
## Contributing
|
|
268
|
+
|
|
269
|
+
Contributions are welcome! Please open an issue or PR.
|
|
270
|
+
|
|
271
|
+
## License
|
|
272
|
+
|
|
273
|
+
MIT © 1291pravin
|
|
274
|
+
|
|
275
|
+
## Support
|
|
276
|
+
|
|
277
|
+
- [GitHub Issues](https://github.com/1291pravin/sqlite-cloud-backup/issues)
|
|
278
|
+
- [Documentation](./docs/)
|
|
279
|
+
|
|
280
|
+
---
|
|
281
|
+
|
|
282
|
+
Made with ❤️ for developers who need simple, reliable database backups.
|