Skip to content

Commit

Permalink
Merge pull request #13 from 9at8/types
Browse files Browse the repository at this point in the history
Add TypeScript declarations
  • Loading branch information
lorensr authored Mar 25, 2020
2 parents bfeb4c2 + 008b75e commit 36cc9e7
Show file tree
Hide file tree
Showing 3 changed files with 1,694 additions and 1,580 deletions.
45 changes: 45 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
declare module 'apollo-datasource-mongodb' {
import { DataSource } from 'apollo-datasource'
import { Collection as MongoCollection, ObjectId } from 'mongodb'
import {
Collection as MongooseCollection,
Document,
Model as MongooseModel
} from 'mongoose'

export type Collection<T> = T extends Document
? MongooseCollection
: MongoCollection<T>

export type Model<T> = T extends Document ? MongooseModel<T> : undefined

export type ModelOrCollection<T> = T extends Document
? Model<T>
: Collection<T>

export interface Options {
ttl: number
}

export class MongoDataSource<TData, TContext = any> extends DataSource<
TContext
> {
protected context: TContext
protected collection: Collection<TData>
protected model: Model<TData>

constructor(modelOrCollection: ModelOrCollection<TData>)

findOneById(
id: ObjectId,
options?: Options
): Promise<TData | null | undefined>

findManyByIds(
ids: ObjectId[],
options?: Options
): Promise<(TData | null | undefined)[]>

deleteFromCacheById(id: ObjectId): void
}
}
Loading

0 comments on commit 36cc9e7

Please sign in to comment.