@devs-toolbox/repository
v1.0.1
Published
A persisting, in-memory database-like item repository
Downloads
3
Readme
Dev's Toolbox // Repository
A persisting, in-memory database-like item repository
API
Repository.prepare(options)
Given a Repository options object, create a repository, load its data and return the newly created repository.
This is commonly preferred over creating and preparing Repository objects manually.
Options
| Option | Default Value | Description |
| ----------- | ------------- | --------------------------------------------------------------------------------------------- |
| typeguard | None | An optional typeguard to check data integrity when loading data from an adapter |
| defaults | None | A partial object of the model type defining default values when inserting partials |
| adapter | None | An interface which provides read
and write
functions for the repository to persist itself |
| idGenerator | uuidV4 | If provided, is called to generate random String IDs |
new Repository(options?)
Create a new repository with the given options. The repository is not populated with data by default. You must manually call .prepare()
on it and await its return.
.insertMany(models)
Insert an array of models into the repository
.insert(model)
Insert a single model into the rpeository
.insertPartial(model)
Insert a single model, applying defaults supplied at repository creation time
.remove(modelOrId)
Remove the given model, or remove a model by ID. If an array is provided, all elements in the array are removed.
.removeWhere(descriptor)
Remove models where the given descriptor
matches (see "Descriptor" section below)
.update(partialModel)
Given a partial model with its ID attached, update the model in the repository
.updateWhere(descriptor, setValues)
Set the values defined in setValues
on models where the descriptor
matches.
.upsert(model)
Inserts the model if the repository does not already contain it. Ohterwise, update the model.
.contains(model)
Check if the given model is in the repository
.containsIndex(id)
Check if the given model ID is found in the repository
.findById(id)
Fetch a model by ID
.find(descriptor, options?)
Find models based on a partial description
or predicate. Pass true
to fetch all models in the repository.
Find Options
| Option | Type | Description |
| ------- | ---------------------------------- | ------------------------------------------------ |
| skip
| number
| How many records to skip |
| limit
| number
| The max number of models to fetch |
| sort
| [keyof Model
, "ASC" or "DESC"
] | A tuple of what field to sort by and ASC or DESC |
.findOne(descriptor)
Find the first element that matches the descriptor
.truncate()
Remove all models in the repository
.size()
Return the number of models in the repository
.generateId()
Return a new ID from this repository's ID generator function
Descriptor
A descriptor
is one of two things:
- A function which acts like a
filter
function, taking models and returningtrue
orfalse
- A partial object containing keys and values to search for matching models