@haygrouve/db-schema v2.0.3
Shared Database Schema
This package provides a shared database schema for multiple projects using Drizzle ORM with PostgreSQL.
Features
- Type-safe database schema definitions
- Migration management
- Environment validation
- Testing setup
- Schema versioning
Installation
npm install @haygrouve/db-schema
Environment Setup
Create a .env
file in your project root:
DATABASE_URL=postgresql://user:password@localhost:5432/dbname
NODE_ENV=development
Usage
import { db } from "@haygrouve/db-schema";
import { recipes } from "@haygrouve/db-schema/schema";
// Query example
const allRecipes = await db.select().from(recipes);
Development
Prerequisites
- Node.js 16+
- PostgreSQL 12+
Setup
- Clone the repository
- Install dependencies:
npm install
- Copy
.env.example
to.env
and configure your database URL - Run migrations:
npm run migrate
Available Scripts
npm run build
- Build the packagenpm run generate
- Generate Drizzle migrationsnpm run migrate
- Run database migrationsnpm run push
- Push schema changes to databasenpm run studio
- Open Drizzle Studionpm test
- Run testsnpm run test:watch
- Run tests in watch modenpm run test:coverage
- Run tests with coveragenpm run validate
- Type-check the codebasenpm run schema:update
- Validate, generate, and push schema changesnpm run schema:publish
- Update schema and publish packagenpm run version:patch
- Increment patch version and publishnpm run version:minor
- Increment minor version and publishnpm run version:major
- Increment major version and publish
Versioning
This package follows Semantic Versioning (MAJOR.MINOR.PATCH):
Version Types and Examples
MAJOR Version (Breaking Changes)
Use npm run version:major
when making incompatible schema changes:
Table Structure Changes:
// Before export const users = pgTable("users", { id: serial("id").primaryKey(), name: text("name"), }); // After (MAJOR change - renamed table) export const accounts = pgTable("accounts", { id: serial("id").primaryKey(), name: text("name"), });
Field Type Changes:
// Before export const products = pgTable("products", { price: integer("price"), }); // After (MAJOR change - changed type) export const products = pgTable("products", { price: decimal("price", { precision: 10, scale: 2 }), });
Removing Fields:
// Before export const posts = pgTable("posts", { id: serial("id").primaryKey(), title: text("title"), oldField: text("old_field"), // Removing this }); // After (MAJOR change - removed field) export const posts = pgTable("posts", { id: serial("id").primaryKey(), title: text("title"), });
MINOR Version (New Features)
Use npm run version:minor
when adding backward-compatible functionality:
Adding New Tables:
// New table addition export const categories = pgTable("categories", { id: serial("id").primaryKey(), name: text("name").notNull(), });
Adding New Fields:
// Before export const recipes = pgTable("recipes", { id: serial("id").primaryKey(), title: text("title"), }); // After (MINOR change - added field) export const recipes = pgTable("recipes", { id: serial("id").primaryKey(), title: text("title"), cookingTime: integer("cooking_time"), // New field });
Adding Indexes:
// Before export const users = pgTable("users", { id: serial("id").primaryKey(), email: text("email"), }); // After (MINOR change - added index) export const users = pgTable( "users", { id: serial("id").primaryKey(), email: text("email"), }, (table) => ({ emailIdx: index("email_idx").on(table.email), }) );
PATCH Version (Bug Fixes)
Use npm run version:patch
for backward-compatible bug fixes:
Fixing Field Constraints:
// Before export const orders = pgTable("orders", { status: text("status"), // Missing notNull constraint }); // After (PATCH change - added constraint) export const orders = pgTable("orders", { status: text("status").notNull(), });
Fixing Default Values:
// Before export const posts = pgTable("posts", { published: boolean("published"), // Missing default }); // After (PATCH change - added default) export const posts = pgTable("posts", { published: boolean("published").default(false), });
Fixing Field Lengths:
// Before export const users = pgTable("users", { username: varchar("username", { length: 50 }), // Too short }); // After (PATCH change - increased length) export const users = pgTable("users", { username: varchar("username", { length: 100 }), });
Version Management
Making Changes:
- Make your schema changes
- Run tests:
npm test
- Update documentation if needed
Choosing Version:
- Breaking changes →
npm run version:major
- New features →
npm run version:minor
- Bug fixes →
npm run version:patch
- Breaking changes →
Version Scripts:
version:patch
: Updates patch version (1.0.0 → 1.0.1)version:minor
: Updates minor version (1.0.0 → 1.1.0)version:major
: Updates major version (1.0.0 → 2.0.0)
Each version script will:
- Update the version in package.json
- Create a git tag
- Run schema updates
- Build the package
- Publish to npm
Making Schema Changes
When you need to modify the database schema, follow these steps:
Prerequisites:
- Ensure you have a backup of your database (especially for production)
- Verify your
.env
file has the correctDATABASE_URL
- Check you have proper database access rights
Make Schema Changes:
- Edit the appropriate schema file in
src/schema/
- Common changes include:
- Adding new fields
- Modifying field types
- Adding indexes
- Creating new tables
- Adding relations
- Edit the appropriate schema file in
Update Schema (Quick Method):
npm run schema:update
This single command will:
- Validate your changes (
npm run validate
) - Generate migration files (
npm run generate
) - Push changes to database (
npm run push
)
- Validate your changes (
Publish Changes (If Needed):
npm run schema:publish
This will:
- Run all schema update steps
- Build the package
- Publish to npm
Manual Steps (Alternative)
If you prefer to run steps individually:
Validate Changes:
npm run validate
Generate Migration:
npm run generate
- Review the generated migration file in the
drizzle
directory
- Review the generated migration file in the
Push Changes:
npm run push
- Verify the changes in Drizzle Studio:
npm run studio
- Verify the changes in Drizzle Studio:
Build and Publish:
npm run build npm publish
Example: Adding a New Field
// src/schema/recipes.ts
export const recipes = pgTable("recipe", {
// ... existing fields ...
newField: varchar("new_field", { length: 256 }),
});
Then run:
npm run schema:update
Schema Structure
The schema is organized by domain/feature:
src/schema/recipes.ts
- Recipe-related tablessrc/schema/sportpredict.ts
- Sports prediction tables
Contributing
- Create a new branch for your feature
- Make your changes
- Add tests if applicable
- Run tests:
npm test
- Submit a pull request
License
MIT