stfrigerio_sqliteDB/main.ts

124 lines
3.2 KiB
TypeScript
Raw Normal View History

import {
App,
Plugin,
PluginSettingTab,
Setting,
2025-01-29 18:20:48 +00:00
Notice,
FileSystemAdapter,
Editor,
MarkdownView,
2025-01-29 19:58:04 +00:00
MarkdownPostProcessorContext
2025-01-29 18:20:48 +00:00
} from "obsidian";
2025-01-29 19:58:04 +00:00
import { DBService } from "./src/dbService";
import { inspectTableStructure, convertEntriesInNotes } from "./src/commands";
2025-02-04 16:08:15 +00:00
import { processSqlBlock, processSqlChartBlock } from "./src/codeblocks";
import { pickTableName } from "./src/helpers";
import { SqliteDBSettings, DEFAULT_SETTINGS } from "./src/types";
2025-01-29 18:20:48 +00:00
export default class SqliteDBPlugin extends Plugin {
settings: SqliteDBSettings;
private dbService: DBService;
2025-01-29 17:35:31 +00:00
async onload() {
console.log("Loading SqliteDBPlugin...");
2025-01-29 17:35:31 +00:00
await this.loadSettings();
2025-02-06 10:29:18 +00:00
this.dbService = new DBService(this.app);
2025-01-29 19:58:04 +00:00
await this.openDatabase();
this.addCommand({
id: "inspect-table-structure",
name: "Inspect table structure",
editorCallback: async (editor: Editor, view: MarkdownView) => {
// ensure DB is loaded (if not, load)
await this.openDatabase();
await inspectTableStructure(this.dbService, editor, this.app);
},
});
2025-01-29 17:35:31 +00:00
this.addCommand({
id: "dump-table-to-notes",
2025-02-22 15:25:13 +00:00
name: "Dump table to notes",
2025-01-29 18:20:48 +00:00
callback: async () => {
await this.openDatabase(); // ensure DB is loaded
// 1) pick a table
const chosenTable = await pickTableName(this.dbService, this.app);
if (!chosenTable) {
return; // user canceled or no tables
}
// 2) Call the dump function with the chosen table
await convertEntriesInNotes(this.dbService, chosenTable, this.app);
2025-01-29 18:20:48 +00:00
},
2025-01-29 17:35:31 +00:00
});
2025-01-29 19:58:04 +00:00
this.registerMarkdownCodeBlockProcessor(
"sql", // <-- the name of your code block (```sql)
async (source: string, el: HTMLElement, ctx: MarkdownPostProcessorContext) => {
await processSqlBlock(this.dbService, source, el);
}
);
2025-01-29 17:35:31 +00:00
2025-02-03 13:28:51 +00:00
this.registerMarkdownCodeBlockProcessor(
"sql-chart",
async (source: string, el: HTMLElement, ctx: MarkdownPostProcessorContext) => {
await processSqlChartBlock(this.dbService, source, el);
}
);
this.addSettingTab(new SqliteDBSettingTab(this.app, this));
2025-01-29 17:35:31 +00:00
}
onunload() {
console.log("Unloading SqliteDBPlugin...");
2025-01-29 18:20:48 +00:00
}
private async openDatabase(forceReload = true) {
const adapter = this.app.vault.adapter;
if (!(adapter instanceof FileSystemAdapter)) {
2025-02-22 15:27:30 +00:00
new Notice("This plugin currently only works on desktop.");
2025-01-29 18:20:48 +00:00
return;
}
const basePath = adapter.getBasePath();
await this.dbService.ensureDBLoaded(this.settings, basePath, forceReload);
2025-01-29 17:35:31 +00:00
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class SqliteDBSettingTab extends PluginSettingTab {
plugin: SqliteDBPlugin;
2025-01-29 17:35:31 +00:00
constructor(app: App, plugin: SqliteDBPlugin) {
2025-01-29 17:35:31 +00:00
super(app, plugin);
this.plugin = plugin;
}
display(): void {
2025-01-29 18:20:48 +00:00
const { containerEl } = this;
2025-01-29 17:35:31 +00:00
containerEl.empty();
new Setting(containerEl)
2025-02-22 15:25:13 +00:00
.setName("Database file path")
2025-01-29 18:20:48 +00:00
.setDesc("Absolute path to the .db file on disk.")
.addText((text) =>
text
.setPlaceholder("/home/user/path/to/your.db")
2025-01-29 18:20:48 +00:00
.setValue(this.plugin.settings.dbFilePath)
.onChange(async (value) => {
this.plugin.settings.dbFilePath = value;
await this.plugin.saveSettings();
})
);
2025-01-29 17:35:31 +00:00
}
}