forked from jsreport/jsreport-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollection.js
More file actions
192 lines (148 loc) · 5.16 KB
/
collection.js
File metadata and controls
192 lines (148 loc) · 5.16 KB
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
const ListenerCollection = require('listener-collection')
const validateEntityName = require('../util/validateEntityName')
function getEntityTypeFromModel (model, entitySetName) {
if (model.entitySets[entitySetName] == null) {
return
}
const entitySet = model.entitySets[entitySetName]
const entityTypeName = entitySet.entityType.replace(model.namespace + '.', '')
return model.entityTypes[entityTypeName]
}
module.exports = (entitySet, provider, model) => ({
name: entitySet,
beforeFindListeners: new ListenerCollection(),
beforeUpdateListeners: new ListenerCollection(),
beforeInsertListeners: new ListenerCollection(),
beforeRemoveListeners: new ListenerCollection(),
entitySet,
load: (...args) => {
provider.load(entitySet, ...args)
},
find (q, p, req) {
if (p && p.__isJsreportRequest__ === true) {
req = p
p = {}
}
p = p || {}
const listenerPromise = this.beforeFindListeners.fire(q, p, req)
// the jsreport backcompatible API for find returns promise with the result array
// the new API returns a cursor like mongo uses
// to make it working for both way of calling, we return
// an object which is a promise and in the same time a cursor
const cursorCalls = []
const functions = ['skip', 'limit', 'sort', 'toArray', 'count']
const fakeCursor = {}
functions.forEach((f) => {
fakeCursor[f] = (...args) => {
cursorCalls.push({f: f, args: args})
return fakeCursor
}
})
const replay = (cursor) => {
cursorCalls.filter((c) => c.f !== 'toArray' && c.f !== 'count').forEach((c) => cursor[c.f].apply(cursor, c.args))
if (cursorCalls.find((c) => c.f === 'count')) {
return cursor.count()
}
return cursor.toArray()
}
return Object.assign(fakeCursor, {
then: (onFulfilled, onRejected) => {
// the node A compatible promise expects then to be called with two functions
// the bluebird expects to return a promise
let promise = listenerPromise.then(() => replay(provider.find(entitySet, q, p)))
// the node A compatible promise expects then to be called with two functions
// the bluebird expects to return a promise
if (typeof onFulfilled === 'function') {
promise = promise.then(onFulfilled)
}
if (typeof onRejected === 'function') {
promise = promise.catch(onRejected)
}
return promise
}
})
},
count (...args) {
return this.find(...args).count()
},
async insert (...args) {
const entityType = getEntityTypeFromModel(model, entitySet)
const data = args[0]
// validate if "name" is defined in entityType
if (entityType && entityType.name != null && data) {
validateEntityName(data.name)
}
await this.beforeInsertListeners.fire(...args)
return provider.insert(entitySet, ...args)
},
async update (q, u, o, req) {
const entityType = getEntityTypeFromModel(model, entitySet)
if (o && o.__isJsreportRequest__) {
req = o
o = {}
}
// validate if "name" is defined in entityType and name is being updated
if (entityType && entityType.name != null && u && u.$set && u.$set.name !== undefined) {
validateEntityName(u.$set.name)
}
await this.beforeUpdateListeners.fire(q, u, o, req)
return provider.update(entitySet, q, u, o, req)
},
async remove (...args) {
await this.beforeRemoveListeners.fire(...args)
return provider.remove(entitySet, ...args)
},
async findOne (...args) {
const res = await this.find(...args)
if (res.length > 0) {
return res[0]
}
return null
},
convertBase64ToBufferInEntity (docs) {
const entitySetInfo = model.entitySets[entitySet]
if (!entitySetInfo) {
return []
}
const entityType = model.entityTypes[entitySetInfo.entityType.replace(model.namespace + '.', '')]
if (!entityType) {
return []
}
return docs.map((doc) => {
const newDoc = Object.assign({}, doc)
for (const prop in newDoc) {
if (!prop) {
continue
}
const propDef = entityType[prop]
if (!propDef) {
continue
}
if (propDef.type === 'Edm.Binary') {
newDoc[prop] = Buffer.from(newDoc[prop], 'base64') // eslint-disable-line
}
}
return newDoc
})
},
convertBufferToBase64InEntity (docs) {
const entitySetInfo = model.entitySets[entitySet]
const entityType = model.entityTypes[entitySetInfo.entityType.replace(model.namespace + '.', '')]
return docs.map((doc) => {
const newDoc = Object.assign({}, doc)
for (const prop in newDoc) {
if (!prop) {
continue
}
const propDef = entityType[prop]
if (!propDef) {
continue
}
if (propDef.type === 'Edm.Binary') {
newDoc[prop] = Buffer.from(newDoc[prop]).toString('base64') // eslint-disable-line
}
}
return newDoc
})
}
})