-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathcollections.js
258 lines (240 loc) · 8.77 KB
/
collections.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
const debug = require('debug')('expressa')
const MongoQS = require('mongo-querystring')
const util = require('../util')
const queryStringParser = new MongoQS({})
function assertValidCollection(req) {
if (!req.db[req.params.collection]) {
throw new util.ApiError(404, 'unknown collection')
}
}
async function validateDocumentOwner(req, doc) {
if (!doc.meta.owner_collection) {
throw new util.ApiError(417, 'no owner_collection for owner found')
}
if (!(doc._id === doc.meta.owner && req.params.collection === doc.meta.owner_collection)) {
const count = await req.db[doc.meta.owner_collection].count({ _id: doc.meta.owner }, undefined, 1)
if (!count) {
throw new util.ApiError(417, 'invalid owner')
}
}
}
async function setDocumentOwner(req, doc) {
const schema = await req.db.collection.get(req.params.collection)
if (schema.documentsHaveOwners) {
util.addIdIfMissing(doc)
doc.meta ??= {}
if (req.uid) {
// request done by logged in user
if (!doc.meta.owner) {
// no owner - make logged in user as owner
doc.meta.owner = req.user._id
doc.meta.owner_collection = req.ucollection
} else {
if (!req.hasPermission(`${req.params.collection}: modify owner`)) {
// has an owner - but if no permission to modify owner then override owner to logged in user
doc.meta.owner = req.user._id
doc.meta.owner_collection = req.ucollection
}
}
} else {
// request done by NOT logged in user
if (schema.enableLogin) {
// is a user/login collection so make self owner
doc.meta.owner = doc._id
doc.meta.owner_collection = req.params.collection
} else {
// is a normal collection
debug('unable to find valid owner')
}
}
if (doc.meta.owner) {
await validateDocumentOwner(req, doc)
}
}
}
exports.getSchema = async function (req) {
const collection = await req.db.collection.get(req.params.collection)
await util.notify('get', req, 'schemas', collection)
return collection.schema
}
exports.get = async function (req) {
assertValidCollection(req)
if (typeof req.db[req.params.collection] === 'undefined') {
throw new util.ApiError(404, 'unknown collection')
}
const fields = req.query.fields ? JSON.parse(req.query.fields) : null
let data, query
if (req.query.query) {
query = JSON.parse(req.query.query)
}
else {
const { skip, offset, limit, page, pageitems, pagemetadisable, orderby, ...params } = req.query // eslint-disable-line no-unused-vars
delete params.fields
query = queryStringParser.parse(params)
}
if (req.query.orderby) {
req.query.orderby = JSON.parse(req.query.orderby)
req.query.orderby = util.normalizeOrderBy(req.query.orderby)
}
if (req.uid) {
// scenario where logged in user can only retrieve own docs
if (!req.hasPermission(`${req.params.collection}: view`) && req.hasPermission(`${req.params.collection}: view own`)) {
query['meta.owner'] = req.uid
// if fields is null then all fields are requested
if (fields) fields['meta.owner'] = 1
}
}
if (req.query.page != null) {
req.query.page = parseInt(req.query.page)
if (req.query.page <= 0) {
throw new util.ApiError(400, 'invalid page number')
}
req.query.pageitems = parseInt(req.query.pageitems) || parseInt(req.query.limit) || 10
req.query.offset = util.getDatabaseOffset(req.query.page, req.query.pageitems)
req.query.orderby = util.orderByForPagedRequests(req.query.orderby)
delete req.query.limit
delete req.query.skip
let pageData, totalItems
if (req.query.pagemetadisable) {
// a way to skip the extra count database call, but lose additional page detail
pageData = await req.db[req.params.collection].find(query, req.query.offset, req.query.pageitems, req.query.orderby, fields)
}
else {
// this is default - full detail
[pageData, totalItems] = await Promise.all([
req.db[req.params.collection].find(query, req.query.offset, req.query.pageitems, req.query.orderby, fields),
req.db[req.params.collection].count(query)
])
}
await Promise.all(pageData.map((doc) => util.notify('get', req, req.params.collection, doc)))
data = util.createPagePagination(pageData, req.query.page, req.query.pageitems, totalItems)
}
else {
if (req.query.limit) {
req.query.limit = parseInt(req.query.limit)
}
if (req.query.skip) {
req.query.offset = parseInt(req.query.skip)
delete req.query.skip
}
else if (req.query.offset) {
req.query.offset = parseInt(req.query.offset)
}
data = await req.db[req.params.collection].find(query, req.query.offset, req.query.limit, req.query.orderby, fields)
await Promise.all(data.map((doc) => util.notify('get', req, req.params.collection, doc)))
}
await util.notify('getpresend', req, req.params.collection, data)
return data
}
exports.insert = async function (req) {
assertValidCollection(req)
const data = req.body
req.customResponseData = req.customResponseData || {}
await util.notify('post', req, req.params.collection, data)
await setDocumentOwner(req, data)
const id = await req.db[req.params.collection].create(data)
await util.notify('changed', req, req.params.collection, data)
return {
status: 'OK',
id: id,
...req.customResponseData,
}
}
exports.getById = async function (req) {
assertValidCollection(req)
const fields = req.query.fields ? JSON.parse(req.query.fields) : null
const data = await req.db[req.params.collection].get(req.params.id, fields)
await util.notify('get', req, req.params.collection, data)
await util.notify('getpresend', req, req.params.collection, data)
return data
}
exports.replaceById = async function (req) {
assertValidCollection(req)
req.customResponseData = req.customResponseData || {}
const data = req.body
let oldDoc = {}
try {
oldDoc = await req.db[req.params.collection].get(req.params.id)
} catch (error) {
// happens when new documents are created via a PUT
oldDoc = { meta: { owner: req.user._id, owner_collection: 'users' } }
data._id = req.params.id.trim()
await setDocumentOwner(req, data)
}
if (data._id) {
data._id = data._id.trim()
} else {
data._id = req.params.id.trim()
}
data.meta = data.meta || {}
data.meta.created = (oldDoc.meta || {}).created
if (data.meta.owner !== oldDoc.meta.owner || data.meta.owner_collection !== oldDoc.meta.owner_ollection) {
if (req.hasPermission(`${req.params.collection}: modify owner`)) {
if (oldDoc.meta.owner) {
await validateDocumentOwner(req, data)
}
} else {
debug('attempting to change document owner.')
data.meta.owner = oldDoc.meta.owner
data.meta.owner_collection = oldDoc.meta.owner_collection
}
}
await util.notify('put', req, req.params.collection, data)
await req.db[req.params.collection].update(req.params.id, data)
await util.notify('changed', req, req.params.collection, data)
return {
status: 'OK',
id: data._id,
...req.customResponseData,
}
}
exports.updateById = async function (req) {
assertValidCollection(req)
req.customResponseData = req.customResponseData || {}
const modifier = req.body
const doc = await req.db[req.params.collection].get(req.params.id)
const owner = doc.meta && doc.meta.owner
const ownerCollection = doc.meta && doc.meta.owner_collection
util.mongoUpdate(doc, modifier)
const newOwner = doc.meta && doc.meta.owner
const newOwnerCollection = doc.meta && doc.meta.owner_collection
if (owner !== newOwner || ownerCollection !== newOwnerCollection) {
if (req.hasPermission(`${req.params.collection}: modify owner`)) {
if (owner) {
await validateDocumentOwner(req, doc)
}
} else {
debug('attempting to change document owner.')
doc.meta.owner = owner
doc.meta.owner_collection = ownerCollection
}
}
if (doc._id) {
doc._id = doc._id.trim()
} else {
throw new util.ApiError(400, '_id property is required')
}
req.body = doc
await util.notify('put', req, req.params.collection, doc)
await req.db[req.params.collection].update(req.params.id, req.body)
await util.notify('changed', req, req.params.collection, req.body)
if (Object.keys(req.customResponseData)) {
return {
...doc,
...req.customResponseData,
}
}
return doc
}
exports.deleteById = async function (req) {
assertValidCollection(req)
req.customResponseData = req.customResponseData || {}
const doc = await req.db[req.params.collection].get(req.params.id)
await util.notify('delete', req, req.params.collection, doc)
await req.db[req.params.collection].delete(req.params.id)
await util.notify('deleted', req, req.params.collection, doc)
return {
status: 'OK',
...req.customResponseData,
}
}