Compare commits
10 Commits
d73d7d22ba
...
de9280186d
Author | SHA1 | Date | |
---|---|---|---|
de9280186d | |||
03cd26e196 | |||
71a1f6fae7 | |||
fbd87c0b89 | |||
075d68826c | |||
567aff2acb | |||
481331b494 | |||
56d2b9cae0 | |||
cd95682b47 | |||
61b8f8f3d6 |
97
README.md
97
README.md
@ -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.
|
||||
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
|
||||
to build + install, run `./build.ps1 -vault /path/to/your/vault` and open the console in obsidian :)
|
||||
|
@ -23,6 +23,7 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@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"
|
||||
}
|
||||
}
|
||||
|
181
src/main.ts
181
src/main.ts
@ -1,50 +1,41 @@
|
||||
import {
|
||||
App,
|
||||
Editor,
|
||||
MarkdownView,
|
||||
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"
|
||||
import { Notice, Plugin } from "obsidian";
|
||||
import * as localforage from "localforage";
|
||||
import TestWorker from "sqlite3.worker";
|
||||
import { sqlite3Worker1Promiser } from "@sqlite.org/sqlite-wasm";
|
||||
|
||||
// Remember to rename these classes and interfaces!
|
||||
|
||||
interface MyPluginSettings {
|
||||
mySetting: string;
|
||||
interface Benchmark {
|
||||
time: number;
|
||||
name: string;
|
||||
}
|
||||
|
||||
const DEFAULT_SETTINGS: MyPluginSettings = {
|
||||
mySetting: "default",
|
||||
};
|
||||
|
||||
export default class MyPlugin extends Plugin {
|
||||
settings: MyPluginSettings;
|
||||
public persister: LocalForage;
|
||||
settings: any;
|
||||
private worker: Worker;
|
||||
private promiser: (...args: any[]) => any;
|
||||
|
||||
public benches: Benchmark[] = [];
|
||||
private dbId: string;
|
||||
async onload() {
|
||||
await this.loadSettings();
|
||||
console.log(import.meta)
|
||||
|
||||
// This creates an icon in the left ribbon.
|
||||
await this.initSqlite();
|
||||
this.benches.push(
|
||||
await this.time("sqlite-init", async () => 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() {
|
||||
this.worker.terminate()
|
||||
this.worker.terminate();
|
||||
}
|
||||
|
||||
async loadSettings() {
|
||||
this.settings = Object.assign(
|
||||
{},
|
||||
DEFAULT_SETTINGS,
|
||||
await this.loadData()
|
||||
);
|
||||
this.settings = Object.assign({}, await this.loadData());
|
||||
}
|
||||
|
||||
async saveSettings() {
|
||||
@ -56,13 +47,36 @@ export default class MyPlugin extends Plugin {
|
||||
new Notice("~LOG~", args.join("\n"));
|
||||
}
|
||||
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() {
|
||||
this.worker = new TestWorker();
|
||||
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) => {
|
||||
const _promiser = sqlite3Worker1Promiser({
|
||||
print: this.printNotice,
|
||||
@ -71,67 +85,84 @@ export default class MyPlugin extends Plugin {
|
||||
onready: () => {
|
||||
resolve(_promiser);
|
||||
},
|
||||
worker: () => w
|
||||
worker: () => w,
|
||||
});
|
||||
});
|
||||
await this.start()
|
||||
(window as any).sqlite3Promiser = this.promiser;
|
||||
}
|
||||
|
||||
async start() {
|
||||
let openRes = await this.promiser("open", {
|
||||
filename: "/test.sqlite2",
|
||||
vfs: "opfs-sahpool",
|
||||
async doInsert(each: (s: number) => Promise<void>) {
|
||||
for (let i = 0; i < 50; i++) {
|
||||
await each(i);
|
||||
}
|
||||
}
|
||||
|
||||
async sqlite() {
|
||||
this.benches.push(
|
||||
await this.time("sqlite-open", async () => {
|
||||
let openRes = await this.promiser("open", {
|
||||
filename: "/managed-sah/test.sqlite3",
|
||||
vfs: "opfs-sahpool",
|
||||
});
|
||||
const { dbId } = openRes;
|
||||
this.dbId = dbId;
|
||||
console.log(openRes);
|
||||
})
|
||||
const {dbId} = openRes
|
||||
console.log(openRes)
|
||||
);
|
||||
try {
|
||||
console.log("Creating a table...")
|
||||
this.benches.push(await this.time("sqlite-create-table", async () => {
|
||||
console.log("Creating a table...");
|
||||
let res = await this.promiser("exec", {
|
||||
dbId,
|
||||
dbId: this.dbId,
|
||||
sql: "DROP TABLE IF EXISTS abcdef; CREATE TABLE IF NOT EXISTS abcdef(a,b,c)",
|
||||
returnValue: "resultRows"
|
||||
})
|
||||
console.log("Result: ", res)
|
||||
returnValue: "resultRows",
|
||||
});
|
||||
console.log("Result: ", res);
|
||||
}));
|
||||
|
||||
this.benches.push(
|
||||
await this.time("sqlite-insert-many", async () => {
|
||||
console.log("Insert some data using exec()...");
|
||||
for (let i = 20; i <= 25; ++i) {
|
||||
await this.doInsert(async (i) => {
|
||||
let innerRes = await this.promiser("exec", {
|
||||
dbId,
|
||||
dbId: this.dbId,
|
||||
sql: "INSERT INTO abcdef(a,b,c) VALUES (?,?,69)",
|
||||
bind: [i, i * 2],
|
||||
returnValue: "resultRows",
|
||||
rowMode: "object"
|
||||
rowMode: "object",
|
||||
});
|
||||
console.log(innerRes)
|
||||
}
|
||||
// console.log(innerRes);
|
||||
});
|
||||
})
|
||||
);
|
||||
this.benches.push(
|
||||
await this.time("sqlite-query-many", async () => {
|
||||
console.log("Query data with exec()...");
|
||||
let rowRes = await this.promiser("exec", {
|
||||
dbId,
|
||||
sql: "SELECT * FROM abcdef ORDER BY a LIMIT 10",
|
||||
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})
|
||||
}
|
||||
// db = new sqlite3.oo1.DB("/test.sqlite3", "ct");
|
||||
/* if (db) {
|
||||
try {
|
||||
console.log("Creating a table...");
|
||||
db.exec();
|
||||
console.log("Insert some data using exec()...");
|
||||
for (let i = 20; i <= 25; ++i) {
|
||||
db.exec({
|
||||
sql: "INSERT INTO t(a,b) VALUES (?,?)",
|
||||
bind: [i, i * 2],
|
||||
rowMode: "object",
|
||||
});
|
||||
}
|
||||
db.exec();
|
||||
console.log(rowRes);
|
||||
})
|
||||
);
|
||||
} finally {
|
||||
db.close();
|
||||
}
|
||||
} */
|
||||
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
4
src/sqlite.d.ts
vendored
@ -1,5 +1,5 @@
|
||||
import "@sqlite.org/sqlite-wasm"
|
||||
import "@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";
|
||||
|
20
src/sqlite3.worker.ts
Normal file
20
src/sqlite3.worker.ts
Normal 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"));
|
||||
}
|
||||
};
|
@ -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
2
src/workers.d.ts
vendored
@ -1,4 +1,4 @@
|
||||
declare module "testthing.worker" {
|
||||
declare module "sqlite3.worker" {
|
||||
const WorkerFactory: new () => Worker;
|
||||
export default WorkerFactory;
|
||||
}
|
||||
|
24
yarn.lock
24
yarn.lock
@ -576,6 +576,11 @@ ignore@^5.2.0:
|
||||
resolved "https://registry.yarnpkg.com/ignore/-/ignore-5.3.1.tgz#5073e554cd42c5b33b394375f538b8593e34d4ef"
|
||||
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:
|
||||
version "2.1.1"
|
||||
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"
|
||||
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:
|
||||
version "5.0.0"
|
||||
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"
|
||||
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:
|
||||
version "5.0.1"
|
||||
resolved "https://registry.yarnpkg.com/to-regex-range/-/to-regex-range-5.0.1.tgz#1648c44aae7c8d988a326018ed72f5b4dd0392e4"
|
||||
|
Loading…
x
Reference in New Issue
Block a user