Skip to content

Commit e992952

Browse files
Copilothotlong
andcommitted
feat: default persistence to 'auto' with 'false' opt-out
Persistence now defaults to 'auto' (auto-detect environment) instead of undefined (no persistence). Users can opt out with `persistence: false`. Default adapter values: - Node.js file path: .objectstack/data/memory-driver.json - Browser localStorage key: objectstack:memory-db Changes: - schema: persistence defaults to 'auto', added false to disable - driver: initPersistence() defaults to 'auto' when undefined - tests: updated to reflect new default, use persistence: false for pure memory Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent 08646aa commit e992952

5 files changed

Lines changed: 31 additions & 18 deletions

File tree

packages/plugins/driver-memory/src/memory-driver.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ describe('InMemoryDriver', () => {
66
const testTable = 'test_table';
77

88
beforeEach(async () => {
9-
driver = new InMemoryDriver();
9+
driver = new InMemoryDriver({ persistence: false });
1010
await driver.connect();
1111
});
1212

packages/plugins/driver-memory/src/memory-driver.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,17 @@ export interface InMemoryDriverConfig {
3232
/** Optional: Logger instance */
3333
logger?: Logger;
3434
/**
35-
* Optional persistence configuration.
35+
* Persistence configuration. Defaults to `'auto'`.
36+
* - `'auto'` (default) — Auto-detect environment (browser → localStorage, Node.js → file)
3637
* - `'file'` — File-system persistence with defaults (Node.js only)
3738
* - `'local'` — localStorage persistence with defaults (Browser only)
38-
* - `'auto'` — Auto-detect environment (browser → localStorage, Node.js → file)
3939
* - `{ type: 'file', path?: string, autoSaveInterval?: number }` — File-system with options
4040
* - `{ type: 'local', key?: string }` — localStorage with options
4141
* - `{ type: 'auto', path?: string, key?: string, autoSaveInterval?: number }` — Auto-detect with options
4242
* - `{ adapter: PersistenceAdapterInterface }` — Custom adapter
43+
* - `false` — Disable persistence (pure in-memory)
4344
*/
44-
persistence?: string | {
45+
persistence?: string | false | {
4546
type?: 'file' | 'local' | 'auto';
4647
path?: string;
4748
key?: string;
@@ -933,10 +934,12 @@ export class InMemoryDriver implements DriverInterface {
933934

934935
/**
935936
* Initialize the persistence adapter based on configuration.
937+
* Defaults to 'auto' when persistence is not specified.
938+
* Use `persistence: false` to explicitly disable persistence.
936939
*/
937940
private async initPersistence(): Promise<void> {
938-
const persistence = this.config.persistence;
939-
if (!persistence) return;
941+
const persistence = this.config.persistence === undefined ? 'auto' : this.config.persistence;
942+
if (persistence === false) return;
940943

941944
if (typeof persistence === 'string') {
942945
if (persistence === 'auto') {

packages/plugins/driver-memory/src/persistence/persistence.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ describe('InMemoryDriver Persistence', () => {
139139
});
140140

141141
describe('Pure Memory (No Persistence)', () => {
142-
it('should work without persistence (default behavior)', async () => {
143-
const driver = new InMemoryDriver();
142+
it('should work without persistence when explicitly disabled', async () => {
143+
const driver = new InMemoryDriver({ persistence: false });
144144
await driver.connect();
145145

146146
await driver.create('items', { id: '1', name: 'Widget' });

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,24 @@ import {
1212
} from './memory.zod';
1313

1414
describe('MemoryConfigSchema', () => {
15-
it('should accept empty config (all optional)', () => {
15+
it('should default persistence to "auto" when empty config', () => {
1616
const config = MemoryConfigSchema.parse({});
1717

1818
expect(config.strictMode).toBe(false);
1919
expect(config.initialData).toBeUndefined();
20-
expect(config.persistence).toBeUndefined();
20+
expect(config.persistence).toBe('auto');
2121
expect(config.indexes).toBeUndefined();
2222
expect(config.maxRecordsPerObject).toBeUndefined();
2323
});
2424

25+
it('should accept persistence: false to disable persistence', () => {
26+
const config = MemoryConfigSchema.parse({
27+
persistence: false,
28+
});
29+
30+
expect(config.persistence).toBe(false);
31+
});
32+
2533
it('should accept config with initial data', () => {
2634
const config = MemoryConfigSchema.parse({
2735
initialData: {

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -178,28 +178,30 @@ export const MemoryConfigSchema = z.object({
178178
strictMode: z.boolean().default(false).describe('Throw on missing records instead of returning null'),
179179

180180
/**
181-
* Optional persistence configuration.
182-
* When configured, the memory store automatically saves and restores data.
181+
* Persistence configuration.
182+
* Defaults to `'auto'` — the memory store automatically detects the environment
183+
* and saves/restores data using the best available strategy.
183184
*
184-
* - `'auto'`: Auto-detect environment (browser → localStorage, Node.js → file)
185+
* - `'auto'` (default): Auto-detect environment (browser → localStorage, Node.js → file)
185186
* - `'file'`: Persist to disk file (Node.js only, default path: `.objectstack/data/memory-driver.json`)
186187
* - `'local'`: Persist to localStorage (Browser only, default key: `objectstack:memory-db`)
187188
* - `{ type: 'auto', path?: string, key?: string }`: Auto-detect with overrides
188189
* - `{ type: 'file', path?: string }`: File-system with custom path
189190
* - `{ type: 'local', key?: string }`: localStorage with custom key
190191
* - `{ adapter: PersistenceAdapter }`: Custom persistence adapter
192+
* - `false`: Disable persistence (pure in-memory, data lost on disconnect)
191193
*
192194
* @example
193-
* // Auto-detect environment
194-
* new InMemoryDriver({ persistence: 'auto' })
195+
* // Auto-detect environment (default)
196+
* new InMemoryDriver()
195197
* // Node.js
196198
* new InMemoryDriver({ persistence: 'file' })
197199
* // Browser
198200
* new InMemoryDriver({ persistence: 'local' })
199-
* // Pure memory (default)
200-
* new InMemoryDriver()
201+
* // Pure memory (no persistence)
202+
* new InMemoryDriver({ persistence: false })
201203
*/
202-
persistence: MemoryPersistenceConfigSchema.optional().describe('Persistence configuration'),
204+
persistence: MemoryPersistenceConfigSchema.or(z.literal(false)).default('auto').describe('Persistence configuration (defaults to auto-detect)'),
203205

204206
/**
205207
* Fields to index for faster lookups.

0 commit comments

Comments
 (0)