Skip to content

Commit f36a93a

Browse files
feat(schema-dsl): add @resource, @field, @id decorators (#10508)
feat: begin adding docs for the schema DSL Co-authored-by: Chris Thoburn <cthoburn@auditboard.com>
1 parent 56797a4 commit f36a93a

27 files changed

Lines changed: 3012 additions & 20 deletions

guides/the-manual/schemas/dsl/index.md

Lines changed: 1665 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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+
```

pnpm-lock.yaml

Lines changed: 69 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/core/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"@warp-drive/holodeck": "workspace:*",
4848
"@warp-drive/internal-config": "workspace:*",
4949
"@warp-drive/json-api": "workspace:*",
50+
"@warp-drive/schema-dsl": "workspace:*",
5051
"eslint-plugin-warp-drive": "workspace:*",
5152
"babel-plugin-ember-template-compilation": "^4.0.0",
5253
"decorator-transforms": "^2.2.2",

0 commit comments

Comments
 (0)