Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,30 @@ const { url } = await startStandaloneServer(server, {
})
```

### Lean

To enable `lean` queries for mongoose models, add the `options.lean` to constructor:

```js
class Users extends MongoDataSource {}

new Users({ modelOrCollection: UserModel, options: { lean: true } })
```

Keep in mind that it will enhance performance but will get rid of getters (like `id` enabled in mongoose schema).
If you want to keep getters with `lean` I recommend using [mongoose-lean-getters](https://github.com/mongoosejs/mongoose-lean-getters) package.

### Key prefix

You can customize the key prefix for the cache by passing `options.keyPrefix` to constructor:

```js
class Users extends MongoDataSource {}

// will use `users` as a key prefix
new Users({ modelOrCollection: UserModel, options: { keyPrefix: 'prefix:' } })
```

### Caching

To enable shared application-level caching, you do everything from the above section, and you add the `ttl` (in seconds) option to `findOneById()`:
Expand Down Expand Up @@ -249,6 +273,18 @@ const resolvers = {

Here we also call [`deleteFromCacheById()`](#deletefromcachebyid) to remove the user from the cache when the user's data changes. If we're okay with people receiving out-of-date data for the duration of our `ttl`—in this case, for as long as a minute—then we don't need to bother adding calls to `deleteFromCacheById()`.

#### Disable memoization

If you want to disable DataLoader memoization, you can pass `options.disableMemoize` to constructor.

```js
class Users extends MongoDataSource {}

new Users({ modelOrCollection: , options: { disableMemoize: true } })
```

Disabling memoization caching will create a new `Promise` and new key in the `batchLoadFn` for every load of the same key. This is equivalent to setting `cacheMap` to `null`.

### TypeScript

Since we are using a typed language, we want the provided methods to be correctly typed as well. This requires us to make the `MongoDataSource` class polymorphic. It requires 1 template argument, which is the type of the document in our collection. If you wish to add additional fields to your data source class, you can extend the typing on constructor options argument to include any fields that you need. For example:
Expand Down Expand Up @@ -316,7 +352,7 @@ const { url } = await startStandaloneServer(server, {
});
```

You can also opt to pass the entire context into your data source class. You can do so by adding a protected context member
You can also opt to pass the entire context into your data source class. You can do so by adding a protected context member
to your data source class and modifying to options argument of the constructor to add a field for the context. Then, call super and
assign the context to the member field on your data source class. Note: context needs to be a class in order to do this.

Expand Down
20 changes: 14 additions & 6 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
declare module 'apollo-datasource-mongodb' {
import { KeyValueCache } from '@apollo/utils.keyvaluecache'
import { Collection as MongoCollection, ObjectId } from 'mongodb'
import {
Collection as MongooseCollection,
Document,
Model as MongooseModel,
} from 'mongoose'
import { Collection as MongoCollection, ObjectId } from 'mongodb'
import {
Document,
Collection as MongooseCollection,
Model as MongooseModel,
} from 'mongoose'

export type Collection<T extends { [key: string]: any }, U = MongoCollection<T>> = T extends Document
? MongooseCollection
Expand All @@ -32,9 +32,17 @@ declare module 'apollo-datasource-mongodb' {
ttl: number
}

export interface MongoDataSourceOptions {
/** When enabled it won't return mongoose getters (like `id`) by default, if you want them back install the plugin https://github.com/mongoosejs/mongoose-lean-getters */
lean?: boolean
disableMemoize?: boolean
keyPrefix?: string
}

export interface MongoDataSourceConfig<TData extends { [key: string]: any }> {
modelOrCollection: ModelOrCollection<TData>
cache?: KeyValueCache<TData>
options?: MongoDataSourceOptions
}

export class MongoDataSource<TData extends { [key: string]: any }> {
Expand Down
Loading