-
Notifications
You must be signed in to change notification settings - Fork 0
/
seed.mjs
85 lines (72 loc) · 3.2 KB
/
seed.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { DynamoDB } from "@aws-sdk/client-dynamodb";
import { faker } from '@faker-js/faker';
import 'dotenv/config';
import './load-env.mjs';
// configures AWS DynamoDB
const dynamoDb = new DynamoDB({
region: process.env.AWS_REGION,
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
}
});
console.log(process.env.AWS_REGION);
// generates new reservations
function generateNewReservations(count) {
const data = [];
// should update this to take from customer table, faker uses count/4 to ensure multiple reservations per member for analytics
const memberIds = faker.helpers.multiple(() => faker.string.alphanumeric({ length: 8, casing: 'upper'}), { count: count/4 });
const transactionDates = faker.helpers.multiple(() => faker.date.past({ years: 3 }), { count: count });
// generates flight dates within 1 year after transaction date
const flightDates = [];
for (let i = 0; i < count; i++) {
flightDates.push(
faker.date.soon({ days: 365, refDate: transactionDates[i] })
)
};
console.log('Generating ', count, ' new reservations');
for (let i = 0; i <count; i++) {
data.push({
RecordLocator: faker.airline.recordLocator({ allowNumerics: true }),
ActivityDateTime: transactionDates[i],
MemberId: faker.helpers.arrayElement(memberIds),
OriginAirport: faker.airline.airport().iataCode,
DestinationAirport: faker.airline.airport().iataCode,
Seat: faker.airline.seat(),
FlightNumber: faker.airline.flightNumber(),
FlightDate: flightDates[i],
TotalFare: faker.finance.amount({ min: 99, max: 5000 })
});
};
return data;
}
// writes reservations to DynamoDB
async function addReservationsToDynamo(data) {
const count = data.length;
console.log('Adding ', count, 'reservations to DynamoDB');
for (let i = 0; i < count; i++) {
// Convert each property to the appropriate DynamoDB AttributeValue type
const dynamoDbItem = {
RecordLocator: { S: data[i].RecordLocator },
ActivityDateTime: { S: data[i].ActivityDateTime.toISOString() }, // Convert Date to ISO string
MemberId: { S: data[i].MemberId },
OriginAirport: { S: data[i].OriginAirport },
DestinationAirport: { S: data[i].DestinationAirport },
Seat: { S: data[i].Seat },
FlightNumber: { S: data[i].FlightNumber },
FlightDate: { S: data[i].FlightDate.toISOString() }, // Convert Date to ISO string
TotalFare: { N: data[i].TotalFare.toString() } // Convert number to string
};
console.log(dynamoDbItem);
await dynamoDb.putItem({ TableName: 'AirlineReservation', Item: dynamoDbItem});
console.log(`Added reservation ${dynamoDbItem.RecordLocator.S}`);
}
}
// seeds database
async function seedDatabase(count) {
const reservations = await generateNewReservations(count);
console.log(reservations);
await addReservationsToDynamo(reservations);
console.log('Added ', reservations.length, ' reservation to DynamoDB.')
};
seedDatabase(50);