Skip to content

Commit aaf746e

Browse files
authored
docs(drivers,spec): say which memory-driver path persists, now that only one of them does (#4083) (#4108)
#4111 made a declared `{ driver: 'memory' }` datasource ephemeral and scoped an explicitly-requested destination per datasource. It left the documentation describing the old world, in the one place a reader looks first: - `drivers.mdx` claimed "Data is lost when the process exits" directly above a `new InMemoryDriver()` snippet — still false for that path, which keeps the `'auto'` default and writes `.objectstack/data/memory-driver.json` relative to the working directory. The section now contrasts the two build paths, notes that two directly-constructed `'auto'` drivers still share that one path, and shows `persistence: false` in the test-usage tip — the exact pitfall that let this repo's own ADR-0062 acceptance test pass on a virgin checkout and fail on every later local run. - `MemoryConfigSchema.persistence` documented `'auto'` as *the* default, with no hint that the datasource factory now overrides it. Its TSDoc and `.describe()` state the split, so the contract says what the runtime does rather than leaving two de-facto answers. Documentation only. This branch's earlier code changes for #4083 were dropped in favour of #4111, which landed the same fix plus per-pool store scoping.
1 parent 4be9d99 commit aaf746e

2 files changed

Lines changed: 29 additions & 4 deletions

File tree

content/docs/data-modeling/drivers.mdx

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -353,20 +353,34 @@ equivalent age-based reap.
353353
## Memory Driver
354354

355355
The in-memory driver keeps records in plain in-process objects (queried via
356-
[`mingo`](https://github.com/kofrasa/mingo)). Data is lost when the process exits.
356+
[`mingo`](https://github.com/kofrasa/mingo)).
357357
It is the **last-resort fallback** in dev mode: `objectstack dev` prefers native
358358
SQLite (`better-sqlite3`), falls back to the pure-JS WASM SQLite driver if the
359359
native binary is unavailable, and only drops to the in-memory driver if WASM also
360360
fails to load. Set `OS_DATABASE_DRIVER=memory` to select it explicitly.
361361

362+
**Whether data survives the process depends on how the driver is built** (#4083):
363+
364+
| How it is built | Persistence |
365+
| :--- | :--- |
366+
| A **declared datasource** — `{ driver: 'memory' }` in a stack/app config | **Ephemeral.** Nothing is written to disk unless the declaration sets `config.persistence`and when it does, the destination is scoped per datasource, so two memory datasources never share one file. |
367+
| `new InMemoryDriver()` **constructed directly** | `persistence: 'auto'`under Node that means a JSON file at `.objectstack/data/memory-driver.json`, relative to the process's working directory, reloaded on the next boot. |
368+
369+
Pass `persistence: false` for a driver you construct yourself and want purely in
370+
memorya test, for instance, which should neither depend on nor leave behind
371+
state in the working directory. Two directly-constructed `'auto'` drivers in one
372+
process still share that single default path.
373+
362374
```typescript
363375
import { InMemoryDriver } from '@objectstack/driver-memory';
364376
365-
new InMemoryDriver();
377+
new InMemoryDriver({ persistence: false }); // pure memory
378+
new InMemoryDriver(); // 'auto' — file-backed under Node
366379
```
367380

368381
<Callout type="tip">
369-
Use the memory driver for unit tests. It requires no setup and runs instantly.
382+
Use the memory driver for unit tests. It requires no setup and runs instantly
383+
with `persistence: false`, so one run cannot see what an earlier run left behind.
370384
</Callout>
371385

372386
## Local Environment Runtime

packages/spec/src/data/driver/memory.zod.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,19 @@ export const MemoryConfigSchema = lazySchema(() => z.object({
216216
* new InMemoryDriver({ persistence: false })
217217
* // Custom adapter for serverless
218218
* new InMemoryDriver({ persistence: { adapter: upstashAdapter } })
219+
*
220+
* **A datasource declared with `driver: 'memory'` is ephemeral unless this
221+
* key is set (#4083).** `'auto'` is the default only for a driver you
222+
* construct yourself. The shared datasource factory
223+
* (`createDefaultDatasourceDriverFactory`) passes `false` when the
224+
* declaration says nothing, so a declared memory datasource cannot silently
225+
* become a file-backed store in the process's working directory — matching
226+
* `objectstack dev`, which already reads an explicit memory driver as the
227+
* user opting out of persistence. Set this key to opt back in; when you do
228+
* without naming a `path`/`key`, the factory scopes the destination per
229+
* datasource, so two memory datasources never share one file.
219230
*/
220-
persistence: MemoryPersistenceConfigSchema.or(z.literal(false)).default('auto').describe('Persistence configuration (defaults to auto-detect)'),
231+
persistence: MemoryPersistenceConfigSchema.or(z.literal(false)).default('auto').describe('Persistence configuration (auto-detect by default; a declared memory datasource is ephemeral unless set)'),
221232

222233
/**
223234
* Fields to index for faster lookups.

0 commit comments

Comments
 (0)