Skip to content

Commit

Permalink
44 (#104)
Browse files Browse the repository at this point in the history
* fix: setBulk, and set for global documents, fixes #103

* fix: actually fix the setBulk function, was focused on set
  • Loading branch information
Stuyk authored Jul 11, 2024
1 parent f73060b commit 56b8fc3
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 12 deletions.
6 changes: 3 additions & 3 deletions docs/api/server/document/document-global.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<MyDataType>('my-identifier-goes-here-for-my-document');

// Overwrite or set a single field
document.set<MyDataType>('totalMoneyCreated', 200);
document.set('totalMoneyCreated', 200);

// Overwrite multiple fields
document.setBulk<MyDataType>({ totalMoneyCreated: 200, totalMoneyDestroyed: 200 });
document.setBulk({ totalMoneyCreated: 200, totalMoneyDestroyed: 200 });
}
```
13 changes: 13 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
45 changes: 36 additions & 9 deletions src/main/server/document/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends Object = Object>(identifier: string) {
if (!data[identifier]) {
let newData = await db.get<{ _id: string; identifier: string }>({ identifier }, CollectionNames.Global);

Expand All @@ -16,22 +16,49 @@ export async function useGlobal(identifier: string) {
data[identifier] = newData;
}

function get<T = Object>(): 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<T = any>(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<K extends keyof T>(fieldName: K): T[K] {
const refData = data[identifier] as T;
return refData[fieldName];
}

async function set(fieldName: string, value: any): Promise<boolean> {
data[identifier][fieldName] = value;
/**
* Set any field name or value in your global document
*
* @param {string} fieldName
* @param {*} value
* @return {Promise<boolean>}
*/
async function set<K extends keyof T>(fieldName: K, value: T[K]): Promise<boolean> {
data[identifier] = Object.assign(data[identifier], { [fieldName]: value });
return await db.update({ _id: data[identifier]._id, [fieldName]: value }, CollectionNames.Global);
}

async function setBulk<T = Object>(data: Partial<T>): Promise<boolean> {
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<T>} data
* @return {Promise<boolean>}
*/
async function setBulk(newData: Partial<T>): Promise<boolean> {
data[identifier] = Object.assign(data[identifier], newData);
return await db.update({ _id: data[identifier]._id, ...newData }, CollectionNames.Global);
}

return {
Expand Down

0 comments on commit 56b8fc3

Please sign in to comment.