|
| 1 | +--- |
| 2 | +title: Resources |
| 3 | +order: 1 |
| 4 | +draft: true |
| 5 | +--- |
| 6 | + |
| 7 | +# Defining Resources |
| 8 | + |
| 9 | +Use `@Resource` to define a resource schema and `@field` to define its fields. |
| 10 | +Put each schema in its own file under a schemas directory. |
| 11 | + |
| 12 | +```ts |
| 13 | +// app/schemas/user.ts |
| 14 | +import { Resource, field } from '@warp-drive/schema-dsl'; |
| 15 | + |
| 16 | +@Resource |
| 17 | +class User { |
| 18 | + @field declare firstName: string; |
| 19 | + @field declare lastName: string; |
| 20 | + @field declare email: string; |
| 21 | +} |
| 22 | +``` |
| 23 | + |
| 24 | +At build time, the Vite plugin reads these files and produces the |
| 25 | +equivalent of calling `withDefaults` by hand — you just write the class. |
| 26 | + |
| 27 | +## Setup |
| 28 | + |
| 29 | +Add the plugin to your Vite config, pointing it at your schema files: |
| 30 | + |
| 31 | +```ts |
| 32 | +// vite.config.ts |
| 33 | +import { schemaDSL } from '@warp-drive/schema-dsl/vite'; |
| 34 | + |
| 35 | +export default defineConfig({ |
| 36 | + plugins: [ |
| 37 | + schemaDSL({ |
| 38 | + schemas: 'app/schemas/**/*.ts', |
| 39 | + }), |
| 40 | + ], |
| 41 | +}); |
| 42 | +``` |
| 43 | + |
| 44 | +Then import and register the compiled output: |
| 45 | + |
| 46 | +```ts |
| 47 | +import schemas from 'virtual:warp-drive-schemas'; |
| 48 | + |
| 49 | +registerDerivations(store.schema); |
| 50 | +store.schema.registerResources(schemas); |
| 51 | +``` |
| 52 | + |
| 53 | +## Type Name |
| 54 | + |
| 55 | +The resource type is derived from the class name: |
| 56 | + |
| 57 | +- `User` → `'user'` |
| 58 | +- `UserProfile` → `'user-profile'` |
| 59 | + |
| 60 | +Override with a string argument: |
| 61 | + |
| 62 | +```ts |
| 63 | +@Resource('person') |
| 64 | +class User { |
| 65 | + @field declare name: string; |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +## Field Options |
| 70 | + |
| 71 | +```ts |
| 72 | +@field({ type: 'date-time' }) |
| 73 | +declare createdAt: Date; |
| 74 | + |
| 75 | +@field({ sourceKey: 'email_address' }) |
| 76 | +declare email: string; |
| 77 | +``` |
| 78 | + |
| 79 | +## Custom Identity |
| 80 | + |
| 81 | +Use `@id` when your identity field isn't `id`: |
| 82 | + |
| 83 | +```ts |
| 84 | +import { Resource, field, id } from '@warp-drive/schema-dsl'; |
| 85 | + |
| 86 | +@Resource |
| 87 | +class User { |
| 88 | + @id declare uuid: string; |
| 89 | + @field declare name: string; |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +## Legacy Mode |
| 94 | + |
| 95 | +For apps using `@warp-drive/legacy/model`: |
| 96 | + |
| 97 | +```ts |
| 98 | +@Resource({ legacy: true }) |
| 99 | +class Post { |
| 100 | + @field declare title: string; |
| 101 | +} |
| 102 | +``` |
0 commit comments