Skip to content

Function GetAttribute

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

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

morph map is used for creating and fetching through relation

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'
});
await user.NewRelation('photos').create({
  name: 'Avatar 2'
});
const post = await user.NewRelation('posts').create({
  name: 'First Post'
});
await post.NewRelation('photos').create({
  name: 'Hero 1'
});
await post.NewRelation('photos').create({
  name: 'Hero 2'
});
ReferenceLooks LikeValue
(await user.photos)[0]instance type exactly matchFedacoTestPhoto
isArray(await post.photos)exactly matchtrue
(await post.photos)[0]instance type exactly matchFedacoTestPhoto
(await user.photos).lengthexactly match2
(await post.photos).lengthexactly match2
(await user.photos)[0].nameexactly match'Avatar 1'
(await user.photos)[1].nameexactly match'Avatar 2'
(await post.photos)[0].nameexactly match'Hero 1'
(await post.photos)[1].nameexactly match'Hero 2'
(await user.photos)[0].getAttribute('imageable_type')exactly match'user'
(await user.photos)[1].getAttribute('imageable_type')exactly match'user'
(await post.photos)[0].getAttribute('imageable_type')exactly match'post'
(await post.photos)[1].getAttribute('imageable_type')exactly match'post'

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

timestamps using custom date format

typescript
const model = new FedacoTestUser();
model.setDateFormat('yyyy-MM-dd HH:mm:ss.SSSS');
model.setRawAttributes({
  created_at: '2017-11-14 08:23:19.0000',
  updated_at: '2017-11-14 08:23:19.7348'
});
ReferenceLooks LikeValue
model.fromDateTime(model.getAttribute('updated_at'))exactly match'2017-11-14 08:23:19.734800'

see also prerequisites

timestamps using default sql server date format

typescript
const model = new FedacoTestUser();
model.setDateFormat('yyyy-MM-dd HH:mm:ss.SSS');
model.setRawAttributes({
  created_at: '2017-11-14 08:23:19.000',
  updated_at: '2017-11-14 08:23:19.734'
});
ReferenceLooks LikeValue
model.fromDateTime(model.getAttribute('updated_at'))exactly match'2017-11-14 08:23:19.734'

see also prerequisites

timestamps using old sql server date format

typescript
const model = new FedacoTestUser();
model.setDateFormat('yyyy-MM-dd HH:mm:ss.000');
model.setRawAttributes({
  created_at: '2017-11-14 08:23:19.000'
});

see also prerequisites

Released under the MIT License.