# Internal Job Banking

> Use sky_base as a lightweight fallback or replacement for society/job account balances.

## Overview

sky_base includes an internal job banking backend for shared society accounts. It stores one row per job in the `sky_job_accounts` table and keeps the current amount in the `balance` column.

This is useful when a banking script does not support a job account the way a Sky script expects it, or when you want a simple, stable backup for job balances.

## Configuration

Use the internal backend as the main society banking system:

```lua [sky_base/config/config.lua]
Config.banking = "sky"
```

Or keep your existing banking resource and only use the internal table when the selected adapter fails:

```lua [sky_base/config/config.lua]
Config.banking = "auto"
Config.useSkyBankingFallback = true
```

The fallback only takes over for failed shared-account calls. If your external banking resource returns a valid balance or accepts the transaction, sky_base keeps using that resource.

## Database Table

sky_base creates the table automatically on start when `Config.banking = "sky"` or `Config.useSkyBankingFallback = true`.

```sql
CREATE TABLE IF NOT EXISTS `sky_job_accounts` (
    `job` VARCHAR(64) NOT NULL,
    `balance` BIGINT NOT NULL DEFAULT 0,
    `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`job`)
);
```

Job names are stored without the `society_` prefix. For example, both `police` and `society_police` use the `police` row.

## Server Exports

```lua
local balance = exports["sky_base"]:getJobBalance("police")
```

```lua
exports["sky_base"]:setJobBalance("police", 25000)
```

```lua
exports["sky_base"]:addJobBalance("police", 5000)
```

```lua
local removed = exports["sky_base"]:removeJobBalance("police", 1500)
if not removed then
    print("Not enough money in the job account")
end
```

`removeJobBalance` never allows the balance to go below zero. It returns `false` when the account has insufficient funds.
