Skip to content

Commit

Permalink
feat: global documents, global document docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Stuyk committed Apr 29, 2024
1 parent 21ad016 commit df35f05
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 0 deletions.
65 changes: 65 additions & 0 deletions docs/api/server/document/document-global.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Global

Global documents work a bit different than the other documents.

They are the only `async` document, and for good reason.

## Create a Document

When creating a global document, it will always come with an identifier.

```ts
import { useGlobal } from '@Server/document/global.js';

// Automatically creates the document, and stores it into the database
async function doSomething() {
const document = await useGlobal('my-identifier-goes-here-for-my-document');
}
```

## Getting Data

```ts
type MyDataType = {
_id: string;
identifier: string;
totalMoneyCreated: number;
totalMoneyDestroyed: number;
};

async function doSomething() {
const document = await useGlobal('my-identifier-goes-here-for-my-document');

// Get the entire document
const data = document.get<MyDataType>();
console.log(data.totalMoneyCreated);

// Get a single field
const totalMoneyCreated = document.getField('totalMoneyCreated');
}
```

## Storing Data

Storing data can take any data type and store it for you.

Below is just a simple numerical example.

```ts
type MyDataType = {
_id: string;
identifier: string;
totalMoneyCreated: number;
totalMoneyDestroyed: number;
};

async function doSomething() {
const document = await useGlobal('my-identifier-goes-here-for-my-document');

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

// Overwrite multiple fields
document.setField<MyDataType>({ totalMoneyCreated: 200, totalMoneyDestroyed: 200 });
}
```
43 changes: 43 additions & 0 deletions src/main/server/document/global.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { useDatabase } from '@Server/database/index.js';
import { CollectionNames } from './shared.js';

const db = useDatabase();
const data: { [key: string]: { [key: string]: any } } = {};

export async function useGlobal(identifier: string) {
if (!data[identifier]) {
let data = await db.get<{ _id: string; identifier: string }>({ identifier }, CollectionNames.Global);

if (!data) {
const _id = await db.create({ identifier }, CollectionNames.Global);
data = await db.get<{ _id: string; identifier: string }>({ _id }, CollectionNames.Global);
}

data[identifier] = data;
}

function get<T = Object>(): T {
return data[identifier] as T;
}

function getField<T = any>(fieldName: string): T {
return data[identifier][fieldName];
}

async function set(fieldName: string, value: any): Promise<boolean> {
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);
}

return {
get,
getField,
set,
setBulk,
};
}
1 change: 1 addition & 0 deletions src/main/server/document/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './account.js';
export * from './character.js';
export * from './global.js';
export * from './vehicle.js';
1 change: 1 addition & 0 deletions src/main/server/document/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export const CollectionNames = {
Accounts: 'Accounts',
Characters: 'Characters',
Vehicles: 'Vehicles',
Global: 'Global',
};

0 comments on commit df35f05

Please sign in to comment.