mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-04-29 12:06:01 +02:00
120 lines
3.8 KiB
JavaScript
120 lines
3.8 KiB
JavaScript
var d=Object.defineProperty;var e=(c,a)=>{for(var b in a)d(c,b,{get:a[b],enumerable:!0});};
|
|
|
|
var f={};e(f,{convertFileSrc:()=>w,invoke:()=>c,transformCallback:()=>s});function u(){return window.crypto.getRandomValues(new Uint32Array(1))[0]}function s(e,r=!1){let n=u(),t=`_${n}`;return Object.defineProperty(window,t,{value:o=>(r&&Reflect.deleteProperty(window,t),e==null?void 0:e(o)),writable:!1,configurable:!0}),n}async function c(e,r={}){return new Promise((n,t)=>{let o=s(i=>{n(i),Reflect.deleteProperty(window,`_${a}`);},!0),a=s(i=>{t(i),Reflect.deleteProperty(window,`_${o}`);},!0);window.__TAURI_IPC__({cmd:e,callback:o,error:a,...r});})}function w(e,r="asset"){let n=encodeURIComponent(e);return navigator.userAgent.includes("Windows")?`https://${r}.localhost/${n}`:`${r}://localhost/${n}`}
|
|
|
|
/**
|
|
* **Database**
|
|
*
|
|
* The `Database` class serves as the primary interface for
|
|
* communicating with the rust side of the sql plugin.
|
|
*/
|
|
class Database {
|
|
constructor(path) {
|
|
this.path = path;
|
|
}
|
|
/**
|
|
* **load**
|
|
*
|
|
* A static initializer which connects to the underlying database and
|
|
* returns a `Database` instance once a connection to the database is established.
|
|
*
|
|
* # Sqlite
|
|
*
|
|
* The path is relative to `tauri::api::path::BaseDirectory::App` and must start with `sqlite:`.
|
|
*
|
|
* @example
|
|
* ```ts
|
|
* const db = await Database.load("sqlite:test.db");
|
|
* ```
|
|
*/
|
|
static async load(path) {
|
|
const _path = await c("plugin:sql|load", {
|
|
db: path,
|
|
});
|
|
return new Database(_path);
|
|
}
|
|
/**
|
|
* **get**
|
|
*
|
|
* A static initializer which synchronously returns an instance of
|
|
* the Database class while deferring the actual database connection
|
|
* until the first invocation or selection on the database.
|
|
*
|
|
* # Sqlite
|
|
*
|
|
* The path is relative to `tauri::api::path::BaseDirectory::App` and must start with `sqlite:`.
|
|
*
|
|
* @example
|
|
* ```ts
|
|
* const db = Database.get("sqlite:test.db");
|
|
* ```
|
|
*/
|
|
static get(path) {
|
|
return new Database(path);
|
|
}
|
|
/**
|
|
* **execute**
|
|
*
|
|
* Passes a SQL expression to the database for execution.
|
|
*
|
|
* @example
|
|
* ```ts
|
|
* const result = await db.execute(
|
|
* "UPDATE todos SET title = $1, completed = $2 WHERE id = $3",
|
|
* [ todos.title, todos.status, todos.id ]
|
|
* );
|
|
* ```
|
|
*/
|
|
async execute(query, bindValues) {
|
|
const [rowsAffected, lastInsertId] = await c("plugin:sql|execute", {
|
|
db: this.path,
|
|
query,
|
|
values: bindValues !== null && bindValues !== void 0 ? bindValues : [],
|
|
});
|
|
return {
|
|
lastInsertId,
|
|
rowsAffected,
|
|
};
|
|
}
|
|
/**
|
|
* **select**
|
|
*
|
|
* Passes in a SELECT query to the database for execution.
|
|
*
|
|
* @example
|
|
* ```ts
|
|
* const result = await db.select(
|
|
* "SELECT * from todos WHERE id = $1", id
|
|
* );
|
|
* ```
|
|
*/
|
|
async select(query, bindValues) {
|
|
const result = await c("plugin:sql|select", {
|
|
db: this.path,
|
|
query,
|
|
values: bindValues !== null && bindValues !== void 0 ? bindValues : [],
|
|
});
|
|
return result;
|
|
}
|
|
/**
|
|
* **close**
|
|
*
|
|
* Closes the database connection pool.
|
|
*
|
|
* @example
|
|
* ```ts
|
|
* const success = await db.close()
|
|
* ```
|
|
* @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) {
|
|
const success = await c("plugin:sql|close", {
|
|
db,
|
|
});
|
|
return success;
|
|
}
|
|
}
|
|
|
|
export { Database as default };
|
|
//# sourceMappingURL=index.min.js.map
|