@orieljs/framework 0.1.0-alpha.14 → 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-X4N74JDO.js → chunk-3QHTNAMZ.js} +6 -4
- package/dist/{chunk-X4N74JDO.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-UBSIBGXR.js → chunk-MG4YEWMV.js} +1351 -497
- package/dist/chunk-MG4YEWMV.js.map +1 -0
- package/dist/{chunk-4NCMY3ZS.js → chunk-YNDMTTWL.js} +121 -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 +2838 -2101
- package/dist/index.js +1805 -647
- 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 +27 -2
- package/docs/deployment.md +42 -18
- package/docs/eloquent-factories.md +7 -3
- package/docs/eloquent-mutators.md +2 -1
- package/docs/eloquent-observers.md +2 -0
- package/docs/eloquent-relationships.md +34 -1
- package/docs/eloquent-resources.md +253 -0
- package/docs/eloquent.md +47 -0
- package/docs/errors.md +12 -3
- package/docs/events.md +3 -1
- package/docs/export.md +3 -3
- package/docs/facades.md +1 -0
- package/docs/helpers.md +33 -2
- package/docs/inertia.md +11 -1
- package/docs/installation.md +6 -2
- package/docs/laravel.md +71 -0
- package/docs/lifecycle.md +9 -5
- package/docs/live.md +88 -8
- package/docs/mail.md +32 -5
- package/docs/middleware.md +20 -12
- package/docs/navigation.json +12 -0
- package/docs/notifications.md +22 -0
- package/docs/pagination.md +4 -0
- package/docs/passwords.md +4 -6
- package/docs/providers.md +4 -6
- package/docs/queues.md +70 -2
- package/docs/requests.md +42 -3
- package/docs/responses.md +9 -5
- package/docs/routing.md +118 -7
- package/docs/scheduling.md +27 -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 +38 -4
- package/docs/upgrade.md +182 -0
- package/docs/validation.md +70 -9
- package/docs/views.md +1 -0
- package/docs/vite.md +20 -1
- package/package.json +43 -35
- package/dist/chunk-4NCMY3ZS.js.map +0 -1
- package/dist/chunk-BKA5F3TH.js.map +0 -1
- package/dist/chunk-UBSIBGXR.js.map +0 -1
|
@@ -148,8 +148,8 @@ class UserFactory extends Factory<User> {
|
|
|
148
148
|
|
|
149
149
|
### Instantiating models
|
|
150
150
|
|
|
151
|
-
`make()` makes models without saving them. With `count()`, or a number given to `factory()`, it makes
|
|
152
|
-
|
|
151
|
+
`make()` makes models without saving them. With `count()`, or a number given to `factory()`, it makes the model's
|
|
152
|
+
[Eloquent collection](eloquent-collections.md), as Laravel's, so `modelKeys()` and `load()` are on it:
|
|
153
153
|
|
|
154
154
|
```ts
|
|
155
155
|
const user = await User.factory().make()
|
|
@@ -184,7 +184,8 @@ const users = await User.factory().count(3).create()
|
|
|
184
184
|
```
|
|
185
185
|
|
|
186
186
|
`makeOne()` and `createOne()` always return one model, typed as one. `createMany()` takes a number, or a list of
|
|
187
|
-
attributes, one model each. `raw()` returns the attributes without making a
|
|
187
|
+
attributes, one model each, and returns them in an Eloquent collection. `raw()` returns the attributes without making a
|
|
188
|
+
model.
|
|
188
189
|
|
|
189
190
|
### Sequences
|
|
190
191
|
|
|
@@ -219,6 +220,9 @@ const user = await User.factory().createOne()
|
|
|
219
220
|
const posts = await Post.factory().count(3).for(user, 'author').create()
|
|
220
221
|
```
|
|
221
222
|
|
|
223
|
+
A parent's factory creates it again at each `create()`, so a factory you keep in a variable and use in several tests
|
|
224
|
+
never points at a parent an earlier test rolled back.
|
|
225
|
+
|
|
222
226
|
An attribute of a factory can also be another factory: the factory creates that model, and the attribute takes its
|
|
223
227
|
key, as `author_id: UserFactory.new()` does in a post's definition.
|
|
224
228
|
|
|
@@ -156,7 +156,8 @@ export class User extends Model {
|
|
|
156
156
|
```
|
|
157
157
|
|
|
158
158
|
Until `save()`, reading the attribute gives what you set. A value that is already a hash made with your current
|
|
159
|
-
settings is kept as it is; one made with other settings is refused.
|
|
159
|
+
settings is kept as it is; one made with other settings is refused. A new app's `User` casts `password` this way, so
|
|
160
|
+
`User.create({ password: 'secret' })` stores a hash, and a password hashed with `Hash.make()` first is kept.
|
|
160
161
|
|
|
161
162
|
> [!NOTE]
|
|
162
163
|
> Laravel hashes as soon as you set the attribute. Hashing is asynchronous in Oriel, so it happens in `save()`, before
|
|
@@ -19,6 +19,8 @@ A model dispatches these events, each with the model:
|
|
|
19
19
|
|
|
20
20
|
Creating a model dispatches `saving`, `creating`, `created`, then `saved`; updating one dispatches `saving`,
|
|
21
21
|
`updating`, `updated`, then `saved`. Saving a model with no changes dispatches only `saving` and `saved`.
|
|
22
|
+
`increment()` and `decrement()` on a model dispatch `updating` and `updated`, as in Laravel, with the new value already on
|
|
23
|
+
the model; an `updating` listener that returns `false` stops them.
|
|
22
24
|
|
|
23
25
|
A listener of a `-ing` event that returns `false` stops the operation: the model isn't saved, deleted or restored.
|
|
24
26
|
|
|
@@ -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
|
|
@@ -446,6 +470,14 @@ Give `with()` an object to add conditions to a relation's query:
|
|
|
446
470
|
const posts = await Post.with({ comments: (query) => query.where('body', 'like', '%code%') }).get()
|
|
447
471
|
```
|
|
448
472
|
|
|
473
|
+
A `limit()` or `offset()` there applies to each model, as in Laravel 11: each post below gets its own three latest
|
|
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.
|
|
476
|
+
|
|
477
|
+
```ts
|
|
478
|
+
const posts = await Post.with({ comments: (query) => query.latest().limit(3) }).get()
|
|
479
|
+
```
|
|
480
|
+
|
|
449
481
|
### Lazy eager loading
|
|
450
482
|
|
|
451
483
|
`load()` loads relations on a model you already have, and `loadMissing()` loads only those that aren't loaded yet:
|
|
@@ -482,7 +514,8 @@ await post.comments().saveMany([Comment.make({ body: 'Another new comment.' }),
|
|
|
482
514
|
|
|
483
515
|
### The `create` method
|
|
484
516
|
|
|
485
|
-
`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:
|
|
486
519
|
|
|
487
520
|
```ts
|
|
488
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
|
|
|
@@ -220,6 +253,20 @@ const active = await Flight.where('active', 1).first()
|
|
|
220
253
|
const first = await Flight.firstWhere('active', 1)
|
|
221
254
|
```
|
|
222
255
|
|
|
256
|
+
Given an array of keys, `find()` returns the models it found, as a collection:
|
|
257
|
+
|
|
258
|
+
```ts
|
|
259
|
+
const flights = await Flight.find([1, 2, 3])
|
|
260
|
+
```
|
|
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
|
+
|
|
223
270
|
#### Not found exceptions
|
|
224
271
|
|
|
225
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/export.md
CHANGED
|
@@ -47,7 +47,7 @@ site, a `mailto:` or `tel:` link, or a link to a file in `public/` is left alone
|
|
|
47
47
|
ignored, so `/docs/routing?tab=1#named` exports `/docs/routing`.
|
|
48
48
|
|
|
49
49
|
Pages nothing links to, such as a feed, are listed in `paths`. Pages to leave out are listed in `exclude`, where `*`
|
|
50
|
-
matches anything. A redirect is listed in the command's output and not written;
|
|
50
|
+
matches anything. A redirect is listed in the command's output and not written; the page it points at is exported, even when nothing else
|
|
51
51
|
links to it.
|
|
52
52
|
|
|
53
53
|
A page that answers with an error (4xx or 5xx) stops the export and names the page. The site is written into a
|
|
@@ -80,8 +80,8 @@ assets among them, are copied into the folder first.
|
|
|
80
80
|
|
|
81
81
|
## The 404 page
|
|
82
82
|
|
|
83
|
-
|
|
84
|
-
and GitHub Pages show that file for an address that doesn't exist.
|
|
83
|
+
The export writes your app's 404 page as `404.html`: yours (`resources/views/errors/404.tsx`), or Oriel's own when you
|
|
84
|
+
have none. Vercel, Netlify and GitHub Pages show that file for an address that doesn't exist.
|
|
85
85
|
|
|
86
86
|
## Hosting the files
|
|
87
87
|
|
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,7 +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,
|
|
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()`.
|
|
52
59
|
|
|
53
60
|
A page with no data of its own needs no controller: `Route.inertia()` routes a URI to it.
|
|
54
61
|
|
|
@@ -457,6 +464,9 @@ Each adapter has Inertia's components and hooks for the props above, under Inert
|
|
|
457
464
|
|
|
458
465
|
They work as Inertia's documentation describes, at [inertiajs.com](https://inertiajs.com).
|
|
459
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
|
+
|
|
460
470
|
## Server-side rendering
|
|
461
471
|
|
|
462
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
|
|
@@ -35,6 +37,8 @@ yarn create @orieljs blog
|
|
|
35
37
|
```
|
|
36
38
|
|
|
37
39
|
`npm create oriel-app` is the same installer under another name: both run `create-oriel-app` at the same version.
|
|
40
|
+
A new app takes each Oriel package at that exact version, so a later release changes nothing until you move to it, as
|
|
41
|
+
the [upgrade guide](upgrade.md) says.
|
|
38
42
|
|
|
39
43
|
The installer asks for the app's name, unless you gave one, then for a stack (Inertia, Live or an API) and, for Inertia,
|
|
40
44
|
its framework, and last whether to install the dependencies and start a git repository. The name is the last part of the
|
|
@@ -140,8 +144,8 @@ restart it.
|
|
|
140
144
|
> restarted, and it doesn't reload the page again once the new server is up. After `serve --watch` prints
|
|
141
145
|
> "Restarting server...", reload the page once more to see your change.
|
|
142
146
|
|
|
143
|
-
`oriel serve` listens on `127.0.0.1`, on port 8000 or the one
|
|
144
|
-
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:
|
|
145
149
|
|
|
146
150
|
```sh
|
|
147
151
|
node oriel serve --watch --host=0.0.0.0 --port=3000
|