@orieljs/framework 0.1.0-alpha.15 → 0.1.0-alpha.16
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/dist/{chunk-GCMPAXIK.js → chunk-3QHTNAMZ.js} +6 -4
- package/dist/{chunk-GCMPAXIK.js.map → chunk-3QHTNAMZ.js.map} +1 -1
- package/dist/{chunk-BKA5F3TH.js → chunk-6AXJB4CD.js} +5 -1
- package/dist/chunk-6AXJB4CD.js.map +1 -0
- package/dist/{chunk-7GPMFMS7.js → chunk-MG4YEWMV.js} +1239 -488
- package/dist/chunk-MG4YEWMV.js.map +1 -0
- package/dist/{chunk-FVI6X3VJ.js → chunk-YNDMTTWL.js} +114 -8
- package/dist/chunk-YNDMTTWL.js.map +1 -0
- package/dist/contracts.d.ts +112 -1
- package/dist/contracts.js +5 -1
- package/dist/{index.d-lffxurFs.d.ts → index.d-DA-Iu2a2.d.ts} +39 -2
- package/dist/index.d.ts +2780 -2137
- package/dist/index.js +1410 -543
- package/dist/index.js.map +1 -1
- package/dist/{jsx-runtime-Bc5KJzwb.d.ts → jsx-runtime-CenT41Y0.d.ts} +1 -1
- package/dist/jsx-runtime.d.ts +2 -2
- package/dist/jsx-runtime.js +3 -3
- package/dist/{testing-CVpo7aRl.d.ts → testing-B8J_HhF4.d.ts} +6 -2
- package/dist/testing.d.ts +2 -2
- package/dist/testing.js +3 -3
- package/docs/authentication.md +45 -3
- package/docs/authorization.md +2 -4
- package/docs/cache.md +29 -5
- package/docs/configuration.md +84 -7
- package/docs/console.md +41 -2
- package/docs/container.md +4 -0
- package/docs/csrf.md +2 -2
- package/docs/database.md +22 -0
- package/docs/deployment.md +39 -17
- package/docs/eloquent-factories.md +4 -3
- package/docs/eloquent-mutators.md +2 -1
- package/docs/eloquent-relationships.md +28 -3
- package/docs/eloquent-resources.md +253 -0
- package/docs/eloquent.md +41 -0
- package/docs/errors.md +12 -3
- package/docs/events.md +3 -1
- package/docs/facades.md +1 -0
- package/docs/helpers.md +33 -2
- package/docs/inertia.md +11 -2
- package/docs/installation.md +4 -2
- package/docs/laravel.md +71 -0
- package/docs/lifecycle.md +5 -2
- package/docs/live.md +88 -8
- package/docs/mail.md +28 -3
- package/docs/middleware.md +20 -12
- package/docs/navigation.json +8 -0
- package/docs/notifications.md +22 -0
- package/docs/pagination.md +4 -0
- package/docs/passwords.md +4 -6
- package/docs/providers.md +3 -5
- package/docs/queues.md +64 -2
- package/docs/requests.md +42 -3
- package/docs/responses.md +7 -4
- package/docs/routing.md +85 -7
- package/docs/scheduling.md +25 -0
- package/docs/session.md +2 -2
- package/docs/starter-kits.md +26 -7
- package/docs/storage.md +10 -4
- package/docs/testing.md +10 -2
- package/docs/two-factor.md +34 -1
- package/docs/upgrade.md +111 -2
- package/docs/validation.md +70 -9
- package/docs/views.md +1 -0
- package/docs/vite.md +2 -1
- package/package.json +43 -35
- package/dist/chunk-7GPMFMS7.js.map +0 -1
- package/dist/chunk-BKA5F3TH.js.map +0 -1
- package/dist/chunk-FVI6X3VJ.js.map +0 -1
|
@@ -147,6 +147,23 @@ export class Project extends Model {
|
|
|
147
147
|
|
|
148
148
|
The keys are `project_id` on `environments` and `environment_id` on `deployments`; the next arguments name others.
|
|
149
149
|
|
|
150
|
+
The models may share a table. A user's followings reach users through a `follows` table, a person's grandchildren reach
|
|
151
|
+
people through people, and `has()`, `withCount()` and eager loads read each table a query names twice under an alias of
|
|
152
|
+
its own:
|
|
153
|
+
|
|
154
|
+
```ts title="app/Models/Person.ts"
|
|
155
|
+
import { Model } from '@orieljs/framework'
|
|
156
|
+
|
|
157
|
+
export class Person extends Model {
|
|
158
|
+
/** The children of this person's children. */
|
|
159
|
+
grandchildren() {
|
|
160
|
+
return this.hasManyThrough(Person, Person, 'parent_id', 'parent_id')
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
Laravel doesn't alias a through table that is the related one, so this grandchildren relation is Oriel's own.
|
|
166
|
+
|
|
150
167
|
## Many to many relationships
|
|
151
168
|
|
|
152
169
|
A user has many roles, and a role belongs to many users. The two tables meet in a third, the pivot table, named after
|
|
@@ -404,6 +421,13 @@ for (const post of posts) {
|
|
|
404
421
|
}
|
|
405
422
|
```
|
|
406
423
|
|
|
424
|
+
With `types/models.d.ts` (see [Inspecting models](eloquent.md#inspecting-models)), relation names are checked:
|
|
425
|
+
`with()`, `load()`, `loadMissing()`, `has()`, `whereHas()` and their kin take a relation of the model, or a path such as
|
|
426
|
+
`'comments.author'` (each segment a relation of the model before it, three deep), and `withCount()` and the other
|
|
427
|
+
aggregates a relation, optionally `'comments as approved_count'`. A name that is a typo, an accessor or one of
|
|
428
|
+
`Model`'s methods is a type error; a name held in a `string` is accepted, as every name is on a model without generated
|
|
429
|
+
types.
|
|
430
|
+
|
|
407
431
|
#### Eager loading multiple relationships
|
|
408
432
|
|
|
409
433
|
```ts
|
|
@@ -447,8 +471,8 @@ const posts = await Post.with({ comments: (query) => query.where('body', 'like',
|
|
|
447
471
|
```
|
|
448
472
|
|
|
449
473
|
A `limit()` or `offset()` there applies to each model, as in Laravel 11: each post below gets its own three latest
|
|
450
|
-
comments. It works for `hasOne`, `hasMany`, their morph forms and `
|
|
451
|
-
3.25+, MySQL 8 and PostgreSQL.
|
|
474
|
+
comments. It works for `hasOne`, `hasMany`, their morph forms, `belongsToMany` and `hasManyThrough`, with the window
|
|
475
|
+
functions of SQLite 3.25+, MySQL 8 and PostgreSQL.
|
|
452
476
|
|
|
453
477
|
```ts
|
|
454
478
|
const posts = await Post.with({ comments: (query) => query.latest().limit(3) }).get()
|
|
@@ -490,7 +514,8 @@ await post.comments().saveMany([Comment.make({ body: 'Another new comment.' }),
|
|
|
490
514
|
|
|
491
515
|
### The `create` method
|
|
492
516
|
|
|
493
|
-
`create()` makes the related model with the foreign key set and saves it, and `createMany()` makes several
|
|
517
|
+
`create()` makes the related model with the foreign key set and saves it, and `createMany()` makes several, returned
|
|
518
|
+
in an Eloquent collection:
|
|
494
519
|
|
|
495
520
|
```ts
|
|
496
521
|
const post = await Post.findOrFail(1)
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
# Eloquent: API resources
|
|
2
|
+
|
|
3
|
+
An API resource sits between your models and the JSON your API answers with: it picks the keys, renames them, adds
|
|
4
|
+
relations when they're loaded, and wraps the whole in `data`, as Laravel's resources do.
|
|
5
|
+
|
|
6
|
+
## Generating resources
|
|
7
|
+
|
|
8
|
+
`make:resource` writes a resource class in `app/Http/Resources`. A name ending in `Collection`, or `--collection`,
|
|
9
|
+
writes a resource collection instead:
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
node oriel make:resource UserResource
|
|
13
|
+
node oriel make:resource UserCollection
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Concept overview
|
|
17
|
+
|
|
18
|
+
A resource extends `JsonResource` and returns its JSON from `toArray()`. The model is `this.resource`:
|
|
19
|
+
|
|
20
|
+
```ts title="app/Http/Resources/UserResource.ts"
|
|
21
|
+
import { JsonResource } from '@orieljs/framework'
|
|
22
|
+
import type { HttpRequest } from '@orieljs/framework/contracts'
|
|
23
|
+
import type { User } from '../../Models/User'
|
|
24
|
+
|
|
25
|
+
export class UserResource extends JsonResource<User> {
|
|
26
|
+
/** Transform the resource into an array. */
|
|
27
|
+
override toArray(_request: HttpRequest) {
|
|
28
|
+
return {
|
|
29
|
+
id: this.resource.id,
|
|
30
|
+
name: this.resource.name,
|
|
31
|
+
email: this.resource.email,
|
|
32
|
+
created_at: this.resource.created_at,
|
|
33
|
+
updated_at: this.resource.updated_at,
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
A route or a controller returns the resource, and the response is its JSON, in a `data` key:
|
|
40
|
+
|
|
41
|
+
```ts title="routes/api.ts"
|
|
42
|
+
// @filename: app/Http/Resources/UserResource.ts
|
|
43
|
+
import { JsonResource } from '@orieljs/framework'
|
|
44
|
+
import type { User } from '../../Models/User'
|
|
45
|
+
export class UserResource extends JsonResource<User> {}
|
|
46
|
+
// ---cut---
|
|
47
|
+
import { Route } from '@orieljs/framework'
|
|
48
|
+
import { UserResource } from '../app/Http/Resources/UserResource'
|
|
49
|
+
import { User } from '../app/Models/User'
|
|
50
|
+
|
|
51
|
+
Route.get('/users/{id}', async (id: string) => UserResource.make(await User.findOrFail(id)))
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Dates come out as a model's do (`2026-01-02T03:04:05.000000Z`), and a resource created during the request answers
|
|
55
|
+
`201`.
|
|
56
|
+
|
|
57
|
+
> [!NOTE]
|
|
58
|
+
> Laravel's resources forward properties to the model, so `$this->name` reads it. TypeScript can't type that, so
|
|
59
|
+
> Oriel's resources read `this.resource.name`.
|
|
60
|
+
|
|
61
|
+
> [!NOTE]
|
|
62
|
+
> `toArray()` is synchronous, as Laravel's: load what it needs before you return the resource. Outside a request (a
|
|
63
|
+
> command, a job), it gets a request for your `APP_URL`, as Laravel's console request.
|
|
64
|
+
|
|
65
|
+
### Resource collections
|
|
66
|
+
|
|
67
|
+
`collection()` makes a collection of resources from a list, a collection or a paginator:
|
|
68
|
+
|
|
69
|
+
```ts title="routes/api.ts"
|
|
70
|
+
// @filename: app/Http/Resources/UserResource.ts
|
|
71
|
+
import { JsonResource } from '@orieljs/framework'
|
|
72
|
+
import type { User } from '../../Models/User'
|
|
73
|
+
export class UserResource extends JsonResource<User> {}
|
|
74
|
+
// ---cut---
|
|
75
|
+
import { Route } from '@orieljs/framework'
|
|
76
|
+
import { UserResource } from '../app/Http/Resources/UserResource'
|
|
77
|
+
import { User } from '../app/Models/User'
|
|
78
|
+
|
|
79
|
+
Route.get('/users', async () => UserResource.collection(await User.all()))
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
A collection of its own extends `ResourceCollection` and names the resource of its items in `collects`:
|
|
83
|
+
|
|
84
|
+
```ts title="app/Http/Resources/UserCollection.ts"
|
|
85
|
+
// @filename: app/Http/Resources/UserResource.ts
|
|
86
|
+
import { JsonResource } from '@orieljs/framework'
|
|
87
|
+
import type { User } from '../../Models/User'
|
|
88
|
+
export class UserResource extends JsonResource<User> {}
|
|
89
|
+
// ---cut---
|
|
90
|
+
import { ResourceCollection } from '@orieljs/framework'
|
|
91
|
+
import { UserResource } from './UserResource'
|
|
92
|
+
|
|
93
|
+
export class UserCollection extends ResourceCollection<UserResource> {
|
|
94
|
+
override collects = UserResource
|
|
95
|
+
}
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
> [!NOTE]
|
|
99
|
+
> Laravel guesses `$collects` from the collection's name (`UserCollection` collects `UserResource`). Oriel's
|
|
100
|
+
> `collects` is an instance field you set; without it, each item keeps its own JSON.
|
|
101
|
+
|
|
102
|
+
## Writing resources
|
|
103
|
+
|
|
104
|
+
### Relationships
|
|
105
|
+
|
|
106
|
+
A relation goes in through its own resource. `whenLoaded()` gives the relation when it was loaded, with `with()` or
|
|
107
|
+
`load()`, and leaves the key out otherwise, so a resource never runs a query of its own:
|
|
108
|
+
|
|
109
|
+
```ts title="app/Http/Resources/PostResource.ts"
|
|
110
|
+
// @filename: app/Http/Resources/UserResource.ts
|
|
111
|
+
import { JsonResource } from '@orieljs/framework'
|
|
112
|
+
import type { User } from '../../Models/User'
|
|
113
|
+
export class UserResource extends JsonResource<User> {}
|
|
114
|
+
// ---cut---
|
|
115
|
+
import { JsonResource } from '@orieljs/framework'
|
|
116
|
+
import type { HttpRequest } from '@orieljs/framework/contracts'
|
|
117
|
+
import type { Post } from '../../Models/Post'
|
|
118
|
+
import { UserResource } from './UserResource'
|
|
119
|
+
|
|
120
|
+
export class PostResource extends JsonResource<Post> {
|
|
121
|
+
override toArray(_request: HttpRequest) {
|
|
122
|
+
return {
|
|
123
|
+
id: this.resource.id,
|
|
124
|
+
title: this.resource.title,
|
|
125
|
+
author: UserResource.make(this.whenLoaded('author')),
|
|
126
|
+
author_name: this.whenLoaded('author', (author) => author.name),
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
`PostResource.collection(this.whenLoaded('comments'))` does the same for a list. With a function, `whenLoaded()` gives
|
|
133
|
+
what the function returns for the loaded models, and `null` for a relation that loaded nothing.
|
|
134
|
+
|
|
135
|
+
### Data wrapping
|
|
136
|
+
|
|
137
|
+
The JSON goes in a `data` key. `withoutWrapping()` leaves it bare, for every resource when you call it on
|
|
138
|
+
`JsonResource`, in a service provider's `boot()`:
|
|
139
|
+
|
|
140
|
+
```ts title="app/Providers/AppServiceProvider.ts"
|
|
141
|
+
import { JsonResource, ServiceProvider } from '@orieljs/framework'
|
|
142
|
+
|
|
143
|
+
export class AppServiceProvider extends ServiceProvider {
|
|
144
|
+
override boot(): void {
|
|
145
|
+
JsonResource.withoutWrapping()
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
`wrap('item')` names another key. A body that has keys beside the data (pagination, `additional()`) keeps the `data`
|
|
151
|
+
key anyway, and a `toArray()` that returns the wrapper key itself isn't wrapped twice.
|
|
152
|
+
|
|
153
|
+
> [!NOTE]
|
|
154
|
+
> Laravel's wrapper is the static `$wrap`. A class can't have a property and a method of one name in TypeScript, so
|
|
155
|
+
> Oriel's is the static `wrapper`, which `wrap()` and `withoutWrapping()` set on the class they're called on.
|
|
156
|
+
|
|
157
|
+
### Pagination
|
|
158
|
+
|
|
159
|
+
A collection of a paginator answers Laravel's `links` and `meta` beside its data:
|
|
160
|
+
|
|
161
|
+
```ts title="routes/api.ts"
|
|
162
|
+
// @filename: app/Http/Resources/UserResource.ts
|
|
163
|
+
import { JsonResource } from '@orieljs/framework'
|
|
164
|
+
import type { User } from '../../Models/User'
|
|
165
|
+
export class UserResource extends JsonResource<User> {}
|
|
166
|
+
// ---cut---
|
|
167
|
+
import { Route } from '@orieljs/framework'
|
|
168
|
+
import { UserResource } from '../app/Http/Resources/UserResource'
|
|
169
|
+
import { User } from '../app/Models/User'
|
|
170
|
+
|
|
171
|
+
Route.get('/users', async () => UserResource.collection(await User.query().paginate()))
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
`links` has `first`, `last`, `prev` and `next`; `meta` has the paginator's other keys (`current_page`, `total`, …).
|
|
175
|
+
`simplePaginate()` and `cursorPaginate()` give theirs.
|
|
176
|
+
|
|
177
|
+
### Conditional attributes
|
|
178
|
+
|
|
179
|
+
`when()` adds a key only when its condition holds; given a function, it calls it then. A third argument is the value
|
|
180
|
+
otherwise. `mergeWhen()` adds several keys at once, spread into the object:
|
|
181
|
+
|
|
182
|
+
```ts title="app/Http/Resources/UserResource.ts"
|
|
183
|
+
import { JsonResource } from '@orieljs/framework'
|
|
184
|
+
import type { HttpRequest } from '@orieljs/framework/contracts'
|
|
185
|
+
import type { User } from '../../Models/User'
|
|
186
|
+
|
|
187
|
+
export class UserResource extends JsonResource<User> {
|
|
188
|
+
override toArray(request: HttpRequest) {
|
|
189
|
+
const admin = request.boolean('admin')
|
|
190
|
+
return {
|
|
191
|
+
id: this.resource.id,
|
|
192
|
+
name: this.resource.name,
|
|
193
|
+
secret: this.when(admin, 'secret-value'),
|
|
194
|
+
verified: this.when(this.resource.email_verified_at !== null, true, false),
|
|
195
|
+
...this.mergeWhen(admin, { email: this.resource.email, created_at: this.resource.created_at }),
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
> [!NOTE]
|
|
202
|
+
> Laravel's `mergeWhen()` returns a value its filter merges. Oriel's returns the keys, or none, for an object spread.
|
|
203
|
+
|
|
204
|
+
### Conditional relationship counts
|
|
205
|
+
|
|
206
|
+
`whenCounted('posts')` gives `posts_count` when `withCount('posts')` or `loadCount('posts')` set it, and leaves the
|
|
207
|
+
key out otherwise.
|
|
208
|
+
|
|
209
|
+
### Adding meta data
|
|
210
|
+
|
|
211
|
+
`additional()` adds keys beside the data for one response, and a `with()` of the resource's own for every one:
|
|
212
|
+
|
|
213
|
+
```ts title="routes/api.ts"
|
|
214
|
+
// @filename: app/Http/Resources/UserResource.ts
|
|
215
|
+
import { JsonResource } from '@orieljs/framework'
|
|
216
|
+
import type { User } from '../../Models/User'
|
|
217
|
+
export class UserResource extends JsonResource<User> {}
|
|
218
|
+
// ---cut---
|
|
219
|
+
import { Route } from '@orieljs/framework'
|
|
220
|
+
import { UserResource } from '../app/Http/Resources/UserResource'
|
|
221
|
+
import { User } from '../app/Models/User'
|
|
222
|
+
|
|
223
|
+
Route.get('/users/{id}', async (id: string) => UserResource.make(await User.findOrFail(id)).additional({ meta: { key: 'value' } }))
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
## Resource responses
|
|
227
|
+
|
|
228
|
+
`response()` gives the response to change, and `withResponse()` in the resource changes every one:
|
|
229
|
+
|
|
230
|
+
```ts title="routes/api.ts"
|
|
231
|
+
// @filename: app/Http/Resources/UserResource.ts
|
|
232
|
+
import { JsonResource } from '@orieljs/framework'
|
|
233
|
+
import type { User } from '../../Models/User'
|
|
234
|
+
export class UserResource extends JsonResource<User> {}
|
|
235
|
+
// ---cut---
|
|
236
|
+
import { Route } from '@orieljs/framework'
|
|
237
|
+
import { UserResource } from '../app/Http/Resources/UserResource'
|
|
238
|
+
import { User } from '../app/Models/User'
|
|
239
|
+
|
|
240
|
+
Route.get('/users/{id}', async (id: string) => UserResource.make(await User.findOrFail(id)).response().setStatusCode(202).header('X-Value', 'True'))
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
## Resources in Inertia pages
|
|
244
|
+
|
|
245
|
+
A resource given as a page prop arrives as its response body, as inertia-laravel sends it: `{ data }`, and `links`
|
|
246
|
+
and `meta` over a paginator. Its type in the page is the body, typed from `toArray()`.
|
|
247
|
+
|
|
248
|
+
> [!NOTE]
|
|
249
|
+
> The page types assume the `data` key; `withoutWrapping()` changes the JSON when the app runs, not the types.
|
|
250
|
+
|
|
251
|
+
> [!NOTE]
|
|
252
|
+
> Not in Oriel yet: `whenHas()`, `whenNotNull()`, `whenAggregated()`, `whenPivotLoaded()`, `merge()`, `unless()`,
|
|
253
|
+
> `preserveKeys` and `paginationInformation()`.
|
package/docs/eloquent.md
CHANGED
|
@@ -36,6 +36,39 @@ node oriel make:model Flight -mfs
|
|
|
36
36
|
`flight.name` is a `string` and `Flight.where('nme', …)` is a type error. `migrate`, `migrate:fresh` and
|
|
37
37
|
`migrate:refresh` run it for you when they finish, outside production, in an app with a `types` folder, as a new app
|
|
38
38
|
has. It reads your tables, so run your migrations first. A model must be a named export of its file to get its types.
|
|
39
|
+
The file also lists each model's `hidden`, `visible` and `appends` names, which [Inertia page props](inertia.md)
|
|
40
|
+
follow, and `with()` and `load()` type their results with the relations they load.
|
|
41
|
+
|
|
42
|
+
With those types, a write is checked too: `create()`, `make()`, `fill()`, `forceFill()`, `update()`, `forceCreate()`,
|
|
43
|
+
`firstOrNew()`, `firstOrCreate()` and `updateOrCreate()` refuse a key that is neither a column nor a set mutator's
|
|
44
|
+
(`setHeadlineAttribute()` makes `headline` one), and a value of the wrong type, such as a list for a `string` column.
|
|
45
|
+
A value Oriel turns into the column's type when it saves is accepted: a date as text or a timestamp, a number as text,
|
|
46
|
+
`0` or `1` for a boolean, an enum member for its value. A key known only when the app runs goes in a
|
|
47
|
+
`Record<string, unknown>`, and a validated field without a type rule, typed `unknown`, is accepted as it is. A model
|
|
48
|
+
without generated types takes any attributes, as before.
|
|
49
|
+
|
|
50
|
+
A date is accepted for a column typed as text (a date column without a cast), and an expression for any column:
|
|
51
|
+
`update({ votes: raw('votes + 1') })`. A validated field that isn't a column is refused, though `fill()` would drop it:
|
|
52
|
+
leave it out by name, so the write says what it keeps.
|
|
53
|
+
|
|
54
|
+
```ts title="app/Http/Controllers/PostController.ts"
|
|
55
|
+
import { request } from '@orieljs/framework'
|
|
56
|
+
import { Post } from '../../Models/Post'
|
|
57
|
+
import { Controller } from './Controller'
|
|
58
|
+
|
|
59
|
+
export class PostController extends Controller {
|
|
60
|
+
async store() {
|
|
61
|
+
const { tags, ...attributes } = await request().validate({ title: 'required|string', body: 'required|string', tags: 'array' })
|
|
62
|
+
const post = await Post.create(attributes)
|
|
63
|
+
return { post, tags }
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
A model's own methods are checked as its callers are: `this.update({ … })` and `this.load('comments')` inside `Post`
|
|
69
|
+
read `Post`'s generated types (the file gives each model a `$self`). A method that returns `this.load(…)` needs its
|
|
70
|
+
return type written, as TypeScript can't infer a type that depends on the method's own model. A generic helper's
|
|
71
|
+
model, `M extends Model`, takes any attributes and relations.
|
|
39
72
|
|
|
40
73
|
## Model conventions
|
|
41
74
|
|
|
@@ -226,6 +259,14 @@ Given an array of keys, `find()` returns the models it found, as a collection:
|
|
|
226
259
|
const flights = await Flight.find([1, 2, 3])
|
|
227
260
|
```
|
|
228
261
|
|
|
262
|
+
A key typed `unknown`, such as a request's input, may be an array when the request runs (`?id[]=1`), so `find()` and
|
|
263
|
+
`findOrFail()` given one are typed as a model or a collection. Pass a key of a known type, `String(id)` for one, or
|
|
264
|
+
check the result:
|
|
265
|
+
|
|
266
|
+
```ts
|
|
267
|
+
const flight = await Flight.find(String(request().input('id')))
|
|
268
|
+
```
|
|
269
|
+
|
|
229
270
|
#### Not found exceptions
|
|
230
271
|
|
|
231
272
|
`findOrFail()` and `firstOrFail()` throw a `ModelNotFoundError` when there is no model. Uncaught, it answers 404:
|
package/docs/errors.md
CHANGED
|
@@ -98,8 +98,8 @@ context to every log entry.
|
|
|
98
98
|
|
|
99
99
|
#### Reporting an error yourself
|
|
100
100
|
|
|
101
|
-
|
|
102
|
-
|
|
101
|
+
To report an error and carry on with the request, pass it to `report()`. The handler logs it, unless it's one the
|
|
102
|
+
handler doesn't report:
|
|
103
103
|
|
|
104
104
|
```ts
|
|
105
105
|
declare function validateTheValue(value: string): void
|
|
@@ -110,13 +110,22 @@ async function isValid(value: string): Promise<boolean> {
|
|
|
110
110
|
|
|
111
111
|
return true
|
|
112
112
|
} catch (error) {
|
|
113
|
-
await
|
|
113
|
+
await report(error)
|
|
114
114
|
|
|
115
115
|
return false
|
|
116
116
|
}
|
|
117
117
|
}
|
|
118
118
|
```
|
|
119
119
|
|
|
120
|
+
`rescue()` does the same in one call: it returns what the function returns, or, when it throws, reports the error and
|
|
121
|
+
returns the fallback:
|
|
122
|
+
|
|
123
|
+
```ts
|
|
124
|
+
declare function validateTheValue(value: string): boolean
|
|
125
|
+
// ---cut---
|
|
126
|
+
const valid = await rescue(() => validateTheValue('value'), false)
|
|
127
|
+
```
|
|
128
|
+
|
|
120
129
|
### Exception log levels
|
|
121
130
|
|
|
122
131
|
Every report is logged at the `error` level. Laravel's `level()`, which changes the level by the error's class, isn't
|
package/docs/events.md
CHANGED
|
@@ -331,6 +331,8 @@ events.assertDispatchedTimes(OrderShipped, 1)
|
|
|
331
331
|
events.assertNotDispatched('order.cancelled')
|
|
332
332
|
```
|
|
333
333
|
|
|
334
|
-
The fake also has `assertNothingDispatched()`, `assertListening(event, listener)` and `dispatched(event)`.
|
|
334
|
+
The fake also has `assertNothingDispatched()`, `assertListening(event, listener)` and `dispatched(event)`. Without a
|
|
335
|
+
list, it records Oriel's own events too, such as the queue's [job events](queues.md#job-events) and Auth's, so
|
|
336
|
+
`assertNothingDispatched()` fails when a job ran: give `Event.fake()` the events you mean.
|
|
335
337
|
`EventFake.fakeExcept([OrderShipped])`, from `@orieljs/framework/testing`, fakes every event but the ones you list.
|
|
336
338
|
[Fakes](mocking.md) covers fakes in full.
|
package/docs/facades.md
CHANGED
|
@@ -243,6 +243,7 @@ and `app().make()` also take:
|
|
|
243
243
|
| `Schema` | The default database connection's schema builder | `SchemaBuilderToken`, from `@orieljs/framework` |
|
|
244
244
|
| `Session` | The current request's session; `Session.extend()` | `Session` (scoped) |
|
|
245
245
|
| `Storage` | The filesystem manager: `disk()`, and the default disk's methods | `FilesystemManager` |
|
|
246
|
+
| `TwoFactor` | Two-factor login: `attempt()`, `verify()`, and setting up an authenticator app ([Two-factor login](two-factor.md)) | `TwoFactorManager`, from `@orieljs/framework` |
|
|
246
247
|
| `Validator` | The validation factory: `make()`, `extend()` | `ValidationFactory` |
|
|
247
248
|
| `View` | The view factory: `make()`, `share()`, `composer()` | `ViewFactory` |
|
|
248
249
|
|
package/docs/helpers.md
CHANGED
|
@@ -57,8 +57,8 @@ export class PostController extends Controller {
|
|
|
57
57
|
[`decrypt()`](#decrypt), [`dispatch()`](#dispatch), [`dispatchSync()`](#dispatchsync), [`encrypt()`](#encrypt),
|
|
58
58
|
[`env()`](#env), [`errors()`](#errors), [`event()`](#event), [`fake()`](#fake), [`inertia()`](#inertia),
|
|
59
59
|
[`inject()`](#inject), [`live()`](#live), [`logger()`](#logger), [`methodField()`](#methodfield), [`now()`](#now),
|
|
60
|
-
[`old()`](#old), [`raw()`](#raw), [`redirect()`](#redirect), [`
|
|
61
|
-
[`retry()`](#retry), [`session()`](#session), [`tap()`](#tap), [`value()`](#value), [`view()`](#view),
|
|
60
|
+
[`old()`](#old), [`raw()`](#raw), [`redirect()`](#redirect), [`report()`](#report), [`request()`](#request),
|
|
61
|
+
[`rescue()`](#rescue), [`response()`](#response), [`retry()`](#retry), [`session()`](#session), [`tap()`](#tap), [`value()`](#value), [`view()`](#view),
|
|
62
62
|
[`vite()`](#vite)
|
|
63
63
|
|
|
64
64
|
**Other utilities:** [Pipeline](#pipeline)
|
|
@@ -1097,6 +1097,17 @@ const toRoute = redirect().route('dashboard')
|
|
|
1097
1097
|
|
|
1098
1098
|
[Responses](responses.md) covers redirects.
|
|
1099
1099
|
|
|
1100
|
+
#### `report()`
|
|
1101
|
+
|
|
1102
|
+
`report()` hands an error to your [exception handler](errors.md), which logs it unless it is one the handler doesn't
|
|
1103
|
+
report, and your code carries on:
|
|
1104
|
+
|
|
1105
|
+
```ts
|
|
1106
|
+
declare const error: unknown
|
|
1107
|
+
// ---cut---
|
|
1108
|
+
await report(error)
|
|
1109
|
+
```
|
|
1110
|
+
|
|
1100
1111
|
#### `request()`
|
|
1101
1112
|
|
|
1102
1113
|
`request()` returns the current request. With a key, it returns that input, or the default:
|
|
@@ -1109,6 +1120,26 @@ const name = request('name', 'Guest')
|
|
|
1109
1120
|
|
|
1110
1121
|
[Requests](requests.md) covers the request.
|
|
1111
1122
|
|
|
1123
|
+
#### `rescue()`
|
|
1124
|
+
|
|
1125
|
+
`rescue()` runs a function and returns what it returns. If the function throws, the error is reported and `rescue()`
|
|
1126
|
+
returns the second argument instead, `undefined` without one:
|
|
1127
|
+
|
|
1128
|
+
```ts
|
|
1129
|
+
declare const fetchRates: () => Promise<number[]>
|
|
1130
|
+
// ---cut---
|
|
1131
|
+
const rates = await rescue(() => fetchRates(), [])
|
|
1132
|
+
```
|
|
1133
|
+
|
|
1134
|
+
The second argument may be a function of the error, which `rescue()` calls for the value. The third, `false`, reports
|
|
1135
|
+
nothing, or a function of the error decides:
|
|
1136
|
+
|
|
1137
|
+
```ts
|
|
1138
|
+
declare const fetchRates: () => Promise<number[]>
|
|
1139
|
+
// ---cut---
|
|
1140
|
+
const rates = await rescue(() => fetchRates(), (error) => [], (error) => !(error instanceof TypeError))
|
|
1141
|
+
```
|
|
1142
|
+
|
|
1112
1143
|
#### `response()`
|
|
1113
1144
|
|
|
1114
1145
|
`response()` returns a `Response` with the content, a status and headers. With no arguments, it returns the response
|
package/docs/inertia.md
CHANGED
|
@@ -48,8 +48,14 @@ export class PostController extends Controller {
|
|
|
48
48
|
|
|
49
49
|
The component's name is its path under `resources/js/pages`, without the extension: `Posts/Index` is
|
|
50
50
|
`resources/js/pages/Posts/Index.tsx` (or `.vue`, `.svelte`, `.component.ts`). Models in the props reach the page as
|
|
51
|
-
their JSON,
|
|
52
|
-
|
|
51
|
+
their JSON, and with `types/models.d.ts` the page's prop types follow it: a model's columns, its `appends` (typed by
|
|
52
|
+
their accessors) and the relations `with()` or `load()` loaded, under their snake_case keys (`coAuthor` is
|
|
53
|
+
`co_author`; `with('author:id,name')` gives only those columns). A `visible` list narrows them, leaving out a relation
|
|
54
|
+
it doesn't name, and `hidden` leaves out the columns, appends and relations it names, as `toJSON()` does. What
|
|
55
|
+
changes at run time, `makeVisible()` or `append()`, arrives on the page but isn't in its types, and a
|
|
56
|
+
`await post.load('author')` on its own line can't change `post`'s type: pass what `load()` returns. An
|
|
57
|
+
[API resource](eloquent-resources.md) given as a prop arrives as its response body, `{ data }` (with `links` and
|
|
58
|
+
`meta` over a paginator), as inertia-laravel sends it, and is typed from its `toArray()`.
|
|
53
59
|
|
|
54
60
|
A page with no data of its own needs no controller: `Route.inertia()` routes a URI to it.
|
|
55
61
|
|
|
@@ -458,6 +464,9 @@ Each adapter has Inertia's components and hooks for the props above, under Inert
|
|
|
458
464
|
|
|
459
465
|
They work as Inertia's documentation describes, at [inertiajs.com](https://inertiajs.com).
|
|
460
466
|
|
|
467
|
+
A page can be a [Live](live.md) page instead: [Live in an Inertia app](live.md#live-in-an-inertia-app) gives it its own
|
|
468
|
+
layout and client beside the Inertia pages.
|
|
469
|
+
|
|
461
470
|
## Server-side rendering
|
|
462
471
|
|
|
463
472
|
The React, Vue, Svelte and Solid kits render the first visit on the server, so it arrives as HTML. `npm run build`
|
package/docs/installation.md
CHANGED
|
@@ -10,6 +10,8 @@ migrations, validation, authentication, queues, mail and the scheduler, under La
|
|
|
10
10
|
pages in React, Vue, Svelte, Solid or Angular, or Live components rendered on the server, and the same app can serve a
|
|
11
11
|
JSON API.
|
|
12
12
|
|
|
13
|
+
Coming from Laravel? [Oriel for Laravel developers](laravel.md) gathers what is different.
|
|
14
|
+
|
|
13
15
|
## Creating an Oriel application
|
|
14
16
|
|
|
15
17
|
### Requirements
|
|
@@ -142,8 +144,8 @@ restart it.
|
|
|
142
144
|
> restarted, and it doesn't reload the page again once the new server is up. After `serve --watch` prints
|
|
143
145
|
> "Restarting server...", reload the page once more to see your change.
|
|
144
146
|
|
|
145
|
-
`oriel serve` listens on `127.0.0.1`, on port 8000 or the one
|
|
146
|
-
others:
|
|
147
|
+
`oriel serve` listens on `127.0.0.1`, or the address the `HOST` variable names, on port 8000 or the one `PORT` names.
|
|
148
|
+
`--host` and `--port` choose others:
|
|
147
149
|
|
|
148
150
|
```sh
|
|
149
151
|
node oriel serve --watch --host=0.0.0.0 --port=3000
|
package/docs/laravel.md
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# Oriel for Laravel developers
|
|
2
|
+
|
|
3
|
+
Oriel follows Laravel's structure, names and docs, so most of what you know carries over: routes, controllers,
|
|
4
|
+
middleware, Eloquent, migrations, queues, mail and the rest work the way their Laravel pages say. This page gathers
|
|
5
|
+
what is different because Oriel is TypeScript, and what isn't in Oriel yet.
|
|
6
|
+
|
|
7
|
+
## On the first day
|
|
8
|
+
|
|
9
|
+
| In Laravel | In Oriel |
|
|
10
|
+
| --- | --- |
|
|
11
|
+
| Constructor injection: `__construct(AppleMusic $apple)` | `inject()` in a field: `readonly #apple = inject(AppleMusic)`, as [the container](container.md#injecting-with-inject) shows |
|
|
12
|
+
| `$request->user()` | `await request().user()`: finding the user is a database query, so it returns a promise |
|
|
13
|
+
| `Route::group(...)` | `Route.group()` returns a promise, settled when its callback has run: await it when the callback is `async` ([route groups](routing.md#route-groups)) |
|
|
14
|
+
| A route that returns `null` answers an empty 200 | It answers an empty 204, "No Content" ([responses](responses.md)) |
|
|
15
|
+
| `config([...])`, `app()->bind()` and `View::share()` in a request | Change it for every later and concurrent request, as the app answers them all from one process; in `local` they log a warning ([configuration](configuration.md#accessing-configuration-values)) |
|
|
16
|
+
| `now()` is a Carbon instance | `now()` is a `Date` ([helpers](helpers.md#now)) |
|
|
17
|
+
| `$post->author` | `post.getRelation('author')` after `with('author')` or `load('author')`, or `await post.author().first()` ([relationships](eloquent-relationships.md#relationship-methods-vs-reading-a-relation)) |
|
|
18
|
+
| Blade templates | TSX views, or a React, Vue, Svelte, Solid or Angular page through Inertia ([views](views.md)) |
|
|
19
|
+
| Livewire's `wire:` attributes | [Live](live.md)'s `o:` attributes: `o:click`, `o:model`, `o:navigate`… |
|
|
20
|
+
|
|
21
|
+
## Error names
|
|
22
|
+
|
|
23
|
+
Laravel's `*Exception` classes are `*Error` in Oriel, as JavaScript names its errors: `ModelNotFoundError`,
|
|
24
|
+
`AuthenticationError`, `AuthorizationError`, `TokenMismatchError`, `ThrottleRequestsError`, `NotFoundHttpError`,
|
|
25
|
+
`MethodNotAllowedHttpError`, `QueryError`, `InvalidSignatureError`, `MassAssignmentError`, `RelationNotFoundError`,
|
|
26
|
+
`MaxAttemptsExceededError`, `BindingResolutionError`, `LockTimeoutError`, `DecryptError`, `MissingAppKeyError`,
|
|
27
|
+
`ItemNotFoundError` and `MultipleItemsFoundError`. Symfony's `RouteNotFoundException` is `RouteNotDefinedError`.
|
|
28
|
+
|
|
29
|
+
Three keep Laravel's names, since you throw them yourself: `ValidationException`, `HttpException` and
|
|
30
|
+
`HttpResponseException`. [Errors](errors.md) covers reporting and rendering them.
|
|
31
|
+
|
|
32
|
+
## Commands Oriel doesn't have
|
|
33
|
+
|
|
34
|
+
`node oriel list` shows the commands an app has ([console](console.md#available-commands)). Of the ones a Laravel deploy
|
|
35
|
+
script or a first week reaches for, these aren't there:
|
|
36
|
+
|
|
37
|
+
| Laravel | In Oriel |
|
|
38
|
+
| --- | --- |
|
|
39
|
+
| `optimize`, `config:cache`, `route:cache`, `view:cache`, `event:cache`, and their `:clear` | Nothing to cache: drop them from your deploy script. Oriel reads its config and routes as code when the app starts |
|
|
40
|
+
| `auth:clear-resets` | The token repository's `deleteExpired()`, in a scheduled task ([deleting expired tokens](passwords.md#deleting-expired-tokens)) |
|
|
41
|
+
| `vendor:publish` | Write the config file yourself: each package's page shows its keys |
|
|
42
|
+
| `make:component`, `make:exception`, `make:enum` | Write the file: a view component is a function in `resources/views/components`, an error a class that extends `Error`, an enum a TypeScript `enum` or a union |
|
|
43
|
+
| `db:show` | Your database's own client |
|
|
44
|
+
| `model:prune` | A [scheduled task](scheduling.md) that deletes the rows |
|
|
45
|
+
| `queue:prune-failed` | `queue:flush` empties the failed jobs; a scheduled query deletes old ones |
|
|
46
|
+
|
|
47
|
+
## New in alpha.16, as in Laravel
|
|
48
|
+
|
|
49
|
+
- [CORS](routing.md#cross-origin-resource-sharing-cors): `api/*` routes answer cross-origin requests, as in Laravel 11.
|
|
50
|
+
- [Maintenance mode](configuration.md#maintenance-mode): `down` and `up`.
|
|
51
|
+
- [Global options](console.md#global-options) on every command: `--env`, `--quiet`, `--no-interaction`, `--version`…
|
|
52
|
+
- [`cache:clear`](cache.md#removing-items-from-the-cache) and `cache:forget`.
|
|
53
|
+
- [Named rate limiters](routing.md#defining-rate-limiters): `RateLimiter.for()` and `throttle:<name>`.
|
|
54
|
+
- Form requests' [`prepareForValidation()`](validation.md#preparing-input-for-validation),
|
|
55
|
+
[`after()` and `stopOnFirstFailure`](validation.md#additional-checks-and-stopping-early), and
|
|
56
|
+
[`safe()`](validation.md#working-with-validated-input).
|
|
57
|
+
- [Jobs dispatched after the commit](queues.md#jobs-and-database-transactions), and
|
|
58
|
+
[`DB.afterCommit()`](database.md#running-code-after-the-commit).
|
|
59
|
+
- The framework's events: [job events](queues.md#job-events), [mail events](mail.md#mail-events),
|
|
60
|
+
[notification events](notifications.md#notification-events) and [scheduled task events](scheduling.md#events).
|
|
61
|
+
- [API resources](eloquent-resources.md): `JsonResource` and `ResourceCollection`.
|
|
62
|
+
- In the browser starter kits, [two-factor authentication](starter-kits.md#two-factor-authentication) and
|
|
63
|
+
[password confirmation](authentication.md#password-confirmation).
|
|
64
|
+
|
|
65
|
+
## More differences
|
|
66
|
+
|
|
67
|
+
Each page names its own differences from Laravel in a note. The pages with the most are
|
|
68
|
+
[console](console.md), [validation](validation.md), [mail](mail.md), [helpers](helpers.md), [cache](cache.md),
|
|
69
|
+
[middleware](middleware.md), [Eloquent relationships](eloquent-relationships.md) and [database](database.md).
|
|
70
|
+
[Live](live.md#differences-from-livewire) lists how it differs from Livewire, and the
|
|
71
|
+
[upgrade guide](upgrade.md) what changed between Oriel's releases.
|
package/docs/lifecycle.md
CHANGED
|
@@ -107,7 +107,8 @@ Application.configure({ basePath: '.' })
|
|
|
107
107
|
`app.handle()` opens a request scope, and the kernel binds the request in it: everything resolved for this request,
|
|
108
108
|
such as the request, its session and its cookie jar, lives in the scope and is dropped with it. The global middleware
|
|
109
109
|
run first: `RenameClasses`, outermost, which renames the classes of a [scrambled](vite.md#class-name-scrambling) build
|
|
110
|
-
in the pages that leave, then `
|
|
110
|
+
in the pages that leave, then `HandleCors`, which answers [CORS](routing.md#cross-origin-resource-sharing-cors) on the
|
|
111
|
+
API's paths, `TrimStrings`, `ConvertEmptyStringsToNull`, and those you add with
|
|
111
112
|
[`withMiddleware()`](middleware.md#global-middleware).
|
|
112
113
|
|
|
113
114
|
The router then finds the route. A path no route matches is a 404, and a path whose routes don't take the request's
|
|
@@ -142,7 +143,9 @@ The server writes the response, and then the app terminates the request, still i
|
|
|
142
143
|
2. the `terminating()` callbacks run, in the order they were added. One of them is the framework's: it runs the jobs
|
|
143
144
|
you dispatched with [`dispatchAfterResponse()`](queues.md#running-a-job-now).
|
|
144
145
|
|
|
145
|
-
On Node, this starts once the response is written; on Bun, right after the response is handed to the server.
|
|
146
|
+
On Node, this starts once the response is written; on Bun, right after the response is handed to the server. On
|
|
147
|
+
Vercel, the function hands this work to Vercel's `waitUntil()` and returns at once, so the visitor's connection is
|
|
148
|
+
free while it runs (see [What changes on Vercel](deployment.md#what-changes-on-vercel)).
|
|
146
149
|
|
|
147
150
|
Register a terminating callback in a service provider. It receives no arguments:
|
|
148
151
|
|