From 210c001d5a124bd3586285b80831145ed1e7ab70 Mon Sep 17 00:00:00 2001 From: renatodellosso Date: Wed, 5 Feb 2025 09:31:36 -0500 Subject: [PATCH 1/6] Update version to 1.1.18 and add LocalStorageDbInterface for managing local storage operations --- .../dbinterfaces/LocalStorageDbInterface.ts | 57 +++++++++++++ package-lock.json | 10 +-- pages/dev/localstoragedb.tsx | 85 +++++++++++++++++++ 3 files changed, 147 insertions(+), 5 deletions(-) create mode 100644 lib/client/dbinterfaces/LocalStorageDbInterface.ts create mode 100644 pages/dev/localstoragedb.tsx diff --git a/lib/client/dbinterfaces/LocalStorageDbInterface.ts b/lib/client/dbinterfaces/LocalStorageDbInterface.ts new file mode 100644 index 00000000..969b2242 --- /dev/null +++ b/lib/client/dbinterfaces/LocalStorageDbInterface.ts @@ -0,0 +1,57 @@ +import { ObjectId } from "bson"; +import CollectionId, { CollectionIdToType } from "@/lib/client/CollectionId"; +import DbInterface, { + WithStringOrObjectIdId, +} from "@/lib/client/dbinterfaces/DbInterface"; +import { default as BaseLocalStorageDbInterface } from "mongo-anywhere/LocalStorageDbInterface"; + +export default class LocalStorageDbInterface + extends BaseLocalStorageDbInterface< + CollectionId, + CollectionIdToType + > + implements DbInterface +{ + init(): Promise { + return super.init(Object.values(CollectionId)); + } + addObject>( + collection: TId, + object: WithStringOrObjectIdId, + ): Promise { + return super.addObject(collection, object); + } + deleteObjectById(collection: CollectionId, id: ObjectId): Promise { + return super.deleteObjectById(collection, id); + } + updateObjectById< + TId extends CollectionId, + TObj extends CollectionIdToType, + >(collection: TId, id: ObjectId, newValues: Partial): Promise { + return super.updateObjectById(collection, id, newValues); + } + findObjectById< + TId extends CollectionId, + TObj extends CollectionIdToType, + >(collection: TId, id: ObjectId): Promise { + return super.findObjectById(collection, id); + } + findObject>( + collection: TId, + query: object, + ): Promise { + return super.findObject(collection, query); + } + findObjects>( + collection: TId, + query: object, + ): Promise { + return super.findObjects(collection, query); + } + countObjects( + collection: CollectionId, + query: object, + ): Promise { + return super.countObjects(collection, query); + } +} diff --git a/package-lock.json b/package-lock.json index b0adfb9f..f7ae8c59 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "sj3", - "version": "1.1.17", + "version": "1.1.18", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "sj3", - "version": "1.1.17", + "version": "1.1.18", "license": "CC BY-NC-SA 4.0", "dependencies": { "dependencies": "^0.0.1", @@ -9515,9 +9515,9 @@ } }, "node_modules/mongo-anywhere": { - "version": "1.0.21", - "resolved": "https://registry.npmjs.org/mongo-anywhere/-/mongo-anywhere-1.0.21.tgz", - "integrity": "sha512-nG+uUuVyrgetvkcVd/JgHSvfQgzrAUjIFflfaDirZeRGL/gdluGo49rJxPc7GjjcBb4+NWLIPFXgGwrmFDH4xw==", + "version": "1.0.22", + "resolved": "https://registry.npmjs.org/mongo-anywhere/-/mongo-anywhere-1.0.22.tgz", + "integrity": "sha512-hmYAXBIwzIxVUAd/bRCBC9MAMBEy1mSEBM1TDXt+HzWBFCekV3gMGc7FsyblvFq3QzIQxUZKKlpN/xnTbb0lhA==", "dependencies": { "bson": "^5.0.0", "minimongo": "^6.19.0", diff --git a/pages/dev/localstoragedb.tsx b/pages/dev/localstoragedb.tsx new file mode 100644 index 00000000..ba5e36f0 --- /dev/null +++ b/pages/dev/localstoragedb.tsx @@ -0,0 +1,85 @@ +import Container from "@/components/Container"; +import CollectionId from "@/lib/client/CollectionId"; +import LocalStorageDbInterface from "@/lib/client/dbinterfaces/LocalStorageDbInterface"; +import { useEffect, useState } from "react"; +import toast from "react-hot-toast"; + +export default function LocalStorageDb() { + const [db, setDb] = useState(); + const [collection, setCollection] = useState(); + const [json, setJson] = useState(); + + const [dbData, setDbData] = useState<{ [collection: string]: object[] }>({}); + + useEffect(() => { + const db = new LocalStorageDbInterface(); + db.init().then(() => { + setDb(db); + updateDbData(db); + }); + }, []); + + async function updateDbData(db: LocalStorageDbInterface) { + if (!db) return; + const newDbData: { [collection: string]: object[] } = {}; + await Promise.all( + Object.values(CollectionId).map(async (collection) => { + const objects = await db.findObjects(collection, {}); + newDbData[collection] = objects; + }), + ); + setDbData(newDbData); + } + + function addObject() { + if (!db || !collection || !json) return; + try { + db.addObject(collection, JSON.parse(json)); + } catch (e: any) { + toast.error(e.message); + } + updateDbData(db); + } + + return ( + +

LocalStorage DB

+
+ +