Skip to content

Commit

Permalink
docs: database
Browse files Browse the repository at this point in the history
  • Loading branch information
Stuyk committed Apr 19, 2024
1 parent 9eae831 commit 719bbe6
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
3 changes: 3 additions & 0 deletions docs/api/index.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
expanded: true
label: 'API'
order: -1001
89 changes: 89 additions & 0 deletions docs/api/server/database.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Database

When using the database in Rebar it can be accessed from any plugin.

However, the database may only be accessed on **server-side**.

!!!
Use the Document system if you are updating characters, accounts, or vehicles.
!!!

## Usage

Import the database into your plugin file, and begin using it.

```ts
import { useDatabase } from '@Server/database/index.js';

const { get, create, getAll, getMany, update, deleteDocument } = useDatabase();
```

### Create Data

Creating data uses the `create` function and returns an `_id` to obtain the data.

```ts
async function test() {
const _id = await create({ name: 'Stuyk', age: 30 }, 'person');
}
```

### Get Data

If you want to get a document by an `_id`, you can use this:

```ts
async function test() {
const _id = await create({ name: 'Stuyk', age: 30 }, 'person');
const user = await get<{ name: string }>(_id);
if (!user) {
console.warn('Could not find the data!');
return;
}

console.warn(`Found ${user.name}`);
}
```

### Find Data

After creating data, if you don't have an `_id` and you want to find it. You can use `getMany`.

```ts
async function test() {
const results = await getMany<{ name: string }>({ name: 'Stuyk' }, 'person');
if (results.length <= 0) {
console.warn('Could not find the data!');
return;
}

const user = results[0];
console.warn(`Found ${user.name}`);
}
```

### Get All Data

It is not recommended to fetch a whole collection unless it's a smaller collection.

In any case, you can use this to fetch a whole collection of data.

```ts
async function test() {
const results = await getAll<{ name: string }>('person');
}
```

### Update

If you want to update a document, or simply append new data to the document.

```ts
async function test() {
const _id = await create({ name: 'Stuyk', age: 30 }, 'person');
const didUpdate = await update({ _id, name: 'NotStuyk' }, 'person');
if (!didUpdate) {
return;
}
}
```
3 changes: 3 additions & 0 deletions docs/api/server/index.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
expanded: true
label: 'Server'
order: -1000

0 comments on commit 719bbe6

Please sign in to comment.