Phase 1: Monorepo scaffold, database, and dev environment
Turborepo monorepo with @forte/shared and @forte/backend workspaces. Docker Compose dev env with PostgreSQL 16 + Valkey 8. Fastify server with Pino JSON logging, request ID tracing, and health endpoint. Drizzle ORM with company + location tables. Includes: - Root config (turbo, tsconfig, eslint, prettier) - @forte/shared: types, schemas, currency/date utils - @forte/backend: Fastify entry, plugins (database, redis, cors, error-handler, dev-auth), health route, Drizzle schema + migration - Dev auth bypass via X-Dev-Company/Location/User headers - Vitest integration test with clean DB per test (forte_test) - Seed script for dev company + location
This commit is contained in:
1
packages/backend/src/db/index.ts
Normal file
1
packages/backend/src/db/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './schema/stores.js'
|
||||
25
packages/backend/src/db/migrations/0000_hot_purifiers.sql
Normal file
25
packages/backend/src/db/migrations/0000_hot_purifiers.sql
Normal file
@@ -0,0 +1,25 @@
|
||||
CREATE TABLE "company" (
|
||||
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
||||
"name" varchar(255) NOT NULL,
|
||||
"phone" varchar(50),
|
||||
"email" varchar(255),
|
||||
"timezone" varchar(100) DEFAULT 'America/Chicago' NOT NULL,
|
||||
"notes" text,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "location" (
|
||||
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
||||
"company_id" uuid NOT NULL,
|
||||
"name" varchar(255) NOT NULL,
|
||||
"address" jsonb,
|
||||
"phone" varchar(50),
|
||||
"email" varchar(255),
|
||||
"timezone" varchar(100),
|
||||
"is_active" boolean DEFAULT true NOT NULL,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
ALTER TABLE "location" ADD CONSTRAINT "location_company_id_company_id_fk" FOREIGN KEY ("company_id") REFERENCES "public"."company"("id") ON DELETE no action ON UPDATE no action;
|
||||
175
packages/backend/src/db/migrations/meta/0000_snapshot.json
Normal file
175
packages/backend/src/db/migrations/meta/0000_snapshot.json
Normal file
@@ -0,0 +1,175 @@
|
||||
{
|
||||
"id": "fd79ece0-66f3-4238-a2ca-af44060363b5",
|
||||
"prevId": "00000000-0000-0000-0000-000000000000",
|
||||
"version": "7",
|
||||
"dialect": "postgresql",
|
||||
"tables": {
|
||||
"public.company": {
|
||||
"name": "company",
|
||||
"schema": "",
|
||||
"columns": {
|
||||
"id": {
|
||||
"name": "id",
|
||||
"type": "uuid",
|
||||
"primaryKey": true,
|
||||
"notNull": true,
|
||||
"default": "gen_random_uuid()"
|
||||
},
|
||||
"name": {
|
||||
"name": "name",
|
||||
"type": "varchar(255)",
|
||||
"primaryKey": false,
|
||||
"notNull": true
|
||||
},
|
||||
"phone": {
|
||||
"name": "phone",
|
||||
"type": "varchar(50)",
|
||||
"primaryKey": false,
|
||||
"notNull": false
|
||||
},
|
||||
"email": {
|
||||
"name": "email",
|
||||
"type": "varchar(255)",
|
||||
"primaryKey": false,
|
||||
"notNull": false
|
||||
},
|
||||
"timezone": {
|
||||
"name": "timezone",
|
||||
"type": "varchar(100)",
|
||||
"primaryKey": false,
|
||||
"notNull": true,
|
||||
"default": "'America/Chicago'"
|
||||
},
|
||||
"notes": {
|
||||
"name": "notes",
|
||||
"type": "text",
|
||||
"primaryKey": false,
|
||||
"notNull": false
|
||||
},
|
||||
"created_at": {
|
||||
"name": "created_at",
|
||||
"type": "timestamp with time zone",
|
||||
"primaryKey": false,
|
||||
"notNull": true,
|
||||
"default": "now()"
|
||||
},
|
||||
"updated_at": {
|
||||
"name": "updated_at",
|
||||
"type": "timestamp with time zone",
|
||||
"primaryKey": false,
|
||||
"notNull": true,
|
||||
"default": "now()"
|
||||
}
|
||||
},
|
||||
"indexes": {},
|
||||
"foreignKeys": {},
|
||||
"compositePrimaryKeys": {},
|
||||
"uniqueConstraints": {},
|
||||
"policies": {},
|
||||
"checkConstraints": {},
|
||||
"isRLSEnabled": false
|
||||
},
|
||||
"public.location": {
|
||||
"name": "location",
|
||||
"schema": "",
|
||||
"columns": {
|
||||
"id": {
|
||||
"name": "id",
|
||||
"type": "uuid",
|
||||
"primaryKey": true,
|
||||
"notNull": true,
|
||||
"default": "gen_random_uuid()"
|
||||
},
|
||||
"company_id": {
|
||||
"name": "company_id",
|
||||
"type": "uuid",
|
||||
"primaryKey": false,
|
||||
"notNull": true
|
||||
},
|
||||
"name": {
|
||||
"name": "name",
|
||||
"type": "varchar(255)",
|
||||
"primaryKey": false,
|
||||
"notNull": true
|
||||
},
|
||||
"address": {
|
||||
"name": "address",
|
||||
"type": "jsonb",
|
||||
"primaryKey": false,
|
||||
"notNull": false
|
||||
},
|
||||
"phone": {
|
||||
"name": "phone",
|
||||
"type": "varchar(50)",
|
||||
"primaryKey": false,
|
||||
"notNull": false
|
||||
},
|
||||
"email": {
|
||||
"name": "email",
|
||||
"type": "varchar(255)",
|
||||
"primaryKey": false,
|
||||
"notNull": false
|
||||
},
|
||||
"timezone": {
|
||||
"name": "timezone",
|
||||
"type": "varchar(100)",
|
||||
"primaryKey": false,
|
||||
"notNull": false
|
||||
},
|
||||
"is_active": {
|
||||
"name": "is_active",
|
||||
"type": "boolean",
|
||||
"primaryKey": false,
|
||||
"notNull": true,
|
||||
"default": true
|
||||
},
|
||||
"created_at": {
|
||||
"name": "created_at",
|
||||
"type": "timestamp with time zone",
|
||||
"primaryKey": false,
|
||||
"notNull": true,
|
||||
"default": "now()"
|
||||
},
|
||||
"updated_at": {
|
||||
"name": "updated_at",
|
||||
"type": "timestamp with time zone",
|
||||
"primaryKey": false,
|
||||
"notNull": true,
|
||||
"default": "now()"
|
||||
}
|
||||
},
|
||||
"indexes": {},
|
||||
"foreignKeys": {
|
||||
"location_company_id_company_id_fk": {
|
||||
"name": "location_company_id_company_id_fk",
|
||||
"tableFrom": "location",
|
||||
"tableTo": "company",
|
||||
"columnsFrom": [
|
||||
"company_id"
|
||||
],
|
||||
"columnsTo": [
|
||||
"id"
|
||||
],
|
||||
"onDelete": "no action",
|
||||
"onUpdate": "no action"
|
||||
}
|
||||
},
|
||||
"compositePrimaryKeys": {},
|
||||
"uniqueConstraints": {},
|
||||
"policies": {},
|
||||
"checkConstraints": {},
|
||||
"isRLSEnabled": false
|
||||
}
|
||||
},
|
||||
"enums": {},
|
||||
"schemas": {},
|
||||
"sequences": {},
|
||||
"roles": {},
|
||||
"policies": {},
|
||||
"views": {},
|
||||
"_meta": {
|
||||
"columns": {},
|
||||
"schemas": {},
|
||||
"tables": {}
|
||||
}
|
||||
}
|
||||
13
packages/backend/src/db/migrations/meta/_journal.json
Normal file
13
packages/backend/src/db/migrations/meta/_journal.json
Normal file
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"version": "7",
|
||||
"dialect": "postgresql",
|
||||
"entries": [
|
||||
{
|
||||
"idx": 0,
|
||||
"version": "7",
|
||||
"when": 1774635439354,
|
||||
"tag": "0000_hot_purifiers",
|
||||
"breakpoints": true
|
||||
}
|
||||
]
|
||||
}
|
||||
37
packages/backend/src/db/schema/stores.ts
Normal file
37
packages/backend/src/db/schema/stores.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { pgTable, uuid, varchar, text, jsonb, timestamp, boolean } from 'drizzle-orm/pg-core'
|
||||
|
||||
export const companies = pgTable('company', {
|
||||
id: uuid('id').primaryKey().defaultRandom(),
|
||||
name: varchar('name', { length: 255 }).notNull(),
|
||||
phone: varchar('phone', { length: 50 }),
|
||||
email: varchar('email', { length: 255 }),
|
||||
timezone: varchar('timezone', { length: 100 }).notNull().default('America/Chicago'),
|
||||
notes: text('notes'),
|
||||
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
|
||||
updatedAt: timestamp('updated_at', { withTimezone: true }).notNull().defaultNow(),
|
||||
})
|
||||
|
||||
export const locations = pgTable('location', {
|
||||
id: uuid('id').primaryKey().defaultRandom(),
|
||||
companyId: uuid('company_id')
|
||||
.notNull()
|
||||
.references(() => companies.id),
|
||||
name: varchar('name', { length: 255 }).notNull(),
|
||||
address: jsonb('address').$type<{
|
||||
street?: string
|
||||
city?: string
|
||||
state?: string
|
||||
zip?: string
|
||||
}>(),
|
||||
phone: varchar('phone', { length: 50 }),
|
||||
email: varchar('email', { length: 255 }),
|
||||
timezone: varchar('timezone', { length: 100 }),
|
||||
isActive: boolean('is_active').notNull().default(true),
|
||||
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
|
||||
updatedAt: timestamp('updated_at', { withTimezone: true }).notNull().defaultNow(),
|
||||
})
|
||||
|
||||
export type Company = typeof companies.$inferSelect
|
||||
export type CompanyInsert = typeof companies.$inferInsert
|
||||
export type Location = typeof locations.$inferSelect
|
||||
export type LocationInsert = typeof locations.$inferInsert
|
||||
50
packages/backend/src/db/seed.ts
Normal file
50
packages/backend/src/db/seed.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import postgres from 'postgres'
|
||||
import { drizzle } from 'drizzle-orm/postgres-js'
|
||||
import { companies, locations } from './schema/stores.js'
|
||||
|
||||
const DEV_COMPANY_ID = '00000000-0000-0000-0000-000000000001'
|
||||
const DEV_LOCATION_ID = '00000000-0000-0000-0000-000000000010'
|
||||
|
||||
async function seed() {
|
||||
const connectionString =
|
||||
process.env.DATABASE_URL ?? 'postgresql://forte:forte@localhost:5432/forte'
|
||||
const sql = postgres(connectionString)
|
||||
const db = drizzle(sql)
|
||||
|
||||
console.log('Seeding database...')
|
||||
|
||||
await db
|
||||
.insert(companies)
|
||||
.values({
|
||||
id: DEV_COMPANY_ID,
|
||||
name: 'Dev Music Co.',
|
||||
timezone: 'America/Chicago',
|
||||
})
|
||||
.onConflictDoNothing()
|
||||
|
||||
await db
|
||||
.insert(locations)
|
||||
.values({
|
||||
id: DEV_LOCATION_ID,
|
||||
companyId: DEV_COMPANY_ID,
|
||||
name: 'Main Store',
|
||||
address: {
|
||||
street: '123 Main St',
|
||||
city: 'Austin',
|
||||
state: 'TX',
|
||||
zip: '78701',
|
||||
},
|
||||
})
|
||||
.onConflictDoNothing()
|
||||
|
||||
console.log(`Seeded dev company: ${DEV_COMPANY_ID}`)
|
||||
console.log(`Seeded dev location: ${DEV_LOCATION_ID}`)
|
||||
|
||||
await sql.end()
|
||||
console.log('Done.')
|
||||
}
|
||||
|
||||
seed().catch((err) => {
|
||||
console.error('Seed failed:', err)
|
||||
process.exit(1)
|
||||
})
|
||||
Reference in New Issue
Block a user