Skip to content

Function First

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 model retrieval

typescript
const factory = new FedacoTestUser();
await factory.NewQuery().create({
  id: 1,
  email: 'linbolen@gradii.com'
});
await factory.NewQuery().create({
  id: 2,
  email: 'xsilen@gradii.com'
});
ReferenceLooks LikeValue
await factory.NewQuery().where('email', 'linbolen@gradii.com').doesntExist()exactly matchfalse
await factory.NewQuery().where('email', 'mohamed@laravel.com').doesntExist()exactly matchtrue
ReferenceLooks LikeValue
model.emailexactly match'linbolen@gradii.com'
model.email !== undefinedexactly matchtrue
ReferenceLooks LikeValue
friends !== undefinedexactly matchtrue
friendsmatch[]
ReferenceLooks LikeValue
modelinstance type exactly matchFedacoTestUser
model.idmatch1
ReferenceLooks LikeValue
modelinstance type exactly matchFedacoTestUser
model.idmatch2
ReferenceLooks LikeValue
missingexactly matchUndefined();
ReferenceLooks LikeValue
isArray(collection)exactly matchtrue
collection.lengthexactly match0
ReferenceLooks LikeValue
isArray(collection)exactly matchtrue
collection.lengthexactly match2
typescript
// .cursor();
for (const m of models) {
  expect(m.id).toEqual(1);
  expect(m.getConnectionName()).toBe('default');
}

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 relationship models are properly hydrated over chunked request

typescript
const user = await FedacoTestUser.createQuery().create({
  email: 'linbolen@gradii.com'
});
const friend = await user.NewRelation('friends').create({
  email: 'xsilen@gradii.com'
});
const user1: FedacoTestUser = await FedacoTestUser.createQuery().first();
await user1
  .NewRelation('friends')
  .chunk(2)
  .pipe(
    tap(({ results: friends }) => {
      expect(friends.length).toBe(1);
      expect(head(friends).email).toBe('xsilen@gradii.com');
      expect(head(friends).getRelation('pivot').getAttribute('user_id')).toBe(
        user.id
      );
      expect(head(friends).getRelation('pivot').getAttribute('friend_id')).toBe(
        friend.id
      );
    })
  )
  .toPromise();

see also prerequisites

belongs to many relationship models are properly hydrated over each request

typescript
const user = await FedacoTestUser.createQuery().create({
  email: 'linbolen@gradii.com'
});
const friend = await user.NewRelation('friends').create({
  email: 'xsilen@gradii.com'
});
await (
  await FedacoTestUser.createQuery().first()
)
  .NewRelation('friends')
  .each()
  .pipe(
    tap(({ item: result, index }) => {
      expect(result.email).toBe('xsilen@gradii.com');
      expect(result.getAttribute('user_id')).toBe(user.id);
      expect(result.getAttribute('friend_id')).toBe(friend.id);
    })
  )
  .toPromise();

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

for page after id correctly paginates

typescript
await FedacoTestUser.createQuery().create({
  id: 1,
  email: 'linbolen@gradii.com'
});
await FedacoTestUser.createQuery().create({
  id: 2,
  email: 'xsilen@gradii.com'
});
let results = await FedacoTestUser.createQuery().forPageAfterId(15, 1);
ReferenceLooks LikeValue
(await results.first()).idmatch2
ReferenceLooks LikeValue
resultsinstance type exactly matchFedacoBuilder
(await results.first()).idmatch2

see also prerequisites

incrementing primary keys are cast to integers by default

typescript
await FedacoTestUser.createQuery().create({
  email: 'linbolen@gradii.com'
});
const user = await FedacoTestUser.createQuery().first();

see also prerequisites

morph map is used when fetching parent

typescript
Relation.morphMap({
  user: FedacoTestUser,
  post: FedacoTestPost
});
const user = await FedacoTestUser.createQuery().create({
  email: 'linbolen@gradii.com'
});
await user.NewRelation('photos').create({
  name: 'Avatar 1'
});
const photo = await FedacoTestPhoto.createQuery().first();
ReferenceLooks LikeValue
await photo.imageableinstance type exactly matchFedacoTestUser

see also prerequisites

morph to relations across database connections

typescript
let item = null;
await FedacoTestItem.createQuery().create({
  id: 1
});
await FedacoTestOrder.createQuery().create({
  id: 1,
  item_type: 'FedacoTestItem',
  item_id: 1
});
try {
  const order = await FedacoTestOrder.createQuery().first();
  item = order.item;
} catch (e) {
  console.log(e);
}

see also prerequisites

one to many relationship

typescript
const user = await FedacoTestUser.createQuery().create({
  email: 'linbolen@gradii.com'
});
await user.NewRelation('posts').create({
  name: 'First Post'
});
await user.NewRelation('posts').create({
  name: 'Second Post'
});
const posts = await user.posts;
const post2 = await user
  .NewRelation('posts')
  .where('name', 'Second Post')
  .first();
ReferenceLooks LikeValue
posts.lengthexactly match2
posts[0]instance type exactly matchFedacoTestPost
posts[1]instance type exactly matchFedacoTestPost
post2instance type exactly matchFedacoTestPost
post2.nameexactly match'Second Post'
await post2.userinstance type exactly matchFedacoTestUser
(await post2.user).emailexactly match'linbolen@gradii.com'

see also prerequisites

Released under the MIT License.