@tauri-apps/plugin-sql 2.4.1 → 2.6.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/README.md CHANGED
@@ -12,7 +12,7 @@ Interface with SQL databases through [sqlx](https://github.com/launchbadge/sqlx)
12
12
 
13
13
  ## Install
14
14
 
15
- _This plugin requires a Rust version of at least **1.77.2**_
15
+ _This plugin requires a Rust version of at least **1.90**_
16
16
 
17
17
  There are three general methods of installation that we can recommend.
18
18
 
package/dist-js/index.cjs CHANGED
@@ -5,13 +5,35 @@ var core = require('@tauri-apps/api/core');
5
5
  // Copyright 2019-2023 Tauri Programme within The Commons Conservancy
6
6
  // SPDX-License-Identifier: Apache-2.0
7
7
  // SPDX-License-Identifier: MIT
8
+ /**
9
+ * Interface with SQL databases through [sqlx](https://github.com/launchbadge/sqlx).
10
+ * Which database engines can be used depends on the drivers enabled on the Rust
11
+ * side of the plugin: SQLite, MySQL and PostgreSQL.
12
+ *
13
+ * @module
14
+ */
8
15
  /**
9
16
  * **Database**
10
17
  *
11
18
  * The `Database` class serves as the primary interface for
12
19
  * communicating with the rust side of the sql plugin.
20
+ *
21
+ * @since 2.0.0
13
22
  */
14
23
  class Database {
24
+ /**
25
+ * Creates a `Database` instance for the given connection string without
26
+ * opening a connection to it. Use {@link Database.load} to connect to the
27
+ * database, or {@link Database.get} for a database that is already loaded.
28
+ *
29
+ * @param path The database connection string, such as `sqlite:test.db`.
30
+ *
31
+ * @example
32
+ * ```typescript
33
+ * import Database from '@tauri-apps/plugin-sql'
34
+ * const db = new Database('sqlite:test.db')
35
+ * ```
36
+ */
15
37
  constructor(path) {
16
38
  this.path = path;
17
39
  }
@@ -26,13 +48,20 @@ class Database {
26
48
  * The path is relative to `tauri::path::BaseDirectory::App` and must start with `sqlite:`.
27
49
  *
28
50
  * @example
29
- * ```ts
30
- * const db = await Database.load("sqlite:test.db");
51
+ * ```typescript
52
+ * import Database from '@tauri-apps/plugin-sql'
53
+ * const db = await Database.load('sqlite:test.db')
54
+ * const existingDb = await Database.load('sqlite:test.db', { autoCreate: false })
31
55
  * ```
56
+ *
57
+ * @param path The database connection string, such as `sqlite:test.db`. The database is created if it does not exist yet (unless `options.autoCreate` is `false`), and any migration registered for it on the Rust side is run.
58
+ * @param options Configuration options for connecting to the database.
59
+ * @returns A promise resolving to a `Database` instance connected to the given database.
32
60
  */
33
- static async load(path) {
61
+ static async load(path, options) {
34
62
  const _path = await core.invoke('plugin:sql|load', {
35
- db: path
63
+ db: path,
64
+ options
36
65
  });
37
66
  return new Database(_path);
38
67
  }
@@ -48,9 +77,13 @@ class Database {
48
77
  * The path is relative to `tauri::path::BaseDirectory::App` and must start with `sqlite:`.
49
78
  *
50
79
  * @example
51
- * ```ts
52
- * const db = Database.get("sqlite:test.db");
80
+ * ```typescript
81
+ * import Database from '@tauri-apps/plugin-sql'
82
+ * const db = Database.get('sqlite:test.db')
53
83
  * ```
84
+ *
85
+ * @param path The database connection string, such as `sqlite:test.db`.
86
+ * @returns A `Database` instance bound to the given connection string.
54
87
  */
55
88
  static get(path) {
56
89
  return new Database(path);
@@ -61,7 +94,10 @@ class Database {
61
94
  * Passes a SQL expression to the database for execution.
62
95
  *
63
96
  * @example
64
- * ```ts
97
+ * ```typescript
98
+ * import Database from '@tauri-apps/plugin-sql'
99
+ * const db = await Database.load('sqlite:test.db')
100
+ *
65
101
  * // for sqlite & postgres
66
102
  * // INSERT example
67
103
  * const result = await db.execute(
@@ -86,6 +122,10 @@ class Database {
86
122
  * [ todos.title, todos.status, todos.id ]
87
123
  * );
88
124
  * ```
125
+ *
126
+ * @param query The SQL statement to run, using `$1`, `$2`, ... placeholders on SQLite and PostgreSQL and `?` placeholders on MySQL.
127
+ * @param bindValues The values bound to the query placeholders, in the order they appear in the statement. Defaults to no values.
128
+ * @returns A promise resolving to the number of rows affected by the statement and the last inserted id.
89
129
  */
90
130
  async execute(query, bindValues) {
91
131
  const [rowsAffected, lastInsertId] = await core.invoke('plugin:sql|execute', {
@@ -104,7 +144,10 @@ class Database {
104
144
  * Passes in a SELECT query to the database for execution.
105
145
  *
106
146
  * @example
107
- * ```ts
147
+ * ```typescript
148
+ * import Database from '@tauri-apps/plugin-sql'
149
+ * const db = await Database.load('sqlite:test.db')
150
+ *
108
151
  * // for sqlite & postgres
109
152
  * const result = await db.select(
110
153
  * "SELECT * from todos WHERE id = $1", [ id ]
@@ -115,6 +158,10 @@ class Database {
115
158
  * "SELECT * from todos WHERE id = ?", [ id ]
116
159
  * );
117
160
  * ```
161
+ *
162
+ * @param query The SQL query to run, using `$1`, `$2`, ... placeholders on SQLite and PostgreSQL and `?` placeholders on MySQL.
163
+ * @param bindValues The values bound to the query placeholders, in the order they appear in the query. Defaults to no values.
164
+ * @returns A promise resolving to the selected rows, each row being an object keyed by column name.
118
165
  */
119
166
  async select(query, bindValues) {
120
167
  const result = await core.invoke('plugin:sql|select', {
@@ -130,10 +177,14 @@ class Database {
130
177
  * Closes the database connection pool.
131
178
  *
132
179
  * @example
133
- * ```ts
180
+ * ```typescript
181
+ * import Database from '@tauri-apps/plugin-sql'
182
+ * const db = await Database.load('sqlite:test.db')
134
183
  * const success = await db.close()
135
184
  * ```
185
+ *
136
186
  * @param db - Optionally state the name of a database if you are managing more than one. Otherwise, all database pools will be in scope.
187
+ * @returns A promise resolving to `true` once the matching connection pools have been closed.
137
188
  */
138
189
  async close(db) {
139
190
  const success = await core.invoke('plugin:sql|close', {
@@ -1,3 +1,6 @@
1
+ /**
2
+ * The outcome of a statement run through {@link Database.execute}.
3
+ */
1
4
  export interface QueryResult {
2
5
  /** The number of rows affected by the query. */
3
6
  rowsAffected: number;
@@ -11,14 +14,46 @@ export interface QueryResult {
11
14
  */
12
15
  lastInsertId?: number;
13
16
  }
17
+ /**
18
+ * Options for loading a database.
19
+ *
20
+ * @since 2.6.0
21
+ */
22
+ export interface LoadOptions {
23
+ /**
24
+ * Whether to automatically create the database if it does not exist yet.
25
+ * Defaults to `true`.
26
+ */
27
+ autoCreate?: boolean;
28
+ }
14
29
  /**
15
30
  * **Database**
16
31
  *
17
32
  * The `Database` class serves as the primary interface for
18
33
  * communicating with the rust side of the sql plugin.
34
+ *
35
+ * @since 2.0.0
19
36
  */
20
37
  export default class Database {
38
+ /**
39
+ * The connection string identifying the database on the Rust side,
40
+ * for instance `sqlite:test.db`, `mysql://user:pass@host/database`
41
+ * or `postgres://user:pass@host/database`.
42
+ */
21
43
  path: string;
44
+ /**
45
+ * Creates a `Database` instance for the given connection string without
46
+ * opening a connection to it. Use {@link Database.load} to connect to the
47
+ * database, or {@link Database.get} for a database that is already loaded.
48
+ *
49
+ * @param path The database connection string, such as `sqlite:test.db`.
50
+ *
51
+ * @example
52
+ * ```typescript
53
+ * import Database from '@tauri-apps/plugin-sql'
54
+ * const db = new Database('sqlite:test.db')
55
+ * ```
56
+ */
22
57
  constructor(path: string);
23
58
  /**
24
59
  * **load**
@@ -31,11 +66,17 @@ export default class Database {
31
66
  * The path is relative to `tauri::path::BaseDirectory::App` and must start with `sqlite:`.
32
67
  *
33
68
  * @example
34
- * ```ts
35
- * const db = await Database.load("sqlite:test.db");
69
+ * ```typescript
70
+ * import Database from '@tauri-apps/plugin-sql'
71
+ * const db = await Database.load('sqlite:test.db')
72
+ * const existingDb = await Database.load('sqlite:test.db', { autoCreate: false })
36
73
  * ```
74
+ *
75
+ * @param path The database connection string, such as `sqlite:test.db`. The database is created if it does not exist yet (unless `options.autoCreate` is `false`), and any migration registered for it on the Rust side is run.
76
+ * @param options Configuration options for connecting to the database.
77
+ * @returns A promise resolving to a `Database` instance connected to the given database.
37
78
  */
38
- static load(path: string): Promise<Database>;
79
+ static load(path: string, options?: LoadOptions): Promise<Database>;
39
80
  /**
40
81
  * **get**
41
82
  *
@@ -48,9 +89,13 @@ export default class Database {
48
89
  * The path is relative to `tauri::path::BaseDirectory::App` and must start with `sqlite:`.
49
90
  *
50
91
  * @example
51
- * ```ts
52
- * const db = Database.get("sqlite:test.db");
92
+ * ```typescript
93
+ * import Database from '@tauri-apps/plugin-sql'
94
+ * const db = Database.get('sqlite:test.db')
53
95
  * ```
96
+ *
97
+ * @param path The database connection string, such as `sqlite:test.db`.
98
+ * @returns A `Database` instance bound to the given connection string.
54
99
  */
55
100
  static get(path: string): Database;
56
101
  /**
@@ -59,7 +104,10 @@ export default class Database {
59
104
  * Passes a SQL expression to the database for execution.
60
105
  *
61
106
  * @example
62
- * ```ts
107
+ * ```typescript
108
+ * import Database from '@tauri-apps/plugin-sql'
109
+ * const db = await Database.load('sqlite:test.db')
110
+ *
63
111
  * // for sqlite & postgres
64
112
  * // INSERT example
65
113
  * const result = await db.execute(
@@ -84,6 +132,10 @@ export default class Database {
84
132
  * [ todos.title, todos.status, todos.id ]
85
133
  * );
86
134
  * ```
135
+ *
136
+ * @param query The SQL statement to run, using `$1`, `$2`, ... placeholders on SQLite and PostgreSQL and `?` placeholders on MySQL.
137
+ * @param bindValues The values bound to the query placeholders, in the order they appear in the statement. Defaults to no values.
138
+ * @returns A promise resolving to the number of rows affected by the statement and the last inserted id.
87
139
  */
88
140
  execute(query: string, bindValues?: unknown[]): Promise<QueryResult>;
89
141
  /**
@@ -92,7 +144,10 @@ export default class Database {
92
144
  * Passes in a SELECT query to the database for execution.
93
145
  *
94
146
  * @example
95
- * ```ts
147
+ * ```typescript
148
+ * import Database from '@tauri-apps/plugin-sql'
149
+ * const db = await Database.load('sqlite:test.db')
150
+ *
96
151
  * // for sqlite & postgres
97
152
  * const result = await db.select(
98
153
  * "SELECT * from todos WHERE id = $1", [ id ]
@@ -103,6 +158,10 @@ export default class Database {
103
158
  * "SELECT * from todos WHERE id = ?", [ id ]
104
159
  * );
105
160
  * ```
161
+ *
162
+ * @param query The SQL query to run, using `$1`, `$2`, ... placeholders on SQLite and PostgreSQL and `?` placeholders on MySQL.
163
+ * @param bindValues The values bound to the query placeholders, in the order they appear in the query. Defaults to no values.
164
+ * @returns A promise resolving to the selected rows, each row being an object keyed by column name.
106
165
  */
107
166
  select<T>(query: string, bindValues?: unknown[]): Promise<T>;
108
167
  /**
@@ -111,10 +170,14 @@ export default class Database {
111
170
  * Closes the database connection pool.
112
171
  *
113
172
  * @example
114
- * ```ts
173
+ * ```typescript
174
+ * import Database from '@tauri-apps/plugin-sql'
175
+ * const db = await Database.load('sqlite:test.db')
115
176
  * const success = await db.close()
116
177
  * ```
178
+ *
117
179
  * @param db - Optionally state the name of a database if you are managing more than one. Otherwise, all database pools will be in scope.
180
+ * @returns A promise resolving to `true` once the matching connection pools have been closed.
118
181
  */
119
182
  close(db?: string): Promise<boolean>;
120
183
  }
package/dist-js/index.js CHANGED
@@ -3,13 +3,35 @@ import { invoke } from '@tauri-apps/api/core';
3
3
  // Copyright 2019-2023 Tauri Programme within The Commons Conservancy
4
4
  // SPDX-License-Identifier: Apache-2.0
5
5
  // SPDX-License-Identifier: MIT
6
+ /**
7
+ * Interface with SQL databases through [sqlx](https://github.com/launchbadge/sqlx).
8
+ * Which database engines can be used depends on the drivers enabled on the Rust
9
+ * side of the plugin: SQLite, MySQL and PostgreSQL.
10
+ *
11
+ * @module
12
+ */
6
13
  /**
7
14
  * **Database**
8
15
  *
9
16
  * The `Database` class serves as the primary interface for
10
17
  * communicating with the rust side of the sql plugin.
18
+ *
19
+ * @since 2.0.0
11
20
  */
12
21
  class Database {
22
+ /**
23
+ * Creates a `Database` instance for the given connection string without
24
+ * opening a connection to it. Use {@link Database.load} to connect to the
25
+ * database, or {@link Database.get} for a database that is already loaded.
26
+ *
27
+ * @param path The database connection string, such as `sqlite:test.db`.
28
+ *
29
+ * @example
30
+ * ```typescript
31
+ * import Database from '@tauri-apps/plugin-sql'
32
+ * const db = new Database('sqlite:test.db')
33
+ * ```
34
+ */
13
35
  constructor(path) {
14
36
  this.path = path;
15
37
  }
@@ -24,13 +46,20 @@ class Database {
24
46
  * The path is relative to `tauri::path::BaseDirectory::App` and must start with `sqlite:`.
25
47
  *
26
48
  * @example
27
- * ```ts
28
- * const db = await Database.load("sqlite:test.db");
49
+ * ```typescript
50
+ * import Database from '@tauri-apps/plugin-sql'
51
+ * const db = await Database.load('sqlite:test.db')
52
+ * const existingDb = await Database.load('sqlite:test.db', { autoCreate: false })
29
53
  * ```
54
+ *
55
+ * @param path The database connection string, such as `sqlite:test.db`. The database is created if it does not exist yet (unless `options.autoCreate` is `false`), and any migration registered for it on the Rust side is run.
56
+ * @param options Configuration options for connecting to the database.
57
+ * @returns A promise resolving to a `Database` instance connected to the given database.
30
58
  */
31
- static async load(path) {
59
+ static async load(path, options) {
32
60
  const _path = await invoke('plugin:sql|load', {
33
- db: path
61
+ db: path,
62
+ options
34
63
  });
35
64
  return new Database(_path);
36
65
  }
@@ -46,9 +75,13 @@ class Database {
46
75
  * The path is relative to `tauri::path::BaseDirectory::App` and must start with `sqlite:`.
47
76
  *
48
77
  * @example
49
- * ```ts
50
- * const db = Database.get("sqlite:test.db");
78
+ * ```typescript
79
+ * import Database from '@tauri-apps/plugin-sql'
80
+ * const db = Database.get('sqlite:test.db')
51
81
  * ```
82
+ *
83
+ * @param path The database connection string, such as `sqlite:test.db`.
84
+ * @returns A `Database` instance bound to the given connection string.
52
85
  */
53
86
  static get(path) {
54
87
  return new Database(path);
@@ -59,7 +92,10 @@ class Database {
59
92
  * Passes a SQL expression to the database for execution.
60
93
  *
61
94
  * @example
62
- * ```ts
95
+ * ```typescript
96
+ * import Database from '@tauri-apps/plugin-sql'
97
+ * const db = await Database.load('sqlite:test.db')
98
+ *
63
99
  * // for sqlite & postgres
64
100
  * // INSERT example
65
101
  * const result = await db.execute(
@@ -84,6 +120,10 @@ class Database {
84
120
  * [ todos.title, todos.status, todos.id ]
85
121
  * );
86
122
  * ```
123
+ *
124
+ * @param query The SQL statement to run, using `$1`, `$2`, ... placeholders on SQLite and PostgreSQL and `?` placeholders on MySQL.
125
+ * @param bindValues The values bound to the query placeholders, in the order they appear in the statement. Defaults to no values.
126
+ * @returns A promise resolving to the number of rows affected by the statement and the last inserted id.
87
127
  */
88
128
  async execute(query, bindValues) {
89
129
  const [rowsAffected, lastInsertId] = await invoke('plugin:sql|execute', {
@@ -102,7 +142,10 @@ class Database {
102
142
  * Passes in a SELECT query to the database for execution.
103
143
  *
104
144
  * @example
105
- * ```ts
145
+ * ```typescript
146
+ * import Database from '@tauri-apps/plugin-sql'
147
+ * const db = await Database.load('sqlite:test.db')
148
+ *
106
149
  * // for sqlite & postgres
107
150
  * const result = await db.select(
108
151
  * "SELECT * from todos WHERE id = $1", [ id ]
@@ -113,6 +156,10 @@ class Database {
113
156
  * "SELECT * from todos WHERE id = ?", [ id ]
114
157
  * );
115
158
  * ```
159
+ *
160
+ * @param query The SQL query to run, using `$1`, `$2`, ... placeholders on SQLite and PostgreSQL and `?` placeholders on MySQL.
161
+ * @param bindValues The values bound to the query placeholders, in the order they appear in the query. Defaults to no values.
162
+ * @returns A promise resolving to the selected rows, each row being an object keyed by column name.
116
163
  */
117
164
  async select(query, bindValues) {
118
165
  const result = await invoke('plugin:sql|select', {
@@ -128,10 +175,14 @@ class Database {
128
175
  * Closes the database connection pool.
129
176
  *
130
177
  * @example
131
- * ```ts
178
+ * ```typescript
179
+ * import Database from '@tauri-apps/plugin-sql'
180
+ * const db = await Database.load('sqlite:test.db')
132
181
  * const success = await db.close()
133
182
  * ```
183
+ *
134
184
  * @param db - Optionally state the name of a database if you are managing more than one. Otherwise, all database pools will be in scope.
185
+ * @returns A promise resolving to `true` once the matching connection pools have been closed.
135
186
  */
136
187
  async close(db) {
137
188
  const success = await invoke('plugin:sql|close', {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tauri-apps/plugin-sql",
3
- "version": "2.4.1",
3
+ "version": "2.6.0",
4
4
  "description": "Interface with SQL databases",
5
5
  "license": "MIT OR Apache-2.0",
6
6
  "authors": [
@@ -25,6 +25,6 @@
25
25
  "LICENSE"
26
26
  ],
27
27
  "dependencies": {
28
- "@tauri-apps/api": "^2.11.0"
28
+ "@tauri-apps/api": "^2.12.0"
29
29
  }
30
30
  }