Consolidate Lit repositories: figma-to-lit, figma_lit_example, lit-3d-piano, lit-calculator, lit-code-editor, lit-draggable-dom, lit-file-based-routing, lit-force-graph, lit-html-editor, lit-html-table, lit-modules, lit-native, lit-node-editor, lit-sheet-music, lit-starter-ts, lit-vscode-extension, lit-wmr, vite-lit-capacitor, vite-lit-element-starter, vite-rxdb-lit
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
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);
|
||||
};
|
||||
Reference in New Issue
Block a user