Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[refactor] aws-sdk v3로 마이그레이션 #345

Merged
merged 6 commits into from
Sep 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion packages/server/dependencies/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"version": "0.0.1",
"type": "module",
"dependencies": {
"aws-sdk": "^2.1454.0",
"jsonwebtoken": "^9.0.2"
}
}
3 changes: 2 additions & 1 deletion packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@
"dryrun": "sam deploy --guided --no-execute-changeset"
},
"devDependencies": {
"@aws-sdk/client-dynamodb": "^3.414.0",
"@aws-sdk/types": "^3.413.0",
"@types/aws-lambda": "^8.10.119",
"@types/jsonwebtoken": "^9.0.2",
"@types/lockerweb": "link:..\\types",
"@typescript-eslint/eslint-plugin": "^6.6.0",
"@typescript-eslint/parser": "^6.6.0",
"aws-sdk": "^2.1454.0",
"chai": "^4.3.8",
"cross-env": "^7.0.3",
"eslint": "^8.49.0",
Expand Down
23 changes: 16 additions & 7 deletions packages/server/src/auth/data.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import type { GetItemInput, UpdateItemInput, UpdateItemOutput } from 'aws-sdk/clients/dynamodb.ts';
import {
UpdateItemCommand,
type GetItemInput,
type UpdateItemInput,
type UpdateItemOutput,
GetItemCommand,
ConditionalCheckFailedException,
} from '@aws-sdk/client-dynamodb';
import { ForbiddenError, UnauthorizedError } from '../util/error.js';
import { adminId, dynamoDB, TableName } from '../util/database.js';
import type { AWSError } from 'aws-sdk';

/* ISSUE/REVOKE TOKEN */

Expand All @@ -22,11 +28,12 @@ export const revokeToken = async function (
},
ReturnValues: 'UPDATED_OLD',
};
const cmd = new UpdateItemCommand(req);
let res: UpdateItemOutput;
try {
res = await dynamoDB.updateItem(req).promise();
res = await dynamoDB.send(cmd);
} catch (e) {
if ((e as AWSError).name === 'ConditionalCheckFailedException') {
if (e instanceof ConditionalCheckFailedException) {
throw new ForbiddenError('Cannot logout when token is invalid');
}
throw e;
Expand Down Expand Up @@ -60,11 +67,12 @@ export const issueToken = async function (
},
ReturnValues: 'ALL_NEW',
};
const cmd = new UpdateItemCommand(req);
let res: UpdateItemOutput;
try {
res = await dynamoDB.updateItem(req).promise();
res = await dynamoDB.send(cmd);
} catch (e) {
if ((e as AWSError).name === 'ConditionalCheckFailedException') {
if (e instanceof ConditionalCheckFailedException) {
throw new ForbiddenError('This user cannot login to service');
}
throw e;
Expand All @@ -90,7 +98,8 @@ export async function assertAccessible(
},
},
};
const authRes = await dynamoDB.getItem(authReq).promise();
const authCmd = new GetItemCommand(authReq);
const authRes = await dynamoDB.send(authCmd);
if (
authRes.Item.id.S !== `${id}` ||
authRes.Item.aT?.S !== token ||
Expand Down
59 changes: 32 additions & 27 deletions packages/server/src/config/data.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
// noinspection JSUnusedLocalSymbols

import type {
DeleteItemInput,
ExpressionAttributeNameMap,
ExpressionAttributeValueMap,
GetItemInput,
GetItemOutput,
QueryInput,
QueryOutput,
UpdateItemInput,
} from 'aws-sdk/clients/dynamodb.ts';
import {
QueryCommand,
type AttributeValue,
type DeleteItemInput,
type GetItemInput,
type GetItemOutput,
type QueryInput,
type QueryOutput,
type UpdateItemInput,
GetItemCommand,
UpdateItemCommand,
DeleteItemCommand,
ConditionalCheckFailedException,
} from '@aws-sdk/client-dynamodb';
import { dynamoDB, TableName } from '../util/database.js';
import { NotFoundError } from '../util/error.js';
import type { AWSError } from 'aws-sdk';

function fromLockerSubsectionData(data: LockerSubsectionData): LockerSubsection {
return {
Expand Down Expand Up @@ -178,17 +181,16 @@ export const queryConfig = async function (startsWith = ''): Promise<Array<Confi
let res: QueryOutput;
do {
try {
res = await dynamoDB
.query({
...req,
...(res &&
res.LastEvaluatedKey && {
ExclusiveStartKey: res.LastEvaluatedKey,
}),
})
.promise();
const cmd = new QueryCommand({
...req,
...(res &&
res.LastEvaluatedKey && {
ExclusiveStartKey: res.LastEvaluatedKey,
}),
});
res = await dynamoDB.send(cmd);
} catch (e) {
if ((e as AWSError).name === 'ConditionalCheckFailedException') {
if (e instanceof ConditionalCheckFailedException) {
throw new NotFoundError('Cannot find config');
}
throw e;
Expand All @@ -215,9 +217,10 @@ export const getConfig = async function (id: string): Promise<Config> {
};
let res: GetItemOutput;
try {
res = await dynamoDB.getItem(req).promise();
const cmd = new GetItemCommand(req);
res = await dynamoDB.send(cmd);
} catch (e) {
if ((e as AWSError).name === 'ConditionalCheckFailedException') {
if (e instanceof ConditionalCheckFailedException) {
throw new NotFoundError(`Cannot find config of id ${id}`);
}
throw e;
Expand All @@ -232,8 +235,8 @@ export const getConfig = async function (id: string): Promise<Config> {
};

export const updateConfig = async function (config: ConfigUpdateRequest) {
const attributes: ExpressionAttributeValueMap = {};
const attributeNames: ExpressionAttributeNameMap = {};
const attributes: Record<string, AttributeValue> = {};
const attributeNames: Record<string, string> = {};
let updateExp = '';
let removeExp = '';
if (config.name) {
Expand Down Expand Up @@ -298,7 +301,8 @@ export const updateConfig = async function (config: ConfigUpdateRequest) {
ExpressionAttributeValues: attributes,
}),
};
await dynamoDB.updateItem(req).promise();
const cmd = new UpdateItemCommand(req);
await dynamoDB.send(cmd);
return config;
};

Expand All @@ -310,6 +314,7 @@ export const deleteConfig = async function (id: string): Promise<string> {
id: { S: id },
},
};
await dynamoDB.deleteItem(req).promise();
const cmd = new DeleteItemCommand(req);
await dynamoDB.send(cmd);
return id;
};
40 changes: 23 additions & 17 deletions packages/server/src/locker/data.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import type {
ExpressionAttributeValueMap,
QueryInput,
QueryOutput,
UpdateItemInput,
UpdateItemOutput,
} from 'aws-sdk/clients/dynamodb.ts';
import {
QueryCommand,
type AttributeValue,
type QueryInput,
type QueryOutput,
type UpdateItemInput,
type UpdateItemOutput,
UpdateItemCommand,
ConditionalCheckFailedException,
} from '@aws-sdk/client-dynamodb';
import { adminId, dynamoDB, TableName } from '../util/database.js';
import { BlockedError, CantClaimError, CantUnclaimError, NotFoundError } from '../util/error.js';
import type { AWSError } from 'aws-sdk';

export const claimLocker = async function (
id: string,
Expand All @@ -34,9 +36,10 @@ export const claimLocker = async function (
':lockerId': { S: lockerId },
},
};
const checkRes = await dynamoDB.query(checkReq).promise();
const checkCmd = new QueryCommand(checkReq);
const checkRes = await dynamoDB.send(checkCmd);
if (checkRes.Count > 0) throw new CantClaimError('Requested locker is already claimed');
let conditionValues: ExpressionAttributeValueMap = {};
let conditionValues: Record<string, AttributeValue> = {};
const blockDeptCondition = blockedDepartments.map((d) => `NOT d = :${d}`).join(' AND ');
let condition = '';
if (isServiceBlocked) {
Expand Down Expand Up @@ -68,9 +71,10 @@ export const claimLocker = async function (
};
let res: UpdateItemOutput;
try {
res = await dynamoDB.updateItem(req).promise();
const cmd = new UpdateItemCommand(req);
res = await dynamoDB.send(cmd);
} catch (e) {
if ((e as AWSError).name === 'ConditionalCheckFailedException') {
if (e instanceof ConditionalCheckFailedException) {
throw new BlockedError();
}
throw e;
Expand All @@ -96,7 +100,7 @@ export const unclaimLocker = async function (
blockedDepartments: Array<string>,
isServiceBlocked: boolean,
): Promise<UnclaimLockerResponse> {
let conditionValues: ExpressionAttributeValueMap = {};
let conditionValues: Record<string, AttributeValue> = {};
const blockDeptCondition = blockedDepartments.map((d) => `NOT d = :${d}`).join(' AND ');
let condition = '';
if (isServiceBlocked) {
Expand Down Expand Up @@ -126,9 +130,10 @@ export const unclaimLocker = async function (
};
let res: UpdateItemOutput;
try {
res = await dynamoDB.updateItem(req).promise();
const cmd = new UpdateItemCommand(req);
res = await dynamoDB.send(cmd);
} catch (e) {
if ((e as AWSError).name === 'ConditionalCheckFailedException') {
if (e instanceof ConditionalCheckFailedException) {
throw new BlockedError();
}
throw e;
Expand Down Expand Up @@ -168,9 +173,10 @@ export const queryLockers = async function (
};
let res: QueryOutput;
try {
res = await dynamoDB.query(req).promise();
const cmd = new QueryCommand(req);
res = await dynamoDB.send(cmd);
} catch (e) {
if ((e as AWSError).name === 'ConditionalCheckFailedException') {
if (e instanceof ConditionalCheckFailedException) {
throw new NotFoundError('Cannot find lockers');
}
throw e;
Expand Down
67 changes: 38 additions & 29 deletions packages/server/src/user/data.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
/* USER CRUD */

import type {
BatchWriteItemInput,
DeleteItemInput,
ExpressionAttributeValueMap,
GetItemInput,
GetItemOutput,
QueryInput,
QueryOutput,
UpdateItemInput,
WriteRequest,
} from 'aws-sdk/clients/dynamodb.ts';
import {
type BatchWriteItemInput,
type DeleteItemInput,
type AttributeValue,
type GetItemInput,
type GetItemOutput,
type QueryInput,
type QueryOutput,
type UpdateItemInput,
type WriteRequest,
GetItemCommand,
QueryCommand,
UpdateItemCommand,
DeleteItemCommand,
BatchWriteItemCommand,
ConditionalCheckFailedException,
} from '@aws-sdk/client-dynamodb';
import { dynamoDB, TableName } from '../util/database.js';
import { InternalError, NotFoundError } from '../util/error.js';
import type { AWSError } from 'aws-sdk';

export const fromUserDao = (dao: UserDao): User => ({
id: dao.id.S,
Expand Down Expand Up @@ -54,9 +59,10 @@ export const getUser = async function (id: string): Promise<User> {
};
let res: GetItemOutput;
try {
res = await dynamoDB.getItem(req).promise();
const cmd = new GetItemCommand(req);
res = await dynamoDB.send(cmd);
} catch (e) {
if ((e as AWSError).name === 'ConditionalCheckFailedException') {
if (e instanceof ConditionalCheckFailedException) {
throw new NotFoundError(`Cannot find user info of id ${id}`);
}
throw e;
Expand Down Expand Up @@ -85,17 +91,16 @@ export const queryUser = async function (startsWith: string): Promise<Array<User
let res: QueryOutput;
do {
try {
res = await dynamoDB
.query({
...req,
...(res &&
res.LastEvaluatedKey && {
ExclusiveStartKey: res.LastEvaluatedKey,
}),
})
.promise();
const cmd = new QueryCommand({
...req,
...(res &&
res.LastEvaluatedKey && {
ExclusiveStartKey: res.LastEvaluatedKey,
}),
});
res = await dynamoDB.send(cmd);
} catch (e) {
if ((e as AWSError).name === 'ConditionalCheckFailedException') {
if (e instanceof ConditionalCheckFailedException) {
throw new NotFoundError('Cannot find user info');
}
throw e;
Expand All @@ -109,7 +114,7 @@ export const queryUser = async function (startsWith: string): Promise<Array<User
};

export const updateUser = async function (info: UserUpdateRequest): Promise<UserUpdateRequest> {
const attributes: ExpressionAttributeValueMap = {};
const attributes: Record<string, AttributeValue> = {};
let updateExp = '';
let removeExp = '';
if (info.name) {
Expand Down Expand Up @@ -149,7 +154,8 @@ export const updateUser = async function (info: UserUpdateRequest): Promise<User
ExpressionAttributeValues: attributes,
}),
};
await dynamoDB.updateItem(req).promise();
const cmd = new UpdateItemCommand(req);
await dynamoDB.send(cmd);
return info;
};

Expand All @@ -161,7 +167,8 @@ export const deleteUser = async function (id: string): Promise<string> {
id: { S: id },
},
};
await dynamoDB.deleteItem(req).promise();
const cmd = new DeleteItemCommand(req);
await dynamoDB.send(cmd);
return id;
};
export const batchPutUser = async function (infos: Array<User>): Promise<Array<User>> {
Expand All @@ -178,7 +185,8 @@ export const batchPutUser = async function (infos: Array<User>): Promise<Array<U
RequestItems: {},
};
req.RequestItems[TableName] = requests;
await dynamoDB.batchWriteItem(req).promise();
const cmd = new BatchWriteItemCommand(req);
await dynamoDB.send(cmd);
return infos;
};

Expand All @@ -201,6 +209,7 @@ export const batchDeleteUser = async function (ids: Array<string>): Promise<Arra
RequestItems: {},
};
req.RequestItems[TableName] = requests;
await dynamoDB.batchWriteItem(req).promise();
const cmd = new BatchWriteItemCommand(req);
await dynamoDB.send(cmd);
return ids;
};
Loading
Loading