From d1c604c73a685449224a6f9bbc9c7583c788f7fd Mon Sep 17 00:00:00 2001 From: Malte Teichert Date: Thu, 30 May 2024 17:16:34 +0200 Subject: [PATCH] add groundwork for IndexedDB persisted store --- package.json | 1 + pnpm-lock.yaml | 8 ++++ src/lib/components/sections/Info.svelte | 1 - src/lib/db/document.ts | 63 +++++++++++++++++++++++++ src/lib/db/index.ts | 12 +++++ src/lib/stores.ts | 21 +++++++++ 6 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 src/lib/db/document.ts create mode 100644 src/lib/db/index.ts create mode 100644 src/lib/stores.ts diff --git a/package.json b/package.json index 6e2d723..d2d8d17 100644 --- a/package.json +++ b/package.json @@ -45,6 +45,7 @@ "dependencies": { "@floating-ui/dom": "1.6.5", "@sveltejs/enhanced-img": "^0.2.0", + "dexie": "^4.0.7", "openapi-types": "^12.1.3", "svelte-persisted-store": "^0.9.2" }, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 06dc0b3..b69c2de 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -14,6 +14,9 @@ importers: '@sveltejs/enhanced-img': specifier: ^0.2.0 version: 0.2.0(rollup@4.17.2)(svelte@4.2.17) + dexie: + specifier: ^4.0.7 + version: 4.0.7 openapi-types: specifier: ^12.1.3 version: 12.1.3 @@ -852,6 +855,9 @@ packages: devalue@5.0.0: resolution: {integrity: sha512-gO+/OMXF7488D+u3ue+G7Y4AA3ZmUnB3eHJXmBTgNHvr4ZNzl36A0ZtG+XCRNYCkYx/bFmw4qtkoFLa+wSrwAA==} + dexie@4.0.7: + resolution: {integrity: sha512-M+Lo6rk4pekIfrc2T0o2tvVJwL6EAAM/B78DNfb8aaxFVoI1f8/rz5KTxuAnApkwqTSuxx7T5t0RKH7qprapGg==} + didyoumean@1.2.2: resolution: {integrity: sha512-gxtyfqMg7GKyhQmb056K7M3xszy/myH8w+B4RT+QXBQsvAOdc3XymqDDPHx1BgPgsdAA5SIifona89YtRATDzw==} @@ -2439,6 +2445,8 @@ snapshots: devalue@5.0.0: {} + dexie@4.0.7: {} + didyoumean@1.2.2: {} dir-glob@3.0.1: diff --git a/src/lib/components/sections/Info.svelte b/src/lib/components/sections/Info.svelte index ed0112a..df7cd61 100644 --- a/src/lib/components/sections/Info.svelte +++ b/src/lib/components/sections/Info.svelte @@ -1,5 +1,4 @@ diff --git a/src/lib/db/document.ts b/src/lib/db/document.ts new file mode 100644 index 0000000..f4ec3d4 --- /dev/null +++ b/src/lib/db/document.ts @@ -0,0 +1,63 @@ +import type { OpenAPIV3_1 } from "openapi-types"; +import { db } from "."; + +export const emptyDocument: OpenAPIV3_1.Document = { + openapi: '3.1.0', // OpenAPI version + jsonSchemaDialect: 'https://json-schema.org/draft/2020-12/schema', + info: { + /** Title of the API (required) */ + title: '', + /** Description of the API (optional) */ + description: '', + /** Terms of service link (optional) */ + termsOfService: '', + /** API Version (required) */ + version: '', + /** Contact Information */ + contact: { + /** Name of the contact person/organization. */ + name: '', // optional + /** URL pointing to the contact information. MUST be in the format of a URL. */ + url: '', // optional + /** Email address of the contact person/organization. MUST be in the format of an email address. */ + email: '' // optional + }, + license: { + name: '', // required if license is included + url: '' // optional + } + }, + servers: [], + paths: {}, + webhooks: {}, + components: {}, + security: [], + tags: [], + externalDocs: { + description: '', + url: '' + } +}; + +/// Get the document from the database +export const getDocument = async () => { + const document = await db.document.toArray(); + // if no document exists, return an empty object + return document[0] || emptyDocument; +}; + +/// Update the document in the database +export const updateDocument = async (document: OpenAPIV3_1.Document) => { + // delete the existing document + await db.document.clear(); + // add the new document to the database + await db.document.add(document); +}; + +/// Delete the document from the database +export const deleteDocument = async () => { + // delete the existing document + await db.document.clear(); + // add an empty document to the database + await db.document.add(emptyDocument); +}; diff --git a/src/lib/db/index.ts b/src/lib/db/index.ts new file mode 100644 index 0000000..6e51e1f --- /dev/null +++ b/src/lib/db/index.ts @@ -0,0 +1,12 @@ +/** This file set's up dexie as an interface to the local indexedDB */ + +import Dexie, { type EntityTable } from 'dexie'; +import type { OpenAPIV3_1 } from 'openapi-types'; + +export const db = new Dexie("openAPI") as Dexie & { + document: EntityTable; +} + +db.version(1).stores({ + document: '' +}); diff --git a/src/lib/stores.ts b/src/lib/stores.ts new file mode 100644 index 0000000..9fdd97a --- /dev/null +++ b/src/lib/stores.ts @@ -0,0 +1,21 @@ +import type { OpenAPIV3_1 } from "openapi-types"; +import { get, writable, type Writable } from "svelte/store"; +import { emptyDocument, updateDocument } from "./db/document"; + +const store: Writable = writable(emptyDocument); + +/// A store that also persists to the indexedDB +export const openApiStore: Writable = { + ...store, + // Extend the store with database persistence + set: (value: OpenAPIV3_1.Document) => { + // Save the value to the database + updateDocument(value); + store.set(value); + }, + update: (updater: (value: OpenAPIV3_1.Document) => OpenAPIV3_1.Document) => { + store.update(updater); + // Update the value in the database + updateDocument(get(store)); + } +}; \ No newline at end of file