-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqldef-wasm.go
More file actions
83 lines (70 loc) · 2.11 KB
/
sqldef-wasm.go
File metadata and controls
83 lines (70 loc) · 2.11 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
76
77
78
79
80
81
82
83
//go:build js
// This is a light wasm wrapper around just the DDL conversion stuff
package main
import (
"fmt"
"strings"
"syscall/js"
"github.com/sqldef/sqldef/v3"
"github.com/sqldef/sqldef/v3/database"
"github.com/sqldef/sqldef/v3/parser"
"github.com/sqldef/sqldef/v3/schema"
)
// sqldefDiff (mode: string, desiredDDLs: string, currentDDLs: string, enableDrop: bool, callback: (err: string | null, result: string | null) -> void) -> bool
func sqldefDiff(this js.Value, args []js.Value) any {
if len(args) != 5 {
panic(fmt.Sprintf("expected 5 arguments, got %d", len(args)))
}
mode := args[0].String()
desiredDDLs := args[1].String()
currentDDLs := args[2].String()
enableDrop := args[3].Bool()
callback := args[4]
generatorMode := schema.GeneratorModeMysql
parserMode := parser.ParserModeMysql
switch mode {
case "postgres":
generatorMode = schema.GeneratorModePostgres
parserMode = parser.ParserModePostgres
case "mysql":
generatorMode = schema.GeneratorModeMysql
parserMode = parser.ParserModeMysql
case "sqlite3":
generatorMode = schema.GeneratorModeSQLite3
parserMode = parser.ParserModeSQLite3
case "mssql":
generatorMode = schema.GeneratorModeMssql
parserMode = parser.ParserModeMssql
default:
callback.Invoke(fmt.Errorf("unsupported database type: %s", mode), nil)
return false
}
sqlParser := database.NewParser(parserMode)
config := database.GeneratorConfig{
EnableDrop: enableDrop,
LegacyIgnoreQuotes: false,
}
defaultSchema := ""
ddls, err := schema.GenerateIdempotentDDLs(generatorMode, sqlParser, desiredDDLs, currentDDLs, config, defaultSchema)
out := strings.Join(ddls, ";\n")
if err != nil {
callback.Invoke(err.Error(), out)
return false
} else {
callback.Invoke(nil, out)
return true
}
}
// sqldefGetFullVersion () -> string
func sqldefGetFullVersion(this js.Value, args []js.Value) any {
return sqldef.GetFullVersion()
}
func main() {
c := make(chan bool)
exports := map[string]any{
"diff": js.FuncOf(sqldefDiff),
"getFullVersion": js.FuncOf(sqldefGetFullVersion),
}
js.Global().Set("_SQLDEF", js.ValueOf(exports))
<-c // block forever
}