Skip to content

Commit bd77d71

Browse files
committed
✨ defaultTTL
1 parent bf0c6cf commit bd77d71

File tree

5 files changed

+118
-35
lines changed

5 files changed

+118
-35
lines changed

lib/index.d.ts

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,44 @@ import { KeyValueCache } from "apollo-server-caching";
33
import { Collection, Document, FindOptions } from "mongodb";
44
declare type DataSourceOperation = "findOne" | "find" | "count";
55
export default class MongoDataSource<TSchema extends Document = Document> implements DataSource {
6-
cache?: KeyValueCache<string> | undefined;
7-
context: any;
6+
/**
7+
* MongoDB collection for the data source.
8+
*/
89
collection: Collection<TSchema>;
10+
/**
11+
* Cache instance
12+
*/
13+
cache?: KeyValueCache<string> | undefined;
14+
/**
15+
* The prefix for the cache key
16+
* @default "mongodb"
17+
*/
918
cachePrefix: string;
1019
private pendingResults;
11-
constructor(collection: Collection<TSchema>, cache?: KeyValueCache<string> | undefined);
20+
private defaultTTL;
21+
constructor(
22+
/**
23+
* MongoDB collection for the data source.
24+
*/
25+
collection: Collection<TSchema>,
26+
/**
27+
* Cache instance
28+
*/
29+
cache?: KeyValueCache<string> | undefined,
30+
/**
31+
* Options for the DataSource
32+
*/
33+
options?: {
34+
/**
35+
* The default TTL for the cache
36+
*/
37+
defaultTTL?: number;
38+
/**
39+
* The prefix for the cache key
40+
*/
41+
cachePrefix?: string;
42+
});
1243
initialize({ cache }: {
13-
context: any;
1444
cache: KeyValueCache;
1545
}): void;
1646
count(query?: {}, options?: {
@@ -24,8 +54,8 @@ export default class MongoDataSource<TSchema extends Document = Document> implem
2454
ttl: number;
2555
findOptions?: FindOptions;
2656
}): Promise<TSchema | null>;
27-
delete(type: DataSourceOperation, fields: any, options?: {
28-
findOptions?: FindOptions<Document>;
57+
delete(type: DataSourceOperation, fields?: any, options?: {
58+
findOptions?: FindOptions;
2959
}): Promise<boolean | void | undefined>;
3060
private getCacheKey;
3161
private antiSpam;

lib/index.js

Lines changed: 36 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "apollo-mongodb-datasource",
3-
"version": "1.0.5",
3+
"version": "1.1.0",
44
"description": "An Apollo GraphQL Datasource using MongoDB.",
55
"main": "lib/index.js",
66
"types": "lib/index.d.ts",
@@ -12,11 +12,11 @@
1212
"dev": "tsc -w"
1313
},
1414
"dependencies": {
15-
"apollo-datasource": "^3.2.0",
16-
"apollo-server-caching": "^3.2.0",
17-
"mongodb": "^4.1.3"
15+
"apollo-datasource": "^3.3.1",
16+
"apollo-server-caching": "^3.3.0",
17+
"mongodb": "^4.3.1"
1818
},
1919
"devDependencies": {
20-
"typescript": "^4.4.4"
20+
"typescript": "^4.5.5"
2121
}
2222
}

src/index.ts

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,50 @@ type DataSourceOperation = "findOne" | "find" | "count";
77
export default class MongoDataSource<TSchema extends Document = Document>
88
implements DataSource
99
{
10-
context: any;
11-
collection: Collection<TSchema>;
12-
13-
cachePrefix: string;
10+
/**
11+
* The prefix for the cache key
12+
* @default "mongodb"
13+
*/
14+
cachePrefix = "mongodb";
1415

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

17-
constructor(collection: Collection<TSchema>, public cache?: KeyValueCache) {
18-
this.collection = collection;
18+
private defaultTTL = 60;
19+
20+
constructor(
21+
/**
22+
* MongoDB collection for the data source.
23+
*/
24+
public collection: Collection<TSchema>,
25+
/**
26+
* Cache instance
27+
*/
28+
public cache?: KeyValueCache,
29+
/**
30+
* Options for the DataSource
31+
*/
32+
options?: {
33+
/**
34+
* The default TTL for the cache
35+
*/
36+
defaultTTL?: number;
37+
/**
38+
* The prefix for the cache key
39+
*/
40+
cachePrefix?: string;
41+
}
42+
) {
43+
this.cachePrefix = `${this.cachePrefix}-${this.collection.dbName}-${this.collection.collectionName}-`;
1944

20-
this.cachePrefix = `mongo-${this.collection.dbName}-${this.collection.collectionName}-`;
45+
if (options?.defaultTTL) this.defaultTTL = options.defaultTTL;
46+
if (options?.cachePrefix) this.cachePrefix = options.cachePrefix;
2147
}
2248

23-
initialize({ cache }: { context: any; cache: KeyValueCache }) {
49+
initialize({ cache }: { cache: KeyValueCache }) {
2450
this.cache = cache;
2551
}
2652

27-
async count(query: {} = {}, options = { ttl: 60 }) {
53+
async count(query: {} = {}, options = { ttl: this.defaultTTL }) {
2854
const cacheKey = this.getCacheKey("count", query),
2955
cacheDoc = await this.cache?.get(cacheKey);
3056
if (cacheDoc) return JSON.parse(cacheDoc);
@@ -42,7 +68,7 @@ export default class MongoDataSource<TSchema extends Document = Document>
4268
async find(
4369
fields: any = {},
4470
options: { ttl: number; findOptions?: FindOptions<Document> } = {
45-
ttl: 60
71+
ttl: this.defaultTTL
4672
}
4773
): Promise<TSchema[]> {
4874
const cacheKey = this.getCacheKey("find", fields, options),
@@ -54,7 +80,7 @@ export default class MongoDataSource<TSchema extends Document = Document>
5480
const r = await this.collection
5581
.find(fields, options.findOptions)
5682
.toArray();
57-
this.cache?.set(cacheKey, JSON.stringify(r), options);
83+
await this.cache?.set(cacheKey, JSON.stringify(r), options);
5884

5985
return r;
6086
});
@@ -65,7 +91,7 @@ export default class MongoDataSource<TSchema extends Document = Document>
6591
async findOne(
6692
fields: any = {},
6793
options: { ttl: number; findOptions?: FindOptions } = {
68-
ttl: 60
94+
ttl: this.defaultTTL
6995
}
7096
): Promise<TSchema | null> {
7197
const cacheKey = this.getCacheKey("findOne", fields, options),
@@ -75,7 +101,7 @@ export default class MongoDataSource<TSchema extends Document = Document>
75101

76102
const docs = await this.antiSpam(cacheKey, async () => {
77103
const r = await this.collection.findOne(fields, options.findOptions);
78-
this.cache?.set(cacheKey, JSON.stringify(r), options);
104+
await this.cache?.set(cacheKey, JSON.stringify(r), options);
79105

80106
return r;
81107
});
@@ -85,7 +111,7 @@ export default class MongoDataSource<TSchema extends Document = Document>
85111

86112
async delete(
87113
type: DataSourceOperation,
88-
fields: any,
114+
fields: any = {},
89115
options: { findOptions?: FindOptions } = {}
90116
) {
91117
return await this.cache?.delete(this.getCacheKey(type, fields, options));

0 commit comments

Comments
 (0)