From 56b8fc3437b31953af9b6a6f7a27fa310c81f4f6 Mon Sep 17 00:00:00 2001 From: Stuyk Date: Thu, 11 Jul 2024 16:18:42 -0600 Subject: [PATCH] 44 (#104) * fix: setBulk, and set for global documents, fixes #103 * fix: actually fix the setBulk function, was focused on set --- docs/api/server/document/document-global.md | 6 +-- docs/changelog.md | 13 ++++++ src/main/server/document/global.ts | 45 ++++++++++++++++----- 3 files changed, 52 insertions(+), 12 deletions(-) diff --git a/docs/api/server/document/document-global.md b/docs/api/server/document/document-global.md index 7d8f4ba24..0d06dea9a 100644 --- a/docs/api/server/document/document-global.md +++ b/docs/api/server/document/document-global.md @@ -56,12 +56,12 @@ type MyDataType = { }; async function doSomething() { - const document = await Rebar.document.global.useGlobal('my-identifier-goes-here-for-my-document'); + const document = await Rebar.document.global.useGlobal('my-identifier-goes-here-for-my-document'); // Overwrite or set a single field - document.set('totalMoneyCreated', 200); + document.set('totalMoneyCreated', 200); // Overwrite multiple fields - document.setBulk({ totalMoneyCreated: 200, totalMoneyDestroyed: 200 }); + document.setBulk({ totalMoneyCreated: 200, totalMoneyDestroyed: 200 }); } ``` diff --git a/docs/changelog.md b/docs/changelog.md index 393e12e91..456f3d1ec 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -1,5 +1,18 @@ # Changelog +## Version 44 + +### Code Changes + +- Fixed issue with setting global document data +- Additionally, improved type casting support for global documents + +### Docs Changes + +- Updated object document api to clarify generics usage + +--- + ## Version 43 ### Code Changes diff --git a/src/main/server/document/global.ts b/src/main/server/document/global.ts index cc760c261..2573d8e85 100644 --- a/src/main/server/document/global.ts +++ b/src/main/server/document/global.ts @@ -4,7 +4,7 @@ import { CollectionNames } from './shared.js'; const db = useDatabase(); const data: { [key: string]: { [key: string]: any } } = {}; -export async function useGlobal(identifier: string) { +export async function useGlobal(identifier: string) { if (!data[identifier]) { let newData = await db.get<{ _id: string; identifier: string }>({ identifier }, CollectionNames.Global); @@ -16,22 +16,49 @@ export async function useGlobal(identifier: string) { data[identifier] = newData; } - function get(): T { + /** + * Returns the entire document, use type casting or generics to define what is returned + * + * @template T + * @return {T} + */ + function get(): T { return data[identifier] as T; } - function getField(fieldName: string): T { - return data[identifier][fieldName]; + /** + * Returns a specific field from the document, with generic type cast support + * + * @template T + * @param {string} fieldName + * @return {T} + */ + function getField(fieldName: K): T[K] { + const refData = data[identifier] as T; + return refData[fieldName]; } - async function set(fieldName: string, value: any): Promise { - data[identifier][fieldName] = value; + /** + * Set any field name or value in your global document + * + * @param {string} fieldName + * @param {*} value + * @return {Promise} + */ + async function set(fieldName: K, value: T[K]): Promise { + data[identifier] = Object.assign(data[identifier], { [fieldName]: value }); return await db.update({ _id: data[identifier]._id, [fieldName]: value }, CollectionNames.Global); } - async function setBulk(data: Partial): Promise { - data[identifier] = Object.assign(data[identifier], data); - return await db.update({ _id: data[identifier]._id, ...data }, CollectionNames.Global); + /** + * Set multiple fields in your global document + * + * @param {Partial} data + * @return {Promise} + */ + async function setBulk(newData: Partial): Promise { + data[identifier] = Object.assign(data[identifier], newData); + return await db.update({ _id: data[identifier]._id, ...newData }, CollectionNames.Global); } return {