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..674767db 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "sj3", - "version": "1.1.17", + "version": "1.1.19", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "sj3", - "version": "1.1.17", + "version": "1.1.19", "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.23", + "resolved": "https://registry.npmjs.org/mongo-anywhere/-/mongo-anywhere-1.0.23.tgz", + "integrity": "sha512-n6N/fLRb2mylontUuxU4C57SFjULCqCXUUyeNbITRU2jZ8SH9brpZn3woEDwQ0DYZ/GXLHYcDqn2esZkAZTnKQ==", "dependencies": { "bson": "^5.0.0", "minimongo": "^6.19.0", @@ -12683,9 +12683,9 @@ } }, "node_modules/unified-api-nextjs": { - "version": "1.0.7", - "resolved": "https://registry.npmjs.org/unified-api-nextjs/-/unified-api-nextjs-1.0.7.tgz", - "integrity": "sha512-9D52rB8Cl1XOf9/JPe2tGmIX9DdKq/Ob5sr2wICA+CDWBEJ4LHD1d0f30ej235dlcpBcKHDq6IzoOMR0cJXOTQ==", + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/unified-api-nextjs/-/unified-api-nextjs-1.0.8.tgz", + "integrity": "sha512-6rX6qqgg1jnbem8xygdiaOKrvTVg3BjMEMDYwERhBRb8prUFbZ6jlW8wH1NXwMlzXsawd7scCnqG8WzjK3dU1A==", "dependencies": { "next": "^15.1.2", "unified-api": "^1.0.19" diff --git a/package.json b/package.json index 0bf5cf2c..f7d0e599 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "sj3", - "version": "1.1.18", + "version": "1.1.19", "private": true, "repository": "https://github.com/Decatur-Robotics/Gearbox", "license": "CC BY-NC-SA 4.0", diff --git a/pages/dev/localstoragedb.tsx b/pages/dev/localstoragedb.tsx new file mode 100644 index 00000000..3ee7061c --- /dev/null +++ b/pages/dev/localstoragedb.tsx @@ -0,0 +1,192 @@ +import Container from "@/components/Container"; +import CollectionId from "@/lib/client/CollectionId"; +import LocalStorageDbInterface from "@/lib/client/dbinterfaces/LocalStorageDbInterface"; +import { ObjectId } from "bson"; +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[] }>({}); + const [flagCount, setFlagCount] = useState(0); + + 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); + + const flaggedDocs: object[] = ( + await Promise.all( + Object.values(CollectionId).map( + async (collection) => + await db.findObjects(collection, { flagged: true }), + ), + ) + ).flat(); + setFlagCount(flaggedDocs.length); + } + + async function addObject() { + if (!db || !collection || !json) return; + + const obj = JSON.parse(json); + + console.log("Adding object", obj, "to", collection); + + try { + await db.addObject(collection, obj); + } catch (e: any) { + toast.error(e.message); + } + updateDbData(db); + } + + async function deleteObject(collection: CollectionId, _id: ObjectId) { + if (!db) return; + + console.log("Deleting object", _id.toString(), "from", collection); + + await db.deleteObjectById(collection, _id); + updateDbData(db); + } + + async function increment(collection: CollectionId, _id: ObjectId) { + if (!db) return; + + console.log("Incrementing object", _id.toString(), "in", collection); + + const object = await db.findObjectById(collection, _id); + + if (!object) { + toast.error("Object not found:" + _id.toString()); + return; + } + + await db.updateObjectById(collection, _id, { + count: (object.count ?? 0) + 1, + }); + + updateDbData(db); + } + + async function toggleFlag(collection: CollectionId, _id: ObjectId) { + if (!db) return; + + console.log("Toggling flag on object", _id.toString(), "in", collection); + + const object = await db.findObjectById(collection, _id); + + if (!object) { + toast.error("Object not found:" + _id.toString()); + return; + } + + await db.updateObjectById(collection, _id, { + flagged: !object.flagged, + }); + + updateDbData(db); + } + + return ( + +

LocalStorage DB

+
+ +