|
1 | | -import { DataSource } from "apollo-datasource"; |
2 | | -import { KeyValueCache } from "apollo-server-caching"; |
3 | | -import { Collection, Document, FindOptions } from "mongodb"; |
4 | | -declare type DataSourceOperation = "findOne" | "find" | "count"; |
5 | | -export default class MongoDataSource<TSchema extends Document = Document> implements DataSource { |
6 | | - /** |
7 | | - * MongoDB collection for the data source. |
8 | | - */ |
9 | | - 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 | | - */ |
18 | | - cachePrefix: string; |
19 | | - private pendingResults; |
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 | | - }); |
43 | | - initialize({ cache }: { |
44 | | - cache: KeyValueCache; |
45 | | - }): void; |
46 | | - count(query?: {}, options?: { |
47 | | - ttl: number; |
48 | | - }): Promise<any>; |
49 | | - find(fields?: any, options?: { |
50 | | - ttl: number; |
51 | | - findOptions?: FindOptions<Document>; |
52 | | - }): Promise<TSchema[]>; |
53 | | - findOne(fields?: any, options?: { |
54 | | - ttl: number; |
55 | | - findOptions?: FindOptions; |
56 | | - }): Promise<TSchema | null>; |
57 | | - delete(type: DataSourceOperation, fields?: any, options?: { |
58 | | - findOptions?: FindOptions; |
59 | | - }): Promise<boolean | void | undefined>; |
60 | | - private getCacheKey; |
61 | | - private antiSpam; |
62 | | -} |
63 | | -export {}; |
| 1 | +import { DataSource } from "apollo-datasource"; |
| 2 | +import { KeyValueCache } from "apollo-server-caching"; |
| 3 | +import { Collection, Document, FindOptions } from "mongodb"; |
| 4 | +declare type DataSourceOperation = "findOne" | "find" | "count"; |
| 5 | +export default class MongoDataSource<TSchema extends Document = Document> implements DataSource { |
| 6 | + /** |
| 7 | + * MongoDB collection for the data source. |
| 8 | + */ |
| 9 | + 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 | + */ |
| 18 | + cachePrefix: string; |
| 19 | + private pendingResults; |
| 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 | + }); |
| 43 | + initialize({ cache }: { |
| 44 | + cache: KeyValueCache; |
| 45 | + }): void; |
| 46 | + count(query?: {}, options?: { |
| 47 | + ttl: number; |
| 48 | + }): Promise<any>; |
| 49 | + find(fields?: any, options?: { |
| 50 | + ttl: number; |
| 51 | + findOptions?: FindOptions<TSchema>; |
| 52 | + }): Promise<TSchema[]>; |
| 53 | + findOne(fields?: any, options?: { |
| 54 | + ttl: number; |
| 55 | + findOptions?: FindOptions<TSchema>; |
| 56 | + }): Promise<TSchema | null>; |
| 57 | + delete(type: DataSourceOperation, fields?: any, options?: { |
| 58 | + findOptions?: FindOptions<TSchema>; |
| 59 | + }): Promise<boolean | void | undefined>; |
| 60 | + private getCacheKey; |
| 61 | + private antiSpam; |
| 62 | +} |
| 63 | +export {}; |
0 commit comments