Class

RamAdapter

RamAdapter()

Constructor

# new RamAdapter()

The RamAdapter provides an in-memory implementation of the persistence layer. It stores data in JavaScript Maps and provides CRUD operations and query capabilities. This adapter is useful for testing, prototyping, and applications that don't require persistent storage across application restarts.

In-memory adapter for data persistence

sequenceDiagram participant Client participant Repository participant RamAdapter participant Storage as In-Memory Storage Client->>Repository: create(model) Repository->>RamAdapter: create(tableName, id, model) RamAdapter->>RamAdapter: lock.acquire() RamAdapter->>Storage: set(id, model) RamAdapter->>RamAdapter: lock.release() RamAdapter-->>Repository: model Repository-->>Client: model Client->>Repository: findById(id) Repository->>RamAdapter: read(tableName, id) RamAdapter->>Storage: get(id) Storage-->>RamAdapter: model RamAdapter-->>Repository: model Repository-->>Client: model

View Source ram/RamAdapter.ts, line 12

Example
```typescript
// Create a new RAM adapter
const adapter = new RamAdapter('myRamAdapter');

// Create a repository for a model
const userRepo = new (adapter.repository<User>())(User, adapter);

// Perform CRUD operations
const user = new User({ name: 'John', email: 'john@example.com' });
await userRepo.create(user);
const retrievedUser = await userRepo.findById(user.id);
```

Methods

# async Sequence(options) → {Promise.<Sequence>}

Factory method that creates a new RamSequence instance for ID generation. This method provides a way to create auto-incrementing sequences for entity IDs.

Creates a new sequence for generating sequential IDs

Parameters:
Name Type Description
options SequenceOptions

Configuration options for the sequence

View Source ram/RamAdapter.ts, line 742

A promise that resolves to the new sequence instance

Promise.<Sequence>

# Statement() → {RamStatement.<M, any>}

Factory method that creates a new RamStatement instance for building queries. This method allows for fluent query construction against the RAM adapter.

Creates a new statement builder for queries

View Source ram/RamAdapter.ts, line 732

A new statement builder instance

RamStatement.<M, any>

# async create(tableName, id, model) → {Promise.<Record.<string, any>>}

Stores a new record in the specified table with the given ID. This method acquires a lock to ensure thread safety, creates the table if it doesn't exist, checks for conflicts, and stores the model.

Creates a new record in the in-memory storage

sequenceDiagram participant Caller participant RamAdapter participant Storage as In-Memory Storage Caller->>RamAdapter: create(tableName, id, model) RamAdapter->>RamAdapter: lock.acquire() RamAdapter->>Storage: has(tableName) alt Table doesn't exist RamAdapter->>Storage: set(tableName, new Map()) end RamAdapter->>Storage: has(id) alt Record exists RamAdapter-->>Caller: throw ConflictError end RamAdapter->>Storage: set(id, model) RamAdapter->>RamAdapter: lock.release() RamAdapter-->>Caller: model
Parameters:
Name Type Description
tableName string

The name of the table to store the record in

id string | number

The unique identifier for the record

model Record.<string, any>

The record data to store

View Source ram/RamAdapter.ts, line 561

A promise that resolves to the stored record

Promise.<Record.<string, any>>

# async delete(tableName, id) → {Promise.<Record.<string, any>>}

Removes a record with the specified ID from the given table. This method acquires a lock to ensure thread safety, checks if the table and record exist, retrieves the record before deletion, and then removes it from storage.

Deletes a record from the in-memory storage

sequenceDiagram participant Caller participant RamAdapter participant Storage as In-Memory Storage Caller->>RamAdapter: delete(tableName, id) RamAdapter->>RamAdapter: lock.acquire() RamAdapter->>Storage: has(tableName) alt Table doesn't exist RamAdapter-->>Caller: throw NotFoundError end RamAdapter->>Storage: has(id) alt Record doesn't exist RamAdapter-->>Caller: throw NotFoundError end RamAdapter->>Storage: get(id) Storage-->>RamAdapter: record RamAdapter->>Storage: delete(id) RamAdapter->>RamAdapter: lock.release() RamAdapter-->>Caller: record
Parameters:
Name Type Description
tableName string

The name of the table containing the record

id string | number

The unique identifier of the record to delete

View Source ram/RamAdapter.ts, line 655

A promise that resolves to the deleted record

Promise.<Record.<string, any>>

# flags(operation, model, flags) → {RamFlags}

Extends the base flags with a UUID for user identification. This method ensures that all operations have a unique identifier for tracking purposes.

Creates operation flags with UUID

Parameters:
Name Type Description
operation OperationKeys

The type of operation being performed

model Constructor.<M>

The model constructor

flags Partial.<RamFlags>

Partial flags to be extended

View Source ram/RamAdapter.ts, line 485

Complete flags with UUID

RamFlags

# async index(models) → {Promise.<any>}

A no-op indexing method for the RAM adapter. Since RAM adapter doesn't require explicit indexing, this method simply resolves immediately.

Indexes models in the RAM adapter

Parameters:
Name Type Description
models

Models to be indexed (unused)

View Source ram/RamAdapter.ts, line 505

A promise that resolves when indexing is complete

Promise.<any>

# async initialize(…args) → {Promise.<void>}

A no-op initialization method for the RAM adapter. Since RAM adapter doesn't require any setup, this method simply resolves immediately.

Initializes the RAM adapter

Parameters:
Name Type Attributes Description
args Array.<any> <repeatable>

Initialization arguments (unused)

View Source ram/RamAdapter.ts, line 495

A promise that resolves when initialization is complete

Promise.<void>

# parseError(err) → {V}

Ensures that errors are of the correct type for consistent error handling. If the error is already a BaseError, it's returned as is; otherwise, it's wrapped in an InternalError.

Parses and converts errors to appropriate types

Parameters:
Name Type Description
err Error

The error to parse

View Source ram/RamAdapter.ts, line 723

The parsed error of the expected type

V

# prepare(model, pk)

Converts a model instance to a format suitable for storage in the RAM adapter. This method extracts the primary key and creates a record without the primary key field.

Prepares a model for storage

Parameters:
Name Type Description
model M

The model instance to prepare

pk

The primary key property name

View Source ram/RamAdapter.ts, line 516

Object containing the record and ID

# async raw(rawInput) → {Promise.<R>}

Performs a query operation on the in-memory data store using the provided query specification. This method supports filtering, sorting, pagination, and field selection.

Executes a raw query against the in-memory storage

sequenceDiagram participant Caller participant RamAdapter participant Storage as In-Memory Storage Caller->>RamAdapter: raw(rawInput) RamAdapter->>RamAdapter: tableFor(from) alt Table doesn't exist RamAdapter-->>Caller: throw InternalError end RamAdapter->>RamAdapter: findPrimaryKey(new from()) RamAdapter->>Storage: entries() Storage-->>RamAdapter: entries loop For each entry RamAdapter->>RamAdapter: revert(r, from, id, pk) end alt Where condition exists RamAdapter->>RamAdapter: result.filter(where) end alt Sort condition exists RamAdapter->>RamAdapter: result.sort(sort) end alt Skip specified RamAdapter->>RamAdapter: result.slice(skip) end alt Limit specified RamAdapter->>RamAdapter: result.slice(0, limit) end alt Select fields specified loop For each result RamAdapter->>RamAdapter: Filter to selected fields end end RamAdapter-->>Caller: result
Parameters:
Name Type Description
rawInput RawRamQuery.<any>

The query specification

View Source ram/RamAdapter.ts, line 713

A promise that resolves to the query results

Promise.<R>

# async read(tableName, id) → {Promise.<Record.<string, any>>}

Fetches a record with the specified ID from the given table. This method checks if the table and record exist and throws appropriate errors if not.

Retrieves a record from in-memory storage

sequenceDiagram participant Caller participant RamAdapter participant Storage as In-Memory Storage Caller->>RamAdapter: read(tableName, id) RamAdapter->>Storage: has(tableName) alt Table doesn't exist RamAdapter-->>Caller: throw NotFoundError end RamAdapter->>Storage: has(id) alt Record doesn't exist RamAdapter-->>Caller: throw NotFoundError end RamAdapter->>Storage: get(id) Storage-->>RamAdapter: record RamAdapter-->>Caller: record
Parameters:
Name Type Description
tableName string

The name of the table to retrieve from

id string | number

The unique identifier of the record to retrieve

View Source ram/RamAdapter.ts, line 590

A promise that resolves to the retrieved record

Promise.<Record.<string, any>>

# repository() → {Constructor.<RamRepository.<M>>}

Returns a constructor for creating repositories that work with the specified model type. This method overrides the base implementation to provide RAM-specific repository functionality.

Gets the repository constructor for a model

View Source ram/RamAdapter.ts, line 473

A constructor for creating RAM repositories

Constructor.<RamRepository.<M>>

# revert(obj, clazz, pk, id) → {M}

Reconstructs a model instance from a stored record by adding back the primary key. This method is the inverse of the prepare method.

Converts a stored record back to a model instance

Parameters:
Name Type Description
obj Record.<string, any>

The stored record

clazz string | Constructor.<M>

The model class or name

pk

The primary key property name

id string | number

The primary key value

View Source ram/RamAdapter.ts, line 529

The reconstructed model instance

M

# protected tableFor(from) → {Map.<(string|number), any>|undefined}

Retrieves the Map representing a table for a given model or table name. If the table doesn't exist, it creates a new one. This is a helper method used by other methods to access the correct storage location.

Gets or creates a table in the in-memory storage

Parameters:
Name Type Description
from string | Constructor.<M>

The model class or table name

View Source ram/RamAdapter.ts, line 667

The table Map or undefined

Map.<(string|number), any> | undefined

# async update(tableName, id, model) → {Promise.<Record.<string, any>>}

Updates a record with the specified ID in the given table. This method acquires a lock to ensure thread safety, checks if the table and record exist, and updates the record with the new data.

Updates an existing record in the in-memory storage

sequenceDiagram participant Caller participant RamAdapter participant Storage as In-Memory Storage Caller->>RamAdapter: update(tableName, id, model) RamAdapter->>RamAdapter: lock.acquire() RamAdapter->>Storage: has(tableName) alt Table doesn't exist RamAdapter-->>Caller: throw NotFoundError end RamAdapter->>Storage: has(id) alt Record doesn't exist RamAdapter-->>Caller: throw NotFoundError end RamAdapter->>Storage: set(id, model) RamAdapter->>RamAdapter: lock.release() RamAdapter-->>Caller: model
Parameters:
Name Type Description
tableName string

The name of the table containing the record

id string | number

The unique identifier of the record to update

model Record.<string, any>

The new record data

View Source ram/RamAdapter.ts, line 622

A promise that resolves to the updated record

Promise.<Record.<string, any>>

# static decoration() → {void}

Configures decorations for createdBy and updatedBy fields in the RAM adapter. This static method is called during initialization to set up handlers that automatically populate these fields with the current user's UUID during create and update operations.

Sets up RAM-specific decorations for model properties

sequenceDiagram participant RamAdapter participant Decoration participant Repository RamAdapter->>Repository: key(PersistenceKeys.CREATED_BY) Repository-->>RamAdapter: createdByKey RamAdapter->>Repository: key(PersistenceKeys.UPDATED_BY) Repository-->>RamAdapter: updatedByKey RamAdapter->>Decoration: flavouredAs(RamFlavour) Decoration-->>RamAdapter: DecoratorBuilder RamAdapter->>Decoration: for(createdByKey) RamAdapter->>Decoration: define(onCreate, propMetadata) RamAdapter->>Decoration: apply() RamAdapter->>Decoration: flavouredAs(RamFlavour) Decoration-->>RamAdapter: DecoratorBuilder RamAdapter->>Decoration: for(updatedByKey) RamAdapter->>Decoration: define(onCreate, propMetadata) RamAdapter->>Decoration: apply()

View Source ram/RamAdapter.ts, line 773

void