Skip to main content

Class: InMemoryThreadRepository

Defined in: adapters/in-memory/InMemoryThreadRepository.ts:37

InMemoryThreadRepository -- the thread repository in Maps, for tests and the sandbox

About​

Behaves like a database without one: copies in and out, turns in insertion order.

Implements IThreadRepository so the ThreadStore runs with no infrastructure. The threads and their turns are two Maps keyed by tenant + id; the thread list is newest first by createdAt, then by id (D37). acquireLease is atomic by construction: one synchronous read and write with no await between them.

Who needs it​

The gateway tests (gatewayConfigBase), the dev sandbox and the cookbook's first run; a host app uses its own adapter.

Pitfalls​

Production: nothing survives the process, nothing is shared between processes.

Implements​

Constructors​

Constructor​

new InMemoryThreadRepository(): InMemoryThreadRepository

Returns​

InMemoryThreadRepository

Methods​

acquireLease()​

acquireLease(__namedParameters): Promise<boolean>

Defined in: adapters/in-memory/InMemoryThreadRepository.ts:332

acquireLease -- takes the thread's lease atomically

About​

Check-and-set (D77): one caller at a time on a thread.

Sets the lease to {token, expiresAt} and returns true only when the thread has no lease or its lease has expired at now; otherwise changes nothing and returns false. One database operation (Mongo: findOneAndUpdate with the condition in the filter), never a load followed by a save.

Who needs it​

ThreadStore.acquireLease, before every call on a thread.

Parameters​

__namedParameters​
expiresAt​

Date

now​

Date

tenantId​

string

threadId​

string

token​

string

Returns​

Promise<boolean>

Implementation of​

IThreadRepository.acquireLease


anonymizeByUser()​

anonymizeByUser(__namedParameters): Promise<IThreadCounts>

Defined in: adapters/in-memory/InMemoryThreadRepository.ts:287

anonymizeByUser -- keeps a user's threads as skeletons

About​

GDPR pseudonymisation: the words go, the record stays.

userId becomes the pseudonym, the title and the working memory are emptied, the status becomes CLOSED, the turns are deleted; id, turnCount and dates stay. Returns how many of each.

Who needs it​

ThreadStore.anonymizeByUser, for the gateway's anonymizeUserData.

Parameters​

__namedParameters​
pseudonym​

string

tenantId​

string

userId​

string

Returns​

Promise<IThreadCounts>

Implementation of​

IThreadRepository.anonymizeByUser


appendTurn()​

appendTurn(__namedParameters): Promise<void>

Defined in: adapters/in-memory/InMemoryThreadRepository.ts:161

appendTurn -- stores one turn at the end of the thread

About​

The order of appending is the order loadTurns returns.

The turn whole: role, parts, dates. The thread's counts are the store's, saved with saveThread.

Who needs it​

ThreadStore.appendTurn.

Parameters​

__namedParameters​
tenantId​

string

threadId​

string

turn​

ITurn

Returns​

Promise<void>

Implementation of​

IThreadRepository.appendTurn


delete()​

delete(__namedParameters): Promise<IThreadCounts>

Defined in: adapters/in-memory/InMemoryThreadRepository.ts:251

delete -- hard-deletes threads with their turns and working memory

About​

The listed ids, or every thread of the tenant for "all".

Narrowed to one user when userId is given. An unknown id or one of another tenant or user is ignored. Returns the counts of the threads and of the turns deleted.

Who needs it​

ThreadStore.delete, for the gateway's delete and deleteUserData.

Parameters​

__namedParameters​
tenantId​

string

threadIds​

TThreadIds

userId?​

string

Returns​

Promise<IThreadCounts>

Implementation of​

IThreadRepository.delete


listThreadIds()​

listThreadIds(__namedParameters): Promise<string[]>

Defined in: adapters/in-memory/InMemoryThreadRepository.ts:223

listThreadIds -- the ids of the tenant's threads

About​

Of one user when userId is given; deleted threads excluded.

The ids only, in any order, for a bulk operation over every thread.

Who needs it​

ThreadStore.listThreadIds, for the gateway's delete with "all".

Parameters​

__namedParameters​
tenantId​

string

userId?​

string

Returns​

Promise<string[]>

Implementation of​

IThreadRepository.listThreadIds


listThreads()​

listThreads(__namedParameters): Promise<IPage<IThread>>

Defined in: adapters/in-memory/InMemoryThreadRepository.ts:101

listThreads -- one page of a user's threads

About​

Newest first by createdAt, the id as the tie-break (D37); the cursors are thread ids.

A thread whose deletedAt is set is not listed. endingBefore reads the limit threads older than it, startingAfter the limit threads right after it (the page next to the cursor, not the newest). A cursor that is not a listed thread of this user is a 404. The store has validated limit and that only one cursor is set.

Who needs it​

ThreadStore.listThreads.

Parameters​

__namedParameters​
page​

IPageArgs

tenantId​

string

userId​

string

Returns​

Promise<IPage<IThread>>

Implementation of​

IThreadRepository.listThreads


loadThread()​

loadThread(__namedParameters): Promise<IThread | null>

Defined in: adapters/in-memory/InMemoryThreadRepository.ts:73

loadThread -- the thread of a tenant by id

About​

As stored, deletedAt included; null when the tenant has no thread with this id.

The thread record whole, without its turns.

Who needs it​

ThreadStore.getThread and findThread.

Parameters​

__namedParameters​
tenantId​

string

threadId​

string

Returns​

Promise<IThread | null>

Implementation of​

IThreadRepository.loadThread


loadTurns()​

loadTurns(__namedParameters): Promise<ITurn[]>

Defined in: adapters/in-memory/InMemoryThreadRepository.ts:189

loadTurns -- a slice of the thread's turns

About​

In the order they were appended, reversed when desc; skip and limit cut it.

The turns whole, as stored.

Who needs it​

ThreadStore.getTurns and getLastTurns.

Parameters​

__namedParameters​
desc​

boolean

Newest first when true.

limit​

number

skip​

number

tenantId​

string

threadId​

string

Returns​

Promise<ITurn[]>

Implementation of​

IThreadRepository.loadTurns


releaseLease()​

releaseLease(__namedParameters): Promise<void>

Defined in: adapters/in-memory/InMemoryThreadRepository.ts:374

releaseLease -- gives the thread's lease back

About​

Only the holder's token clears it.

Clears the lease when it carries this token; a lease another call took meanwhile stays.

Who needs it​

ThreadStore.releaseLease, when the call ends.

Parameters​

__namedParameters​
tenantId​

string

threadId​

string

token​

string

Returns​

Promise<void>

Implementation of​

IThreadRepository.releaseLease


saveThread()​

saveThread(__namedParameters): Promise<void>

Defined in: adapters/in-memory/InMemoryThreadRepository.ts:54

saveThread -- inserts or replaces the thread

About​

By thread.id within thread.tenantId; the turns are not part of it.

The thread record whole: title, status, working memory, lease, dates.

Who needs it​

ThreadStore.createThread, updateThread, appendTurn (the counts), setWorkingMemory.

Parameters​

__namedParameters​
thread​

IThread

Returns​

Promise<void>

Implementation of​

IThreadRepository.saveThread