-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqldef.mjs
More file actions
75 lines (62 loc) · 1.68 KB
/
sqldef.mjs
File metadata and controls
75 lines (62 loc) · 1.68 KB
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
import "./wasm_exec.js";
const { Go } = globalThis; // defined in wasm_exec.js
if (typeof Go === "undefined") {
throw new Error("Go is not initialized");
}
let SQLDEF = null;
let isLoading = false;
function formatMs(ms) {
return ms.toFixed(4).replace(/\.?0+$/, "");
}
async function getInstance() {
while (isLoading) {
await new Promise((resolve) => setTimeout(resolve));
}
if (SQLDEF) {
return SQLDEF;
}
isLoading = true;
try {
const t0 = performance.now();
const response = await fetch("sqldef.wasm");
const t1 = performance.now();
const wasmBinary = await response.arrayBuffer();
const t2 = performance.now();
const go = new Go();
const result = await WebAssembly.instantiate(wasmBinary, go.importObject);
go.run(result.instance); // defines globalThis._SQLDEF
const t3 = performance.now();
console.debug(
`sqldef.wasm loading time: fetch: ${formatMs(
t1 - t0
)}ms, instantiate: ${formatMs(t2 - t1)}ms, start: ${formatMs(t3 - t2)}ms`
);
SQLDEF = globalThis._SQLDEF;
} finally {
isLoading = false;
}
return SQLDEF;
}
export async function sqldef(
dbType,
desiredDDLs,
currentDDLs,
enableDrop = false
) {
if (typeof WebAssembly === "undefined") {
throw new Error("WebAssembly is not supported in your browser");
}
const sqldef = await getInstance();
return new Promise((resolve, reject) => {
sqldef.diff(dbType, desiredDDLs, currentDDLs, enableDrop, (err, ret) => {
if (err) {
return reject(new Error(err));
}
resolve(ret);
});
});
}
export async function getFullVersion() {
const sqldef = await getInstance();
return sqldef.getFullVersion();
}