@mygo-plugins/sqlite 0.0.0-stage → 0.2.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 MyGo contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,86 @@
1
- # Temporary Holding Version
1
+ # SQLite
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ SQLite for MyGo, with Go connections and the `@mygo-plugins/sqlite`
4
+ TypeScript client. The plugin ships prebuilt SQLite libraries and loads
5
+ them through purego, so apps build without cgo or a C compiler.
6
+
7
+ Register `mygo.Use(sqlite.Plugin)` only when the web frontend uses
8
+ `@mygo-plugins/sqlite`. Go and native UI apps call `sqlite.Open` directly;
9
+ they need no plugin registration and close their own connections.
10
+
11
+ ```go
12
+ import (
13
+ "github.com/egoist/mygo"
14
+ "github.com/egoist/mygo/plugins/sqlite"
15
+ )
16
+
17
+ func main() {
18
+ mygo.Use(sqlite.Plugin)
19
+ // Create windows, then call mygo.App.Run().
20
+ }
21
+ ```
22
+
23
+ ```ts
24
+ import { open } from "@mygo-plugins/sqlite";
25
+
26
+ const db = await open("app.db");
27
+ await db.execute("CREATE TABLE IF NOT EXISTS tasks (id INTEGER PRIMARY KEY, title TEXT, done INTEGER)");
28
+ const inserted = await db.execute("INSERT INTO tasks(title, done) VALUES (?, ?)", ["Learn MyGo", false]);
29
+ const { columns, rows } = await db.query("SELECT id, title, done FROM tasks WHERE done = ?", [false]);
30
+ await db.transaction([
31
+ { sql: "UPDATE tasks SET done = ? WHERE id = ?", params: [true, inserted.lastInsertId] },
32
+ { sql: "INSERT INTO tasks(title, done) VALUES (?, ?)", params: ["Ship the app", false] },
33
+ ]);
34
+ await db.close();
35
+ ```
36
+
37
+ Use `bun add @mygo-plugins/sqlite` once the npm package is published. In a
38
+ checkout, `bun install && bun run build` installs and builds the workspace
39
+ package. See [the full guide](../../docs/plugins/sqlite.md) for the Go API,
40
+ types, cancellation, options and packaging.
41
+
42
+ ## Building the native libraries
43
+
44
+ Prebuilt libraries are published in
45
+ [sqlite-3.53.4-1](https://github.com/egoist/mygo/releases/tag/sqlite-3.53.4-1).
46
+ CI and unpackaged programs download the matching library and verify its
47
+ SHA-256. Testing and building apps require no Zig installation.
48
+
49
+ To update SQLite or its C shim, with Zig **0.16.0** on the path:
50
+
51
+ ```sh
52
+ go generate ./plugins/sqlite
53
+ # Or build just this machine, from plugins/sqlite:
54
+ go run ./internal/libbuild -target host
55
+ ```
56
+
57
+ The generator downloads SQLite **3.53.4** from sqlite.org and checks its
58
+ published SHA3-256 before extracting `sqlite3.c` and `sqlite3.h`. It builds
59
+ macOS, Linux and Windows for amd64 and arm64, writes SHA-256 and release
60
+ URLs to `mygo-plugin.json`, and populates the CLI's native-library cache.
61
+ `mygo dev` and `mygo build` discover the manifest and bundle the matching
62
+ library; macOS universal builds combine both architectures. Linux targets
63
+ glibc 2.28 or later; macOS targets 12 or later. Binaries in `build/` are
64
+ ignored by git.
65
+
66
+ New binaries must be published before a fresh checkout or app build can
67
+ download them. The default asset release is `sqlite-3.53.4-1`:
68
+
69
+ ```sh
70
+ gh release create sqlite-3.53.4-1 plugins/sqlite/build/* --latest=false \
71
+ --title "SQLite 3.53.4 for MyGo" --notes "SQLite compiled with Zig 0.16.0 and the MyGo C shim."
72
+ ```
73
+
74
+ Change the asset tag (or pass `-release <URL>`) when the shim, compiler or
75
+ compile options change; regenerate and publish the manifest and binaries
76
+ together. `-src <directory>` uses a local amalgamation instead of the
77
+ verified download, intended for development.
78
+
79
+ `MYGO_SQLITE_LIBRARY` overrides the library path. Otherwise the plugin
80
+ looks in app resources, next to the executable, then the CLI's cache.
81
+ Unpackaged programs download a missing library and verify its SHA-256;
82
+ packaged apps never download native code at run time.
83
+
84
+ SQLite is public domain; the plugin and C shim use the repository's MIT
85
+ license. FTS5, RTree and JSON are enabled, with foreign keys on by default.
86
+ Loadable extensions, shared cache and deprecated APIs are omitted.
@@ -0,0 +1,40 @@
1
+ /** SQLite values: integers beyond Number's safe range are returned as bigint. */
2
+ export type Value = null | number | bigint | string | Uint8Array;
3
+ /** Booleans bind as SQLite integers 0 and 1. */
4
+ export type Parameter = Value | boolean;
5
+ export interface OpenOptions {
6
+ readOnly?: boolean;
7
+ }
8
+ export interface Result {
9
+ changes: number;
10
+ /** The last inserted rowid on this connection, without precision loss. */
11
+ lastInsertId: bigint;
12
+ }
13
+ /** Ordered columns and cells preserve duplicate column names. */
14
+ export interface Rows {
15
+ columns: string[];
16
+ rows: Value[][];
17
+ }
18
+ export interface Statement {
19
+ sql: string;
20
+ params?: readonly Parameter[];
21
+ }
22
+ /** Opens a file name in the Go plugin's directory, or a private :memory: database. */
23
+ export declare function open(name: string, options?: OpenOptions): Promise<Database>;
24
+ /** A page-owned connection. It closes when the page navigates or the app quits. */
25
+ export declare class Database {
26
+ private readonly id;
27
+ private closing?;
28
+ private closed;
29
+ private constructor();
30
+ static open(name: string, options?: OpenOptions): Promise<Database>;
31
+ /** Executes exactly one statement. Use placeholders, never interpolate values. */
32
+ execute(sql: string, params?: readonly Parameter[]): Promise<Result>;
33
+ /** Queries one statement, including RETURNING; select a bounded page for large results. */
34
+ query(sql: string, params?: readonly Parameter[]): Promise<Rows>;
35
+ /** Runs an atomic BEGIN IMMEDIATE batch. A failure rolls back every statement. */
36
+ transaction(statements: readonly Statement[]): Promise<Result[]>;
37
+ /** Closes this connection. Concurrent and repeated closes share the same call. */
38
+ close(): Promise<void>;
39
+ private assertOpen;
40
+ }
package/dist/index.js ADDED
@@ -0,0 +1,104 @@
1
+ // src/index.ts
2
+ import { call } from "mygo-runtime";
3
+ async function open(name, options = {}) {
4
+ return Database.open(name, options);
5
+ }
6
+
7
+ class Database {
8
+ id;
9
+ closing;
10
+ closed = false;
11
+ constructor(id) {
12
+ this.id = id;
13
+ }
14
+ static async open(name, options = {}) {
15
+ const id = await call("plugin:sqlite.Open", name, { readOnly: options.readOnly ?? false });
16
+ return new Database(id);
17
+ }
18
+ async execute(sql, params = []) {
19
+ this.assertOpen();
20
+ return result(await call("plugin:sqlite.Execute", this.id, sql, params.map(encode)));
21
+ }
22
+ async query(sql, params = []) {
23
+ this.assertOpen();
24
+ const value = await call("plugin:sqlite.Query", this.id, sql, params.map(encode));
25
+ return { columns: value.columns, rows: value.rows.map((row) => row.map(decode)) };
26
+ }
27
+ async transaction(statements) {
28
+ this.assertOpen();
29
+ const batch = statements.map((stmt) => ({ sql: stmt.sql, args: (stmt.params ?? []).map(encode) }));
30
+ return (await call("plugin:sqlite.Transaction", this.id, batch)).map(result);
31
+ }
32
+ close() {
33
+ if (this.closed)
34
+ return Promise.resolve();
35
+ if (!this.closing) {
36
+ this.closing = call("plugin:sqlite.Close", this.id).then(() => {
37
+ this.closed = true;
38
+ }, (error) => {
39
+ this.closing = undefined;
40
+ throw error;
41
+ });
42
+ }
43
+ return this.closing;
44
+ }
45
+ assertOpen() {
46
+ if (this.closed || this.closing)
47
+ throw new Error("sqlite: database is closed");
48
+ }
49
+ }
50
+ function result(value) {
51
+ return { changes: value.changes, lastInsertId: BigInt(value.lastInsertId) };
52
+ }
53
+ function encode(value) {
54
+ if (value === null)
55
+ return { type: "null" };
56
+ if (typeof value === "boolean")
57
+ return { type: "integer", integer: value ? "1" : "0" };
58
+ if (typeof value === "bigint") {
59
+ if (value < -(1n << 63n) || value >= 1n << 63n)
60
+ throw new RangeError("sqlite: integer exceeds signed 64-bit range");
61
+ return { type: "integer", integer: String(value) };
62
+ }
63
+ if (typeof value === "number") {
64
+ if (!Number.isFinite(value))
65
+ throw new TypeError("sqlite: number must be finite");
66
+ if (Number.isInteger(value)) {
67
+ if (!Number.isSafeInteger(value))
68
+ throw new RangeError("sqlite: use bigint for integers outside Number's safe range");
69
+ return { type: "integer", integer: String(value) };
70
+ }
71
+ return { type: "real", real: value };
72
+ }
73
+ if (typeof value === "string")
74
+ return { type: "text", text: value };
75
+ if (value instanceof Uint8Array) {
76
+ let text = "";
77
+ for (let i = 0;i < value.length; i += 8192)
78
+ text += String.fromCharCode(...value.subarray(i, i + 8192));
79
+ return { type: "blob", blob: btoa(text) };
80
+ }
81
+ throw new TypeError("sqlite: unsupported parameter");
82
+ }
83
+ function decode(value) {
84
+ switch (value.type) {
85
+ case "null":
86
+ return null;
87
+ case "integer": {
88
+ const n = BigInt(value.integer);
89
+ return n >= BigInt(Number.MIN_SAFE_INTEGER) && n <= BigInt(Number.MAX_SAFE_INTEGER) ? Number(n) : n;
90
+ }
91
+ case "real":
92
+ return Number(value.special ?? value.real ?? 0);
93
+ case "text":
94
+ return value.text ?? "";
95
+ case "blob":
96
+ return Uint8Array.from(atob(value.blob ?? ""), (c) => c.charCodeAt(0));
97
+ default:
98
+ throw new TypeError(`sqlite: unknown value type ${value.type}`);
99
+ }
100
+ }
101
+ export {
102
+ Database,
103
+ open
104
+ };
package/package.json CHANGED
@@ -1,6 +1,42 @@
1
1
  {
2
2
  "name": "@mygo-plugins/sqlite",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.2.17",
4
+ "description": "Parameterized SQLite queries and transactions for MyGo desktop apps",
5
+ "keywords": [
6
+ "mygo",
7
+ "desktop",
8
+ "sqlite",
9
+ "go",
10
+ "zig"
11
+ ],
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git+https://github.com/egoist/mygo.git",
15
+ "directory": "plugins/sqlite"
16
+ },
17
+ "license": "MIT",
18
+ "type": "module",
19
+ "sideEffects": false,
20
+ "exports": {
21
+ ".": {
22
+ "types": "./dist/index.d.ts",
23
+ "default": "./dist/index.js"
24
+ }
25
+ },
26
+ "types": "./dist/index.d.ts",
27
+ "files": [
28
+ "dist",
29
+ "README.md",
30
+ "LICENSE"
31
+ ],
32
+ "scripts": {
33
+ "build": "bun run ../build.ts",
34
+ "test": "bun test"
35
+ },
36
+ "peerDependencies": {
37
+ "mygo-runtime": "^0.2.17"
38
+ },
39
+ "devDependencies": {
40
+ "mygo-runtime": "^0.2.17"
41
+ }
42
+ }