Last active
May 3, 2022 11:41
-
-
Save tanveerprottoy/3bd3257c226210837cd0dc4ff727dedd to your computer and use it in GitHub Desktop.
app.repository.ts
This file contains hidden or 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
| import { Injectable } from "@nestjs/common"; | |
| import { CreateAppDto } from "./create-app.dto"; | |
| import { App } from "./data/entities/app.entity"; | |
| import { v4 as uuidv4 } from 'uuid'; | |
| import { DbDataOpsInstance } from "./libs/dynamodb"; | |
| import { DeleteCommandInput, GetCommandInput, PutCommandInput, ScanCommandInput, UpdateCommandInput } from "@aws-sdk/lib-dynamodb"; | |
| import { Constants } from "./constants"; | |
| @Injectable() | |
| export class AppRepository { | |
| async create(dto: CreateAppDto): Promise<App | null> { | |
| try { | |
| console.log(dto); | |
| const { name } = dto; | |
| const item = { | |
| id: uuidv4(), | |
| name | |
| } as App; | |
| const params: PutCommandInput = { | |
| TableName: "Apps", | |
| Item: item | |
| }; | |
| const data = await DbDataOpsInstance.put(params); | |
| console.log(data); | |
| if(data.$metadata.httpStatusCode == Constants.HTTP_200) { | |
| return item; | |
| } | |
| return null; | |
| } | |
| catch(e) { | |
| console.error(e); | |
| return null; | |
| } | |
| } | |
| async findAll(): Promise<App[]> { | |
| try { | |
| const params: ScanCommandInput = { | |
| TableName: "Apps", | |
| Limit: 10 | |
| }; | |
| const data = await DbDataOpsInstance.scan(params); | |
| console.log(data); | |
| return data.Items; | |
| } | |
| catch(e) { | |
| console.error(e); | |
| return []; | |
| } | |
| } | |
| async findOne(id: string): Promise<App | null> { | |
| try { | |
| const params: GetCommandInput = { | |
| TableName: "Apps", | |
| Key: { | |
| id: id | |
| } | |
| } | |
| const data = await DbDataOpsInstance.get(params); | |
| return data.Item; | |
| } | |
| catch(e) { | |
| console.error(e); | |
| return null; | |
| } | |
| } | |
| async update(id: string): Promise<App | null> { | |
| try { | |
| const params: UpdateCommandInput = { | |
| TableName: "Apps", | |
| Key: { | |
| id: id | |
| } | |
| } | |
| const data = await DbDataOpsInstance.update(params); | |
| return data.Item; | |
| } | |
| catch(e) { | |
| console.error(e); | |
| return null; | |
| } | |
| } | |
| async delete(id: string): Promise<App | null> { | |
| try { | |
| const params: DeleteCommandInput = { | |
| TableName: "Apps", | |
| Key: { | |
| id: id | |
| } | |
| }; | |
| const data = await DbDataOpsInstance.delete(params); | |
| return data.Item; | |
| } | |
| catch(e) { | |
| console.error(e); | |
| return null; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment