Update

With JTC.update object can be partially updated with new values

Object

class Address {
   public country: string;
   public city: string;
}

const ADDRESS_META = new ObjectMeta({
   builder: Address,
   fields: {
      country: new StringField(),
      city: new StringField(),
   }
});

class User {
   public id: number;
   public name: string;
   public age: number;
   public address: Address;
}

const USER_META = new ObjectMeta({
   builder: User,
   fields: {
      id: new NumberField(),
      name: new StringField(),
      age: new NumberField(),
      address: new ObjectField({ meta: ADDRESS_META })
   }
});

const users: User[] = [
   {
      id: 1,
      name: `Vasya`,
      age: 21,
      address: {
         country: `Ukraine`,
         city: `Odessa`
      }
   },
   {
      id: 2,
      name: `Masha`,
      age: 25,
      address: {
         country: `USA`,
         city: `Alexandria`
      }
   }
];

const deltas: TDeepPartial<User>[] = [
   {
      name: `Andrey`,
      address: {
         city: `Chornomorsk`
      }
   },
   {
      age: 32,
      address: {
         country: `Egypt`
      }
   },
];

const entries: IUpdateEntry<User>[] = [
   { value: users[0], partial: deltas[0] },
   { value: users[1], partial: deltas[1] },
];

const updated = JTC.update({
   id: `Update`,
   metas: [USER_META],
   entries,
});

console.log(updated);

Array

It is also possible to update data inside iterable fields.

circle-info

In order to skip entries which shouldn't be updated null should be setted at their place.

circle-info

If partial value have extra entries which don't correspond to updatable they will be omitted.

Error

Before performing an update entries will be checked for validity with JTC.convert and in case of problems UpdateError will thrown.

Last updated