@holoyan/adonisjs-polymorphic 0.1.0 → 0.1.1
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/README.md +25 -32
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -149,8 +149,8 @@ export default class Video extends BaseModel {
|
|
|
149
149
|
**Eager load (preload):**
|
|
150
150
|
|
|
151
151
|
```ts
|
|
152
|
-
const post = await
|
|
153
|
-
.preload('image')
|
|
152
|
+
const post = await Post.query()
|
|
153
|
+
.preload('image' as any)
|
|
154
154
|
.firstOrFail()
|
|
155
155
|
|
|
156
156
|
console.log(post.image) // Image | null
|
|
@@ -160,8 +160,8 @@ console.log(post.image?.url) // 'photo.jpg'
|
|
|
160
160
|
**Preload multiple parents at once:**
|
|
161
161
|
|
|
162
162
|
```ts
|
|
163
|
-
const posts = await
|
|
164
|
-
.preload('image') as Post[]
|
|
163
|
+
const posts = await Post.query()
|
|
164
|
+
.preload('image' as any) as Post[]
|
|
165
165
|
|
|
166
166
|
// One SQL query — no N+1
|
|
167
167
|
// SELECT * FROM images WHERE imageable_type = 'posts' AND imageable_id IN (1, 2, 3)
|
|
@@ -170,8 +170,7 @@ const posts = await (Post.query() as any)
|
|
|
170
170
|
**Ad-hoc query:**
|
|
171
171
|
|
|
172
172
|
```ts
|
|
173
|
-
const image = await (
|
|
174
|
-
.related('image')
|
|
173
|
+
const image = await post.related('image' as any)
|
|
175
174
|
.query()
|
|
176
175
|
.firstOrFail()
|
|
177
176
|
```
|
|
@@ -182,8 +181,7 @@ const image = await (post as any)
|
|
|
182
181
|
|
|
183
182
|
```ts
|
|
184
183
|
// imageableType and imageableId are set automatically
|
|
185
|
-
const image = await (
|
|
186
|
-
.related('image')
|
|
184
|
+
const image = await post.related('image' as any)
|
|
187
185
|
.create({ url: 'photo.jpg' })
|
|
188
186
|
```
|
|
189
187
|
|
|
@@ -193,22 +191,20 @@ const image = await (post as any)
|
|
|
193
191
|
const image = new Image()
|
|
194
192
|
image.url = 'photo.jpg'
|
|
195
193
|
|
|
196
|
-
await
|
|
194
|
+
await post.related('image' as any).save(image)
|
|
197
195
|
```
|
|
198
196
|
|
|
199
197
|
**Find or create:**
|
|
200
198
|
|
|
201
199
|
```ts
|
|
202
|
-
const image = await (
|
|
203
|
-
.related('image')
|
|
200
|
+
const image = await post.related('image' as any)
|
|
204
201
|
.firstOrCreate({ url: 'photo.jpg' })
|
|
205
202
|
```
|
|
206
203
|
|
|
207
204
|
**Update or create:**
|
|
208
205
|
|
|
209
206
|
```ts
|
|
210
|
-
const image = await (
|
|
211
|
-
.related('image')
|
|
207
|
+
const image = await post.related('image' as any)
|
|
212
208
|
.updateOrCreate({ imageableId: post.id }, { url: 'new-photo.jpg' })
|
|
213
209
|
```
|
|
214
210
|
|
|
@@ -279,8 +275,8 @@ export default class Post extends BaseModel {
|
|
|
279
275
|
**Eager load:**
|
|
280
276
|
|
|
281
277
|
```ts
|
|
282
|
-
const post = await
|
|
283
|
-
.preload('comments')
|
|
278
|
+
const post = await Post.query()
|
|
279
|
+
.preload('comments' as any)
|
|
284
280
|
.firstOrFail()
|
|
285
281
|
|
|
286
282
|
console.log(post.comments) // Comment[]
|
|
@@ -290,8 +286,8 @@ console.log(post.comments.length) // 3
|
|
|
290
286
|
**Comments are isolated by type — a post only gets its own comments, not a video's:**
|
|
291
287
|
|
|
292
288
|
```ts
|
|
293
|
-
const post = await
|
|
294
|
-
const video = await
|
|
289
|
+
const post = await Post.query().preload('comments' as any).firstOrFail()
|
|
290
|
+
const video = await Video.query().preload('comments' as any).firstOrFail()
|
|
295
291
|
|
|
296
292
|
// Each only sees their own comments
|
|
297
293
|
```
|
|
@@ -299,8 +295,7 @@ const video = await (Video.query() as any).preload('comments').firstOrFail()
|
|
|
299
295
|
**Ad-hoc query with additional constraints:**
|
|
300
296
|
|
|
301
297
|
```ts
|
|
302
|
-
const recentComments = await (
|
|
303
|
-
.related('comments')
|
|
298
|
+
const recentComments = await post.related('comments' as any)
|
|
304
299
|
.query()
|
|
305
300
|
.orderBy('created_at', 'desc')
|
|
306
301
|
.limit(5)
|
|
@@ -311,8 +306,7 @@ const recentComments = await (post as any)
|
|
|
311
306
|
**Create one:**
|
|
312
307
|
|
|
313
308
|
```ts
|
|
314
|
-
const comment = await (
|
|
315
|
-
.related('comments')
|
|
309
|
+
const comment = await post.related('comments' as any)
|
|
316
310
|
.create({ body: 'Great post!' })
|
|
317
311
|
|
|
318
312
|
console.log(comment.commentableType) // 'posts'
|
|
@@ -322,7 +316,7 @@ console.log(comment.commentableId) // post.id
|
|
|
322
316
|
**Create many:**
|
|
323
317
|
|
|
324
318
|
```ts
|
|
325
|
-
await
|
|
319
|
+
await post.related('comments' as any).createMany([
|
|
326
320
|
{ body: 'First comment' },
|
|
327
321
|
{ body: 'Second comment' },
|
|
328
322
|
])
|
|
@@ -334,13 +328,13 @@ await (post as any).related('comments').createMany([
|
|
|
334
328
|
const comment = new Comment()
|
|
335
329
|
comment.body = 'Hello'
|
|
336
330
|
|
|
337
|
-
await
|
|
331
|
+
await post.related('comments' as any).save(comment)
|
|
338
332
|
```
|
|
339
333
|
|
|
340
334
|
**Save many:**
|
|
341
335
|
|
|
342
336
|
```ts
|
|
343
|
-
await
|
|
337
|
+
await post.related('comments' as any).saveMany([comment1, comment2])
|
|
344
338
|
```
|
|
345
339
|
|
|
346
340
|
---
|
|
@@ -354,8 +348,8 @@ The child side of a polymorphic relation. A comment **belongs to** either a `Pos
|
|
|
354
348
|
**Preload the parent:**
|
|
355
349
|
|
|
356
350
|
```ts
|
|
357
|
-
const comment = await
|
|
358
|
-
.preload('commentable')
|
|
351
|
+
const comment = await Comment.query()
|
|
352
|
+
.preload('commentable' as any)
|
|
359
353
|
.firstOrFail()
|
|
360
354
|
|
|
361
355
|
if (comment.commentable instanceof Post) {
|
|
@@ -369,15 +363,14 @@ if (comment.commentable instanceof Post) {
|
|
|
369
363
|
|
|
370
364
|
```ts
|
|
371
365
|
// All comments in one query, parents resolved in two queries (posts + videos)
|
|
372
|
-
const comments = await
|
|
373
|
-
.preload('commentable') as Comment[]
|
|
366
|
+
const comments = await Comment.query()
|
|
367
|
+
.preload('commentable' as any) as Comment[]
|
|
374
368
|
```
|
|
375
369
|
|
|
376
370
|
**Ad-hoc query:**
|
|
377
371
|
|
|
378
372
|
```ts
|
|
379
|
-
const parent = await (
|
|
380
|
-
.related('commentable')
|
|
373
|
+
const parent = await comment.related('commentable' as any)
|
|
381
374
|
.query()
|
|
382
375
|
.firstOrFail()
|
|
383
376
|
```
|
|
@@ -388,7 +381,7 @@ const parent = await (comment as any)
|
|
|
388
381
|
|
|
389
382
|
```ts
|
|
390
383
|
const post = await Post.findOrFail(1)
|
|
391
|
-
await
|
|
384
|
+
await comment.related('commentable' as any).associate(post)
|
|
392
385
|
|
|
393
386
|
// comment.commentableType is now 'posts'
|
|
394
387
|
// comment.commentableId is now post.id
|
|
@@ -397,7 +390,7 @@ await (comment as any).related('commentable').associate(post)
|
|
|
397
390
|
**Dissociate from parent:**
|
|
398
391
|
|
|
399
392
|
```ts
|
|
400
|
-
await
|
|
393
|
+
await comment.related('commentable' as any).dissociate()
|
|
401
394
|
|
|
402
395
|
// comment.commentableType is now null
|
|
403
396
|
// comment.commentableId is now null
|