-
Notifications
You must be signed in to change notification settings - Fork 27
/
listeners.js
65 lines (62 loc) · 2.1 KB
/
listeners.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const debug = require('debug')('expressa')
const util = require('./util')
module.exports = function (api) {
/* Listeners to avoid the need for a server restart */
api.addCollectionListener('changed', 'collection', async function setupCollectionStorage (req, collection, data) {
if (!(data.storage === 'memory' && util.getPath(api.db[data._id], 'type') === 'memory')) {
await api.setupCollectionDb(data)
}
debug('updated ' + data._id + ' collection storage')
})
api.addCollectionListener('delete', 'collection', function cleanupCollectionStorage (req, collection, data) {
delete api.db[data._id]
debug('removed ' + data._id + ' collection storage')
})
api.addCollectionListener('changed', 'settings', function updateInMemorySettings (req, collection, data) {
// TODO: only reload if current environment is updated
Object.assign(req.settings, data)
})
api.addListener(['put', 'post'], async function updateMetadata (req, collection, data, { event }) {
data.meta ??= {}
data.meta.updated = new Date().toISOString()
if (event === 'post') {
data.meta.created = new Date().toISOString()
}
})
api.addCollectionListener('get', 'schemas', function addMetaToSchema (req, collection, data) {
const schema = data.schema
if (!schema.properties.meta) {
schema.properties.meta = {
type: 'object',
properties: {
created: {
type: 'string'
},
updated: {
type: 'string'
}
}
}
}
if (schema.properties.meta.propertyOrder === undefined) {
schema.properties.meta.propertyOrder = 2000
}
if (data.documentsHaveOwners) {
if (!schema.properties.meta.properties.owner) {
schema.properties.meta.properties.owner = {
type: 'string',
links: [
{
rel: '» view owner user',
href: '/admin/#/edit/users/{{self}}',
class: 'comment-link'
}
]
},
schema.properties.meta.properties.owner_collection = {
type: 'string'
}
}
}
})
}