# 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
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 |
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
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
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 |
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
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 |
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 |
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) |
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) |
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 |
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 |
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
Parameters:
Name | Type | Description |
---|---|---|
rawInput |
RawRamQuery.<any>
|
The query specification |
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
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 |
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
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 |
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 |
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
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 |
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
void