-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: global documents, global document docs
- Loading branch information
Showing
4 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters