Skip to content

Commit 82671a7

Browse files
committed
use es modules
1 parent 18b1cb3 commit 82671a7

File tree

5 files changed

+151
-136
lines changed

5 files changed

+151
-136
lines changed

Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
sqldef.wasm: go.mod go.sum sqldef-wasm.go
1+
sqldef.wasm: go.mod go.sum sqldef-wasm.go wasm_exec.js
22
GOOS=js GOARCH=wasm go build $(GOFLAGS) -o sqldef.wasm ./sqldef-wasm.go
3+
4+
wasm_exec.js:
35
cp $$(go env GOROOT)/lib/wasm/wasm_exec.js .
46

57
dev: sqldef.wasm

app.mjs

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import { sqldef } from "./sqldef_browser.mjs";
2+
3+
const dbType = document.getElementById("dbType");
4+
const inputA = document.getElementById("inputA");
5+
const inputB = document.getElementById("inputB");
6+
const outputUp = document.getElementById("outputUp");
7+
const errorUp = document.getElementById("errorUp");
8+
const outputDown = document.getElementById("outputDown");
9+
const errorDown = document.getElementById("errorDown");
10+
11+
const schemaExamples = {
12+
mysql: {
13+
current: `CREATE TABLE users (
14+
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
15+
name VARCHAR(128) DEFAULT 'konsumer'
16+
) Engine=InnoDB DEFAULT CHARSET=utf8mb4;`,
17+
desired: `CREATE TABLE users (
18+
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
19+
name VARCHAR(128) DEFAULT 'konsumer',
20+
created_at DATETIME NOT NULL
21+
) Engine=InnoDB DEFAULT CHARSET=utf8mb4;`,
22+
},
23+
postgres: {
24+
current: `CREATE TABLE users (
25+
id BIGSERIAL PRIMARY KEY,
26+
name VARCHAR(128) DEFAULT 'konsumer'
27+
);`,
28+
desired: `CREATE TABLE users (
29+
id BIGSERIAL PRIMARY KEY,
30+
name VARCHAR(128) DEFAULT 'konsumer',
31+
created_at TIMESTAMP NOT NULL
32+
);`,
33+
},
34+
sqlite3: {
35+
current: `CREATE TABLE users (
36+
id INTEGER PRIMARY KEY AUTOINCREMENT,
37+
name TEXT DEFAULT 'konsumer'
38+
);`,
39+
desired: `CREATE TABLE users (
40+
id INTEGER PRIMARY KEY AUTOINCREMENT,
41+
name TEXT DEFAULT 'konsumer',
42+
created_at TEXT NOT NULL
43+
);`,
44+
},
45+
mssql: {
46+
current: `CREATE TABLE users (
47+
id BIGINT IDENTITY(1,1) PRIMARY KEY,
48+
name NVARCHAR(128) DEFAULT 'konsumer'
49+
);`,
50+
desired: `CREATE TABLE users (
51+
id BIGINT IDENTITY(1,1) PRIMARY KEY,
52+
name NVARCHAR(128) DEFAULT 'konsumer',
53+
created_at DATETIME NOT NULL
54+
);`,
55+
},
56+
};
57+
58+
async function runDiff() {
59+
// Run up diff (current -> desired)
60+
outputUp.style.display = "none";
61+
errorUp.style.display = "none";
62+
try {
63+
const result = await sqldef(
64+
dbType.value,
65+
inputB.value,
66+
inputA.value
67+
);
68+
outputUp.innerHTML = result;
69+
outputUp.style.display = "block";
70+
} catch (e) {
71+
errorUp.style.display = "block";
72+
errorUp.innerHTML = e.message;
73+
}
74+
75+
// Run down diff (desired -> current)
76+
outputDown.style.display = "none";
77+
errorDown.style.display = "none";
78+
try {
79+
const result = await sqldef(
80+
dbType.value,
81+
inputA.value,
82+
inputB.value
83+
);
84+
outputDown.innerHTML = result;
85+
outputDown.style.display = "block";
86+
} catch (e) {
87+
errorDown.style.display = "block";
88+
errorDown.innerHTML = e.message;
89+
}
90+
}
91+
92+
dbType.addEventListener("change", () => {
93+
const examples = schemaExamples[dbType.value];
94+
if (examples) {
95+
inputA.value = examples.current;
96+
inputB.value = examples.desired;
97+
}
98+
runDiff();
99+
});
100+
101+
inputA.addEventListener("input", runDiff);
102+
inputB.addEventListener("input", runDiff);
103+
104+
// Run diff on initial load
105+
runDiff();

index.html

Lines changed: 4 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020

2121
<link href="https://fonts.googleapis.com/css?family=Raleway&display=swap" rel="stylesheet">
2222

23-
<script src="wasm_exec.js"></script>
24-
<script src="sqldef_browser.js"></script>
2523
<style>
2624
body {
2725
font-family: 'Raleway', sans-serif;
@@ -155,107 +153,11 @@ <h4 class="schema-header">Desired schema</h4>
155153
<pre id="errorDown" class="error"></pre>
156154
</div>
157155

158-
<script>
159-
const dbType = document.getElementById('dbType');
160-
const inputA = document.getElementById('inputA');
161-
const inputB = document.getElementById('inputB');
162-
const outputUp = document.getElementById('outputUp');
163-
const errorUp = document.getElementById('errorUp');
164-
const outputDown = document.getElementById('outputDown');
165-
const errorDown = document.getElementById('errorDown');
166-
167-
const schemaExamples = {
168-
mysql: {
169-
current: `CREATE TABLE users (
170-
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
171-
name VARCHAR(128) DEFAULT 'konsumer'
172-
) Engine=InnoDB DEFAULT CHARSET=utf8mb4;`,
173-
desired: `CREATE TABLE users (
174-
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
175-
name VARCHAR(128) DEFAULT 'konsumer',
176-
created_at DATETIME NOT NULL
177-
) Engine=InnoDB DEFAULT CHARSET=utf8mb4;`
178-
},
179-
postgres: {
180-
current: `CREATE TABLE users (
181-
id BIGSERIAL PRIMARY KEY,
182-
name VARCHAR(128) DEFAULT 'konsumer'
183-
);`,
184-
desired: `CREATE TABLE users (
185-
id BIGSERIAL PRIMARY KEY,
186-
name VARCHAR(128) DEFAULT 'konsumer',
187-
created_at TIMESTAMP NOT NULL
188-
);`
189-
},
190-
sqlite3: {
191-
current: `CREATE TABLE users (
192-
id INTEGER PRIMARY KEY AUTOINCREMENT,
193-
name TEXT DEFAULT 'konsumer'
194-
);`,
195-
desired: `CREATE TABLE users (
196-
id INTEGER PRIMARY KEY AUTOINCREMENT,
197-
name TEXT DEFAULT 'konsumer',
198-
created_at TEXT NOT NULL
199-
);`
200-
},
201-
mssql: {
202-
current: `CREATE TABLE users (
203-
id BIGINT IDENTITY(1,1) PRIMARY KEY,
204-
name NVARCHAR(128) DEFAULT 'konsumer'
205-
);`,
206-
desired: `CREATE TABLE users (
207-
id BIGINT IDENTITY(1,1) PRIMARY KEY,
208-
name NVARCHAR(128) DEFAULT 'konsumer',
209-
created_at DATETIME NOT NULL
210-
);`
211-
},
212-
};
213-
214-
async function runDiff() {
215-
// Run up diff (current -> desired)
216-
outputUp.style.display = 'none';
217-
errorUp.style.display = 'none';
218-
try {
219-
const result = await window.sqldef(dbType.value, inputB.value, inputA.value);
220-
outputUp.innerHTML = result;
221-
outputUp.style.display = 'block';
222-
} catch (e) {
223-
errorUp.style.display = 'block';
224-
errorUp.innerHTML = e.message;
225-
}
226-
227-
// Run down diff (desired -> current)
228-
outputDown.style.display = 'none';
229-
errorDown.style.display = 'none';
230-
try {
231-
const result = await window.sqldef(dbType.value, inputA.value, inputB.value);
232-
outputDown.innerHTML = result;
233-
outputDown.style.display = 'block';
234-
} catch (e) {
235-
errorDown.style.display = 'block';
236-
errorDown.innerHTML = e.message;
237-
}
238-
}
239-
240-
dbType.addEventListener('change', () => {
241-
const examples = schemaExamples[dbType.value];
242-
if (examples) {
243-
inputA.value = examples.current;
244-
inputB.value = examples.desired;
245-
}
246-
runDiff();
247-
})
248-
249-
inputA.addEventListener('input', runDiff);
250-
inputB.addEventListener('input', runDiff);
251-
252-
// Run diff on initial load
253-
runDiff();
254-
</script>
255-
256156
<footer>
257-
<p><a href="https://github.com/sqldef/sqldef">https://github.com/sqldef/sqldef</a></p>
157+
<p>
158+
<a href="https://github.com/sqldef/sqldef">https://github.com/sqldef/sqldef</a>
159+
</p>
258160
</footer>
161+
<script src="app.mjs" type="module"></script>
259162
</body>
260-
261163
</html>

sqldef_browser.js

Lines changed: 0 additions & 33 deletions
This file was deleted.

sqldef_browser.mjs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import "./wasm_exec.js";
2+
const { Go } = globalThis; // defined in wasm_exec.js
3+
4+
if (typeof Go === "undefined") {
5+
throw new Error("Go is not initialized");
6+
}
7+
8+
let SQLDEF = null;
9+
10+
const getInstance = async () => {
11+
if (SQLDEF) {
12+
return SQLDEF;
13+
}
14+
15+
const response = await fetch("sqldef.wasm");
16+
const wasmBinary = await response.arrayBuffer();
17+
const go = new Go();
18+
const result = await WebAssembly.instantiate(wasmBinary, go.importObject);
19+
go.run(result.instance); // defines globalThis._SQLDEF
20+
SQLDEF = globalThis._SQLDEF;
21+
return SQLDEF;
22+
};
23+
24+
export async function sqldef(dbType, desiredDDLs, currentDDLs) {
25+
if (typeof WebAssembly === "undefined") {
26+
throw new Error("WebAssembly is not supported in your browser");
27+
}
28+
29+
const SQLDEF = await getInstance();
30+
31+
return new Promise((resolve, reject) => {
32+
SQLDEF.diff(dbType, desiredDDLs, currentDDLs, (err, ret) => {
33+
if (err) {
34+
return reject(new Error(err));
35+
}
36+
resolve(ret);
37+
});
38+
});
39+
}

0 commit comments

Comments
 (0)