# Where API Maker keeps its data

> API Maker keeps its own data (accounts, schemas, code, settings, logs) in a store. The embedded store (SQLite) runs inside API Maker, with no database to install. MongoDB is for several API Maker servers. Choose, move and check the store.

Source: https://docs.apimaker.dev/v4/docs/features/where-api-maker-keeps-its-data.html

API Maker keeps its own data in a **store** : your accounts, schemas, code, settings, secrets and logs. Your own databases (the instances) are not in it.

There are two stores. You choose one when you install API Maker.

> **In short**
>
> - **SQLite (embedded)** : a file inside API Maker. No database to install. Less memory. One API Maker server.
> - **MongoDB** : a database of its own. For several API Maker servers, or automatic failover.
> - Both work the same way : every API, every page, every transaction.
> - You can move the data from one store to the other later. [API Maker Launchpad](https://docs.apimaker.dev/v4/docs/getting-started/install-on-server.html) does it for you.

| | SQLite (embedded) | MongoDB |
|---|---|---|
| Where it runs | Inside the API Maker process | A server of its own, a container, or Atlas |
| Memory beside API Maker | None | About 266 MB when almost empty |
| API Maker servers | One | One or more |
| Automatic failover | No | Yes, with a replica set of three members |
| Best for | One server, a small machine, a laptop | Several servers, a load balancer, a database server apart |

## Do I need MongoDB ?

- **No**, for one API Maker server. SQLite does the same work, with less memory and nothing to install.
- **Yes**, if you run more than one API Maker server behind a load balancer.
- **Yes**, if API Maker's data must be on a server of its own, or on Atlas.
- **Yes**, if you need automatic failover of the data.
- **Not now ?** Start with SQLite. One operation moves the data to MongoDB when you need it.

## How the store is set

The `.env` file of each server names the store :

```env
# SQLite : a file. A relative path is relative to the folder of API Maker.
am__dataStore=sqlite:data/api_maker.db

# Or MongoDB : a replica set, the database in the path.
am__dataStore=mongodb://user:password@db-host:27017/api_maker?replicaSet=rs0
```

- An installation made before `dataStore` keeps working : `mongo_db_connection` is its store.
- API Maker never makes a store by surprise. A new file is made only when `dataStore` names it.
- Other kinds (`postgres://`, `mysql://`) are refused at the start, with a clear message.
- The value can be encrypted in `.env`, like a connection string.

### The logs

The logs and the analytics go to the store of the logs :

| Set | The logs go to |
|---|---|
| The **Log DB Connection String** of Deployment Settings | that store (`sqlite:` or `mongodb://`) |
| `am__logs__dataStore` | that store |
| Nothing, with SQLite | a file of their own next to the data : `data/api_maker_logs.db` |
| Nothing, with MongoDB | nowhere : calls are not logged, as before |

The logs have their own file, so a busy log never slows a save.

## What is the same on both stores

- **The answers.** The embedded store answers filters, updates, sorts and pipelines as MongoDB 8.0 does. It is tested against a real MongoDB, case by case.
- **The free shape of the data.** Every document is kept whole. There is no table per field and no migration of your data.
- **Transactions.** A git pull, a new account and the other saves of many documents are all or nothing, on both stores.
- **Unique indexes, TTL indexes, partial indexes.** Kept and checked the same way.

### What only MongoDB runs

Some pipeline stages need MongoDB : `$graphLookup`, `$geoNear`, `$search`, `$vectorSearch`, `$setWindowFields`, `$densify`, `$fill`, `$sample`, `$bucket`, `$bucketAuto`, `$redact`, `$out` and `$merge`, and text, geo and wildcard indexes, and indexes with a collation. So do updates of every element of a list, or of the elements a filter picks (`$[]`, `$[name]` with `arrayFilters`). API Maker's own pages never use them. When one is asked of the embedded store, the error names it.

## The commands of the store

The server program has the tools of the store. Launchpad runs them for you over SSH. You can run them too.

| Command | What it does |
|---|---|
| `api_maker_be store info` | The kind, the format, the collections, their counts and sizes. |
| `api_maker_be store copy --from=URL --to=URL` | Copies every collection and index, then checks the copy. A target that holds data is refused : `--replace` makes it hold the source and nothing else. |
| `api_maker_be store verify --from=URL --to=URL` | Compares two stores : the counts, and a SHA-256 of the documents. |
| `api_maker_be store backup --to=FILE` | Copies an embedded file while API Maker runs, then gives its SHA-256. |
| `api_maker_be store check` | Checks an embedded file and every index entry. |
| `api_maker_be store compact` | Gives back the disk space of removed data (embedded). |
| `api_maker_be store dump --to=FOLDER` | Writes the layout of `mongodump`. `mongorestore` reads it. |
| `api_maker_be store restore --from=FOLDER` | Reads that layout, and what `mongodump` wrote. |
| `api_maker_be store migrate --dry-run` | Says whether an embedded file needs a newer format. |

`--store=URL` works on another store than the one of `.env`. `--logs` works on the store of the logs. Add `--json` to read the answer from a script.

## Move the data

[API Maker Launchpad](https://docs.apimaker.dev/v4/docs/getting-started/install-on-server.html) moves the data between the stores in one operation, with a progress bar and a way back :

1. It checks the target and the free disk.
2. It stops API Maker.
3. It copies the data and checks the copy.
4. It marks the old store as moved. A server never writes there again.
5. It writes the new store in `.env` and starts API Maker.
6. The logs follow in the background.

The old store stays until you remove it. Nothing is lost if a step fails : API Maker starts again on the old store.

- **On servers** : choose the store on the card **API Maker's data** of the architecture, then deploy. The review shows the move, and the deployment does it.
- **On this computer** (Launchpad's Local mode) : **Project Settings → API Maker's data** moves it while the project is stopped.

> **Tip : SQLite on another server**
>
> SQLite runs only inside API Maker. To keep API Maker's data on another server, Launchpad installs MongoDB there and moves the data.

## Upgrades

The embedded file has a format. A newer API Maker upgrades it at its start, after a copy of the file next to it. An older API Maker never opens a newer format : it says which version is needed. Launchpad keeps a copy before each upgrade, and restores it if you go back to the older release.

## Where to see it

**Dashboard → Servers** shows the store of each server : SQLite with its file and its size, or MongoDB with its hosts. A password is never shown.
