@take-out/docs 0.0.42

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/database.md ADDED
@@ -0,0 +1,229 @@
1
+ ---
2
+ name: database
3
+ description: Database guide for PostgreSQL, Drizzle ORM, and Zero sync. INVOKE WHEN: Drizzle, migrations, schema changes, ALTER TABLE, SQL queries, getDb, getDBClient, queryDb, connection pooling, Aurora, pgvector, vector search.
4
+ ---
5
+
6
+ # Database
7
+
8
+ This guide covers the database architecture, tools, and patterns used in this
9
+ codebase.
10
+
11
+ ## Overview
12
+
13
+ The application uses PostgreSQL as its primary database with multiple access
14
+ patterns and tools:
15
+
16
+ PostgreSQL - primary database (locally via Docker, production via AWS Aurora
17
+ Serverless v2) Drizzle ORM - type-safe ORM for schema management pgvector -
18
+ vector database extension for semantic search Zero - real-time sync system with
19
+ its own database requirements pg - direct PostgreSQL client for custom queries
20
+
21
+ ## Local Development
22
+
23
+ ### Database GUI - TablePlus
24
+
25
+ TablePlus is the recommended database GUI for macOS. It provides a clean, native
26
+ macOS interface, support for multiple database types, query editor with syntax
27
+ highlighting, import/export capabilities, and SSH tunnel support for production
28
+ databases.
29
+
30
+ To connect to your local development database:
31
+
32
+ Open TablePlus, create a new PostgreSQL connection, and use the connection
33
+ string from `ZERO_UPSTREAM_DB` environment variable. Default local connection:
34
+ `postgresql://user:password@127.0.0.1:5433/postgres`
35
+
36
+ ### Command Line Access
37
+
38
+ ```bash
39
+ # connect to local database
40
+ bun env:dev bunx postgres psql
41
+
42
+ # run a query
43
+ bun env:dev bunx postgres psql --query "SELECT * FROM users LIMIT 5"
44
+ ```
45
+
46
+ ## Query Patterns
47
+
48
+ The codebase uses multiple query patterns depending on the use case.
49
+
50
+ ### Drizzle ORM via getDb()
51
+
52
+ Standard ORM queries for type-safe database operations:
53
+
54
+ ```typescript
55
+ import { getDb } from '~/database'
56
+ import { users } from '~/database/schema-public'
57
+
58
+ const db = getDb()
59
+ const allUsers = await db.select().from(users)
60
+ ```
61
+
62
+ Location: src/database/index.ts:28
63
+
64
+ ### Direct SQL with Template Literals
65
+
66
+ For complex queries using the sql helper:
67
+
68
+ ```typescript
69
+ import { sql } from '~/database/sql'
70
+
71
+ const result = await sql`
72
+ SELECT * FROM users
73
+ WHERE created_at > ${date}
74
+ ORDER BY created_at DESC
75
+ `
76
+ ```
77
+
78
+ Location: src/database/sql.ts:10
79
+
80
+ ### Raw pg Client via getDBClient()
81
+
82
+ For connection pooling and advanced PostgreSQL features:
83
+
84
+ ```typescript
85
+ import { getDBClient, queryDb } from '~/database/helpers'
86
+
87
+ const client = await getDBClient()
88
+ try {
89
+ const result = await client.query('SELECT * FROM users')
90
+ } finally {
91
+ client.release()
92
+ }
93
+
94
+ const result = await queryDb('SELECT * FROM users WHERE id = $1', [userId])
95
+ ```
96
+
97
+ Location: src/database/getDBClient.ts:36
98
+
99
+ ## Production Infrastructure (AWS Aurora)
100
+
101
+ The production database runs on AWS Aurora Serverless v2 with PostgreSQL 16.6.
102
+ Configuration defined in sst.config.ts.
103
+
104
+ ### Connection Management
105
+
106
+ The application implements sophisticated connection pooling to handle Aurora's
107
+ connection limits:
108
+
109
+ Connection pool with retry logic (src/database/getDBClient.ts:36), automatic
110
+ connection limit monitoring, pool recycling when approaching limits,
111
+ configurable timeouts and retry strategies.
112
+
113
+ ## Migrations
114
+
115
+ We have a unified migration system that combines Drizzle schema migrations with
116
+ custom TypeScript migrations. You don't need to touch Drizzle directly.
117
+
118
+ ### Migration Workflow
119
+
120
+ 1. Edit `src/database/schema-public.ts` or `src/database/schema-private.ts`
121
+ 2. Run migrations:
122
+
123
+ ```bash
124
+ # local development - generates and runs migrations
125
+ bun migrate run
126
+
127
+ # production - just generates and builds (CI deploys)
128
+ bun migrate build
129
+ ```
130
+
131
+ ### Custom Migrations
132
+
133
+ For data migrations or complex changes that can't be expressed in schema:
134
+
135
+ ```bash
136
+ bun db:migrate-add update-user-stats
137
+ ```
138
+
139
+ This creates a TypeScript migration file:
140
+
141
+ ```typescript
142
+ import type { PoolClient } from 'pg'
143
+
144
+ export async function up(client: PoolClient) {
145
+ await client.query(`
146
+ UPDATE users SET email = lower(email)
147
+ `)
148
+ }
149
+ ```
150
+
151
+ We don't do down migrations.
152
+
153
+ ### Troubleshooting
154
+
155
+ **Migration file is blank/empty:**
156
+ Drizzle found no schema changes. Either your changes aren't saved, or the schema
157
+ already matches the database. Check your schema file and compare with what's in
158
+ the database.
159
+
160
+ **Check which migrations have run:**
161
+ ```bash
162
+ bun env:dev bunx postgres psql --query "SELECT * FROM migrations ORDER BY id"
163
+ ```
164
+
165
+ **Migration failed and left database in bad state:**
166
+ Migrations run in a transaction, so failed migrations should rollback
167
+ automatically. If you need to manually fix:
168
+
169
+ 1. Check what migrations have run (query above)
170
+ 2. If a bad migration was applied, write a new migration to fix it
171
+ 3. If the migration is stuck in a partial state, you may need to manually delete
172
+ from the migrations table and fix the schema:
173
+ ```sql
174
+ DELETE FROM migrations WHERE name = '0003_bad_migration';
175
+ ```
176
+
177
+ **Schema changes not being detected:**
178
+ - Make sure you're editing the right file (`schema-public.ts` vs `schema-private.ts`)
179
+ - Both schemas go to Drizzle - "private" just means auth tables, not "excluded from migrations"
180
+ - Run `bun migrate run` which regenerates migrations fresh
181
+
182
+ ### Generated Columns in PostgreSQL
183
+
184
+ When creating generated columns:
185
+
186
+ Correct syntax for adding generated columns:
187
+
188
+ ```sql
189
+ ALTER TABLE "table_name"
190
+ ADD COLUMN "column_name" data_type
191
+ GENERATED ALWAYS AS (expression) STORED;
192
+ ```
193
+
194
+ Modifying existing columns to be generated: first drop the existing column, then
195
+ add it back as generated. Cannot directly convert a regular column to generated.
196
+
197
+ In Drizzle schema (src/database/schema-public.ts):
198
+
199
+ ```typescript
200
+ columnName: t.integer().generatedAlwaysAs(sql`expression`)
201
+ ```
202
+
203
+ ## Zero Database Requirements
204
+
205
+ Zero requires specific database configuration:
206
+
207
+ Three separate databases: main, CVR, and change tracking. Logical replication
208
+ enabled. Connection strings configured via environment: ZERO_UPSTREAM_DB (main
209
+ database), ZERO_CVR_DB (client view records), ZERO_CHANGE_DB (change tracking).
210
+
211
+ ## Best Practices
212
+
213
+ Connection Management: always release clients when using getDBClient(), use
214
+ queryDb() helper for one-off queries, monitor connection pool usage in
215
+ production.
216
+
217
+ Migrations: test migrations locally before production, use transactions for data
218
+ migrations.
219
+
220
+ Query Performance: use indexes for frequently queried columns, monitor slow
221
+ queries via Aurora logs, use EXPLAIN ANALYZE for query optimization.
222
+
223
+ Security: never log sensitive data, use parameterized queries to prevent SQL
224
+ injection, keep connection strings in environment variables.
225
+
226
+ ## Integration Testing
227
+
228
+ For information on database state management during integration testing, see the
229
+ Integration Testing Guide (./testing-integration.md).
package/docs.md ADDED
@@ -0,0 +1,8 @@
1
+ ---
2
+ name: docs
3
+ description: Docs/documentation, writing english, JSDoc code comments, markdown, md, mdx, english
4
+ ---
5
+
6
+ # Writing documentation
7
+
8
+ When writing markdown/docs, JSDoc, or other natural english, we prefer the keep things simple, avoid writing too many lists, avoid bolding things too much, avoid emojis. In general avoid over-use of tables unless it's a pattern found in similar docs elsewhere or truly essential to list out a table-like structure.