Database Tables
Images Table
Theimages table stores metadata about uploaded image files.
migrations/20250717082325_create_images_table.js
The
name field stores the UUID-based filename generated during upload. The unique constraint ensures no duplicate filenames.id- Auto-incrementing primary keyname- Unique filename (UUID + original name)created_at- Timestamp of record creationupdated_at- Timestamp of last update
Tags Table
Thetags table stores categorical labels that can be applied to items.
migrations/20250717082710_create_tags_table.js
id- Auto-incrementing primary keyname- Unique tag name
Items Table
Theitems table stores the main data entities with references to images.
migrations/20250717082805_create_items_table.js
id- Auto-incrementing primary keyname- Unique item namedescription- Optional text descriptionimage_id- Foreign key to images table (CASCADE on delete)created_at- Timestamp of record creationupdated_at- Timestamp of last update
Items to Tags Junction Table
Theitems_to_tags table implements a many-to-many relationship between items and tags.
migrations/20250717083056_create_items_to_tags_table.js
id- Auto-incrementing primary keyitem_id- Foreign key to items table (CASCADE on delete)tag_id- Foreign key to tags table (CASCADE on delete)
Database Relationships
The schema implements the following relationships:One-to-Many: Images to Items
Each image can be associated with multiple items, but each item references only one image. When an image is deleted, all associated items are automatically removed via CASCADE.
Database Functions
The API provides helper functions for interacting with the images table:src/db-functions.ts
Migration Execution Order
Migration Execution Order
Migrations must be run in the correct order due to foreign key dependencies:
20250717082325_create_images_table.js- Creates images table first20250717082710_create_tags_table.js- Creates tags table20250717082805_create_items_table.js- Creates items table (references images)20250717083056_create_items_to_tags_table.js- Creates junction table (references items and tags)
Timestamps
Theimages and items tables use Knex’s timestamps() method with two boolean parameters:
- First parameter (
true) - Use timestamps - Second parameter (
true) - UseCURRENT_TIMESTAMPas default
created_at and updated_at columns that are managed by the database.