Skip to content

Commit

Permalink
Merge pull request #453 from claushaas/dev
Browse files Browse the repository at this point in the history
generate dynamo db client on constructor
  • Loading branch information
claushaas authored Jan 10, 2025
2 parents d8134c9 + 8faa2ca commit be303aa
Show file tree
Hide file tree
Showing 3 changed files with 218 additions and 216 deletions.
27 changes: 14 additions & 13 deletions app/services/migration.service.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {memoryCache} from '~/cache/memory-cache';
export class MigrationService {
private readonly _model: PrismaClient;
private readonly _awsClient: CognitoIdentityProviderClient;
private readonly _dynamoClient: DynamoDBDocument;

constructor(
model: PrismaClient = database,
Expand All @@ -23,29 +24,29 @@ export class MigrationService {
) {
this._awsClient = awsClient;
this._model = model;

const client = new DynamoDB({
region: process.env.AWS_REGION ?? 'us-east-1',
credentials: fromEnv(),
requestHandler: {
requestTimeout: 60_000,
keepAlive: true,
},
});
this._dynamoClient = DynamoDBDocument.from(client);
}

public async migrateSavedAndFavoritedLessonsForUsers(users: UserType[]): Promise<TServiceReturn<string>> {
try {
const client = new DynamoDB({
region: process.env.AWS_REGION ?? 'us-east-1',
credentials: fromEnv(),
requestHandler: {
requestTimeout: 60_000,
keepAlive: true,
},
});
const ddbDocumentClient = DynamoDBDocument.from(client);

users.forEach(async user => { // eslint-disable-line unicorn/no-array-for-each
const userId = user.Attributes?.find(attribute => attribute.Name === 'custom:id')?.Value ?? user.Attributes!.find(attribute => attribute.Name === 'sub')!.Value!;
const newUserId = user.Attributes!.find(attribute => attribute.Name === 'sub')!.Value!;
logger.logDebug(`User: ${userId} - New User Id: ${newUserId}`);

const {data: favoritedLessons} = await this.getFavoritedLessonsForUser(userId, ddbDocumentClient);
const {data: favoritedLessons} = await this.getFavoritedLessonsForUser(userId, this._dynamoClient);
logger.logDebug(`User: ${userId} - Favorited Lessons: ${favoritedLessons?.length}`);

const {data: completedLessons} = await this.getCompletedLessonsForUser(userId, ddbDocumentClient);
const {data: completedLessons} = await this.getCompletedLessonsForUser(userId, this._dynamoClient);
logger.logDebug(`User: ${userId} - Completed Lessons: ${completedLessons?.length}`);

if (favoritedLessons && favoritedLessons.length > 0) {
Expand Down Expand Up @@ -148,7 +149,7 @@ export class MigrationService {
await this.migrateSavedAndFavoritedLessonsForUsers(result.Users);
}

if (result.PaginationToken && pages < 20) {
if (result.PaginationToken && pages < 150) {
await addUsersToList(result.PaginationToken, pages + 1);
}
} catch (error) {
Expand Down
Loading

0 comments on commit be303aa

Please sign in to comment.