Overview
The database package (@skyteam/database) provides functions for querying and manipulating data. All operations use Drizzle ORM.
User Operations
Location:packages/database/src/users.ts
fetchUser
Fetches a user by their ROBLOX user ID.userId- ROBLOX user ID
createUser
Creates a new user in the database.data.userId- ROBLOX user IDdata.username- ROBLOX usernamedata.displayName- Display namedata.avatarUrl- Optional avatar URL
incrementMiles
Increases a user’s miles balance.userId- ROBLOX user IDamount- Miles to add (default: 1)tx- Optional transaction context
spendMiles
Deducts miles from a user’s balance with validation.userId- ROBLOX user IDamount- Miles to spend (must be positive)note- Optional transaction note
"User not found"- User doesn’t exist"Insufficient miles"- User has fewer miles than amount"amount must be positive"- Invalid amount
- Atomic transaction - either both miles deduction and transaction record succeed, or neither
- Automatically creates a miles transaction record
Flight Operations
Location:packages/database/src/flights.ts
fetchFlight
Fetches a single flight by ID.fetchFlightsByAirline
Fetches all flights for an airline.fetchComingFlights
Fetches upcoming flights (whereendTime is null).
startTime
createFlight
Creates a new flight.startFlight
Marks a flight as started by settingstartedAt.
endFlight
Marks a flight as ended by settingendTime.
fetchUserFlights
Fetches all flights a user has participated in.miles field showing miles earned
Implementation:
- Joins
flightsandflightPassengerstables - Filters by
userId
Airline Operations
Location:packages/database/src/airlines.ts
fetchAirline
Fetches an airline by ID.fetchAllAirlines
Fetches all airlines.fetchAirlineByToken
Fetches an airline by API token (used for authentication).createAirline
Creates a new airline with auto-generated token.- Automatically generates a CUID2 token
fetchMilesProducts
Fetches all products for an airline.createMilesProduct
Creates a new miles product.- Automatically generates a 6-character alphanumeric product ID
- Uses nanoid with custom alphabet:
0123456789abcdefghijklmnopqrstuvwxyz
Brand Operations
Location:packages/database/src/brands.ts
fetchAllBrands
Fetches all brands.fetchBrand
Fetches a brand by ID.fetchBrandByIATA
Fetches a brand by IATA code.fetchAirlineBrands
Fetches all brands for an airline.fetchPrimaryBrand
Fetches the primary brand for an airline.createBrand
Creates a new brand.Flight Passenger Operations
Location:packages/database/src/flightPassengers.ts
addPassengerToFlight
Adds a passenger to a flight and awards miles.- Atomic transaction
- Creates passenger record
- Increments user’s miles
- Creates miles transaction record
fetchFlightPassengers
Fetches all passengers for a flight.Miles Transaction Operations
Location:packages/database/src/miles.ts
addMilesTransaction
Logs a miles transaction.data.userId- User IDdata.amount- Miles amount (positive for earn, negative for spend)data.type- “earn” or “spend”data.source- Transaction sourcedata.flightId- Optional flight IDdata.productId- Optional product IDdata.note- Optional descriptiontx- Optional transaction context
fetchMilesTransactions
Fetches all transactions for a user.Utility Functions
Location:packages/database/src/utils.ts
increment
Helper for incrementing numeric columns in SQL.Transaction Support
Many functions accept an optionaltx parameter for running operations within a database transaction: