add groundwork for IndexedDB persisted store

This commit is contained in:
Malte Teichert
2024-05-30 17:16:34 +02:00
parent f035b17c36
commit d1c604c73a
6 changed files with 105 additions and 1 deletions

View File

@@ -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"
},

8
pnpm-lock.yaml generated
View File

@@ -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:

View File

@@ -1,5 +1,4 @@
<script lang="ts">
import Info from '../icons/Info.svelte';
import LicenseAtom from '../atoms/LicenseAtom.svelte';
import { openApiStore } from '$lib';
</script>

63
src/lib/db/document.ts Normal file
View File

@@ -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);
};

12
src/lib/db/index.ts Normal file
View File

@@ -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<OpenAPIV3_1.Document>;
}
db.version(1).stores({
document: ''
});

21
src/lib/stores.ts Normal file
View File

@@ -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<OpenAPIV3_1.Document> = writable(emptyDocument);
/// A store that also persists to the indexedDB
export const openApiStore: Writable<OpenAPIV3_1.Document> = {
...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));
}
};