1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { Statement, type StatementInstance } from "https://esm.town/v/postpostscript/sqliteBuilder";
import { sqliteDump, type SqliteDumpOptions } from "https://esm.town/v/postpostscript/sqliteDump";
import { createSqlite } from "https://esm.town/v/postpostscript/sqliteWasm";
import type { MaybePromise } from "https://esm.town/v/postpostscript/typeUtils";
import { blob as blobAPI } from "https://esm.town/v/std/blob";
import { type InStatement, sqlite as sqliteAPI } from "https://esm.town/v/std/sqlite";
import { type ResultSet } from "npm:@libsql/client";
export async function sqliteBackup(
{ name = `backup:sqlite:${new Date().getTime()}`, blob = blobAPI, dumpOptions = {} }: SqliteBackupOptions = {},
) {
return {
name,
content: await sqliteToBlob({
name,
blob,
dumpOptions,
}),
};
}
export async function sqliteToBlob(
{ name, dumpOptions, blob = blobAPI }: SqliteToBlobOptions,
) {
const statements = await sqliteDump(dumpOptions);
await blob.setJSON(name, statements);
return JSON.stringify(statements);
}
export async function sqliteFromBlob(
{ name, blob = blobAPI }: SqliteFromBlobOptions,
) {
const statements = await blob.getJSON(name);
if (!statements) {
throw new Error(`blob ${name} does not exist`);
}
const sqlite = createSqlite();
sqlite.batch(await statements);
return sqlite;
}
// export async function sqliteSync(
// { name, sqlite, callback, debounceMs = 500 }: SqliteSyncOptions,
// ) {
// let timeout;
// let lastStale;
// return {
// savingPromise: undefined as Promise<any> | undefined,
// async sync() {
// clearTimeout(timeout);
// const staleTime = this.lastStale;
// await this.savingPromise;
// this.savingPromise = sqliteDump({
// sqlite,
// }).then(callback);
// if (staleTime === this.lastStale) {
// this.stale = false;
// }
// return this.savingPromise;
// },
// markStale() {
// clearTimeout(timeout);
// timeout = setTimeout(() => {
// this.sync();
// }, debounceMs);
// this.stale = true;
// lastStale = new Date().getTime();
// },
// async execute(statement: InStatement) {
// statementWrites(statement) && this.markStale();
// return sqlite.execute(statement);
// },
// async batch(statements: InStatement[]) {
// statements.find(statementWrites) && this.markStale();
// return sqlite.batch(statements);
// },
// };
// }
function statementWrites(statement: InStatement) {
const sql = typeof statement === "string"
? statement
: statement.sql;
return !!` ${sql} `.match(/\s(INSERT|UPDATE|DELETE|CREATE)\s/i);
}
export type SqliteBackupOptions = {
name?: string;
dumpOptions?: SqliteDumpOptions;
blob?: {
setJSON: (...args: any[]) => any;
getJSON: (...args: any[]) => any;
};
};
export type SqliteToBlobOptions = {
name: string;
dumpOptions: SqliteDumpOptions;
blob?: {
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
March 9, 2024