@tauri-apps/plugin-sql 2.0.0-alpha.0

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.spdx ADDED
@@ -0,0 +1,20 @@
1
+ SPDXVersion: SPDX-2.1
2
+ DataLicense: CC0-1.0
3
+ PackageName: tauri
4
+ DataFormat: SPDXRef-1
5
+ PackageSupplier: Organization: The Tauri Programme in the Commons Conservancy
6
+ PackageHomePage: https://tauri.app
7
+ PackageLicenseDeclared: Apache-2.0
8
+ PackageLicenseDeclared: MIT
9
+ PackageCopyrightText: 2019-2022, The Tauri Programme in the Commons Conservancy
10
+ PackageSummary: <text>Tauri is a rust project that enables developers to make secure
11
+ and small desktop applications using a web frontend.
12
+ </text>
13
+ PackageComment: <text>The package includes the following libraries; see
14
+ Relationship information.
15
+ </text>
16
+ Created: 2019-05-20T09:00:00Z
17
+ PackageDownloadLocation: git://github.com/tauri-apps/tauri
18
+ PackageDownloadLocation: git+https://github.com/tauri-apps/tauri.git
19
+ PackageDownloadLocation: git+ssh://github.com/tauri-apps/tauri.git
20
+ Creator: Person: Daniel Thompson-Yvetot
package/README.md ADDED
@@ -0,0 +1,85 @@
1
+ ![plugin-sql](banner.png)
2
+
3
+ Interface with SQL databases through [sqlx](https://github.com/launchbadge/sqlx). It supports the `sqlite`, `mysql` and `postgres` drivers, enabled by a Cargo feature.
4
+
5
+ ## Install
6
+
7
+ _This plugin requires a Rust version of at least **1.65**_
8
+
9
+ There are three general methods of installation that we can recommend.
10
+
11
+ 1. Use crates.io and npm (easiest, and requires you to trust that our publishing pipeline worked)
12
+ 2. Pull sources directly from Github using git tags / revision hashes (most secure)
13
+ 3. Git submodule install this repo in your tauri project and then use file protocol to ingest the source (most secure, but inconvenient to use)
14
+
15
+ Install the Core plugin by adding the following to your `Cargo.toml` file:
16
+
17
+ `src-tauri/Cargo.toml`
18
+
19
+ ```toml
20
+ [dependencies.tauri-plugin-sql]
21
+ features = ["sqlite"] # or "postgres", or "mysql"
22
+ version = "2.0.0-alpha"
23
+ # alternatively with Git
24
+ git = "https://github.com/tauri-apps/plugins-workspace"
25
+ branch = "v2"
26
+ ```
27
+
28
+ You can install the JavaScript Guest bindings using your preferred JavaScript package manager:
29
+
30
+ > Note: Since most JavaScript package managers are unable to install packages from git monorepos we provide read-only mirrors of each plugin. This makes installation option 2 more ergonomic to use.
31
+
32
+ ```sh
33
+ pnpm add @tauri-apps/plugin-sql
34
+ # or
35
+ npm add @tauri-apps/plugin-sql
36
+ # or
37
+ yarn add @tauri-apps/plugin-sql
38
+
39
+ # alternatively with Git:
40
+ pnpm add https://github.com/tauri-apps/tauri-plugin-sql#v2
41
+ # or
42
+ npm add https://github.com/tauri-apps/tauri-plugin-sql#v2
43
+ # or
44
+ yarn add https://github.com/tauri-apps/tauri-plugin-sql#v2
45
+ ```
46
+
47
+ ## Usage
48
+
49
+ First you need to register the core plugin with Tauri:
50
+
51
+ `src-tauri/src/main.rs`
52
+
53
+ ```rust
54
+ fn main() {
55
+ tauri::Builder::default()
56
+ .plugin(tauri_plugin_sql::Builder::default().build())
57
+ .run(tauri::generate_context!())
58
+ .expect("error while running tauri application");
59
+ }
60
+ ```
61
+
62
+ Afterwards all the plugin's APIs are available through the JavaScript guest bindings:
63
+
64
+ ```javascript
65
+ import Database from "@tauri-apps/plugin-sql";
66
+
67
+ // sqlite. The path is relative to `tauri::api::path::BaseDirectory::App`.
68
+ const db = await Database.load("sqlite:test.db");
69
+ // mysql
70
+ const db = await Database.load("mysql://user:pass@host/database");
71
+ // postgres
72
+ const db = await Database.load("postgres://postgres:password@localhost/test");
73
+
74
+ await db.execute("INSERT INTO ...");
75
+ ```
76
+
77
+ ## Contributing
78
+
79
+ PRs accepted. Please make sure to read the Contributing Guide before making a pull request.
80
+
81
+ ## License
82
+
83
+ Code: (c) 2015 - Present - The Tauri Programme within The Commons Conservancy.
84
+
85
+ MIT or MIT/Apache 2.0 where applicable.
@@ -0,0 +1,100 @@
1
+ declare global {
2
+ interface Window {
3
+ __TAURI_INVOKE__: <T>(cmd: string, args?: unknown) => Promise<T>;
4
+ }
5
+ }
6
+ export interface QueryResult {
7
+ /** The number of rows affected by the query. */
8
+ rowsAffected: number;
9
+ /**
10
+ * The last inserted `id`.
11
+ *
12
+ * This value is always `0` when using the Postgres driver. If the
13
+ * last inserted id is required on Postgres, the `select` function
14
+ * must be used, with a `RETURNING` clause
15
+ * (`INSERT INTO todos (title) VALUES ($1) RETURNING id`).
16
+ */
17
+ lastInsertId: number;
18
+ }
19
+ /**
20
+ * **Database**
21
+ *
22
+ * The `Database` class serves as the primary interface for
23
+ * communicating with the rust side of the sql plugin.
24
+ */
25
+ export default class Database {
26
+ path: string;
27
+ constructor(path: string);
28
+ /**
29
+ * **load**
30
+ *
31
+ * A static initializer which connects to the underlying database and
32
+ * returns a `Database` instance once a connection to the database is established.
33
+ *
34
+ * # Sqlite
35
+ *
36
+ * The path is relative to `tauri::api::path::BaseDirectory::App` and must start with `sqlite:`.
37
+ *
38
+ * @example
39
+ * ```ts
40
+ * const db = await Database.load("sqlite:test.db");
41
+ * ```
42
+ */
43
+ static load(path: string): Promise<Database>;
44
+ /**
45
+ * **get**
46
+ *
47
+ * A static initializer which synchronously returns an instance of
48
+ * the Database class while deferring the actual database connection
49
+ * until the first invocation or selection on the database.
50
+ *
51
+ * # Sqlite
52
+ *
53
+ * The path is relative to `tauri::api::path::BaseDirectory::App` and must start with `sqlite:`.
54
+ *
55
+ * @example
56
+ * ```ts
57
+ * const db = Database.get("sqlite:test.db");
58
+ * ```
59
+ */
60
+ static get(path: string): Database;
61
+ /**
62
+ * **execute**
63
+ *
64
+ * Passes a SQL expression to the database for execution.
65
+ *
66
+ * @example
67
+ * ```ts
68
+ * const result = await db.execute(
69
+ * "UPDATE todos SET title = $1, completed = $2 WHERE id = $3",
70
+ * [ todos.title, todos.status, todos.id ]
71
+ * );
72
+ * ```
73
+ */
74
+ execute(query: string, bindValues?: unknown[]): Promise<QueryResult>;
75
+ /**
76
+ * **select**
77
+ *
78
+ * Passes in a SELECT query to the database for execution.
79
+ *
80
+ * @example
81
+ * ```ts
82
+ * const result = await db.select(
83
+ * "SELECT * from todos WHERE id = $1", id
84
+ * );
85
+ * ```
86
+ */
87
+ select<T>(query: string, bindValues?: unknown[]): Promise<T>;
88
+ /**
89
+ * **close**
90
+ *
91
+ * Closes the database connection pool.
92
+ *
93
+ * @example
94
+ * ```ts
95
+ * const success = await db.close()
96
+ * ```
97
+ * @param db - Optionally state the name of a database if you are managing more than one. Otherwise, all database pools will be in scope.
98
+ */
99
+ close(db?: string): Promise<boolean>;
100
+ }
@@ -0,0 +1,118 @@
1
+ // Copyright 2019-2023 Tauri Programme within The Commons Conservancy
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ // SPDX-License-Identifier: MIT
4
+ /**
5
+ * **Database**
6
+ *
7
+ * The `Database` class serves as the primary interface for
8
+ * communicating with the rust side of the sql plugin.
9
+ */
10
+ class Database {
11
+ constructor(path) {
12
+ this.path = path;
13
+ }
14
+ /**
15
+ * **load**
16
+ *
17
+ * A static initializer which connects to the underlying database and
18
+ * returns a `Database` instance once a connection to the database is established.
19
+ *
20
+ * # Sqlite
21
+ *
22
+ * The path is relative to `tauri::api::path::BaseDirectory::App` and must start with `sqlite:`.
23
+ *
24
+ * @example
25
+ * ```ts
26
+ * const db = await Database.load("sqlite:test.db");
27
+ * ```
28
+ */
29
+ static async load(path) {
30
+ const _path = await window.__TAURI_INVOKE__("plugin:sql|load", {
31
+ db: path,
32
+ });
33
+ return new Database(_path);
34
+ }
35
+ /**
36
+ * **get**
37
+ *
38
+ * A static initializer which synchronously returns an instance of
39
+ * the Database class while deferring the actual database connection
40
+ * until the first invocation or selection on the database.
41
+ *
42
+ * # Sqlite
43
+ *
44
+ * The path is relative to `tauri::api::path::BaseDirectory::App` and must start with `sqlite:`.
45
+ *
46
+ * @example
47
+ * ```ts
48
+ * const db = Database.get("sqlite:test.db");
49
+ * ```
50
+ */
51
+ static get(path) {
52
+ return new Database(path);
53
+ }
54
+ /**
55
+ * **execute**
56
+ *
57
+ * Passes a SQL expression to the database for execution.
58
+ *
59
+ * @example
60
+ * ```ts
61
+ * const result = await db.execute(
62
+ * "UPDATE todos SET title = $1, completed = $2 WHERE id = $3",
63
+ * [ todos.title, todos.status, todos.id ]
64
+ * );
65
+ * ```
66
+ */
67
+ async execute(query, bindValues) {
68
+ const [rowsAffected, lastInsertId] = await window.__TAURI_INVOKE__("plugin:sql|execute", {
69
+ db: this.path,
70
+ query,
71
+ values: bindValues !== null && bindValues !== void 0 ? bindValues : [],
72
+ });
73
+ return {
74
+ lastInsertId,
75
+ rowsAffected,
76
+ };
77
+ }
78
+ /**
79
+ * **select**
80
+ *
81
+ * Passes in a SELECT query to the database for execution.
82
+ *
83
+ * @example
84
+ * ```ts
85
+ * const result = await db.select(
86
+ * "SELECT * from todos WHERE id = $1", id
87
+ * );
88
+ * ```
89
+ */
90
+ async select(query, bindValues) {
91
+ const result = await window.__TAURI_INVOKE__("plugin:sql|select", {
92
+ db: this.path,
93
+ query,
94
+ values: bindValues !== null && bindValues !== void 0 ? bindValues : [],
95
+ });
96
+ return result;
97
+ }
98
+ /**
99
+ * **close**
100
+ *
101
+ * Closes the database connection pool.
102
+ *
103
+ * @example
104
+ * ```ts
105
+ * const success = await db.close()
106
+ * ```
107
+ * @param db - Optionally state the name of a database if you are managing more than one. Otherwise, all database pools will be in scope.
108
+ */
109
+ async close(db) {
110
+ const success = await window.__TAURI_INVOKE__("plugin:sql|close", {
111
+ db,
112
+ });
113
+ return success;
114
+ }
115
+ }
116
+
117
+ export { Database as default };
118
+ //# sourceMappingURL=index.min.js.map
@@ -0,0 +1,118 @@
1
+ // Copyright 2019-2023 Tauri Programme within The Commons Conservancy
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ // SPDX-License-Identifier: MIT
4
+ /**
5
+ * **Database**
6
+ *
7
+ * The `Database` class serves as the primary interface for
8
+ * communicating with the rust side of the sql plugin.
9
+ */
10
+ class Database {
11
+ constructor(path) {
12
+ this.path = path;
13
+ }
14
+ /**
15
+ * **load**
16
+ *
17
+ * A static initializer which connects to the underlying database and
18
+ * returns a `Database` instance once a connection to the database is established.
19
+ *
20
+ * # Sqlite
21
+ *
22
+ * The path is relative to `tauri::api::path::BaseDirectory::App` and must start with `sqlite:`.
23
+ *
24
+ * @example
25
+ * ```ts
26
+ * const db = await Database.load("sqlite:test.db");
27
+ * ```
28
+ */
29
+ static async load(path) {
30
+ const _path = await window.__TAURI_INVOKE__("plugin:sql|load", {
31
+ db: path,
32
+ });
33
+ return new Database(_path);
34
+ }
35
+ /**
36
+ * **get**
37
+ *
38
+ * A static initializer which synchronously returns an instance of
39
+ * the Database class while deferring the actual database connection
40
+ * until the first invocation or selection on the database.
41
+ *
42
+ * # Sqlite
43
+ *
44
+ * The path is relative to `tauri::api::path::BaseDirectory::App` and must start with `sqlite:`.
45
+ *
46
+ * @example
47
+ * ```ts
48
+ * const db = Database.get("sqlite:test.db");
49
+ * ```
50
+ */
51
+ static get(path) {
52
+ return new Database(path);
53
+ }
54
+ /**
55
+ * **execute**
56
+ *
57
+ * Passes a SQL expression to the database for execution.
58
+ *
59
+ * @example
60
+ * ```ts
61
+ * const result = await db.execute(
62
+ * "UPDATE todos SET title = $1, completed = $2 WHERE id = $3",
63
+ * [ todos.title, todos.status, todos.id ]
64
+ * );
65
+ * ```
66
+ */
67
+ async execute(query, bindValues) {
68
+ const [rowsAffected, lastInsertId] = await window.__TAURI_INVOKE__("plugin:sql|execute", {
69
+ db: this.path,
70
+ query,
71
+ values: bindValues !== null && bindValues !== void 0 ? bindValues : [],
72
+ });
73
+ return {
74
+ lastInsertId,
75
+ rowsAffected,
76
+ };
77
+ }
78
+ /**
79
+ * **select**
80
+ *
81
+ * Passes in a SELECT query to the database for execution.
82
+ *
83
+ * @example
84
+ * ```ts
85
+ * const result = await db.select(
86
+ * "SELECT * from todos WHERE id = $1", id
87
+ * );
88
+ * ```
89
+ */
90
+ async select(query, bindValues) {
91
+ const result = await window.__TAURI_INVOKE__("plugin:sql|select", {
92
+ db: this.path,
93
+ query,
94
+ values: bindValues !== null && bindValues !== void 0 ? bindValues : [],
95
+ });
96
+ return result;
97
+ }
98
+ /**
99
+ * **close**
100
+ *
101
+ * Closes the database connection pool.
102
+ *
103
+ * @example
104
+ * ```ts
105
+ * const success = await db.close()
106
+ * ```
107
+ * @param db - Optionally state the name of a database if you are managing more than one. Otherwise, all database pools will be in scope.
108
+ */
109
+ async close(db) {
110
+ const success = await window.__TAURI_INVOKE__("plugin:sql|close", {
111
+ db,
112
+ });
113
+ return success;
114
+ }
115
+ }
116
+
117
+ export { Database as default };
118
+ //# sourceMappingURL=index.mjs.map
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@tauri-apps/plugin-sql",
3
+ "version": "2.0.0-alpha.0",
4
+ "description": "Interface with SQL databases",
5
+ "license": "MIT or APACHE-2.0",
6
+ "authors": [
7
+ "Tauri Programme within The Commons Conservancy"
8
+ ],
9
+ "type": "module",
10
+ "browser": "dist-js/index.min.js",
11
+ "module": "dist-js/index.mjs",
12
+ "types": "dist-js/index.d.ts",
13
+ "exports": {
14
+ "import": "./dist-js/index.mjs",
15
+ "types": "./dist-js/index.d.ts",
16
+ "browser": "./dist-js/index.min.js"
17
+ },
18
+ "files": [
19
+ "dist-js",
20
+ "!dist-js/**/*.map",
21
+ "README.md",
22
+ "LICENSE"
23
+ ],
24
+ "devDependencies": {
25
+ "tslib": "^2.5.0"
26
+ },
27
+ "dependencies": {
28
+ "@tauri-apps/api": "2.0.0-alpha.4"
29
+ },
30
+ "scripts": {
31
+ "build": "rollup -c"
32
+ }
33
+ }