PlusMagi's Blog By Pitt Phunsanit Backend,Database Astro: Project Setup & Directory Structure

Astro: Project Setup & Directory Structure

As a Senior Architect, designing a robust and scalable data infrastructure is paramount. When utilizing PostgreSQL, the concept of ‘project setup’ extends beyond mere file organization; it encompasses the logical structuring of your database schemas, modules, and data relationships. A well-defined directory structure in the database ensures maintainability, security, and performance for any complex application.

Understanding PostgreSQL Project Architecture

Unlike file-system projects, database projects require logical segmentation. PostgreSQL provides powerful features like schemas and extensions to mimic a clean directory structure. We recommend adopting a modular approach where each major application component or domain (e.g., ‘users’, ‘inventory’, ‘billing’) resides in its own dedicated schema. This isolation prevents naming conflicts and simplifies deployment.

Schema Isolation: The Database Directory

The primary method for achieving structural separation is the use of schemas. Instead of placing all tables into the default ‘public’ schema, creating dedicated schemas acts as logical containers, much like separate directories for different project modules.

SQL

-- 1. Create the main schemas (modules) for the project structure
CREATE SCHEMA IF NOT EXISTS app_users;
CREATE SCHEMA IF NOT EXISTS app_inventory;
CREATE SCHEMA IF NOT EXISTS app_billing;

-- 2. Create tables within their respective schemas
CREATE TABLE app_users.profiles (
    user_id SERIAL PRIMARY KEY,
    username VARCHAR(100) UNIQUE NOT NULL,
    created_at TIMESTAMP DEFAULT NOW()
);

CREATE TABLE app_inventory.products (
    product_id SERIAL PRIMARY KEY,
    product_name VARCHAR(255) NOT NULL,
    stock_count INTEGER
);

CREATE TABLE app_billing.transactions (
    transaction_id SERIAL PRIMARY KEY,
    user_id INTEGER REFERENCES app_users.profiles(user_id),
    amount NUMERIC(10, 2)
);

Advanced Data Structuring and Querying

Beyond basic table creation, efficient project setup requires handling complex, semi-structured data. PostgreSQL excels here, allowing us to treat comma-separated values (CSV) or lists as structured arrays. This capability is crucial for maintaining data integrity while accommodating flexible inputs.

For instance, if a product needs to list multiple tags, rather than creating a separate join table, we can use the `text[]` array type and functions like `unnest()` to treat the data as a structured list.

SQL

-- Example: Adding a tags column to the inventory schema
ALTER TABLE app_inventory.products ADD COLUMN tags TEXT[];

-- Inserting data with multiple tags (using simple, short values)
INSERT INTO app_inventory.products (product_name, stock_count, tags) VALUES
('Laptop Pro', 50, ARRAY['tech', 'electronics', 'premium']),
('Mouse Pad', 200, ARRAY['accessory', 'office']),
('Keyboard', 75, ARRAY['tech', 'office']);

-- Querying the structured data
SELECT product_name, tags FROM app_inventory.products WHERE 'tech' = ANY(tags);

Conclusion: Scalable Database Design

By adopting schema isolation and leveraging PostgreSQL’s native array and indexing capabilities, you establish a highly scalable and maintainable project structure. This approach ensures that as your application grows, the database remains logically segmented, minimizing coupling and maximizing performance. Always start with a clear schema definition to guide your application development.