|
1 | | -import * as fflate from "https://cdn.skypack.dev/[email protected]?min"; |
2 | | - |
3 | 1 | // REF: https://stackoverflow.com/questions/46338176/javascript-reading-local-file-to-uint8array-fast |
4 | | -(async function loadWasm() { |
5 | | - if (wasm !== undefined) return wasm; |
6 | | - |
| 2 | +async function fetchWasm() { |
7 | 3 | const input = new URL("surrealdb.wasm.gz", import.meta.url); |
8 | | - const imports = __wbg_get_imports(); |
9 | 4 | const response = await fetch(input); |
10 | | - |
11 | | - __wbg_init_memory(imports); |
12 | | - |
13 | 5 | const data = new Uint8Array(await response.arrayBuffer()); |
14 | 6 | const decompressed_data = fflate.gunzipSync(data); |
15 | 7 | const bytes = decompressed_data.buffer; |
16 | | - |
17 | | - const { instance, module } = await WebAssembly.instantiate(bytes, imports); |
18 | | - |
19 | | - console.log("Surreal had been initialized!"); |
20 | | - |
21 | | - return __wbg_finalize_init(instance, module); |
22 | | -})(); |
23 | | - |
24 | | -class SurrealWrapper { |
25 | | - async set(key, value) { |
26 | | - await this.db.set(key, JSON.parse(value)); |
27 | | - } |
28 | | - |
29 | | - async unset(key) { |
30 | | - await this.db.unset(key); |
31 | | - } |
32 | | - |
33 | | - async signup(credentials) { |
34 | | - return await this.db.signup(JSON.parse(credentials)); |
35 | | - } |
36 | | - |
37 | | - async signin(credentials) { |
38 | | - return await this.db.signin(JSON.parse(credentials)); |
39 | | - } |
40 | | - |
41 | | - async invalidate() { |
42 | | - await this.db.invalidate(); |
43 | | - } |
44 | | - |
45 | | - async authenticate(token) { |
46 | | - await this.db.authenticate(token); |
47 | | - } |
48 | | - |
49 | | - async patch(resource, data) { |
50 | | - return await this.db.patch(resource, JSON.parse(data)); |
51 | | - } |
52 | | - |
53 | | - async version() { |
54 | | - return await this.db.version(); |
55 | | - } |
56 | | - |
57 | | - async health() { |
58 | | - await this.db.health(); |
59 | | - } |
60 | | - /** |
61 | | - * Construct the database engine |
62 | | - * |
63 | | - * ```js |
64 | | - * const db = new SurrealDB(); |
65 | | - * ``` |
66 | | - */ |
67 | | - constructor() { |
68 | | - this.db = new Surreal(); |
69 | | - Object.freeze(this); |
70 | | - } |
71 | | - |
72 | | - /* @param {string} endpoint |
73 | | - * @param {any} opts |
74 | | - * @returns {Promise<void>} |
75 | | - */ |
76 | | - async connect(endpoint, opts) { |
77 | | - const options = typeof opts == "string" ? JSON.parse(opts) : opts; |
78 | | - await this.db.connect(endpoint, options); |
79 | | - } |
80 | | - |
81 | | - /** |
82 | | - * @param {any} value |
83 | | - * @returns {Promise<void>} |
84 | | - */ |
85 | | - // await db.use({ ns: "test", db: "test" }); |
86 | | - async use(value) { |
87 | | - const nsdb = typeof value == "string" ? JSON.parse(value) : value; |
88 | | - await this.db.use(nsdb); |
89 | | - } |
90 | | - |
91 | | - /** |
92 | | - * @param {string} resource |
93 | | - * @param {any} data |
94 | | - * @returns {Promise<any>} |
95 | | - */ |
96 | | - async create(resource, data) { |
97 | | - const value = typeof data == "string" ? JSON.parse(data) : data; |
98 | | - console.log("surrealdb.js create() value", value); |
99 | | - const result = await this.db.create(resource, value); |
100 | | - console.log("surrealdb.js create()", result); |
101 | | - return result; |
102 | | - } |
103 | | - |
104 | | - /** |
105 | | - * @param {string} resource |
106 | | - * @param {any} data |
107 | | - * @returns {Promise<any>} |
108 | | - */ |
109 | | - async update(resource, data) { |
110 | | - const value = typeof data == "string" ? JSON.parse(data) : data; |
111 | | - const result = await this.db.update(resource, value); |
112 | | - console.log("surrealdb.js update()", result); |
113 | | - return result; |
114 | | - } |
115 | | - |
116 | | - /** |
117 | | - * @param {string} resource |
118 | | - * @param {any} data |
119 | | - * @returns {Promise<any>} |
120 | | - */ |
121 | | - async merge(resource, data) { |
122 | | - const value = typeof data == "string" ? JSON.parse(data) : data; |
123 | | - const result = await this.db.merge(resource, value); |
124 | | - console.log("surrealdb.js merge()", result); |
125 | | - return result; |
126 | | - } |
127 | | - /** |
128 | | - * @param {string} resource |
129 | | - * @returns {Promise<any>} |
130 | | - */ |
131 | | - async select(resource) { |
132 | | - const result = await this.db.select(resource); |
133 | | - console.log("surrealdb.js select()", result); |
134 | | - return result; |
135 | | - } |
136 | | - |
137 | | - /** |
138 | | - * @param {string} sql |
139 | | - * @param {any} bindings |
140 | | - * @returns {Promise<any>} |
141 | | - */ |
142 | | - async query(sql, bindings) { |
143 | | - console.log("surrealdb.js query()", sql, bindings); |
144 | | - const bindings_value = |
145 | | - typeof bindings == "string" ? JSON.parse(bindings) : bindings; |
146 | | - console.log("surrealdb.js query() bindings_value", bindings_value); |
147 | | - const result = await this.db.query(sql, bindings_value); |
148 | | - console.log("surrealdb.js query() result", result); |
149 | | - return result; |
150 | | - } |
151 | | - |
152 | | - /** |
153 | | - * @param {string} resource |
154 | | - * @returns {Promise<any>} |
155 | | - */ |
156 | | - async delete(resource) { |
157 | | - const result = await this.db.delete(resource); |
158 | | - console.log("surrealdb.js delete()", result); |
159 | | - return result; |
160 | | - } |
161 | | -} |
162 | | - |
163 | | -if (typeof window !== "undefined") { |
164 | | - window.SurrealWrapper = SurrealWrapper; |
| 8 | + return bytes; |
165 | 9 | } |
0 commit comments