Function
Static Public Summary | ||
public |
Creates a store that reads and writes data in memory. |
|
public |
createModelMiddleware(config: object): Promise<Router, Error> 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.
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.
Return:
Promise<Router, Error> | A promise that resolves with an Express router. The router has an additional
property |
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
Example:
app.use(createResponderMiddleware())