How is Normalizr used properly with 3 a merged Array of Objects?

78 Views Asked by At

I'm trying to get my head around normalizing my arrays of objects with Normalizr, but do not get the right, or expected result.

These are 3 arrays of objects:

const assistants = [
  {
    id: 1,
    first_name: "Nickolas",
    last_name: "Seamans",
    phone: "+62 949 597 4013",
    email: "[email protected]"
  },
  {
    id: 2,
    first_name: "Peri",
    last_name: "Helversen",
    phone: "+51 886 232 9275",
    email: "[email protected]"
  }
];

and

const clients = [
  {
    id: 1,
    first_name: "Mona",
    last_name: "Shakelade",
    phone: "+63 475 243 2059",
    email: "[email protected]",
    date_of_birth: "26/01/1987",
    status: null
  },
  {
    id: 2,
    first_name: "Dario",
    last_name: "Aizikovitz",
    phone: "+33 454 959 7355",
    email: "[email protected]",
    date_of_birth: "16/08/1999",
    status: null
  }
];

and

const dentists = [
  {
    id: 1,
    first_name: "Tessa",
    last_name: "Iiannone",
    phone: "+234 325 319 4277",
    email: "[email protected]"
  },
  {
    id: 2,
    first_name: "Kennett",
    last_name: "Pedreschi",
    phone: "+48 204 144 9885",
    email: "[email protected]"
  }
];

and I merge them with:

import assistants from "../data/assistants";
import dentists from "../data/dentists";
import clients from "../data/clients";

const users = {
  dentists: dentists,
  assistants: assistants,
  clients: clients
};

so that the outcome of a console.log(users) is:

{dentists: Array(2), assistants: Array(2), clients: Array(2)}
assistants: (2) [{…}, {…}]
clients: (2) [{…}, {…}]
dentists: (2) [{…}, {…}]
[[Prototype]]: Object

I have fabricated this normalizr steps:

const dentistsList = new schema.Entity("dentists", {}, { idAttribute: "id" });
const dentistsSchema = [dentistsList];
const assistantsList = new schema.Entity(
  "assistants",
  {},
  { idAttribute: "id" }
);
const assistantsSchema = [assistantsList];
const clientsList = new schema.Entity("clients", {}, { idAttribute: "id" });
const clientsSchema = [clientsList];

const usersSchema = new schema.Entity("users", {
  clients: clientsSchema,
  assistants: assistantsSchema,
  dentists: dentistsSchema
});

const usersArraySchema = [usersSchema];

const normalizedArray = normalize(users, [usersArraySchema]);

Now my outcome with a console.log(normalizedArray) is:

{entities: {…}, result: Array(3)}
entities:
users: {1: {…}, 2: {…}}
[[Prototype]]: Object
result: Array(3)
0: (2) [1, 2]
1: (2) [1, 2]
2: (2) [1, 2]
length: 3
[[Prototype]]: Array(0)
[[Prototype]]: Object

so where are my assistants and dentists went? They are counted separately in the 'result' section though...

Can anyone help me out?

1

There are 1 best solutions below

0
Wolk9 On

In the end, the solution was not that hard, but that is always the case once you know it. The following notation did the trick:

const assistant = new schema.Entity("assistants");
const dentist = new schema.Entity("dentists");
const client = new schema.Entity("clients");
const userSchema = {
  assistants: [assistant],
  dentists: [dentist],
  clients: [client]
};
const normalizedUsers = normalize(users, userSchema);

the outcome of a console.log(normalisedUsers) is now:

{entities: {…}, result: {…}}
entities:
assistants: {201: {…}, 202: {…}}
clients: {1: {…}, 2: {…}}
dentists: {101: {…}, 102: {…}}
[[Prototype]]: Object
result:
assistants: (2) [201, 202]
clients: (2) [1, 2]
dentists: (2) [101, 102]
[[Prototype]]: Object
[[Prototype]]: Object

I hope one could benefit from my solution sharing.