Skip to content

Function With

basic has many eager loading

typescript
let user: FedacoTestUser = await FedacoTestUser.createQuery().create({
  email: 'linbolen@gradii.com'
});
await user.NewRelation('posts').create({
  name: 'First Post'
});
user = await FedacoTestUser.createQuery()
  .with('posts')
  .where('email', 'linbolen@gradii.com')
  .first();
typescript
const post = await FedacoTestPost.createQuery()
  .with('user')
  .where('name', 'First Post')
  .get();

see also prerequisites

basic nested self referencing has many eager loading

typescript
let user: FedacoTestUser = await FedacoTestUser.createQuery().create({
  email: 'linbolen@gradii.com'
});
const post: FedacoTestPost = await user.NewRelation('posts').create({
  name: 'First Post'
});
await post.NewRelation('childPosts').create({
  name: 'Child Post',
  user_id: user.id
});
user = await FedacoTestUser.createQuery()
  .with('posts.childPosts')
  .where('email', 'linbolen@gradii.com')
  .first();
ReferenceLooks LikeValue
head(await user.posts).nameexactly match'First Post'
head(await head(await user.posts).childPosts)exactly not matchnull
head(await head(await user.posts).childPosts as any[]).nameexactly match'Child Post'
ReferenceLooks LikeValue
(await head(posts).parentPost)exactly not matchnull
(await head(posts).parentPost).userexactly not matchnull
(await head(posts).parentPost).user.emailexactly match'linbolen@gradii.com'

see also prerequisites

belongs to many custom pivot

typescript
const john = await FedacoTestUserWithCustomFriendPivot.createQuery().create({
  id: 1,
  name: 'John Doe',
  email: 'johndoe@example.com'
});
const jane = await FedacoTestUserWithCustomFriendPivot.createQuery().create({
  id: 2,
  name: 'Jane Doe',
  email: 'janedoe@example.com'
});
const jack = await FedacoTestUserWithCustomFriendPivot.createQuery().create({
  id: 3,
  name: 'Jack Doe',
  email: 'jackdoe@example.com'
});
const jule = await FedacoTestUserWithCustomFriendPivot.createQuery().create({
  id: 4,
  name: 'Jule Doe',
  email: 'juledoe@example.com'
});
await FedacoTestFriendLevel.createQuery().create({
  id: 1,
  level: 'acquaintance'
});
await FedacoTestFriendLevel.createQuery().create({
  id: 2,
  level: 'friend'
});
await FedacoTestFriendLevel.createQuery().create({
  id: 3,
  level: 'bff'
});
await john.NewRelation('friends').attach(jane, {
  friend_level_id: 1
});
await john.NewRelation('friends').attach(jack, {
  friend_level_id: 2
});
await john.NewRelation('friends').attach(jule, {
  friend_level_id: 3
});
const johnWithFriends = await FedacoTestUserWithCustomFriendPivot.createQuery()
  .with('friends')
  .find(1);
ReferenceLooks LikeValue
await (await johnWithFriends.friends.find(it => it.id === 3).getAttribute( 'pivot').level).levelexactly match'friend'
(await johnWithFriends.friends.find(it => it.id === 4).getAttribute( 'pivot').friend).nameexactly match'Jule Doe'

see also prerequisites

eager loaded morph to relations on another database connection

typescript
await FedacoTestPost.createQuery().create({
  id: 1,
  name: 'Default Connection Post',
  user_id: 1
});
await FedacoTestPhoto.createQuery().create({
  id: 1,
  imageable_type: 'post',
  imageable_id: 1,
  name: 'Photo'
});
await FedacoTestPost.useConnection('second_connection').create({
  id: 1,
  name: 'Second Connection Post',
  user_id: 1
});
await FedacoTestPhoto.useConnection('second_connection').create({
  id: 1,
  imageable_type: 'post',
  imageable_id: 1,
  name: 'Photo'
});
const defaultConnectionPost = (
  await FedacoTestPhoto.createQuery().with('imageable').first()
).imageable;
const secondConnectionPost = (
  await FedacoTestPhoto.useConnection('second_connection')
    .with('imageable')
    .first()
).imageable;
ReferenceLooks LikeValue
'Second Connection Post'matchsecondConnectionPost.name

see also prerequisites

Released under the MIT License.