local-lite 1.0.1 → 1.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/dist/actions/manager.js +6 -2
- package/dist/actions/store.js +33 -19
- package/dist/adapters/local/expo-sqlite.js +4 -2
- package/dist/adapters/local/libsql.js +3 -1
- package/dist/adapters/local/op-sqlite.js +2 -1
- package/dist/adapters/network/index.js +10 -5
- package/dist/adapters/remote/convex.js +37 -10
- package/dist/adapters/remote/firestore.js +11 -4
- package/dist/adapters/remote/self-hosted.js +9 -5
- package/dist/adapters/remote/supabase.js +95 -29
- package/dist/auth/apple.js +8 -4
- package/dist/auth/github.js +15 -7
- package/dist/auth/google.js +7 -4
- package/dist/auth/identity.js +5 -1
- package/dist/auth/local.js +74 -21
- package/dist/auth/microsoft.js +22 -8
- package/dist/auth/session.js +45 -34
- package/dist/auth/unified.js +3 -2
- package/dist/backups/cloud-storage.js +10 -7
- package/dist/backups/encryption.js +4 -2
- package/dist/backups/onedrive.js +3 -3
- package/dist/backups/operations.js +4 -2
- package/dist/config/index.d.ts +1 -1
- package/dist/config/resolve-local.js +18 -6
- package/dist/config/resolve-remote.js +2 -1
- package/dist/core/client.d.ts +2 -2
- package/dist/core/client.js +64 -26
- package/dist/core/index.d.ts +1 -1
- package/dist/core/maintenance.js +5 -4
- package/dist/core/meta-store.js +3 -3
- package/dist/core/outbox-store.js +32 -25
- package/dist/core/query-builder.js +23 -10
- package/dist/core/query.js +19 -7
- package/dist/core/schema-manager.js +6 -2
- package/dist/core/sync-worker.js +38 -12
- package/dist/core/table-manager.js +8 -2
- package/dist/core/types.d.ts +1 -1
- package/dist/devtools/BenchmarkModal.d.ts +1 -1
- package/dist/devtools/BenchmarkModal.js +234 -54
- package/dist/devtools/SyncInspector.js +260 -37
- package/dist/email/index.js +46 -18
- package/dist/keys/generator.js +4 -1
- package/dist/keys/manager.js +22 -4
- package/dist/keys/signed-token.js +12 -4
- package/dist/keys/store.js +23 -16
- package/dist/kv/manager.js +5 -2
- package/dist/kv/store.js +13 -13
- package/dist/react/auth.js +12 -12
- package/dist/react/backups.js +1 -1
- package/dist/react/index.js +14 -9
- package/dist/react/runner.js +1 -1
- package/dist/react/status.js +6 -2
- package/dist/reactive/event-emitter.js +2 -2
- package/dist/scheduler/cron.js +3 -1
- package/dist/scheduler/store.js +37 -31
- package/dist/schema/generator.js +37 -34
- package/dist/search/manager.js +3 -1
- package/dist/search/store.js +17 -16
- package/dist/server/email.js +3 -2
- package/dist/server/index.js +4 -4
- package/dist/server/sync.js +11 -4
- package/dist/storage/driver.js +2 -1
- package/dist/storage/expo.js +5 -2
- package/dist/storage/manager.js +3 -1
- package/dist/storage/store.js +26 -25
- package/dist/utils/dynamic.js +4 -2
- package/dist/utils/logger.js +13 -6
- package/dist/utils/portability.js +21 -9
- package/package.json +1 -1
package/dist/search/store.js
CHANGED
|
@@ -85,15 +85,15 @@ class SearchStore {
|
|
|
85
85
|
// Snippet column index in FTS5: columns are 0-based: col 0 is row_id, col 1 is columns[0], etc.
|
|
86
86
|
// We pick column index 1 (or the first indexed text column) for snippet extraction
|
|
87
87
|
const snippetColIndex = 1;
|
|
88
|
-
let sql = `
|
|
89
|
-
SELECT
|
|
90
|
-
row_id,
|
|
91
|
-
rank,
|
|
92
|
-
snippet(${ftsTable}, ${snippetColIndex}, '${preTag}', '${postTag}', '...', 15) as snippet_text
|
|
93
|
-
FROM ${ftsTable}
|
|
94
|
-
WHERE ${ftsTable} MATCH ?
|
|
95
|
-
ORDER BY rank
|
|
96
|
-
LIMIT ? OFFSET ?;
|
|
88
|
+
let sql = `
|
|
89
|
+
SELECT
|
|
90
|
+
row_id,
|
|
91
|
+
rank,
|
|
92
|
+
snippet(${ftsTable}, ${snippetColIndex}, '${preTag}', '${postTag}', '...', 15) as snippet_text
|
|
93
|
+
FROM ${ftsTable}
|
|
94
|
+
WHERE ${ftsTable} MATCH ?
|
|
95
|
+
ORDER BY rank
|
|
96
|
+
LIMIT ? OFFSET ?;
|
|
97
97
|
`;
|
|
98
98
|
try {
|
|
99
99
|
const rows = await db.execute(sql, [matchQuery, limit, offset]);
|
|
@@ -105,13 +105,14 @@ class SearchStore {
|
|
|
105
105
|
}
|
|
106
106
|
catch (err) {
|
|
107
107
|
// In case snippet() function is unavailable in the environment, fallback to rank-only search
|
|
108
|
-
if (err?.message?.includes('snippet') ||
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
108
|
+
if (err?.message?.includes('snippet') ||
|
|
109
|
+
err?.message?.includes('no such function')) {
|
|
110
|
+
const fallbackSql = `
|
|
111
|
+
SELECT row_id, rank
|
|
112
|
+
FROM ${ftsTable}
|
|
113
|
+
WHERE ${ftsTable} MATCH ?
|
|
114
|
+
ORDER BY rank
|
|
115
|
+
LIMIT ? OFFSET ?;
|
|
115
116
|
`;
|
|
116
117
|
const rows = await db.execute(fallbackSql, [matchQuery, limit, offset]);
|
|
117
118
|
return (rows || []).map((r) => ({
|
package/dist/server/email.js
CHANGED
|
@@ -11,10 +11,11 @@ function createEmailEndpoint(options = {}) {
|
|
|
11
11
|
'Access-Control-Allow-Methods': 'POST, OPTIONS',
|
|
12
12
|
'Access-Control-Allow-Headers': 'Content-Type, Authorization, X-App-Secret, X-Message-ID',
|
|
13
13
|
};
|
|
14
|
-
const handlePost = async ({ request }) => {
|
|
14
|
+
const handlePost = async ({ request, }) => {
|
|
15
15
|
// 1. Check secret token if configured
|
|
16
16
|
if (options.secret) {
|
|
17
|
-
const authHeader = request.headers.get('authorization') ||
|
|
17
|
+
const authHeader = request.headers.get('authorization') ||
|
|
18
|
+
request.headers.get('x-app-secret');
|
|
18
19
|
const expectedBearer = `Bearer ${options.secret}`;
|
|
19
20
|
if (authHeader !== expectedBearer && authHeader !== options.secret) {
|
|
20
21
|
return Response.json({ error: 'Unauthorized: Invalid email endpoint secret' }, { status: 401, headers: corsHeaders });
|
package/dist/server/index.js
CHANGED
|
@@ -53,7 +53,7 @@ function defineServerConfig(config) {
|
|
|
53
53
|
for (const [path, endpoint] of Object.entries(config.endpoints)) {
|
|
54
54
|
if (!path.startsWith('/'))
|
|
55
55
|
throw new TypeError(`API endpoint path must start with '/': ${path}`);
|
|
56
|
-
if (!Object.values(endpoint).some(handler => typeof handler === 'function')) {
|
|
56
|
+
if (!Object.values(endpoint).some((handler) => typeof handler === 'function')) {
|
|
57
57
|
throw new TypeError(`API endpoint '${path}' must define at least one method handler.`);
|
|
58
58
|
}
|
|
59
59
|
}
|
|
@@ -119,7 +119,7 @@ function createServer(config) {
|
|
|
119
119
|
handlers,
|
|
120
120
|
handler(path, method) {
|
|
121
121
|
const normalized = normalizePath(path);
|
|
122
|
-
const definition = definitions.find(item => item.path === normalized);
|
|
122
|
+
const definition = definitions.find((item) => item.path === normalized);
|
|
123
123
|
if (!definition || !definition.endpoint[method]) {
|
|
124
124
|
throw new Error(`No ${method} API handler is configured for '${normalized}'.`);
|
|
125
125
|
}
|
|
@@ -139,8 +139,8 @@ function createServer(config) {
|
|
|
139
139
|
}
|
|
140
140
|
if (pathMatched) {
|
|
141
141
|
const allowed = definitions
|
|
142
|
-
.filter(definition => matchPath(definition.path, url.pathname))
|
|
143
|
-
.flatMap(definition => Object.keys(definition.endpoint));
|
|
142
|
+
.filter((definition) => matchPath(definition.path, url.pathname))
|
|
143
|
+
.flatMap((definition) => Object.keys(definition.endpoint));
|
|
144
144
|
return methodNotAllowed([...new Set(allowed)]);
|
|
145
145
|
}
|
|
146
146
|
return Response.json({ error: 'Not Found' }, { status: 404 });
|
package/dist/server/sync.js
CHANGED
|
@@ -117,7 +117,7 @@ function createSyncEndpoint(options = {}) {
|
|
|
117
117
|
return match ? { type: 'admin', isAdmin: true } : false;
|
|
118
118
|
};
|
|
119
119
|
// POST: Push batch of mutations (or combined push+pull batch)
|
|
120
|
-
const handlePost = async ({ request }) => {
|
|
120
|
+
const handlePost = async ({ request, }) => {
|
|
121
121
|
const auth = await verifyAuth(request);
|
|
122
122
|
if (!auth) {
|
|
123
123
|
return Response.json({ error: 'Unauthorized: Invalid sync authentication' }, { status: 401, headers: corsHeaders });
|
|
@@ -129,7 +129,9 @@ function createSyncEndpoint(options = {}) {
|
|
|
129
129
|
catch {
|
|
130
130
|
return Response.json({ error: 'Bad Request: Invalid JSON body' }, { status: 400, headers: corsHeaders });
|
|
131
131
|
}
|
|
132
|
-
let operations = Array.isArray(body?.operations)
|
|
132
|
+
let operations = Array.isArray(body?.operations)
|
|
133
|
+
? body.operations
|
|
134
|
+
: [];
|
|
133
135
|
if (options.onBeforePush) {
|
|
134
136
|
const modified = await options.onBeforePush(operations, request, auth);
|
|
135
137
|
if (Array.isArray(modified))
|
|
@@ -166,7 +168,7 @@ function createSyncEndpoint(options = {}) {
|
|
|
166
168
|
}, { status: 200, headers: corsHeaders });
|
|
167
169
|
};
|
|
168
170
|
// GET: Pull deltas since cursor/timestamp
|
|
169
|
-
const handleGet = async ({ request }) => {
|
|
171
|
+
const handleGet = async ({ request, }) => {
|
|
170
172
|
const auth = await verifyAuth(request);
|
|
171
173
|
if (!auth) {
|
|
172
174
|
return Response.json({ error: 'Unauthorized: Invalid sync authentication' }, { status: 401, headers: corsHeaders });
|
|
@@ -175,7 +177,12 @@ function createSyncEndpoint(options = {}) {
|
|
|
175
177
|
const tablesParam = url.searchParams.get('tables');
|
|
176
178
|
const sinceParam = url.searchParams.get('since');
|
|
177
179
|
const limitParam = url.searchParams.get('limit');
|
|
178
|
-
let tables = tablesParam
|
|
180
|
+
let tables = tablesParam
|
|
181
|
+
? tablesParam
|
|
182
|
+
.split(',')
|
|
183
|
+
.map((t) => t.trim())
|
|
184
|
+
.filter(Boolean)
|
|
185
|
+
: [];
|
|
179
186
|
let since = sinceParam ? Number(sinceParam) : 0;
|
|
180
187
|
let limit = limitParam ? Number(limitParam) : 500;
|
|
181
188
|
if (options.onBeforePull) {
|
package/dist/storage/driver.js
CHANGED
|
@@ -40,7 +40,8 @@ class NodeFileDriver {
|
|
|
40
40
|
try {
|
|
41
41
|
const fs = (0, dynamic_js_1.safeDynamicRequire)(NODE_FS_PROMISES_PKG) ??
|
|
42
42
|
(await (0, dynamic_js_1.safeDynamicImport)(NODE_FS_PROMISES_PKG));
|
|
43
|
-
const path = (0, dynamic_js_1.safeDynamicRequire)(NODE_PATH_PKG) ??
|
|
43
|
+
const path = (0, dynamic_js_1.safeDynamicRequire)(NODE_PATH_PKG) ??
|
|
44
|
+
(await (0, dynamic_js_1.safeDynamicImport)(NODE_PATH_PKG));
|
|
44
45
|
if (!fs || !path) {
|
|
45
46
|
throw new Error('Node fs/path unavailable');
|
|
46
47
|
}
|
package/dist/storage/expo.js
CHANGED
|
@@ -102,7 +102,8 @@ class ExpoFileDriver {
|
|
|
102
102
|
}
|
|
103
103
|
if (this.resolvedBaseDir) {
|
|
104
104
|
try {
|
|
105
|
-
if (!this.resolvedBaseDir.exists &&
|
|
105
|
+
if (!this.resolvedBaseDir.exists &&
|
|
106
|
+
typeof this.resolvedBaseDir.create === 'function') {
|
|
106
107
|
this.resolvedBaseDir.create({ idempotent: true });
|
|
107
108
|
}
|
|
108
109
|
}
|
|
@@ -239,7 +240,9 @@ class ExpoFileDriver {
|
|
|
239
240
|
const file = await this.resolveFile(key);
|
|
240
241
|
if (typeof this.FileClass.downloadFileAsync === 'function') {
|
|
241
242
|
try {
|
|
242
|
-
await this.FileClass.downloadFileAsync(remoteUrl, file, {
|
|
243
|
+
await this.FileClass.downloadFileAsync(remoteUrl, file, {
|
|
244
|
+
idempotent: true,
|
|
245
|
+
});
|
|
243
246
|
return file.uri;
|
|
244
247
|
}
|
|
245
248
|
catch { }
|
package/dist/storage/manager.js
CHANGED
|
@@ -111,7 +111,9 @@ class BlobStorageManager {
|
|
|
111
111
|
async delete(key) {
|
|
112
112
|
await this.driver.delete(key);
|
|
113
113
|
await store_js_1.BlobStore.deleteByKey(this.db, this.tableName, key);
|
|
114
|
-
if (this.deleteRemoteOnDelete &&
|
|
114
|
+
if (this.deleteRemoteOnDelete &&
|
|
115
|
+
this.uploader &&
|
|
116
|
+
typeof this.uploader.delete === 'function') {
|
|
115
117
|
try {
|
|
116
118
|
await this.uploader.delete(key);
|
|
117
119
|
}
|
package/dist/storage/store.js
CHANGED
|
@@ -7,7 +7,8 @@ function mapBlobRow(r) {
|
|
|
7
7
|
let meta;
|
|
8
8
|
if (r.metadata) {
|
|
9
9
|
try {
|
|
10
|
-
meta =
|
|
10
|
+
meta =
|
|
11
|
+
typeof r.metadata === 'string' ? JSON.parse(r.metadata) : r.metadata;
|
|
11
12
|
}
|
|
12
13
|
catch {
|
|
13
14
|
meta = undefined;
|
|
@@ -30,36 +31,36 @@ function mapBlobRow(r) {
|
|
|
30
31
|
}
|
|
31
32
|
class BlobStore {
|
|
32
33
|
static async ensureSchema(db, tableName = exports.DEFAULT_BLOBS_TABLE) {
|
|
33
|
-
await db.execute(`CREATE TABLE IF NOT EXISTS ${tableName} (
|
|
34
|
-
id TEXT PRIMARY KEY,
|
|
35
|
-
key TEXT UNIQUE NOT NULL,
|
|
36
|
-
name TEXT NOT NULL,
|
|
37
|
-
mime_type TEXT NOT NULL,
|
|
38
|
-
size INTEGER NOT NULL,
|
|
39
|
-
local_path TEXT NOT NULL,
|
|
40
|
-
remote_url TEXT,
|
|
41
|
-
status TEXT NOT NULL,
|
|
42
|
-
sha256 TEXT,
|
|
43
|
-
metadata TEXT,
|
|
44
|
-
created_at INTEGER NOT NULL,
|
|
45
|
-
updated_at INTEGER NOT NULL
|
|
34
|
+
await db.execute(`CREATE TABLE IF NOT EXISTS ${tableName} (
|
|
35
|
+
id TEXT PRIMARY KEY,
|
|
36
|
+
key TEXT UNIQUE NOT NULL,
|
|
37
|
+
name TEXT NOT NULL,
|
|
38
|
+
mime_type TEXT NOT NULL,
|
|
39
|
+
size INTEGER NOT NULL,
|
|
40
|
+
local_path TEXT NOT NULL,
|
|
41
|
+
remote_url TEXT,
|
|
42
|
+
status TEXT NOT NULL,
|
|
43
|
+
sha256 TEXT,
|
|
44
|
+
metadata TEXT,
|
|
45
|
+
created_at INTEGER NOT NULL,
|
|
46
|
+
updated_at INTEGER NOT NULL
|
|
46
47
|
);`);
|
|
47
48
|
await db.execute(`CREATE INDEX IF NOT EXISTS idx_${tableName}_status ON ${tableName} (status);`);
|
|
48
49
|
await db.execute(`CREATE INDEX IF NOT EXISTS idx_${tableName}_key ON ${tableName} (key);`);
|
|
49
50
|
}
|
|
50
51
|
static async insertOrUpdate(db, tableName = exports.DEFAULT_BLOBS_TABLE, record) {
|
|
51
52
|
const metaStr = record.metadata ? JSON.stringify(record.metadata) : null;
|
|
52
|
-
await db.execute(`INSERT INTO ${tableName} (id, key, name, mime_type, size, local_path, remote_url, status, sha256, metadata, created_at, updated_at)
|
|
53
|
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
54
|
-
ON CONFLICT(key) DO UPDATE SET
|
|
55
|
-
name = excluded.name,
|
|
56
|
-
mime_type = excluded.mime_type,
|
|
57
|
-
size = excluded.size,
|
|
58
|
-
local_path = excluded.local_path,
|
|
59
|
-
remote_url = excluded.remote_url,
|
|
60
|
-
status = excluded.status,
|
|
61
|
-
sha256 = excluded.sha256,
|
|
62
|
-
metadata = excluded.metadata,
|
|
53
|
+
await db.execute(`INSERT INTO ${tableName} (id, key, name, mime_type, size, local_path, remote_url, status, sha256, metadata, created_at, updated_at)
|
|
54
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
55
|
+
ON CONFLICT(key) DO UPDATE SET
|
|
56
|
+
name = excluded.name,
|
|
57
|
+
mime_type = excluded.mime_type,
|
|
58
|
+
size = excluded.size,
|
|
59
|
+
local_path = excluded.local_path,
|
|
60
|
+
remote_url = excluded.remote_url,
|
|
61
|
+
status = excluded.status,
|
|
62
|
+
sha256 = excluded.sha256,
|
|
63
|
+
metadata = excluded.metadata,
|
|
63
64
|
updated_at = excluded.updated_at;`, [
|
|
64
65
|
record.id,
|
|
65
66
|
record.key,
|
package/dist/utils/dynamic.js
CHANGED
|
@@ -16,7 +16,8 @@ function safeDynamicRequire(pkgName) {
|
|
|
16
16
|
}
|
|
17
17
|
// In Metro bundler (__r), calling require(variableString) triggers Metro's
|
|
18
18
|
// unknownModuleError console.error before throwing. Skip non-literal require in Metro.
|
|
19
|
-
if (typeof globalThis !== 'undefined' &&
|
|
19
|
+
if (typeof globalThis !== 'undefined' &&
|
|
20
|
+
typeof globalThis.__r === 'function') {
|
|
20
21
|
return undefined;
|
|
21
22
|
}
|
|
22
23
|
const req = typeof __non_webpack_require__ !== 'undefined'
|
|
@@ -36,7 +37,8 @@ async function safeDynamicImport(pkgName) {
|
|
|
36
37
|
if (syncMod !== undefined) {
|
|
37
38
|
return syncMod;
|
|
38
39
|
}
|
|
39
|
-
if (typeof globalThis !== 'undefined' &&
|
|
40
|
+
if (typeof globalThis !== 'undefined' &&
|
|
41
|
+
typeof globalThis.__r === 'function') {
|
|
40
42
|
return undefined;
|
|
41
43
|
}
|
|
42
44
|
// Metro's AST worker rejects `import(dynamicVariable)` with "Invalid call at line ...: import(...)".
|
package/dist/utils/logger.js
CHANGED
|
@@ -76,7 +76,8 @@ class SyncLogger {
|
|
|
76
76
|
error(message, ...args) {
|
|
77
77
|
if (!this.shouldLog('error'))
|
|
78
78
|
return;
|
|
79
|
-
if (message.includes('Network Offline') ||
|
|
79
|
+
if (message.includes('Network Offline') ||
|
|
80
|
+
message.includes('Failed to reach')) {
|
|
80
81
|
this.warn(message, ...args);
|
|
81
82
|
return;
|
|
82
83
|
}
|
|
@@ -155,17 +156,21 @@ function formatSupabaseError(error, context = {}) {
|
|
|
155
156
|
function formatFirestoreError(error, context = {}) {
|
|
156
157
|
if (!error)
|
|
157
158
|
return 'Unknown Firestore error';
|
|
158
|
-
const collection = context.collection
|
|
159
|
+
const collection = context.collection
|
|
160
|
+
? `"${context.collection}"`
|
|
161
|
+
: 'collection';
|
|
159
162
|
const op = context.operation ? ` (${context.operation})` : '';
|
|
160
163
|
const code = String(error.code || '').toLowerCase();
|
|
161
164
|
const rawMsg = error.message || String(error);
|
|
162
|
-
if (code.includes('permission-denied') ||
|
|
165
|
+
if (code.includes('permission-denied') ||
|
|
166
|
+
rawMsg.toLowerCase().includes('insufficient permissions')) {
|
|
163
167
|
return `🔒 Permission Denied on collection ${collection}${op}: Check your Firestore Security Rules.`;
|
|
164
168
|
}
|
|
165
169
|
if (code.includes('unauthenticated')) {
|
|
166
170
|
return `🔑 Unauthenticated on collection ${collection}${op}: User must be signed into Firebase Auth.`;
|
|
167
171
|
}
|
|
168
|
-
if (code.includes('unavailable') ||
|
|
172
|
+
if (code.includes('unavailable') ||
|
|
173
|
+
rawMsg.toLowerCase().includes('network')) {
|
|
169
174
|
return `📡 Firestore Offline: Backend unreachable. Mutations preserved in local SQLite outbox.`;
|
|
170
175
|
}
|
|
171
176
|
return `❌ Firestore error on ${collection}${op}: ${rawMsg}`;
|
|
@@ -179,10 +184,12 @@ function formatConvexError(error, context = {}) {
|
|
|
179
184
|
const table = context.table ? `"${context.table}"` : 'table';
|
|
180
185
|
const op = context.operation ? ` (${context.operation})` : '';
|
|
181
186
|
const rawMsg = error.message || String(error);
|
|
182
|
-
if (rawMsg.toLowerCase().includes('unauthorized') ||
|
|
187
|
+
if (rawMsg.toLowerCase().includes('unauthorized') ||
|
|
188
|
+
rawMsg.toLowerCase().includes('unauthenticated')) {
|
|
183
189
|
return `🔒 Convex Permission Denied on ${table}${op}: Request unauthorized.`;
|
|
184
190
|
}
|
|
185
|
-
if (rawMsg.toLowerCase().includes('network') ||
|
|
191
|
+
if (rawMsg.toLowerCase().includes('network') ||
|
|
192
|
+
rawMsg.toLowerCase().includes('failed to fetch')) {
|
|
186
193
|
return `📡 Convex Offline: Server unreachable. Mutations preserved in local SQLite outbox.`;
|
|
187
194
|
}
|
|
188
195
|
return `❌ Convex error on ${table}${op}: ${rawMsg}`;
|
|
@@ -21,7 +21,10 @@ function escapeCsvValue(val, delimiter) {
|
|
|
21
21
|
str = String(val);
|
|
22
22
|
}
|
|
23
23
|
// Quote if contains delimiter, double quote, or newlines
|
|
24
|
-
if (str.includes(delimiter) ||
|
|
24
|
+
if (str.includes(delimiter) ||
|
|
25
|
+
str.includes('"') ||
|
|
26
|
+
str.includes('\n') ||
|
|
27
|
+
str.includes('\r')) {
|
|
25
28
|
return `"${str.replace(/"/g, '""')}"`;
|
|
26
29
|
}
|
|
27
30
|
return str;
|
|
@@ -103,7 +106,7 @@ function parseCsv(text, delimiter = ',') {
|
|
|
103
106
|
async function exportTablesToJson(client, tables, options = {}) {
|
|
104
107
|
let targetTables = tables && tables.length > 0 ? tables : client.tables;
|
|
105
108
|
if (!options.includeInternal) {
|
|
106
|
-
targetTables = targetTables.filter(t => !(0, sql_js_1.isInternalTable)(t));
|
|
109
|
+
targetTables = targetTables.filter((t) => !(0, sql_js_1.isInternalTable)(t));
|
|
107
110
|
}
|
|
108
111
|
const result = {
|
|
109
112
|
version: 1,
|
|
@@ -165,7 +168,10 @@ async function importTablesFromJson(client, jsonString, options = {}) {
|
|
|
165
168
|
if (!id)
|
|
166
169
|
continue;
|
|
167
170
|
// Upsert row
|
|
168
|
-
await tx.mutate(table, id, 'INSERT', row, {
|
|
171
|
+
await tx.mutate(table, id, 'INSERT', row, {
|
|
172
|
+
primaryKey: pk,
|
|
173
|
+
skipOutbox,
|
|
174
|
+
});
|
|
169
175
|
rowCounts[table]++;
|
|
170
176
|
}
|
|
171
177
|
});
|
|
@@ -181,7 +187,9 @@ async function exportTableToCsv(client, tableName, options = {}) {
|
|
|
181
187
|
const rows = await client.findMany(tableName);
|
|
182
188
|
if (rows.length === 0) {
|
|
183
189
|
if (options.columns && includeHeader) {
|
|
184
|
-
return options.columns
|
|
190
|
+
return options.columns
|
|
191
|
+
.map((c) => escapeCsvValue(c, delimiter))
|
|
192
|
+
.join(delimiter);
|
|
185
193
|
}
|
|
186
194
|
return '';
|
|
187
195
|
}
|
|
@@ -191,10 +199,10 @@ async function exportTableToCsv(client, tableName, options = {}) {
|
|
|
191
199
|
: Object.keys(rows[0]);
|
|
192
200
|
const lines = [];
|
|
193
201
|
if (includeHeader) {
|
|
194
|
-
lines.push(columns.map(c => escapeCsvValue(c, delimiter)).join(delimiter));
|
|
202
|
+
lines.push(columns.map((c) => escapeCsvValue(c, delimiter)).join(delimiter));
|
|
195
203
|
}
|
|
196
204
|
for (const row of rows) {
|
|
197
|
-
const values = columns.map(col => escapeCsvValue(row[col], delimiter));
|
|
205
|
+
const values = columns.map((col) => escapeCsvValue(row[col], delimiter));
|
|
198
206
|
lines.push(values.join(delimiter));
|
|
199
207
|
}
|
|
200
208
|
return lines.join('\n');
|
|
@@ -218,7 +226,7 @@ async function importTableFromCsv(client, tableName, csvString, options = {}) {
|
|
|
218
226
|
let columns;
|
|
219
227
|
let dataRows;
|
|
220
228
|
if (hasHeader) {
|
|
221
|
-
columns = parsedRows[0].map(c => c.trim());
|
|
229
|
+
columns = parsedRows[0].map((c) => c.trim());
|
|
222
230
|
dataRows = parsedRows.slice(1);
|
|
223
231
|
}
|
|
224
232
|
else {
|
|
@@ -235,7 +243,8 @@ async function importTableFromCsv(client, tableName, csvString, options = {}) {
|
|
|
235
243
|
await tx.query(`DELETE FROM ${safeTable}`);
|
|
236
244
|
}
|
|
237
245
|
for (const rowVals of dataRows) {
|
|
238
|
-
if (rowVals.length === 0 ||
|
|
246
|
+
if (rowVals.length === 0 ||
|
|
247
|
+
(rowVals.length === 1 && !rowVals[0].trim())) {
|
|
239
248
|
continue;
|
|
240
249
|
}
|
|
241
250
|
const payload = {};
|
|
@@ -274,7 +283,10 @@ async function importTableFromCsv(client, tableName, csvString, options = {}) {
|
|
|
274
283
|
const id = String(payload[pk] ?? payload.id);
|
|
275
284
|
if (!id)
|
|
276
285
|
continue;
|
|
277
|
-
await tx.mutate(tableName, id, 'INSERT', payload, {
|
|
286
|
+
await tx.mutate(tableName, id, 'INSERT', payload, {
|
|
287
|
+
primaryKey: pk,
|
|
288
|
+
skipOutbox,
|
|
289
|
+
});
|
|
278
290
|
importedCount++;
|
|
279
291
|
}
|
|
280
292
|
});
|
package/package.json
CHANGED