Home Reference Source

Function

Static Public Summary
public

Creates a store that reads and writes data in memory.

public

Creates an Express middleware router that binds routes for all of the given models.

public

Creates an Express middleware that responds to the request with either the requested data or the error.

Static Public

public createInMemoryStore(): Store source

import createInMemoryStore from 'autonym/lib/createInMemoryStore'

Creates a store that reads and writes data in memory.

Return:

Store

A complete set of store methods.

Example:

const Person = new Model({
  name: 'person',
  schema: {
    type: 'object',
    properties: {
      firstName: { type: 'string' },
      lastName: { type: 'string' },
    },
    required: ['firstName', 'lastName'],
  },
  store: createInMemoryStore(),
})

const data = await Person.create({ firstName: 'Matt', lastName: 'Miller' })
console.log(data) // { id: '1', firstName: 'Matt', lastName: 'Miller' }

const data = await Person.find()
console.log(data) // [{ id: '1', firstName: 'Matt', lastName: 'Miller' }]

const data = await Person.findOne('1')
console.log(data) // { id: '1', firstName: 'Matt', lastName: 'Miller' }

const data = await Person.findOneAndUpdate('1', { firstName: 'Matthew' })
console.log(data) // { id: '1', firstName: 'Matthew', lastName: 'Miller' }

const data = await Person.findOneAndDelete('1')
console.log(data) // { id: '1' }

try {
  await Person.findOne('1')
} catch (err) {
  console.log(err) // '[NOT_FOUND] Record not found.'
}

public createModelMiddleware(config: object): Promise<Router, Error> source

import createModelMiddleware from 'autonym/lib/createModelMiddleware'

Creates an Express middleware router that binds routes for all of the given models.

Params:

NameTypeAttributeDescription
config object

Configuration.

config.models Model[] | object<string, Model>

A collection of model instances. This may be an object or an array, but if it is an object, the keys will be ignored.

Return:

Promise<Router, Error>

A promise that resolves with an Express router. The router has an additional property modelInitializations which is an array of promises, in case you want to capture errors from the init functions.

Example:

app.use(createModelMiddleware({
  models: [Post, User],
}))
const modelMiddleware = createModelMiddleware({
  models: [Post, User],
})
app.use(modelMiddleware)

Promise.all(modelMiddleware.modelInitializations).catch(err => {
  console.error(err)
  process.exit(1)
})

public createResponderMiddleware(): function[] source

import createResponderMiddleware from 'autonym/lib/createResponderMiddleware'

Creates an Express middleware that responds to the request with either the requested data or the error. It only handles requests that have passed through createModelMiddleware, and will forward other requests to the next middleware

Return:

function[]

The Express middleware.

Example:

app.use(createResponderMiddleware())