The widget should manage two queues:
- People Queue: Tracks the number of people waiting for a taxi.
- Taxi Queue: Tracks the number of taxis available.
-
People Joining the Queue:
- Add 1 to the People Queue.
-
People Leaving the Queue:
- Remove 1 from the People Queue.
-
Taxis Joining the Queue:
- Add 1 to the Taxi Queue.
-
Taxis Leaving the Queue:
- Remove 1 from the Taxi Queue.
- Remove 12 people from the People Queue.
- A taxi can only leave if there are at least 12 people in the People Queue.
- Ensure that the app maintains the conditions for a taxi departure.
- Handle edge cases like attempting to remove a person or taxi when the respective queues are empty.
- Extend functionality as needed for a larger system.
-
Initialize NestJS Project:
- Created a new NestJS project using the Nest CLI.
- Installed necessary packages:
npm install @nestjs/config @nestjs/typeorm @nestjs/common
-
Configure Drizzle ORM:
- Installed Drizzle ORM:
npm install drizzle-orm drizzle-orm/pg
- Set up PostgreSQL as the database.
- Created schema definitions using
pgTablefor entities:rankTable,taxiRouteTable,queueTable, andqueueRouteTable.
- Installed Drizzle ORM:
-
Implemented Core Features:
- Added services for:
- Creating routes (
addRoutemethod). - Deleting related routes based on conditions.
- Fetching route details with relations using
leftJoin.
- Creating routes (
- Added services for:
- Cause: Misconfiguration of the Drizzle ORM query builder.
- Solution: Updated Drizzle ORM setup to correctly initialize relationships. Ensured proper usage of
leftJoinin query building.
- Cause: When re-creating a deleted route, the arrival route was also duplicated.
- Solution: Modified the
addRoutemethod to first check for existing routes using aselectquery before inserting new records.
-
Schema Definition:
- Created
pgTableentities for database tables (rank,route,queue,queue_route).
- Created
-
Relationships:
- Added foreign key references between tables (
fromRankId,toRankIdinroute, andqueueIdinqueue_route).
- Added foreign key references between tables (
-
Query Optimization:
- Wrote queries for filtering and joining tables:
- Fetch routes by
queueId. - Filter ranks not associated with specific routes.
- Fetch routes by
- Wrote queries for filtering and joining tables:
- Cause: Joins didn't fetch
queueIdfrom thequeue_routetable. - Solution: Updated query to include
queueIdin the selected fields usingleftJoin.
- Cause: Filtering logic for ranks not linked to routes was incorrect.
- Solution: Used
filterandsomein JavaScript to exclude ranks with matchingtoRankId.
-
Project Initialization:
- Created a React project using Vite.
- Installed Material-UI for components:
npm install @mui/material @emotion/react @emotion/styled
-
Avatar and Images:
- Placed static images in the
publicfolder and referenced them with relative paths.
- Placed static images in the
-
Stats Section:
- Designed a grid layout using Material-UI to display stats like passengers departed, taxis needed, passengers needed, and fare made.
-
Route Details Component:
- Created a component to display route details:
- From Rank
- To Rank
- Fare
- Additional analytics
- Created a component to display route details:
- Cause: Incorrect path used in the
srcattribute of theAvatarcomponent. - Solution: Updated the path to include
/public, or usedimport.meta.urlfor relative references:<Avatar src="/queue.png" alt="Queue" />
- Cause:
setQueueIdwas called inside the component body instead of withinuseEffect. - Solution: Moved
setQueueIdinto auseEffect:useEffect(() => { setQueueId(Number(id)); }, [id, setQueueId]);
- Cause: Incorrect use of the
sizeprop in Material-UI'sGrid. - Solution: Updated to use
xsandmdprops separately:<Grid item xs={12} md={3}>
-
Backend (NestJS + Drizzle ORM):
- Set up a CRUD API for managing routes and queues.
- Resolved issues with duplicate route creation and complex queries using joins and filters.
-
Frontend (React):
- Created a dashboard to display queue and route analytics.
- Resolved UI issues with static images and grid layouts.
-
Integrations:
- Successfully linked frontend stats with backend queries.
This approach provided a modular and scalable solution to manage and analyze taxi routes and queues effectively.
Drizzle ORM simplifies database management, and using npx drizzle-kit enhances it further by providing easy migration tools. This guide explains the commands you'll commonly use, their purpose, and when to use them.
First, install Drizzle ORM, the PostgreSQL adapter, and drizzle-kit CLI tools:
npm install drizzle-orm drizzle-orm/pg-core
npm install -D drizzle-kitSet up your database configuration and schema:
- Create a
db.tsfile to define your database connection:import { drizzle } from 'drizzle-orm/node-postgres'; import { Pool } from 'pg'; const pool = new Pool({ connectionString: process.env.DATABASE_URL, }); export const db = drizzle(pool);
- Define tables in separate files using
pgTable:import { pgTable, integer, text } from 'drizzle-orm/pg-core'; export const rankTable = pgTable("rank", { id: integer().primaryKey().generatedAlwaysAsIdentity(), rankName: text().notNull(), });
Create a .env file to specify your database connection string:
DATABASE_URL=your_database_connection_string
Run the following command to create the initial Drizzle configuration:
npx drizzle-kit generate:configThis will create a drizzle.config.ts file in your project.
Whenever you make schema changes (e.g., adding or modifying tables), you need to generate a migration file.
npx drizzle-kit generate- Compares your current schema files to the database.
- Generates a migration file in the
migrationsfolder containing the SQL changes required.
- After making changes to your
pgTabledefinitions.
Apply the generated migrations to your database.
npx drizzle-kit up- Executes the migration scripts in the
migrationsfolder. - Updates your database schema to match your Drizzle definitions.
- After running
npx drizzle-kit generateto generate migrations. - When deploying a project and setting up the database schema on a new server.
If a migration causes issues, you can roll it back.
npx drizzle-kit down- Reverts the most recent migration applied to the database.
- If a migration introduces errors or is unnecessary.
You can now use Drizzle ORM to interact with your database.
const routes = await db.select().from(rankTable);const newRank = await db.insert(rankTable).values({ rankName: 'Cape Town' }).returning();await db.update(rankTable).set({ rankName: 'New Name' }).where(eq(rankTable.id, 1));await db.delete(rankTable).where(eq(rankTable.id, 1));- Cause: You forgot to run
npx drizzle-kit generateafter schema updates. - Solution: Run
npx drizzle-kit generateto regenerate the migration file, then apply it withnpx drizzle-kit up.
- Cause: You manually modified the database schema without updating Drizzle's schema files.
- Solution: Always update schema files and regenerate migrations with
npx drizzle-kit generate.
- Define or update table schemas using
pgTable. - Run
npx drizzle-kit generateto generate migration files. - Apply migrations to the database with
npx drizzle-kit up. - Use Drizzle ORM methods to query and manipulate data.
- If needed, roll back problematic migrations using
npx drizzle-kit down.
With these commands, you can efficiently manage your database schema and data using Drizzle ORM!