-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from preetjdp/Implement-Dataloader
Implement dataloader
- Loading branch information
Showing
12 changed files
with
138 additions
and
60 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import Container, { Service } from "typedi"; | ||
import DataLoader from "dataloader" | ||
import { firestore } from "../../../db/firebase"; | ||
import { FieldPath, DocumentSnapshot, DocumentData } from "@google-cloud/firestore"; | ||
import { createParamDecorator } from "type-graphql"; | ||
import { ApplicationContext } from "../../../utils/appContext"; | ||
|
||
@Service() | ||
export class UserDataLoader extends DataLoader<string, DocumentSnapshot | undefined> { | ||
constructor() { | ||
super(async (ids) => { | ||
console.log("Asked For ID's ", ids) | ||
const usersPromise: Promise<DocumentSnapshot<DocumentData>>[] = ids.map(async (id) => { | ||
return await firestore.collection('users').doc(id).get() | ||
}) | ||
const users = await Promise.all(usersPromise) | ||
console.log("Dataloader Response", users.length) | ||
return ids.map((id) => users.find((user) => user.id === id)); | ||
}); | ||
} | ||
} | ||
|
||
export function RequestContainer(): ParameterDecorator { | ||
return function (target: Object, propertyName: string | symbol, index: number) { | ||
return createParamDecorator<ApplicationContext>(({ context }) => { | ||
const paramtypes = Reflect.getMetadata('design:paramtypes', target, propertyName); | ||
const requestContainer = Container.of(context.requestId); | ||
return requestContainer.get(paramtypes[index]); | ||
})(target, propertyName, index); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { DocumentSnapshot, Timestamp } from "@google-cloud/firestore"; | ||
import { User } from "../../../models/User"; | ||
|
||
type userSnapshotMapType = (snapshot: DocumentSnapshot) => User; | ||
|
||
export const mapUserSnapshot: userSnapshotMapType = snapshot => { | ||
const userData = snapshot.data() | ||
if (!snapshot.exists) { | ||
throw Error("User Does Not Exist") | ||
} | ||
const created: Timestamp = userData!.created | ||
return { | ||
id: snapshot.id, | ||
name: userData!.name, | ||
image: userData!.image, | ||
mobileNo: userData!.mobile_no, | ||
fcmToken: userData!.fcm_token, | ||
created: created.toDate() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ import { Request } from 'express' | |
|
||
export interface ApplicationContext { | ||
req: Request; | ||
requestId: number | ||
} |