-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: seed a bigger variety of flights
- Loading branch information
Showing
9 changed files
with
99 additions
and
54 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,56 @@ | ||
import { format } from 'date-fns'; | ||
import { Sync as Factory } from 'factory.ts'; | ||
import { Kysely } from 'kysely'; | ||
|
||
import { createFlightPrimitive } from '../../src/lib/db/queries'; | ||
import { AIRPORTS } from '../../src/lib/data/airports'; | ||
import { createManyFlightsPrimitive } from '../../src/lib/db/queries'; | ||
import type { DB } from '../../src/lib/db/schema'; | ||
import type { CreateFlight } from '../../src/lib/db/types'; | ||
import { estimateFlightDuration } from '../../src/lib/utils/datetime'; | ||
import { distanceBetween } from '../../src/lib/utils/distance'; | ||
|
||
const routes = [ | ||
{ from: 'EGLL', to: 'KJFK' }, // London-Heathrow to New York-JFK | ||
{ from: 'KMIA', to: 'OMDB' }, // Miami to Dubai | ||
{ from: 'HECA', to: 'OEJN' }, // Cairo to Jeddah | ||
{ from: 'VHHH', to: 'RCTP' }, // Hong Kong to Taipei-Taoyuan | ||
{ from: 'OMDB', to: 'OERK' }, // Dubai to Riyadh | ||
{ from: 'VTBS', to: 'RKSI' }, // Bangkok-Suvarnabhumi to Seoul-Incheon | ||
]; | ||
|
||
const baseFlightFactory = Factory.makeFactory<CreateFlight>({ | ||
seats: [{ userId: 'PLACEHOLDER' }], | ||
// @ts-expect-error - TS doesn't understand the modulo operation | ||
from: Factory.each((i) => routes[i % routes.length].from), | ||
// @ts-expect-error - TS doesn't understand the modulo operation | ||
to: Factory.each((i) => routes[i % routes.length].to), | ||
date: Factory.each(() => | ||
format( | ||
new Date(Date.now() - Math.random() * 10 * 365 * 24 * 60 * 60 * 1000), // Random date within the last 10 years | ||
'yyyy-MM-dd', | ||
), | ||
), | ||
}).withDerivation2(['from', 'to'], 'duration', (fromIcao, toIcao) => { | ||
const from = AIRPORTS.find((a) => a.ICAO === fromIcao); | ||
const to = AIRPORTS.find((a) => a.ICAO === toIcao); | ||
return from && to | ||
? estimateFlightDuration( | ||
distanceBetween([from.lon, from.lat], [to.lon, to.lat]) / 1000, | ||
) | ||
: 0; | ||
}); | ||
|
||
export const seedFlight = async (db: Kysely<DB>, userId: string) => { | ||
await createFlightPrimitive(db, { | ||
const flights = baseFlightFactory.buildList(10, { | ||
seats: [ | ||
{ userId, seat: 'window', seatNumber: '11F', seatClass: 'economy' }, | ||
{ | ||
userId, | ||
seat: 'window', | ||
seatNumber: '11F', | ||
seatClass: 'economy', | ||
}, | ||
], | ||
from: 'EKCH', | ||
to: 'ESSA', | ||
date: format(new Date(), 'yyyy-MM-dd'), | ||
duration: 70 * 60, | ||
}); | ||
|
||
await createManyFlightsPrimitive(db, flights); | ||
}; |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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