-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathllms.txt
More file actions
627 lines (525 loc) · 14.6 KB
/
llms.txt
File metadata and controls
627 lines (525 loc) · 14.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
# @casekit/orm
> A type-safe, PostgreSQL-native ORM for TypeScript with deep relational querying, middleware support, and declarative schema definitions.
## Overview
@casekit/orm is a TypeScript ORM designed for PostgreSQL that prioritizes:
- **Full compile-time type safety** for queries and results
- **Deep relational queries** with 1:1, 1:N, N:1, and N:N relationships
- **Middleware system** for query interception (multi-tenancy, timestamps, etc.)
- **Typescript model definitions ** - no DSLs, just Typescript
- **SQL for complex aggregations** - we don't try to do everything in the ORM, but instead give you tools to type your SQL queries easily.
The ORM uses LATERAL JOINs for efficient multi-relation queries and validates all results with Zod schemas at runtime.
## Installation
```bash
npm add @casekit/orm @casekit/orm-cli @casekit/orm-migrate pg zod
```
## Quick Start
### 1. Initialize Project
```bash
npm orm init --directory ./src/db
```
This creates:
- `src/db/index.ts` - Database connection
- `src/db/models/index.ts` - Model exports
- `orm.config.ts` - CLI configuration
### 2. Define Models
```typescript
// src/db/models/author.ts
import type { ModelDefinition } from "@casekit/orm";
import { sql } from "@casekit/sql";
export const author = {
fields: {
id: { type: "serial", primaryKey: true },
name: { type: "text" },
email: { type: "text", unique: true },
createdAt: { type: "timestamp", default: sql`now()` },
},
relations: {
books: {
type: "1:N",
model: "book",
fromField: "id",
toField: "authorId",
},
},
} as const satisfies ModelDefinition;
// src/db/models/book.ts
export const book = {
fields: {
id: { type: "serial", primaryKey: true },
title: { type: "text" },
authorId: {
type: "integer",
references: { model: "author", field: "id" },
},
createdAt: { type: "timestamp", default: sql`now()` },
},
relations: {
author: {
type: "N:1",
model: "author",
fromField: "authorId",
toField: "id",
},
},
} as const satisfies ModelDefinition;
```
### 3. Configure Database
```typescript
// src/db/index.ts
import { orm, type Config, type ModelType, type Orm } from "@casekit/orm";
import { snakeCase } from "es-toolkit";
import { models } from "./models";
const config = {
models,
naming: { column: snakeCase },
connection: { database: "myapp" },
} as const satisfies Config;
let db: Orm<typeof config>;
// Reuse connection in development
declare global {
var __db: Orm<typeof config>;
}
if (process.env.NODE_ENV === "production") {
db = orm(config);
await db.connect();
} else {
if (!global.__db) {
global.__db = orm(config);
await global.__db.connect();
}
db = global.__db;
}
export type DB = Orm<typeof config>;
export type Models = typeof models;
export type Model<M extends keyof Models> = ModelType<Models[M]>;
export { db };
```
### 4. Push Schema to Database
```bash
npm orm db push
```
## Core API
### CRUD Operations
```typescript
// Find single record (throws if not found)
const author = await db.findOne("author", {
select: ["id", "name", "email"],
where: { id: 1 },
});
// Find multiple records
const authors = await db.findMany("author", {
select: ["id", "name"],
where: { name: { [$ilike]: "%john%" } },
orderBy: [["name", "asc"]],
limit: 10,
offset: 0,
});
// Count records
const count = await db.count("author", {
where: { createdAt: { [$gte]: someDate } },
});
// Create single record
const newAuthor = await db.createOne("author", {
values: { name: "Jane", email: "jane@example.com" },
returning: ["id", "createdAt"],
});
// Create multiple records
const newBooks = await db.createMany("book", {
values: [
{ title: "Book 1", authorId: 1 },
{ title: "Book 2", authorId: 1 },
],
returning: ["id"],
});
// Update single record (throws if != 1 match)
await db.updateOne("author", {
set: { name: "Jane Doe" },
where: { id: 1 },
returning: ["id", "name"],
});
// Update multiple records
await db.updateMany("book", {
set: { authorId: 2 },
where: { authorId: 1 },
});
// Delete single record (throws if != 1 match)
await db.deleteOne("author", {
where: { id: 1 },
returning: ["id"],
});
// Delete multiple records
await db.deleteMany("book", {
where: { authorId: 1 },
});
```
### Relational Queries
```typescript
// Include related records
const authorWithBooks = await db.findOne("author", {
select: ["id", "name"],
include: {
books: {
select: ["id", "title", "createdAt"],
where: { createdAt: { [$gte]: lastMonth } },
orderBy: [["title", "asc"]],
limit: 5,
},
},
where: { id: 1 },
});
// Deep nested includes
const bookWithDetails = await db.findOne("book", {
select: ["id", "title"],
include: {
author: {
select: ["id", "name"],
include: {
books: {
select: ["id", "title"],
},
},
},
},
where: { id: 1 },
});
```
### WHERE Operators
Import operators from the ORM:
```typescript
import {
$eq, $ne, $gt, $gte, $lt, $lte,
$in, $like, $ilike,
$is, $not, $and, $or
} from "@casekit/orm";
```
Usage:
```typescript
// Comparison operators
where: { age: { [$gte]: 18 } }
where: { status: { [$in]: ["active", "pending"] } }
where: { email: { [$ilike]: "%@gmail.com" } }
// NULL checks
where: { deletedAt: { [$is]: null } }
where: { active: { [$is]: true } }
// Logical operators
where: {
[$or]: [
{ role: "admin" },
{ [$and]: [
{ status: "active" },
{ verified: true }
]}
]
}
```
### Transactions
```typescript
await db.transact(async (tx) => {
const author = await tx.createOne("author", {
values: { name: "New Author", email: "new@example.com" },
returning: ["id"],
});
await tx.createOne("book", {
values: { title: "New Book", authorId: author.id },
});
// Auto-commits on success, auto-rollback on error
});
// Force rollback (useful for testing)
await db.transact(async (tx) => {
// ... operations ...
}, { rollback: true });
```
### Middleware
Two example middlewares:
```typescript
export const tenancy = ({ org }: { org: Organisation }): Middleware => ({
where: (config, modelName, where) => {
if ("organisationId" in getModel(config.models, modelName).fields) {
return { ...where, organisaitonId: org.id };
} else {
return where;
}
},
values: (config, modelName, values) => {
if ("organisationId" in getModel(config.models, modelName).fields) {
return { ...values, organisationId: org.id };
} else {
return values;
}
},
});
export const userstamps = ({ user }: { user: User }): Middleware => ({
values: (config, modelName, values) => {
if ("createdById" in getModel(config.models, modelName).fields) {
return { ...values, updatedById: values["createdById"] ?? user.id };
} else {
return values;
}
},
set: (config, modelName, set) => {
if ("updatedById" in getModel(config.models, modelName).fields) {
return { ...set, updatedById: set["updatedById"] ?? user.id };
} else {
return set;
}
},
});
const dbWithMiddleware = db.middleware([
tenancy({ org }), // add a where clause on organisationId to every table that has an organisationId column
userstamps({ user: currentUser }), // Auto-set created_by/updated_by
]);
### Model Restriction
```typescript
// Restrict access to specific models
const restrictedDb = db.restrict(["user", "profile"]);
// Throws at runtime if code tries to access other models
await restrictedDb.findMany("post", { ... }); // Error!
```
### Raw Queries
```typescript
import { sql } from "@casekit/sql";
import { z } from "zod";
// With Zod schema validation
const results = await db.query(
z.object({ id: z.number(), name: z.string() }),
sql`SELECT id, name FROM authors WHERE id = ${authorId}`
);
// Template literal syntax
const rows = await db.query<{ count: number }>`
SELECT COUNT(*) as count FROM books
`;
```
## Model Definition Reference
### Field Types
PostgreSQL types supported (with array variants using `[]`):
- **Numeric**: `integer`, `bigint`, `smallint`, `serial`, `bigserial`, `smallserial`, `numeric`, `decimal`, `real`, `double precision`, `money`
- **Text**: `text`, `varchar`, `char`
- **Boolean**: `boolean`
- **Date/Time**: `date`, `time`, `timestamp`, `timestamptz`, `interval`
- **JSON**: `json`, `jsonb`
- **UUID**: `uuid`
- **Binary**: `bytea`
- **Network**: `inet`, `cidr`, `macaddr`
- **Geometric**: `point`, `line`, `polygon`, `box`, etc.
### Field Definition
```typescript
{
fields: {
id: {
type: "serial",
primaryKey: true,
},
email: {
type: "text",
column: "email_address", // Override column name
unique: true, // Simple unique constraint
nullable: false, // NOT NULL (default)
},
status: {
type: "text",
default: "'active'", // SQL default value
unique: {
where: sql`deleted_at IS NULL`, // Partial unique
nullsNotDistinct: true,
},
},
orgId: {
type: "integer",
references: {
model: "organisation",
field: "id",
onDelete: "CASCADE",
onUpdate: "RESTRICT",
},
provided: true, // Set by middleware, not required in inserts
},
data: {
type: "jsonb",
zodSchema: z.object({ // Custom Zod validation
key: z.string(),
}),
},
tags: {
type: "text[]", // Array type
default: sql`ARRAY[]::text[]`,
},
},
}
```
### Relation Types
```typescript
{
relations: {
// One-to-Many: Author has many Books
books: {
type: "1:N",
model: "book",
fromField: "id", // this.id
toField: "authorId", // book.authorId
},
// Many-to-One: Book belongs to Author
author: {
type: "N:1",
model: "author",
fromField: "authorId", // this.authorId
toField: "id", // author.id
optional: true, // LEFT JOIN instead of INNER JOIN
},
// Many-to-Many: Post has many Tags through PostTag
tags: {
type: "N:N",
model: "tag",
through: {
model: "postTag",
fromRelation: "post", // postTag.post relation
toRelation: "tag", // postTag.tag relation
},
},
},
}
```
### Constraints
```typescript
{
// Multi-column primary key
primaryKey: ["orgId", "slug"],
// Multi-column unique constraints
uniqueConstraints: [
{
name: "unique_org_email",
fields: ["orgId", "email"],
where: sql`deleted_at IS NULL`,
nullsNotDistinct: false,
},
],
// Multi-column foreign keys
foreignKeys: [
{
name: "fk_location",
fields: ["countryCode", "regionCode"],
references: {
model: "region",
fields: ["countryCode", "code"],
},
onDelete: "SET NULL",
onUpdate: "CASCADE",
},
],
}
```
## CLI Reference
### Commands
```bash
# Initialize new project
npm orm init --directory ./src/db
# Push schema to database (creates tables)
npm orm db push
# Pull schema from database (generates model files)
npm orm db pull --schema public --schema other_schema
# Drop all schemas
npm orm db drop
# Generate skeleton model file
npm orm generate model user
```
### Configuration
```typescript
// orm.config.ts
import { orm, type Config } from "@casekit/orm";
import type { OrmCLIConfig } from "@casekit/orm-cli";
import { sql } from "@casekit/sql";
import { snakeCase } from "es-toolkit";
import { models } from "./src/db/models";
const config = {
models,
naming: { column: snakeCase },
connection: { database: "myapp" },
} as const satisfies Config;
export default {
db: orm(config),
directory: "./src/db",
} satisfies OrmCLIConfig;
```
## Config Reference
```typescript
interface Config {
models: ModelDefinitions; // Required
schema?: string; // Default schema
connection?: pg.ConnectionConfig | pg.PoolConfig;
pool?: boolean;
extensions?: readonly string[]; // e.g., ["uuid-ossp"]
operators?: OperatorDefinitions; // Custom WHERE operators
naming?: {
column?: (name: string) => string; // e.g., snakeCase
table?: (name: string) => string;
};
logger?: Logger;
}
```
## Type Exports
```typescript
import {
// Core
orm,
Orm,
Config,
// Model types
ModelDefinition,
ModelDefinitions,
ModelType,
ModelName,
// Field types
FieldDefinition,
FieldName,
FieldType,
RequiredField,
OptionalField,
NullableField,
// Relation types
RelationDefinition,
RelationName,
RelationModel,
// Query types
FindParams,
CountParams,
CreateOneParams,
CreateManyParams,
UpdateParams,
DeleteParams,
// Clause types
WhereClause,
SelectClause,
IncludeClause,
OrderByClause,
ReturningClause,
// Result types
FindResult,
CreateOneResult,
CreateManyResult,
UpdateOneResult,
UpdateManyResult,
DeleteOneResult,
DeleteManyResult,
// Middleware
Middleware,
// Operators
$eq, $ne, $gt, $gte, $lt, $lte,
$in, $like, $ilike, $is, $not, $and, $or,
DefaultOperators,
} from "@casekit/orm";
```
## Package Structure
```
@casekit/orm # Core ORM library
@casekit/orm-cli # CLI for init, push, pull, generate
@casekit/orm-migrate # Migration engine (push/pull/drop)
@casekit/orm-schema # Type definitions
@casekit/orm-config # Config normalization
@casekit/sql # SQL statement builder
```
## Optional Information
For more details, see the source code at:
- ORM core: packages/orm/src/
- CLI: packages/orm-cli/src/
- Migrations: packages/orm-migrate/src/
- Types: packages/orm-schema/src/
- Example app: examples/tanstack-start-example/