# Persistence

Cache lifecycle and adapter contracts.

[Read the persistent cache guide](/docs/persistence/). Signatures below come from the published SDK declarations. Access SDK services through `db`; use their constructors only when integrating at a lower level.

## QueuedEvent


```ts
/** Only signed wire events are persisted. Decrypted records are never written here. */
interface QueuedEvent {
  event: NostrEvent;
  queuedAt: number;
  attempts: number;
  relays: RelayResult[];
}
```

## PersistenceAdapter


```ts
interface PersistenceAdapter {
  loadEvents(namespace?: string): Promise<NostrEvent[]>;
  putEvents(events: readonly NostrEvent[], namespace?: string): Promise<void>;
  loadQueue(namespace?: string): Promise<QueuedEvent[]>;
  putQueue(entry: QueuedEvent, namespace?: string): Promise<void>;
  removeQueue(eventId: string, namespace?: string): Promise<void>;
  close(): void | Promise<void>;
}
```

## PersistenceOptions


```ts
interface PersistenceOptions {
  adapter: PersistenceAdapter;
  flushInterval?: number;
  batchSize?: number;
  onError?: (error: NostrbaseError) => void;
}
```

## MemoryPersistenceAdapter


```ts
/** Useful for tests, temporary caches, and explicit in-memory offline queues. */
declare class MemoryPersistenceAdapter implements PersistenceAdapter {}
```

### loadEvents


```ts
loadEvents(namespace?: string): Promise<NostrEvent[]>;
```

### putEvents


```ts
putEvents(events: readonly NostrEvent[], namespace?: string): Promise<void>;
```

### loadQueue


```ts
loadQueue(namespace?: string): Promise<QueuedEvent[]>;
```

### putQueue


```ts
putQueue(entry: QueuedEvent, namespace?: string): Promise<void>;
```

### removeQueue


```ts
removeQueue(eventId: string, namespace?: string): Promise<void>;
```

### close


```ts
close(): void;
```

## IndexedDBPersistenceAdapter


```ts
/** Use a distinct name per namespace/profile. The supplied factory also permits worker/test use. */
declare class IndexedDBPersistenceAdapter implements PersistenceAdapter {}
```

### constructor


```ts
constructor(name?: string, factory?: IDBFactory | undefined);
```

### loadEvents


```ts
loadEvents(namespace?: string): Promise<NostrEvent[]>;
```

### putEvents


```ts
putEvents(events: readonly NostrEvent[], namespace?: string): Promise<void>;
```

### loadQueue


```ts
loadQueue(namespace?: string): Promise<QueuedEvent[]>;
```

### putQueue


```ts
putQueue(entry: QueuedEvent, namespace?: string): Promise<void>;
```

### removeQueue


```ts
removeQueue(eventId: string, namespace?: string): Promise<void>;
```

### close


```ts
close(): Promise<void>;
```

## NostrbasePersistence


```ts
/** Persist all verified ingested events, including deletion tombstones and ciphertext. */
declare class NostrbasePersistence {}
```

### Properties


```ts
readonly adapter: PersistenceAdapter;
```

### constructor


```ts
constructor(host: PersistenceHost, options: PersistenceOptions);
```

### ready


```ts
ready(): Promise<void>;
```

### record


```ts
record(event: NostrEvent): void;
```

### flush


```ts
flush(): Promise<void>;
```

### close


```ts
close(): Promise<void>;
```


## Supporting declarations

These types appear in public signatures but are not package exports.

### PersistenceHost


```ts
interface PersistenceHost {
  namespace: string;
  ingest(event: NostrEvent): boolean;
}
```
