Skip to content

Function Update

fresh method on model

typescript
const now = new Date();
const nowSerialized = formatISO(startOfSecond(now));
const nowWithFractionsSerialized = now.toJSON();
// Carbon.setTestNow(now);
const storedUser1 = await FedacoTestUser.createQuery().create({
  id: 1,
  email: 'linbolen@gradii.com',
  birthday: now
});
await storedUser1.NewQuery().update({
  email: 'dev@mathieutu.ovh',
  name: 'Mathieu TUDISCO'
});
const freshStoredUser1 = await storedUser1.Fresh();
const storedUser2 = await FedacoTestUser.createQuery().create({
  id: 2,
  email: 'linbolen@gradii.com',
  birthday: now
});
await storedUser2.NewQuery().update({
  email: 'dev@mathieutu.ovh'
});
const freshStoredUser2 = await storedUser2.Fresh();
const notStoredUser = FedacoTestUser.initAttributes({
  id: 3,
  email: 'linbolen@gradii.com',
  birthday: now
});
const freshNotStoredUser = await notStoredUser.Fresh();
ReferenceLooks LikeValue
freshStoredUser1.toArray()match({
  'id' : 1,
   'name' : 'Mathieu TUDISCO',
   'email' : 'dev@mathieutu.ovh',
   'birthday' : nowWithFractionsSerialized,
   'created_at': nowSerialized,
   'updated_at': nowSerialized
});
storedUser1instance type exactly matchFedacoTestUser
storedUser2.toArray()match({
  'id' : 2,
  'email' : 'linbolen@gradii.com',
   'birthday' : nowWithFractionsSerialized,
   'created_at': nowSerialized,
   'updated_at': nowSerialized
});`
freshStoredUser2.toArray()match({
   'id' : 2,
   'name' : null,
   'email' : 'dev@mathieutu.ovh',
   'birthday' : nowWithFractionsSerialized,
   'created_at': nowSerialized,
  'updated_at': nowSerialized
});
storedUser2instance type exactly matchFedacoTestUser
notStoredUser.toArray()match({
   'id' : 3, 'email' : 'linbolen@gradii.com',
   'birthday': nowWithFractionsSerialized
});
freshNotStoredUserexactly matchnull

see also prerequisites

saving json fields

typescript
const model = await FedacoTestWithJSON.createQuery().create({
  json: {
    x: 0
  }
});
typescript
model.fillable(['json->y', 'json->a->b']);
await model.update({
  'json->y': '1'
});
ReferenceLooks LikeValue
model.jsonmatch({
   'x': 0,
   'y': '1'
});
ReferenceLooks LikeValue
'json->a->b' in model.toArray()exactly matchfalse
model.jsonmatch({
   'x': 0,
   'y': '1',
   'a': {
   'b': '3'
   }
});

see also prerequisites

Released under the MIT License.