Skip to content

Commit

Permalink
feat: implement updated ddb client api
Browse files Browse the repository at this point in the history
  • Loading branch information
ap0nia committed Feb 14, 2024
1 parent bcd40ef commit 2d84f88
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 29 deletions.
63 changes: 41 additions & 22 deletions apps/backend/src/db/ddb.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DynamoDBDocument } from '@aws-sdk/lib-dynamodb';
import { DynamoDB } from '@aws-sdk/client-dynamodb';

import { ScheduleSaveState } from '@packages/antalmanac-types';
import { ScheduleSaveState, User, UserSchema, AuthUser, AuthUserSchema } from '@packages/antalmanac-types';
import env from '../env';

// Initialise DynamoDB Client
Expand All @@ -20,30 +20,49 @@ const documentClient = DynamoDBDocument.from(client, {
},
});

const TABLENAME = env.USERDATA_TABLE_NAME;
class DDBClient<T extends Record<string, any>> {
private tableName: string;
private schema: any;

async function getById(id: string) {
const params = {
TableName: TABLENAME,
Key: {
id: id,
},
};
constructor(tableName: string, schema: any) {
this.tableName = tableName;
this.schema = schema;
}

const data = await documentClient.get(params);
return data.Item;
}
async get(id: string): Promise<T | undefined> {
const params = {
TableName: this.tableName,
Key: {
id: id,
},
};

const { Item } = await documentClient.get(params);
const { data, problems } = this.schema(Item);
return problems === undefined ? data : undefined;
}

async function insertById(id: string, userData: ScheduleSaveState) {
const params = {
TableName: TABLENAME,
Item: {
id: id,
userData: userData,
},
};
async insertItem(item: T) {
await documentClient.put({
TableName: this.tableName,
Item: item,
});
}

await documentClient.put(params);
async updateSchedule(id: string, schedule: ScheduleSaveState) {
const params = {
TableName: this.tableName,
Key: {
id: id,
},
UpdateExpression: 'set userData = :u',
ExpressionAttributeValues: {
':u': schedule,
},
};
await documentClient.update(params);
}
}

export { getById, insertById };
export const ScheduleCodeClient = new DDBClient<User>(env.USERDATA_TABLE_NAME, UserSchema);
export const AuthUserClient = new DDBClient<AuthUser>(env.AUTH_USERDATA_TABLE_NAME, AuthUserSchema);
10 changes: 3 additions & 7 deletions apps/backend/src/routers/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
ShortCourseSchema,
} from '@packages/antalmanac-types';
import { router, procedure } from '../trpc';
import { getById, insertById } from '../db/ddb';
import { ScheduleCodeClient } from '../db/ddb';
import LegacyUserModel from '../models/User';

import connectToMongoDB from '../db/mongodb';
Expand Down Expand Up @@ -55,19 +55,15 @@ async function getLegacyUserData(userId: string) {
}

async function getUserData(userId: string) {
const { data: userData, problems } = UserSchema(await getById(userId));
if (problems !== undefined) {
return undefined;
}
return userData;
return (await ScheduleCodeClient.get(userId))?.userData;
}

const usersRouter = router({
getUserData: procedure.input(type({ userId: 'string' }).assert).query(async ({ input }) => {
return (await getUserData(input.userId)) ?? (await getLegacyUserData(input.userId));
}),
saveUserData: procedure.input(UserSchema.assert).mutation(async ({ input }) => {
await insertById(input.id, input.userData);
await ScheduleCodeClient.insertItem(input);
}),
});

Expand Down

0 comments on commit 2d84f88

Please sign in to comment.