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.

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

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

class Note {
   public text: string;
   public date: number;
}

const NOTE_META = new ObjectMeta({
   builder: Note,
   fields: {
      text: new StringField(),
      date: new NumberField()
   }
});

class User {
   public id: number;
   public notes: Note[];
}

const USER_META = new ObjectMeta({
   builder: User,
   fields: {
      id: new NumberField(),
      notes: new ObjectArrayField({ meta: NOTE_META }),
   }
});

const users: User[] = [
   {
      id: 1,
      notes: [
         { text: `Test_1`, date: 1 },
         { text: `Test_2`, date: 2 },
      ]
   },
   {
      id: 2,
      notes: [
         { text: `Test_3`, date: 3 },
         { text: `Test_4`, date: 4 },
         { text: `Test_5`, date: 5 },
      ]
   }
];

const deltas: TDeepPartial<User>[] = [
   {
      notes: [
         { text: `Hi` },
         { date: 22 }
      ]
   },
   {
      notes: [
         null,
         null,
         { text: `HOI` },
         { text: `Will be skiped` },
      ]
   }
];

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);

Error

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

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

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

const users = [
   {
      id: 1,
      name: 567,
   },
   {
      name: `Masha`,
      age: `25`,
   }
];

const deltas: TDeepPartial<User>[] = [
   {
      name: `Andrey`,
   },
   null
];

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

try {
   JTC.update({
      id: `Update`,
      metas: [USER_META],
      entries,
   });
} catch (e) {
   const error = e as UpdaterError;

   console.log(error);
   /* 0 -> age -> undefined | Value is absent
      0 -> name -> 567 | Expected string, but got number
      1 -> age -> 25 | Expected number, but got string
      1 -> id -> undefined | Value is absent
   */
   console.log(JTC.log.asString(error.valuesTree));
   /* 1 -> null | Members can only be of "object" type, but got "null" */
   console.log(JTC.log.asString(error.partialsTree));
}

Last updated

Was this helpful?