a simple repository pattern for caching, creating, loading collections

Usage no npm install needed!

<script type="module">
  import coboxcoopRepository from 'https://cdn.skypack.dev/@coboxcoop/repository';



standard-readme compliant

Table of Contents


CoBox is an encrypted p2p file system and distributed back-up tool. README provides a map of the project.

repository provides a simple repository pattern for caching, creating, loading and saving the following models:


In CoBox we're using this to load each multifeed and kappa-core instance and cache them in this repository. This ensures that our indexes are always up to date by calling the ready function on each instance before returning the instance itself. The basic querying functionality allows for our controllers to pass a set of parameters to a where or findBy function and match on that basis.

Currently supports matching parameters based on the following types:

  • Buffers
  • Strings


npm i -g @coboxcoop/schemas


const Repository = require('@coboxcoop/repository')

const repository = Repository(storage, factory, opts)

All opts get passed down to the instance generated by the factory.

Here's an example of what a controller would look like using @coboxcoop/repository:

const Repository = require('@coboxcoop/repository')

// Suppose a POST create has been sent with the following parameters
var params = {
  name: 'myspace',
  address: '4cef27e9702ae90c07056aa88cb4527a7037368311c0b0ed58db28344613ebd6'

function SpacesController () {
  const spaces = Repository('path/to/yaml/file/for/spaces', createSpace, { onCreate })

  return { create }

  async function create (request, response) {
    try {
      await spaces.ready()
      var space = await spaces.create(request.params)
      response.send('success! you created a space')
    } catch (err) {
      if (err.found) return response.send('validation failed: space with these parameters already exists')

  function onCreate (entry, cb) {
    // callback executes just before caching, but after building


All functions can be called either with a callback as the last argument, or using async/await (i.e. they return a promise).


Get all entries


Get all entries that match the strongly matches the attributes provided (will ignore if one or more attributes doesn't match while but others match)


Get first entry that matches attributes provided (same as where but returns the first one)


Use the factory function provided to build an instance and cache it. ready function expected on the entry class produced by the factory.


Load and get all entries ready. should always be called first.


Gracefully close all entries. close function expected on the entry class produced by the factory


Persist all entries to a YAML file. create one if it doesn't exist already.


Load all entries from the YAML file.


PRs accepted.

Small note: If editing the README, please conform to the standard-readme specification.