Skip to content

Commit 1616e33

Browse files
committed
feat(types): adds support for passing the type to the collection
1 parent 217ccb2 commit 1616e33

File tree

4 files changed

+20
-18
lines changed

4 files changed

+20
-18
lines changed

lib/index.d.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { DataSource } from "apollo-datasource";
22
import { KeyValueCache } from "apollo-server-caching";
3-
import { Collection, FindOptions } from "mongodb";
3+
import { Collection, Document, FindOptions } from "mongodb";
44
declare type DataSourceOperation = "findOne" | "find" | "count";
5-
export default class MongoDataSource implements DataSource {
5+
export default class MongoDataSource<TSchema extends Document = Document> implements DataSource {
66
cache?: KeyValueCache<string> | undefined;
77
context: any;
8-
collection: Collection<Document>;
8+
collection: Collection<TSchema>;
99
cachePrefix: string;
1010
private pendingResults;
11-
constructor(collection: Collection<Document>, cache?: KeyValueCache<string> | undefined);
11+
constructor(collection: Collection<TSchema>, cache?: KeyValueCache<string> | undefined);
1212
initialize({ cache }: {
1313
context: any;
1414
cache: KeyValueCache;
@@ -19,11 +19,11 @@ export default class MongoDataSource implements DataSource {
1919
find(fields?: any, options?: {
2020
ttl: number;
2121
findOptions?: FindOptions<Document>;
22-
}): Promise<any[]>;
22+
}): Promise<TSchema[]>;
2323
findOne(fields?: any, options?: {
2424
ttl: number;
25-
findOptions?: FindOptions<Document>;
26-
}): Promise<any>;
25+
findOptions?: FindOptions;
26+
}): Promise<TSchema | null>;
2727
delete(type: DataSourceOperation, fields: any, options?: {
2828
findOptions?: FindOptions<Document>;
2929
}): Promise<boolean | void | undefined>;

lib/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "apollo-mongodb-datasource",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "An Apollo GraphQL Datasource using MongoDB.",
55
"main": "lib/index.js",
66
"types": "lib/index.d.ts",

src/index.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
import { DataSource } from "apollo-datasource";
22
import { KeyValueCache } from "apollo-server-caching";
3-
import { Collection, FindOptions } from "mongodb";
3+
import { Collection, Document, FindOptions } from "mongodb";
44

55
type DataSourceOperation = "findOne" | "find" | "count";
66

7-
export default class MongoDataSource implements DataSource {
7+
export default class MongoDataSource<TSchema extends Document = Document>
8+
implements DataSource
9+
{
810
context: any;
9-
collection: Collection<Document>;
11+
collection: Collection<TSchema>;
1012

1113
cachePrefix: string;
1214

1315
private pendingResults: { key: string; promise: Promise<any> }[] = [];
1416

15-
constructor(collection: Collection<Document>, public cache?: KeyValueCache) {
17+
constructor(collection: Collection<TSchema>, public cache?: KeyValueCache) {
1618
this.collection = collection;
1719

1820
this.cachePrefix = `mongo-${this.collection.dbName}-${this.collection.collectionName}-`;
@@ -42,7 +44,7 @@ export default class MongoDataSource implements DataSource {
4244
options: { ttl: number; findOptions?: FindOptions<Document> } = {
4345
ttl: 60
4446
}
45-
): Promise<any[]> {
47+
): Promise<TSchema[]> {
4648
const cacheKey = this.getCacheKey("find", fields, options),
4749
cacheDoc = await this.cache?.get(cacheKey);
4850

@@ -62,10 +64,10 @@ export default class MongoDataSource implements DataSource {
6264

6365
async findOne(
6466
fields: any = {},
65-
options: { ttl: number; findOptions?: FindOptions<Document> } = {
67+
options: { ttl: number; findOptions?: FindOptions } = {
6668
ttl: 60
6769
}
68-
) {
70+
): Promise<TSchema | null> {
6971
const cacheKey = this.getCacheKey("findOne", fields, options),
7072
cacheDoc = await this.cache?.get(cacheKey);
7173

@@ -84,15 +86,15 @@ export default class MongoDataSource implements DataSource {
8486
async delete(
8587
type: DataSourceOperation,
8688
fields: any,
87-
options: { findOptions?: FindOptions<Document> } = {}
89+
options: { findOptions?: FindOptions } = {}
8890
) {
8991
return await this.cache?.delete(this.getCacheKey(type, fields, options));
9092
}
9193

9294
private getCacheKey(
9395
type: DataSourceOperation,
9496
fields: any,
95-
options: { findOptions?: FindOptions<Document> } = {}
97+
options: { findOptions?: FindOptions } = {}
9698
) {
9799
return (
98100
this.cachePrefix +

0 commit comments

Comments
 (0)