Compare commits

...

10 Commits

10 changed files with 184 additions and 224 deletions

View File

@ -1,96 +1,5 @@
# Obsidian Sample Plugin # obsidian + sqlite3 + opfs proof-of-concept
This is a sample plugin for Obsidian (https://obsidian.md). creates and queries a sqlite3 database with the [`opfs-sahpool` vfs](https://sqlite.org/wasm/doc/trunk/persistence.md#vfs-opfs-sahpool)
This project uses Typescript to provide type checking and documentation. to build + install, run `./build.ps1 -vault /path/to/your/vault` and open the console in obsidian :)
The repo depends on the latest plugin API (obsidian.d.ts) in Typescript Definition format, which contains TSDoc comments describing what it does.
**Note:** The Obsidian API is still in early alpha and is subject to change at any time!
This sample plugin demonstrates some of the basic functionality the plugin API can do.
- Adds a ribbon icon, which shows a Notice when clicked.
- Adds a command "Open Sample Modal" which opens a Modal.
- Adds a plugin setting tab to the settings page.
- Registers a global click event and output 'click' to the console.
- Registers a global interval which logs 'setInterval' to the console.
## First time developing plugins?
Quick starting guide for new plugin devs:
- Check if [someone already developed a plugin for what you want](https://obsidian.md/plugins)! There might be an existing plugin similar enough that you can partner up with.
- Make a copy of this repo as a template with the "Use this template" button (login to GitHub if you don't see it).
- Clone your repo to a local development folder. For convenience, you can place this folder in your `.obsidian/plugins/your-plugin-name` folder.
- Install NodeJS, then run `npm i` in the command line under your repo folder.
- Run `npm run dev` to compile your plugin from `main.ts` to `main.js`.
- Make changes to `main.ts` (or create new `.ts` files). Those changes should be automatically compiled into `main.js`.
- Reload Obsidian to load the new version of your plugin.
- Enable plugin in settings window.
- For updates to the Obsidian API run `npm update` in the command line under your repo folder.
## Releasing new releases
- Update your `manifest.json` with your new version number, such as `1.0.1`, and the minimum Obsidian version required for your latest release.
- Update your `versions.json` file with `"new-plugin-version": "minimum-obsidian-version"` so older versions of Obsidian can download an older version of your plugin that's compatible.
- Create new GitHub release using your new version number as the "Tag version". Use the exact version number, don't include a prefix `v`. See here for an example: https://github.com/obsidianmd/obsidian-sample-plugin/releases
- Upload the files `manifest.json`, `main.js`, `styles.css` as binary attachments. Note: The manifest.json file must be in two places, first the root path of your repository and also in the release.
- Publish the release.
> You can simplify the version bump process by running `npm version patch`, `npm version minor` or `npm version major` after updating `minAppVersion` manually in `manifest.json`.
> The command will bump version in `manifest.json` and `package.json`, and add the entry for the new version to `versions.json`
## Adding your plugin to the community plugin list
- Check https://github.com/obsidianmd/obsidian-releases/blob/master/plugin-review.md
- Publish an initial version.
- Make sure you have a `README.md` file in the root of your repo.
- Make a pull request at https://github.com/obsidianmd/obsidian-releases to add your plugin.
## How to use
- Clone this repo.
- Make sure your NodeJS is at least v16 (`node --version`).
- `npm i` or `yarn` to install dependencies.
- `npm run dev` to start compilation in watch mode.
## Manually installing the plugin
- Copy over `main.js`, `styles.css`, `manifest.json` to your vault `VaultFolder/.obsidian/plugins/your-plugin-id/`.
## Improve code quality with eslint (optional)
- [ESLint](https://eslint.org/) is a tool that analyzes your code to quickly find problems. You can run ESLint against your plugin to find common bugs and ways to improve your code.
- To use eslint with this project, make sure to install eslint from terminal:
- `npm install -g eslint`
- To use eslint to analyze this project use this command:
- `eslint main.ts`
- eslint will then create a report with suggestions for code improvement by file and line number.
- If your source code is in a folder, such as `src`, you can use eslint with this command to analyze all files in that folder:
- `eslint .\src\`
## Funding URL
You can include funding URLs where people who use your plugin can financially support it.
The simple way is to set the `fundingUrl` field to your link in your `manifest.json` file:
```json
{
"fundingUrl": "https://buymeacoffee.com"
}
```
If you have multiple URLs, you can also do:
```json
{
"fundingUrl": {
"Buy Me a Coffee": "https://buymeacoffee.com",
"GitHub Sponsor": "https://github.com/sponsors",
"Patreon": "https://www.patreon.com/"
}
}
```
## API Documentation
See https://github.com/obsidianmd/obsidian-api

View File

@ -17,7 +17,7 @@ async function build(prod) {
}, },
target: "es2020", target: "es2020",
format: "cjs", format: "cjs",
sourcemap: prod ? false : "inline", sourcemap: prod ? false : "inline",
}), }),
], ],
entryPoints: ["src/main.ts"], entryPoints: ["src/main.ts"],

View File

@ -23,6 +23,7 @@
}, },
"dependencies": { "dependencies": {
"@sqlite.org/sqlite-wasm": "^3.45.1-build1", "@sqlite.org/sqlite-wasm": "^3.45.1-build1",
"esbuild-plugin-inline-worker": "^0.1.1" "esbuild-plugin-inline-worker": "^0.1.1",
"localforage": "^1.10.0"
} }
} }

View File

@ -1,50 +1,41 @@
import { import { Notice, Plugin } from "obsidian";
App, import * as localforage from "localforage";
Editor, import TestWorker from "sqlite3.worker";
MarkdownView, import { sqlite3Worker1Promiser } from "@sqlite.org/sqlite-wasm";
Modal,
Notice,
Plugin,
PluginSettingTab,
Setting,
} from "obsidian";
import TestWorker from "testthing.worker"
import sqlite3InitModule, { Sqlite3Static } from "@sqlite.org/sqlite-wasm";
import {sqlite3Worker1Promiser} from "@sqlite.org/sqlite-wasm"
// Remember to rename these classes and interfaces! // Remember to rename these classes and interfaces!
interface MyPluginSettings { interface Benchmark {
mySetting: string; time: number;
name: string;
} }
const DEFAULT_SETTINGS: MyPluginSettings = {
mySetting: "default",
};
export default class MyPlugin extends Plugin { export default class MyPlugin extends Plugin {
settings: MyPluginSettings; public persister: LocalForage;
settings: any;
private worker: Worker; private worker: Worker;
private promiser: (...args: any[]) => any; private promiser: (...args: any[]) => any;
public benches: Benchmark[] = [];
private dbId: string;
async onload() { async onload() {
await this.loadSettings(); await this.loadSettings();
console.log(import.meta) this.benches.push(
await this.time("sqlite-init", async () => await this.initSqlite())
// This creates an icon in the left ribbon. );
await this.initSqlite(); await this.sqlite();
this.benches.push(
await this.time("idb-init", async () => await this.initIDB())
);
await this.idb();
console.table(this.benches)
} }
onunload() { onunload() {
this.worker.terminate() this.worker.terminate();
} }
async loadSettings() { async loadSettings() {
this.settings = Object.assign( this.settings = Object.assign({}, await this.loadData());
{},
DEFAULT_SETTINGS,
await this.loadData()
);
} }
async saveSettings() { async saveSettings() {
@ -56,82 +47,122 @@ export default class MyPlugin extends Plugin {
new Notice("~LOG~", args.join("\n")); new Notice("~LOG~", args.join("\n"));
} }
locateFile(path: string, prefix: string): string { locateFile(path: string, prefix: string): string {
return this.app.vault.adapter.getResourcePath(`${this.manifest.dir}/${path}`) return this.app.vault.adapter.getResourcePath(
`${this.manifest.dir}/${path}`
);
}
async time(benchName: string, func: () => Promise<void>): Promise<Benchmark> {
const start = performance.now();
await func();
const end = performance.now();
return {
name: benchName,
time: end - start,
};
}
async initIDB() {
this.persister = localforage.createInstance({
name: "sqlite-bench",
driver: localforage.INDEXEDDB,
description: "we do a little benchmarking",
});
} }
async initSqlite() { async initSqlite() {
this.worker = new TestWorker(); this.worker = new TestWorker();
let w = this.worker; let w = this.worker;
this.worker.postMessage({type: "init", path: this.locateFile("sqlite3.wasm", "")}) this.worker.postMessage({
type: "init",
path: this.locateFile("sqlite3.wasm", ""),
});
this.promiser = await new Promise<typeof this.promiser>((resolve) => { this.promiser = await new Promise<typeof this.promiser>((resolve) => {
const _promiser = sqlite3Worker1Promiser({ const _promiser = sqlite3Worker1Promiser({
print: this.printNotice, print: this.printNotice,
printErr: console.error, printErr: console.error,
locateFile: this.locateFile.bind(this), locateFile: this.locateFile.bind(this),
onready: () => { onready: () => {
resolve(_promiser); resolve(_promiser);
}, },
worker: () => w worker: () => w,
}); });
}); });
await this.start() (window as any).sqlite3Promiser = this.promiser;
} }
async start() { async doInsert(each: (s: number) => Promise<void>) {
let openRes = await this.promiser("open", { for (let i = 0; i < 50; i++) {
filename: "/test.sqlite2", await each(i);
vfs: "opfs-sahpool", }
}
}) async sqlite() {
const {dbId} = openRes this.benches.push(
console.log(openRes) await this.time("sqlite-open", async () => {
try { let openRes = await this.promiser("open", {
console.log("Creating a table...") filename: "/managed-sah/test.sqlite3",
let res = await this.promiser("exec", { vfs: "opfs-sahpool",
dbId, });
sql: "DROP TABLE IF EXISTS abcdef; CREATE TABLE IF NOT EXISTS abcdef(a,b,c)", const { dbId } = openRes;
returnValue: "resultRows" this.dbId = dbId;
console.log(openRes);
}) })
console.log("Result: ", res) );
console.log("Insert some data using exec()..."); try {
for (let i = 20; i <= 25; ++i) { this.benches.push(await this.time("sqlite-create-table", async () => {
let innerRes = await this.promiser("exec", { console.log("Creating a table...");
dbId, let res = await this.promiser("exec", {
sql: "INSERT INTO abcdef(a,b,c) VALUES (?,?,69)", dbId: this.dbId,
bind: [i, i * 2], sql: "DROP TABLE IF EXISTS abcdef; CREATE TABLE IF NOT EXISTS abcdef(a,b,c)",
returnValue: "resultRows",
rowMode: "object"
});
console.log(innerRes)
}
console.log("Query data with exec()...");
let rowRes = await this.promiser("exec", {
dbId,
sql: "SELECT * FROM abcdef ORDER BY a LIMIT 10",
returnValue: "resultRows", returnValue: "resultRows",
rowMode: "object" });
}) console.log("Result: ", res);
console.log(rowRes) }));
} finally { this.benches.push(
await this.promiser("close", {dbId}) await this.time("sqlite-insert-many", async () => {
} console.log("Insert some data using exec()...");
// db = new sqlite3.oo1.DB("/test.sqlite3", "ct"); await this.doInsert(async (i) => {
/* if (db) { let innerRes = await this.promiser("exec", {
try { dbId: this.dbId,
console.log("Creating a table..."); sql: "INSERT INTO abcdef(a,b,c) VALUES (?,?,69)",
db.exec(); bind: [i, i * 2],
console.log("Insert some data using exec()..."); returnValue: "resultRows",
for (let i = 20; i <= 25; ++i) { rowMode: "object",
db.exec({ });
sql: "INSERT INTO t(a,b) VALUES (?,?)", // console.log(innerRes);
bind: [i, i * 2],
}); });
} })
db.exec(); );
} finally { this.benches.push(
db.close(); await this.time("sqlite-query-many", async () => {
} console.log("Query data with exec()...");
} */ let rowRes = await this.promiser("exec", {
dbId: this.dbId,
sql: "SELECT * FROM abcdef ORDER BY a LIMIT 50",
returnValue: "resultRows",
rowMode: "object",
});
console.log(rowRes);
})
);
} finally {
await this.promiser("close", { dbId: this.dbId });
}
}
async idb() {
this.benches.push(await this.time("idb-insert-many", async() => {
await this.doInsert(async (s) => {
await this.persister.setItem("i/" +s, "brian tatler fucked and abused sean harris\n".repeat(Math.floor(s * 1.1)))
})
}))
this.benches.push(await this.time("idb-query-many", async() => {
let q: any[] = [];
await this.doInsert(async (s) => {
q.push(await this.persister.getItem("i/" + s))
})
console.log("qlen", q.length)
}))
} }
} }

4
src/sqlite.d.ts vendored
View File

@ -1,5 +1,5 @@
import "@sqlite.org/sqlite-wasm" import "@sqlite.org/sqlite-wasm";
declare module "@sqlite.org/sqlite-wasm" { declare module "@sqlite.org/sqlite-wasm" {
export const sqlite3Worker1Promiser: any export const sqlite3Worker1Promiser: any;
} }
declare module "@sqlite.org/sqlite-wasm/sqlite-wasm/jswasm/sqlite3-worker1-bundler-friendly"; declare module "@sqlite.org/sqlite-wasm/sqlite-wasm/jswasm/sqlite3-worker1-bundler-friendly";

20
src/sqlite3.worker.ts Normal file
View File

@ -0,0 +1,20 @@
import sqlite3InitModule from "@sqlite.org/sqlite-wasm";
(async () => {})();
let sqlite3;
onmessage = async (event) => {
console.log(event);
if (event.data.type == "init") {
sqlite3 = await sqlite3InitModule({
locateFile: (path, prefix) => event.data.path,
});
// debugger;
console.log(`Running sqlite version: ${sqlite3.version.libVersion}`);
await sqlite3.installOpfsSAHPoolVfs({
clearOnInit: false,
initialCapacity: 65536,
directory: "/var/managed-sah"
});
sqlite3.initWorker1API();
console.log(sqlite3.capi.sqlite3_vfs_find("opfs-sahpool"));
}
};

View File

@ -1,15 +0,0 @@
import sqlite3InitModule from "@sqlite.org/sqlite-wasm"
(async()=> {
})()
let sqlite3;
onmessage = async (event) => {
console.log(event)
if(event.data.type== "init") {
sqlite3 = await sqlite3InitModule({locateFile: (path, prefix) => event.data.path})
// debugger;
console.log(`Running sqlite version: ${sqlite3.version.libVersion}`);
await sqlite3.installOpfsSAHPoolVfs({clearOnInit: false, initialCapacity: 65536});
sqlite3.initWorker1API()
console.log(sqlite3.capi.sqlite3_vfs_find("opfs"))
}
}

2
src/workers.d.ts vendored
View File

@ -1,4 +1,4 @@
declare module "testthing.worker" { declare module "sqlite3.worker" {
const WorkerFactory: new () => Worker; const WorkerFactory: new () => Worker;
export default WorkerFactory; export default WorkerFactory;
} }

View File

@ -576,6 +576,11 @@ ignore@^5.2.0:
resolved "https://registry.yarnpkg.com/ignore/-/ignore-5.3.1.tgz#5073e554cd42c5b33b394375f538b8593e34d4ef" resolved "https://registry.yarnpkg.com/ignore/-/ignore-5.3.1.tgz#5073e554cd42c5b33b394375f538b8593e34d4ef"
integrity sha512-5Fytz/IraMjqpwfd34ke28PTVMjZjJG2MPn5t7OE4eUCUNf8BAa7b5WUS9/Qvr6mwOQS7Mk6vdsMno5he+T8Xw== integrity sha512-5Fytz/IraMjqpwfd34ke28PTVMjZjJG2MPn5t7OE4eUCUNf8BAa7b5WUS9/Qvr6mwOQS7Mk6vdsMno5he+T8Xw==
immediate@~3.0.5:
version "3.0.6"
resolved "https://registry.yarnpkg.com/immediate/-/immediate-3.0.6.tgz#9db1dbd0faf8de6fbe0f5dd5e56bb606280de69b"
integrity sha512-XXOFtyqDjNDAQxVfYxuF7g9Il/IbWmmlQg2MYKOH8ExIT1qg6xc4zyS3HaEEATgs1btfzxq15ciUiY7gjSXRGQ==
is-extglob@^2.1.1: is-extglob@^2.1.1:
version "2.1.1" version "2.1.1"
resolved "https://registry.yarnpkg.com/is-extglob/-/is-extglob-2.1.1.tgz#a88c02535791f02ed37c76a1b9ea9773c833f8c2" resolved "https://registry.yarnpkg.com/is-extglob/-/is-extglob-2.1.1.tgz#a88c02535791f02ed37c76a1b9ea9773c833f8c2"
@ -593,6 +598,20 @@ is-number@^7.0.0:
resolved "https://registry.yarnpkg.com/is-number/-/is-number-7.0.0.tgz#7535345b896734d5f80c4d06c50955527a14f12b" resolved "https://registry.yarnpkg.com/is-number/-/is-number-7.0.0.tgz#7535345b896734d5f80c4d06c50955527a14f12b"
integrity sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng== integrity sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng==
lie@3.1.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/lie/-/lie-3.1.1.tgz#9a436b2cc7746ca59de7a41fa469b3efb76bd87e"
integrity sha512-RiNhHysUjhrDQntfYSfY4MU24coXXdEOgw9WGcKHNeEwffDYbF//u87M1EWaMGzuFoSbqW0C9C6lEEhDOAswfw==
dependencies:
immediate "~3.0.5"
localforage@^1.10.0:
version "1.10.0"
resolved "https://registry.yarnpkg.com/localforage/-/localforage-1.10.0.tgz#5c465dc5f62b2807c3a84c0c6a1b1b3212781dd4"
integrity sha512-14/H1aX7hzBBmmh7sGPd+AOMkkIrHM3Z1PAyGgZigA1H1p5O5ANnMyWzvpAETtG68/dC4pC0ncy3+PPGzXZHPg==
dependencies:
lie "3.1.1"
locate-path@^5.0.0: locate-path@^5.0.0:
version "5.0.0" version "5.0.0"
resolved "https://registry.yarnpkg.com/locate-path/-/locate-path-5.0.0.tgz#1afba396afd676a6d42504d0a67a3a7eb9f62aa0" resolved "https://registry.yarnpkg.com/locate-path/-/locate-path-5.0.0.tgz#1afba396afd676a6d42504d0a67a3a7eb9f62aa0"
@ -725,11 +744,6 @@ slash@^3.0.0:
resolved "https://registry.yarnpkg.com/slash/-/slash-3.0.0.tgz#6539be870c165adbd5240220dbe361f1bc4d4634" resolved "https://registry.yarnpkg.com/slash/-/slash-3.0.0.tgz#6539be870c165adbd5240220dbe361f1bc4d4634"
integrity sha512-g9Q1haeby36OSStwb4ntCGGGaKsaVSjQ68fBxoQcutl5fS1vuY18H3wSt3jFyFtrkx+Kz0V1G85A4MyAdDMi2Q== integrity sha512-g9Q1haeby36OSStwb4ntCGGGaKsaVSjQ68fBxoQcutl5fS1vuY18H3wSt3jFyFtrkx+Kz0V1G85A4MyAdDMi2Q==
sqlite-wasm-esm@^0.0.30:
version "0.0.30"
resolved "https://registry.yarnpkg.com/sqlite-wasm-esm/-/sqlite-wasm-esm-0.0.30.tgz#b25492775bc9f26d260d683820787b840b466592"
integrity sha512-rLl+STKLfGXyBcpQlH6uEMMh76YXixY3s+qDEMzIiMMsyN7iXLmo4Mk1Su/6GoJFprSWP+cgOCWQsAbLELCEPg==
to-regex-range@^5.0.1: to-regex-range@^5.0.1:
version "5.0.1" version "5.0.1"
resolved "https://registry.yarnpkg.com/to-regex-range/-/to-regex-range-5.0.1.tgz#1648c44aae7c8d988a326018ed72f5b4dd0392e4" resolved "https://registry.yarnpkg.com/to-regex-range/-/to-regex-range-5.0.1.tgz#1648c44aae7c8d988a326018ed72f5b4dd0392e4"