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

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/README.md CHANGED
@@ -1,10 +1,10 @@
1
- ![plugin-sql](banner.png)
1
+ ![plugin-sql](https://github.com/tauri-apps/plugins-workspace/raw/v2/plugins/sql/banner.png)
2
2
 
3
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
4
 
5
5
  ## Install
6
6
 
7
- _This plugin requires a Rust version of at least **1.65**_
7
+ _This plugin requires a Rust version of at least **1.70**_
8
8
 
9
9
  There are three general methods of installation that we can recommend.
10
10
 
@@ -74,6 +74,37 @@ const db = await Database.load("postgres://postgres:password@localhost/test");
74
74
  await db.execute("INSERT INTO ...");
75
75
  ```
76
76
 
77
+ ## Syntax
78
+
79
+ We use sqlx as our underlying library, adopting their query syntax:
80
+
81
+ - sqlite and postgres use the "$#" syntax when substituting query data
82
+ - mysql uses "?" when substituting query data
83
+
84
+ ```javascript
85
+ // INSERT and UPDATE examples for sqlite and postgres
86
+ const result = await db.execute(
87
+ "INSERT into todos (id, title, status) VALUES ($1, $2, $3)",
88
+ [todos.id, todos.title, todos.status],
89
+ );
90
+
91
+ const result = await db.execute(
92
+ "UPDATE todos SET title = $1, completed = $2 WHERE id = $3",
93
+ [todos.title, todos.status, todos.id],
94
+ );
95
+
96
+ // INSERT and UPDATE examples for mysql
97
+ const result = await db.execute(
98
+ "INSERT into todos (id, title, status) VALUES (?, ?, ?)",
99
+ [todos.id, todos.title, todos.status],
100
+ );
101
+
102
+ const result = await db.execute(
103
+ "UPDATE todos SET title = ?, completed = ? WHERE id = ?",
104
+ [todos.title, todos.status, todos.id],
105
+ );
106
+ ```
107
+
77
108
  ## Contributing
78
109
 
79
110
  PRs accepted. Please make sure to read the Contributing Guide before making a pull request.
@@ -1,8 +1,3 @@
1
- declare global {
2
- interface Window {
3
- __TAURI_INVOKE__: <T>(cmd: string, args?: unknown) => Promise<T>;
4
- }
5
- }
6
1
  export interface QueryResult {
7
2
  /** The number of rows affected by the query. */
8
3
  rowsAffected: number;
@@ -33,7 +28,7 @@ export default class Database {
33
28
  *
34
29
  * # Sqlite
35
30
  *
36
- * The path is relative to `tauri::api::path::BaseDirectory::App` and must start with `sqlite:`.
31
+ * The path is relative to `tauri::path::BaseDirectory::App` and must start with `sqlite:`.
37
32
  *
38
33
  * @example
39
34
  * ```ts
@@ -50,7 +45,7 @@ export default class Database {
50
45
  *
51
46
  * # Sqlite
52
47
  *
53
- * The path is relative to `tauri::api::path::BaseDirectory::App` and must start with `sqlite:`.
48
+ * The path is relative to `tauri::path::BaseDirectory::App` and must start with `sqlite:`.
54
49
  *
55
50
  * @example
56
51
  * ```ts
@@ -65,10 +60,29 @@ export default class Database {
65
60
  *
66
61
  * @example
67
62
  * ```ts
63
+ * // for sqlite & postgres
64
+ * // INSERT example
65
+ * const result = await db.execute(
66
+ * "INSERT into todos (id, title, status) VALUES ($1, $2, $3)",
67
+ * [ todos.id, todos.title, todos.status ]
68
+ * );
69
+ * // UPDATE example
68
70
  * const result = await db.execute(
69
71
  * "UPDATE todos SET title = $1, completed = $2 WHERE id = $3",
70
72
  * [ todos.title, todos.status, todos.id ]
71
73
  * );
74
+ *
75
+ * // for mysql
76
+ * // INSERT example
77
+ * const result = await db.execute(
78
+ * "INSERT into todos (id, title, status) VALUES (?, ?, ?)",
79
+ * [ todos.id, todos.title, todos.status ]
80
+ * );
81
+ * // UPDATE example
82
+ * const result = await db.execute(
83
+ * "UPDATE todos SET title = ?, completed = ? WHERE id = ?",
84
+ * [ todos.title, todos.status, todos.id ]
85
+ * );
72
86
  * ```
73
87
  */
74
88
  execute(query: string, bindValues?: unknown[]): Promise<QueryResult>;
@@ -79,9 +93,15 @@ export default class Database {
79
93
  *
80
94
  * @example
81
95
  * ```ts
96
+ * // for sqlite & postgres
82
97
  * const result = await db.select(
83
98
  * "SELECT * from todos WHERE id = $1", id
84
99
  * );
100
+ *
101
+ * // for mysql
102
+ * const result = await db.select(
103
+ * "SELECT * from todos WHERE id = ?", id
104
+ * );
85
105
  * ```
86
106
  */
87
107
  select<T>(query: string, bindValues?: unknown[]): Promise<T>;
@@ -1,3 +1,7 @@
1
+ var f=Object.defineProperty;var g=(a,b)=>{for(var c in b)f(a,c,{get:b[c],enumerable:!0});};var e=(a,b,c)=>{if(!b.has(a))throw TypeError("Cannot "+c)};var h=(a,b,c)=>(e(a,b,"read from private field"),c?c.call(a):b.get(a)),i$1=(a,b,c)=>{if(b.has(a))throw TypeError("Cannot add the same private member more than once");b instanceof WeakSet?b.add(a):b.set(a,c);},j=(a,b,c,d)=>(e(a,b,"write to private field"),d?d.call(a,c):b.set(a,c),c);
2
+
3
+ var T={};g(T,{Channel:()=>i,PluginListener:()=>o,addPluginListener:()=>l,convertFileSrc:()=>v,invoke:()=>u,transformCallback:()=>_});function _(e,n=!1){return window.__TAURI_INTERNALS__.transformCallback(e,n)}var r,i=class{constructor(){this.__TAURI_CHANNEL_MARKER__=!0;i$1(this,r,()=>{});this.id=_(n=>{h(this,r).call(this,n);});}set onmessage(n){j(this,r,n);}get onmessage(){return h(this,r)}toJSON(){return `__CHANNEL__:${this.id}`}};r=new WeakMap;var o=class{constructor(n,s,t){this.plugin=n,this.event=s,this.channelId=t;}async unregister(){return u(`plugin:${this.plugin}|remove_listener`,{event:this.event,channelId:this.channelId})}};async function l(e,n,s){let t=new i;return t.onmessage=s,u(`plugin:${e}|register_listener`,{event:n,handler:t}).then(()=>new o(e,n,t.id))}async function u(e,n={},s){return window.__TAURI_INTERNALS__.invoke(e,n,s)}function v(e,n="asset"){return window.__TAURI_INTERNALS__.convertFileSrc(e,n)}
4
+
1
5
  // Copyright 2019-2023 Tauri Programme within The Commons Conservancy
2
6
  // SPDX-License-Identifier: Apache-2.0
3
7
  // SPDX-License-Identifier: MIT
@@ -19,7 +23,7 @@ class Database {
19
23
  *
20
24
  * # Sqlite
21
25
  *
22
- * The path is relative to `tauri::api::path::BaseDirectory::App` and must start with `sqlite:`.
26
+ * The path is relative to `tauri::path::BaseDirectory::App` and must start with `sqlite:`.
23
27
  *
24
28
  * @example
25
29
  * ```ts
@@ -27,7 +31,7 @@ class Database {
27
31
  * ```
28
32
  */
29
33
  static async load(path) {
30
- const _path = await window.__TAURI_INVOKE__("plugin:sql|load", {
34
+ const _path = await u("plugin:sql|load", {
31
35
  db: path,
32
36
  });
33
37
  return new Database(_path);
@@ -41,7 +45,7 @@ class Database {
41
45
  *
42
46
  * # Sqlite
43
47
  *
44
- * The path is relative to `tauri::api::path::BaseDirectory::App` and must start with `sqlite:`.
48
+ * The path is relative to `tauri::path::BaseDirectory::App` and must start with `sqlite:`.
45
49
  *
46
50
  * @example
47
51
  * ```ts
@@ -58,14 +62,33 @@ class Database {
58
62
  *
59
63
  * @example
60
64
  * ```ts
65
+ * // for sqlite & postgres
66
+ * // INSERT example
67
+ * const result = await db.execute(
68
+ * "INSERT into todos (id, title, status) VALUES ($1, $2, $3)",
69
+ * [ todos.id, todos.title, todos.status ]
70
+ * );
71
+ * // UPDATE example
61
72
  * const result = await db.execute(
62
73
  * "UPDATE todos SET title = $1, completed = $2 WHERE id = $3",
63
74
  * [ todos.title, todos.status, todos.id ]
64
75
  * );
76
+ *
77
+ * // for mysql
78
+ * // INSERT example
79
+ * const result = await db.execute(
80
+ * "INSERT into todos (id, title, status) VALUES (?, ?, ?)",
81
+ * [ todos.id, todos.title, todos.status ]
82
+ * );
83
+ * // UPDATE example
84
+ * const result = await db.execute(
85
+ * "UPDATE todos SET title = ?, completed = ? WHERE id = ?",
86
+ * [ todos.title, todos.status, todos.id ]
87
+ * );
65
88
  * ```
66
89
  */
67
90
  async execute(query, bindValues) {
68
- const [rowsAffected, lastInsertId] = await window.__TAURI_INVOKE__("plugin:sql|execute", {
91
+ const [rowsAffected, lastInsertId] = await u("plugin:sql|execute", {
69
92
  db: this.path,
70
93
  query,
71
94
  values: bindValues !== null && bindValues !== void 0 ? bindValues : [],
@@ -82,13 +105,19 @@ class Database {
82
105
  *
83
106
  * @example
84
107
  * ```ts
108
+ * // for sqlite & postgres
85
109
  * const result = await db.select(
86
110
  * "SELECT * from todos WHERE id = $1", id
87
111
  * );
112
+ *
113
+ * // for mysql
114
+ * const result = await db.select(
115
+ * "SELECT * from todos WHERE id = ?", id
116
+ * );
88
117
  * ```
89
118
  */
90
119
  async select(query, bindValues) {
91
- const result = await window.__TAURI_INVOKE__("plugin:sql|select", {
120
+ const result = await u("plugin:sql|select", {
92
121
  db: this.path,
93
122
  query,
94
123
  values: bindValues !== null && bindValues !== void 0 ? bindValues : [],
@@ -107,7 +136,7 @@ class Database {
107
136
  * @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
137
  */
109
138
  async close(db) {
110
- const success = await window.__TAURI_INVOKE__("plugin:sql|close", {
139
+ const success = await u("plugin:sql|close", {
111
140
  db,
112
141
  });
113
142
  return success;
package/dist-js/index.mjs CHANGED
@@ -1,3 +1,5 @@
1
+ import { invoke } from '@tauri-apps/api/primitives';
2
+
1
3
  // Copyright 2019-2023 Tauri Programme within The Commons Conservancy
2
4
  // SPDX-License-Identifier: Apache-2.0
3
5
  // SPDX-License-Identifier: MIT
@@ -19,7 +21,7 @@ class Database {
19
21
  *
20
22
  * # Sqlite
21
23
  *
22
- * The path is relative to `tauri::api::path::BaseDirectory::App` and must start with `sqlite:`.
24
+ * The path is relative to `tauri::path::BaseDirectory::App` and must start with `sqlite:`.
23
25
  *
24
26
  * @example
25
27
  * ```ts
@@ -27,7 +29,7 @@ class Database {
27
29
  * ```
28
30
  */
29
31
  static async load(path) {
30
- const _path = await window.__TAURI_INVOKE__("plugin:sql|load", {
32
+ const _path = await invoke("plugin:sql|load", {
31
33
  db: path,
32
34
  });
33
35
  return new Database(_path);
@@ -41,7 +43,7 @@ class Database {
41
43
  *
42
44
  * # Sqlite
43
45
  *
44
- * The path is relative to `tauri::api::path::BaseDirectory::App` and must start with `sqlite:`.
46
+ * The path is relative to `tauri::path::BaseDirectory::App` and must start with `sqlite:`.
45
47
  *
46
48
  * @example
47
49
  * ```ts
@@ -58,14 +60,33 @@ class Database {
58
60
  *
59
61
  * @example
60
62
  * ```ts
63
+ * // for sqlite & postgres
64
+ * // INSERT example
65
+ * const result = await db.execute(
66
+ * "INSERT into todos (id, title, status) VALUES ($1, $2, $3)",
67
+ * [ todos.id, todos.title, todos.status ]
68
+ * );
69
+ * // UPDATE example
61
70
  * const result = await db.execute(
62
71
  * "UPDATE todos SET title = $1, completed = $2 WHERE id = $3",
63
72
  * [ todos.title, todos.status, todos.id ]
64
73
  * );
74
+ *
75
+ * // for mysql
76
+ * // INSERT example
77
+ * const result = await db.execute(
78
+ * "INSERT into todos (id, title, status) VALUES (?, ?, ?)",
79
+ * [ todos.id, todos.title, todos.status ]
80
+ * );
81
+ * // UPDATE example
82
+ * const result = await db.execute(
83
+ * "UPDATE todos SET title = ?, completed = ? WHERE id = ?",
84
+ * [ todos.title, todos.status, todos.id ]
85
+ * );
65
86
  * ```
66
87
  */
67
88
  async execute(query, bindValues) {
68
- const [rowsAffected, lastInsertId] = await window.__TAURI_INVOKE__("plugin:sql|execute", {
89
+ const [rowsAffected, lastInsertId] = await invoke("plugin:sql|execute", {
69
90
  db: this.path,
70
91
  query,
71
92
  values: bindValues !== null && bindValues !== void 0 ? bindValues : [],
@@ -82,13 +103,19 @@ class Database {
82
103
  *
83
104
  * @example
84
105
  * ```ts
106
+ * // for sqlite & postgres
85
107
  * const result = await db.select(
86
108
  * "SELECT * from todos WHERE id = $1", id
87
109
  * );
110
+ *
111
+ * // for mysql
112
+ * const result = await db.select(
113
+ * "SELECT * from todos WHERE id = ?", id
114
+ * );
88
115
  * ```
89
116
  */
90
117
  async select(query, bindValues) {
91
- const result = await window.__TAURI_INVOKE__("plugin:sql|select", {
118
+ const result = await invoke("plugin:sql|select", {
92
119
  db: this.path,
93
120
  query,
94
121
  values: bindValues !== null && bindValues !== void 0 ? bindValues : [],
@@ -107,7 +134,7 @@ class Database {
107
134
  * @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
135
  */
109
136
  async close(db) {
110
- const success = await window.__TAURI_INVOKE__("plugin:sql|close", {
137
+ const success = await invoke("plugin:sql|close", {
111
138
  db,
112
139
  });
113
140
  return success;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tauri-apps/plugin-sql",
3
- "version": "2.0.0-alpha.0",
3
+ "version": "2.0.0-alpha.2",
4
4
  "description": "Interface with SQL databases",
5
5
  "license": "MIT or APACHE-2.0",
6
6
  "authors": [
@@ -22,10 +22,10 @@
22
22
  "LICENSE"
23
23
  ],
24
24
  "devDependencies": {
25
- "tslib": "^2.5.0"
25
+ "tslib": "2.6.0"
26
26
  },
27
27
  "dependencies": {
28
- "@tauri-apps/api": "2.0.0-alpha.4"
28
+ "@tauri-apps/api": "2.0.0-alpha.9"
29
29
  },
30
30
  "scripts": {
31
31
  "build": "rollup -c"