You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
docs: add Views section and document ensure errors
- Add Views section explaining view() function, read-only nature, and .active
- Add view and isView to Core Exports
- Document EnsureError and SchemaDriftError in error types
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Opus 4.5 <[email protected]>
**Table identity**: A table definition is a singleton value which is passed to database methods for validation, normalization, schema management, and convenient CRUD operations. It is not a class.
388
388
389
+
## Views
390
+
391
+
Views are read-only projections of tables with predefined WHERE clauses:
392
+
393
+
```typescript
394
+
import {z, table, view} from"@b9g/zen";
395
+
396
+
const Users =table("users", {
397
+
id: z.string().db.primary(),
398
+
name: z.string(),
399
+
role: z.enum(["user", "admin"]),
400
+
deletedAt: z.date().nullable().db.softDelete(),
401
+
});
402
+
403
+
// Define views with explicit names
404
+
const ActiveUsers =view("active_users", Users)`
405
+
WHERE ${Users.cols.deletedAt} IS NULL
406
+
`;
407
+
408
+
const AdminUsers =view("admin_users", Users)`
409
+
WHERE ${Users.cols.role} = ${"admin"}
410
+
`;
411
+
412
+
// Query from views (same API as tables)
413
+
const admins =awaitdb.all(AdminUsers)``;
414
+
const admin =awaitdb.get(AdminUsers, "u1");
415
+
416
+
// Views are read-only — mutations throw errors
417
+
awaitdb.insert(AdminUsers, {...}); // ✗ Error
418
+
awaitdb.update(AdminUsers, {...}); // ✗ Error
419
+
awaitdb.delete(AdminUsers, "u1"); // ✗ Error
420
+
```
421
+
422
+
**Auto-generated `.active` view:** Tables with a `.db.softDelete()` field automatically get an `.active` view:
423
+
424
+
```typescript
425
+
// Equivalent to: view("users_active", Users)`WHERE deletedAt IS NULL`
426
+
const activeUsers =awaitdb.all(Users.active)``;
427
+
```
428
+
429
+
**Views preserve table relationships:** Views inherit references from their base table, so JOINs work identically:
0 commit comments