-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathcache.js
225 lines (188 loc) · 6.5 KB
/
cache.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
import DataLoader from 'dataloader'
import { ObjectId } from 'mongodb'
import { EJSON } from 'bson'
import { getCollection, log } from './helpers'
export const idToString = id => {
if (id instanceof ObjectId) {
return id.toHexString()
} else {
return id && id.toString ? id.toString() : id
}
}
// https://www.geeksforgeeks.org/how-to-check-if-a-string-is-valid-mongodb-objectid-in-nodejs/
export const isValidObjectIdString = string =>
ObjectId.isValid(string) && String(new ObjectId(string)) === string
export const stringToId = string => {
if (string instanceof ObjectId) {
return string
}
if (isValidObjectIdString(string)) {
return new ObjectId(string)
}
return string
}
export function prepFields(fields) {
const cleanedFields = {}
Object.keys(fields)
.sort()
.forEach(key => {
if (typeof key !== 'undefined') {
cleanedFields[key] = Array.isArray(fields[key])
? fields[key]
: [fields[key]]
}
})
return { loaderKey: EJSON.stringify(cleanedFields), cleanedFields }
}
// getNestedValue({ nested: { foo: 'bar' } }, 'nested.foo')
// => 'bar'
export function getNestedValue(object, string) {
string = string.replace(/\[(\w+)\]/g, '.$1') // convert indexes to properties
string = string.replace(/^\./, '') // strip a leading dot
var a = string.split('.')
for (var i = 0, n = a.length; i < n; ++i) {
var k = a[i]
if (k in object) {
object = object[k]
} else {
return
}
}
return object
}
// https://github.com/graphql/dataloader#batch-function
// "The Array of values must be the same length as the Array of keys."
// "Each index in the Array of values must correspond to the same index in the Array of keys."
const orderDocs = (fieldsArray, docs) =>
fieldsArray.map(fields =>
docs.filter(doc => {
for (let fieldName of Object.keys(fields)) {
const fieldValue = getNestedValue(fields, fieldName)
if (typeof fieldValue === 'undefined') continue
const filterValuesArr = Array.isArray(fieldValue)
? fieldValue.map(val => idToString(val))
: [idToString(fieldValue)]
const docValue = doc[fieldName]
const docValuesArr = Array.isArray(docValue)
? docValue.map(val => idToString(val))
: [idToString(docValue)]
let isMatch = false
for (const filterVal of filterValuesArr) {
if (docValuesArr.includes(filterVal)) {
isMatch = true
}
}
if (!isMatch) return false
}
return true
})
)
export const createCachingMethods = ({ collection, model, cache }) => {
const loader = new DataLoader(async ejsonArray => {
const fieldsArray = ejsonArray.map(EJSON.parse)
log('fieldsArray', fieldsArray)
const filterArray = fieldsArray.reduce((filterArray, fields) => {
const existingFieldsFilter = filterArray.find(
filter =>
[...Object.keys(filter)].sort().join() ===
[...Object.keys(fields)].sort().join()
)
const filter = existingFieldsFilter || {}
for (const fieldName in fields) {
if (typeof fields[fieldName] === 'undefined') continue
if (!filter[fieldName]) filter[fieldName] = { $in: [] }
let newVals = Array.isArray(fields[fieldName])
? fields[fieldName]
: [fields[fieldName]]
filter[fieldName].$in = [
...filter[fieldName].$in,
...newVals
.map(stringToId)
.filter(val => !filter[fieldName].$in.includes(val))
]
}
if (existingFieldsFilter) return filterArray
return [...filterArray, filter]
}, [])
log('filterArray: ', filterArray)
const filter =
filterArray.length === 1
? filterArray[0]
: {
$or: filterArray
}
log('filter: ', filter)
const findPromise = model
? model.find(filter).exec()
: collection.find(filter).toArray()
const results = await findPromise
log('results: ', results)
const orderedDocs = orderDocs(fieldsArray, results)
log('orderedDocs: ', orderedDocs)
return orderedDocs
})
const cachePrefix = `mongo-${getCollection(collection).collectionName}-`
const methods = {
findOneById: async (_id, { ttl } = {}) => {
const cacheKey = cachePrefix + idToString(_id)
const cacheDoc = await cache.get(cacheKey)
log('findOneById found in cache:', cacheDoc)
if (cacheDoc) {
return EJSON.parse(cacheDoc)
}
log(`Dataloader.load: ${EJSON.stringify({ _id })}`)
const docs = await loader.load(EJSON.stringify({ _id }))
log('Dataloader.load returned: ', docs)
if (Number.isInteger(ttl)) {
// https://github.com/apollographql/apollo-server/tree/master/packages/apollo-server-caching#apollo-server-caching
cache.set(cacheKey, EJSON.stringify(docs[0]), { ttl })
}
return docs[0]
},
findManyByIds: (ids, { ttl } = {}) => {
return Promise.all(ids.map(id => methods.findOneById(id, { ttl })))
},
findByFields: async (fields, { ttl } = {}) => {
const { cleanedFields, loaderKey } = prepFields(fields)
const cacheKey = cachePrefix + loaderKey
const cacheDoc = await cache.get(cacheKey)
if (cacheDoc) {
return EJSON.parse(cacheDoc)
}
const fieldNames = Object.keys(cleanedFields)
let docs
if (fieldNames.length === 1) {
const field = cleanedFields[fieldNames[0]]
const fieldArray = Array.isArray(field) ? field : [field]
const docsArray = await Promise.all(
fieldArray.map(value => {
const filter = {}
filter[fieldNames[0]] = value
return loader.load(EJSON.stringify(filter))
})
)
docs = [].concat(...docsArray)
} else {
docs = await loader.load(loaderKey)
}
if (Number.isInteger(ttl)) {
// https://github.com/apollographql/apollo-server/tree/master/packages/apollo-server-caching#apollo-server-caching
cache.set(cacheKey, EJSON.stringify(docs), { ttl })
}
return docs
},
deleteFromCacheById: async _id => {
loader.clear(EJSON.stringify({ _id }))
const cacheKey = cachePrefix + idToString(_id)
log('Deleting cache key: ', cacheKey)
await cache.delete(cacheKey)
},
deleteFromCacheByFields: async fields => {
const { loaderKey } = prepFields(fields)
const cacheKey = cachePrefix + loaderKey
loader.clear(loaderKey)
await cache.delete(cacheKey)
}
}
return methods
}