b9e42c3ef4
Build and Deploy / build-and-deploy (push) Has been cancelled
- lit-force-graph: Refactored for instance reuse, better lifecycle management, and added dynamic imports for AR/VR modes. - lit-html-table: Improved visual design, readability of headers, and fixed reactivity in editable mode. - lit-draggable-dom: Fixed clipping issues by switching to relative/absolute positioning and resolved a panning speed bug caused by CSS variable inheritance.
55 lines
1.2 KiB
TypeScript
55 lines
1.2 KiB
TypeScript
// import "@babel/polyfill";
|
|
|
|
import {
|
|
addRxPlugin,
|
|
createRxDatabase,
|
|
RxDatabase,
|
|
RxDocument,
|
|
RxQuery,
|
|
} from "rxdb";
|
|
|
|
// @ts-ignore
|
|
import * as idb from "pouchdb-adapter-idb";
|
|
import { TemplateResult } from "lit";
|
|
import { futureBuilder } from "../components/future-builder";
|
|
import { Todo, todosSchema } from "./todos";
|
|
import { Base, BaseCollection } from "./base";
|
|
import { DEFAULT_LOADING } from "../components/loading";
|
|
|
|
const DATABASE_NAME = "todos" + "db";
|
|
|
|
addRxPlugin(idb);
|
|
|
|
export type Schema = {
|
|
todos: BaseCollection<Todo>;
|
|
};
|
|
|
|
export var db: RxDatabase<Schema>;
|
|
|
|
async function setupDB() {
|
|
if (db) return db;
|
|
db = await createRxDatabase<Schema>({
|
|
name: DATABASE_NAME,
|
|
adapter: "idb",
|
|
});
|
|
await db.addCollections({
|
|
todos: todosSchema,
|
|
});
|
|
return db;
|
|
}
|
|
|
|
export const dbProvider = (
|
|
builder: (db: RxDatabase<Schema>) => TemplateResult,
|
|
loading: TemplateResult = DEFAULT_LOADING
|
|
) => {
|
|
return futureBuilder(setupDB(), (res) => builder(res), loading);
|
|
};
|
|
|
|
export const queryBuilder = <T extends Base>(
|
|
query: RxQuery<any>,
|
|
builder: (result: RxDocument<T> | RxDocument<T>[]) => TemplateResult,
|
|
loading: TemplateResult = DEFAULT_LOADING
|
|
) => {
|
|
return futureBuilder(query.exec(), (res) => builder(res), loading);
|
|
};
|