1.0.2 • Published 2 years ago

@boolan_dev/json-map v1.0.2

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

JSON MAP

Simple query for relational json arrays

Persons Array

[
    {id: 1, name: 'test', stat: 't', tasks: ['t1', 't2']},
    {id: 2, name: 'test2', stat: 't2', tasks: ['t1', 't2']}
]

Info Array

[
    {
        id: 1, age: 20, __personID: 1
    },
    {
        id: 2, age: 21, __personID: 2
    }
]

Contacts Array

[
    { id: 1, email: 'test@email.com', num: "091234", __infoID: 1 },
    { id: 2, email: 'test2@email.com', num: "097777", infoID: 2 }
]

Schema

{
    __person: 'Persons',
    __info: 'Infos',
    __contact: 'Contacts'
}

Query Example

{
    __person(id=1): {
        name,
        __info: {
            age,
            email,
            __contact: {
                num
            }
        },
        tasks(0)
    }
}

Functions

  • .query(queryString);

Code Example

const data = {
    Persons: [
        {id: 1, name: 'test', stat: 't', tasks: ['t1', 't2']},
        {id: 2, name: 'test2', stat: 't2', tasks: ['t1', 't2']}
    ],
    Infos: [
        {
            id: 1, age: 20, email: 'test@email.com', __personID: 1
        },
        {
            id: 2, age: 21, email: 'test2@email.com', __personID: 2
        }
    ],
    Contacts: [
        { id: 1, age: 20, email: 'test@email.com', __infoID: 1 },
        { id: 2, age: 21, email: 'test2@email.com', __infoID: 2 }
    ]
}

const schema = {
    __person: 'Persons',
    __info: 'Infos',
    __contact: 'Contacts'
}

const $ = new JsonMap(data, schema);
const query = `
{
    __person(id=1): {
        name,
        __info: {
            age,
            email,
            __contact: {
                id
            }
        },
        tasks
    }
}
`;
const result = $.query(query);

Result

{
  "name": "test",
  "__info": {
    "age": 20,
    "email": "test@email.com",
    "__contact": {
      "num": "091234"
    }
  },
  "tasks": "t1"
}