forked from GraphQLGuide/apollo-datasource-mongodb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatasource.js
32 lines (26 loc) · 863 Bytes
/
datasource.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
import { GraphQLError } from 'graphql'
import { InMemoryLRUCache } from '@apollo/utils.keyvaluecache'
import { createCachingMethods } from './cache'
import { isCollectionOrModel, isModel } from './helpers'
class MongoDataSource {
constructor({ modelOrCollection, cache }) {
if (!isCollectionOrModel(modelOrCollection)) {
throw new GraphQLError(
'MongoDataSource constructor must be given a collection or Mongoose model'
)
}
if (isModel(modelOrCollection)) {
this.model = modelOrCollection
this.collection = this.model.collection
} else {
this.collection = modelOrCollection
}
const methods = createCachingMethods({
collection: this.collection,
model: this.model,
cache: cache || new InMemoryLRUCache()
})
Object.assign(this, methods)
}
}
export { MongoDataSource }