# Database

MOH Business System uses four MySQL tables, all created by running the included `sql.sql` file. The resource uses **oxmysql** for all database operations.

## Tables Overview

| Table                  | Purpose                                                                       |
| ---------------------- | ----------------------------------------------------------------------------- |
| `ammo_stash_passwords` | Stores each player's stash password, per store, per supplier NPC index        |
| `ammo_autosell_status` | Tracks whether a player's auto-sell is currently active for each store        |
| `ammo_stash_items`     | Holds the shared item pool for each store (all owners write to the same pool) |
| `ammo_item_prices`     | Custom sell prices set by each owner, per store, per item                     |

## Table: `ammo_stash_passwords`

One row per player per store. The password is required to access or manage the stash.

| Column      | Type                | Description                                                |
| ----------- | ------------------- | ---------------------------------------------------------- |
| `id`        | INT AUTO\_INCREMENT | Primary key                                                |
| `citizenid` | VARCHAR(50)         | Player's QBCore citizen ID                                 |
| `store_id`  | VARCHAR(50)         | Store identifier from config (e.g. `ammunation`)           |
| `npc_index` | INT                 | Supplier NPC index (always 1 for non-mission stash access) |
| `password`  | VARCHAR(100)        | The player-set password (plain text)                       |

## Table: `ammo_autosell_status`

Tracks which players have the auto-sell NPC active at each store. Only one player can have auto-sell active per store at a time.

| Column      | Type                | Description                               |
| ----------- | ------------------- | ----------------------------------------- |
| `id`        | INT AUTO\_INCREMENT | Primary key                               |
| `citizenid` | VARCHAR(50)         | Player's QBCore citizen ID                |
| `store_id`  | VARCHAR(50)         | Store identifier                          |
| `active`    | TINYINT(1)          | `1` = auto-sell is active, `0` = inactive |

## Table: `ammo_stash_items`

The shared item pool for each store. All items ordered by any player using the same store go into this shared pool. When a buyer purchases through the auto-sell NPC, amounts are reduced here.

| Column      | Type                | Description                                     |
| ----------- | ------------------- | ----------------------------------------------- |
| `id`        | INT AUTO\_INCREMENT | Primary key                                     |
| `store_id`  | VARCHAR(50)         | Store identifier                                |
| `item_name` | VARCHAR(100)        | Internal item name (e.g. `weapon_combatpistol`) |
| `amount`    | INT                 | Current stock quantity                          |

## Table: `ammo_item_prices`

Custom prices set by store owners. When an auto-sell buyer checks prices, this table is consulted first. If no custom price exists for an item, the default markup from `Config.AutoSellMarkup.default` is applied to the base price.

| Column      | Type                | Description                  |
| ----------- | ------------------- | ---------------------------- |
| `id`        | INT AUTO\_INCREMENT | Primary key                  |
| `citizenid` | VARCHAR(50)         | Owner's QBCore citizen ID    |
| `store_id`  | VARCHAR(50)         | Store identifier             |
| `item_name` | VARCHAR(100)        | Internal item name           |
| `price`     | INT                 | Custom sell price in dollars |

## Migration from v1.0

If you are upgrading from v1.0 of the resource (which did not have a `store_id` column), run the following `ALTER TABLE` statements. They are included and commented out at the bottom of `sql.sql`:

```sql
ALTER TABLE `ammo_stash_passwords` ADD COLUMN `store_id` VARCHAR(50) NOT NULL DEFAULT 'ammunation' AFTER `citizenid`;
ALTER TABLE `ammo_autosell_status` ADD COLUMN `store_id` VARCHAR(50) NOT NULL DEFAULT 'ammunation' AFTER `citizenid`;
ALTER TABLE `ammo_stash_items` ADD COLUMN `store_id` VARCHAR(50) NOT NULL DEFAULT 'ammunation' AFTER `id`;
ALTER TABLE `ammo_item_prices` ADD COLUMN `store_id` VARCHAR(50) NOT NULL DEFAULT 'ammunation' AFTER `citizenid`;
```

These are safe to skip on a fresh installation.

## Data Loaded on Resource Start

When the resource starts, the server script reads all four tables into memory. Console messages confirm successful loading:

```
[MOH Business System] Loaded N stash passwords
[MOH Business System] Loaded N auto-sell statuses
[MOH Business System] Loaded N custom item prices
```

Stash items are fetched live from the database each time they are needed, rather than cached in memory, to keep stock levels accurate.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://mohssins.gitbook.io/mohscriptsdocs/moh-businesssystem/database.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
