mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-04-29 12:06:01 +02:00
feat(plugins): inject API on window.__TAURI__ (#383)
This commit is contained in:
committed by
GitHub
parent
3c8577bc9a
commit
b131bc8f7c
@@ -2,7 +2,11 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import { invoke } from "@tauri-apps/api/tauri";
|
||||
declare global {
|
||||
interface Window {
|
||||
__TAURI_INVOKE__: <T>(cmd: string, args?: unknown) => Promise<T>;
|
||||
}
|
||||
}
|
||||
|
||||
export interface QueryResult {
|
||||
/** The number of rows affected by the query. */
|
||||
@@ -46,7 +50,7 @@ export default class Database {
|
||||
* ```
|
||||
*/
|
||||
static async load(path: string): Promise<Database> {
|
||||
const _path = await invoke<string>("plugin:sql|load", {
|
||||
const _path = await window.__TAURI_INVOKE__<string>("plugin:sql|load", {
|
||||
db: path,
|
||||
});
|
||||
|
||||
@@ -87,14 +91,13 @@ export default class Database {
|
||||
* ```
|
||||
*/
|
||||
async execute(query: string, bindValues?: unknown[]): Promise<QueryResult> {
|
||||
const [rowsAffected, lastInsertId] = await invoke<[number, number]>(
|
||||
"plugin:sql|execute",
|
||||
{
|
||||
db: this.path,
|
||||
query,
|
||||
values: bindValues ?? [],
|
||||
}
|
||||
);
|
||||
const [rowsAffected, lastInsertId] = await window.__TAURI_INVOKE__<
|
||||
[number, number]
|
||||
>("plugin:sql|execute", {
|
||||
db: this.path,
|
||||
query,
|
||||
values: bindValues ?? [],
|
||||
});
|
||||
|
||||
return {
|
||||
lastInsertId,
|
||||
@@ -115,7 +118,7 @@ export default class Database {
|
||||
* ```
|
||||
*/
|
||||
async select<T>(query: string, bindValues?: unknown[]): Promise<T> {
|
||||
const result = await invoke<T>("plugin:sql|select", {
|
||||
const result = await window.__TAURI_INVOKE__<T>("plugin:sql|select", {
|
||||
db: this.path,
|
||||
query,
|
||||
values: bindValues ?? [],
|
||||
@@ -136,7 +139,7 @@ export default class Database {
|
||||
* @param db - Optionally state the name of a database if you are managing more than one. Otherwise, all database pools will be in scope.
|
||||
*/
|
||||
async close(db?: string): Promise<boolean> {
|
||||
const success = await invoke<boolean>("plugin:sql|close", {
|
||||
const success = await window.__TAURI_INVOKE__<boolean>("plugin:sql|close", {
|
||||
db,
|
||||
});
|
||||
return success;
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
if("__TAURI__"in window){var __TAURI_SQL__=function(){"use strict";class _{constructor(_){this.path=_}static async load(t){const e=await window.__TAURI_INVOKE__("plugin:sql|load",{db:t});return new _(e)}static get(t){return new _(t)}async execute(_,t){const[e,n]=await window.__TAURI_INVOKE__("plugin:sql|execute",{db:this.path,query:_,values:null!=t?t:[]});return{lastInsertId:n,rowsAffected:e}}async select(_,t){return await window.__TAURI_INVOKE__("plugin:sql|select",{db:this.path,query:_,values:null!=t?t:[]})}async close(_){return await window.__TAURI_INVOKE__("plugin:sql|close",{db:_})}}return _}();Object.defineProperty(window.__TAURI__,"sql",{value:__TAURI_SQL__})}
|
||||
@@ -280,6 +280,7 @@ impl Builder {
|
||||
|
||||
pub fn build<R: Runtime>(mut self) -> TauriPlugin<R, Option<PluginConfig>> {
|
||||
PluginBuilder::<R, Option<PluginConfig>>::new("sql")
|
||||
.js_init_script(include_str!("api-iife.js").to_string())
|
||||
.invoke_handler(tauri::generate_handler![load, execute, select, close])
|
||||
.setup(|app, api| {
|
||||
let config = api.config().clone().unwrap_or_default();
|
||||
|
||||
Reference in New Issue
Block a user