{"id":15828692,"url":"https://github.com/sagold/json-relationship","last_synced_at":"2025-03-15T04:31:39.819Z","repository":{"id":57256399,"uuid":"73641526","full_name":"sagold/json-relationship","owner":"sagold","description":"Transform json-data using relational concepts","archived":false,"fork":false,"pushed_at":"2018-03-26T17:42:07.000Z","size":738,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-26T23:51:14.739Z","etag":null,"topics":["data-transformation","json","json-relationship","relation-extraction"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sagold.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-11-13T20:38:12.000Z","updated_at":"2025-02-11T23:18:36.000Z","dependencies_parsed_at":"2022-08-25T02:30:36.540Z","dependency_job_id":null,"html_url":"https://github.com/sagold/json-relationship","commit_stats":null,"previous_names":["sagold/json-relationship"],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sagold%2Fjson-relationship","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sagold%2Fjson-relationship/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sagold%2Fjson-relationship/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sagold%2Fjson-relationship/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sagold","download_url":"https://codeload.github.com/sagold/json-relationship/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243685506,"owners_count":20330980,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["data-transformation","json","json-relationship","relation-extraction"],"created_at":"2024-10-05T10:41:53.672Z","updated_at":"2025-03-15T04:31:39.572Z","avatar_url":"https://github.com/sagold.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003e\u003cimg src=\"./docs/gson-relationship.png\" width=\"228\" alt=\"gson-relationship\"\u003e\u003c/h1\u003e\n\n**Transform json-data using relational concepts**\n\n\u003e This is a basic *json relationship* implementation following the minimal [specification](./Specification.md).\n\u003e\n\u003e - Quick transformation of nested json-data\n\u003e - reusable json-objects within a json-document (dry)\n\n`npm i gson-relationship --save`\n\n\n- [Examples](#examples)\n- [Usage Examples](#usage-examples)\n- [API](#api)\n\n\n## Examples\n\n### `1:1` relationship\n\nNormalize nested json-objects, like\n\n```js\n{\n  server: {\n    serverA: {\n      id: \"serverA\",\n      service: { name: \"A\" }\n    },\n    serverB: {\n      id: \"serverB\",\n      services: { name: \"B\" }\n    }\n  }\n}\n```\n\nto the following normalized representation\n\n```js\n{\n  server: {\n    serverA: { id: \"serverA\" },\n    serverB: { id: \"serverB\" }\n  },\n  server_services: {\n    serverA: \"serviceA\"\n    serverB: \"serviceB\"\n  },\n  services: {\n    serviceA: { name: \"A\" },\n    serviceB: { name: \"B\" },\n  }\n}\n```\n\nand vice versa\n\n\n### `1:n` relationship\n\nNormalize nested json-objects, like\n\n```js\n{\n  server: {\n    serverA: {\n      id: \"serverA\",\n      services: {\n        serviceA: { name: \"A\" },\n        serviceB: { name: \"B\" }\n      }\n    },\n    serverB: {\n      id: \"serverB\",\n      services: {\n        serviceB: { name: \"B\" }\n      }\n    }\n  }\n}\n```\n\nto the following normalized representation\n\n```js\n{\n  server: {\n    serverA: { id: \"serverA\" },\n    serverB: { id: \"serverB\" }\n  },\n  server_services: {\n    serverA: [\"serviceA\", \"serviceB\"],\n    serverB: [\"serviceB\"]\n  },\n  services: {\n    serviceA: { name: \"A\" },\n    serviceB: { name: \"B\" }\n  }\n}\n```\n\nand vice versa\n\n\n## Usage example\n\n\u003e Transforms the above example having a server-service relationship to service-server relationship\n\n```js\nconst { join, normalize, invertPivot } = require('json-relationship');\n\nconst data = {\n  server: {\n    serverA: {\n      id: \"serverA\",\n      services: {\n        serviceA: { name: \"A\" },\n        serviceB: { name: \"B\" }\n      }\n    },\n    serverB: {\n      id: \"serverB\",\n      services: {\n        serviceB: { name: \"B\" }\n      }\n    }\n  }\n}\n\n// normalize the table as described in 'examples'\nconst normalized = normalize(data, {\n  type: \"1:n\",\n  model: \"server\",\n  alias: \"services\",\n  pivot: \"server_services\",\n  reference: \"services\"\n});\n\n// we need to invert the mapping table for our purpose\nconst inverted = invertPivot(normalized, \"server_services\", \"services_server\");\n\n// then rebuild the data with the inverted relationship\nconst services = join(inverted, {\n  type: \"1:n\",\n  model: \"services\",\n  alias: \"server\",\n  pivot: \"services_server\",\n  reference: \"server\"\n});\n```\n\nwhich results in `services` structured as\n\n```js\n{\n  services: {\n    serviceA: {\n      name: \"A\",\n      server: {\n        serverA: { id: \"serverA\" }\n      }\n    },\n    serviceB: {\n      name: \"B\",\n      server: {\n        serverA: { id: \"serverA\" },\n        serverB: { id: \"serverB\" }\n      }\n    }\n  }\n}\n```\n\n\n## API\n\n### relation description\n\n| property                 | description\n| ------------------------ | ----------------------------------------------------------------------------\n| `model`:string           | json-pointer to parent tupels, e.g. `/data/server`\n| `reference`:string       | json-pointer to related tupels e.g. `/data/services`\n| `pivot`:string           | json-pointer to pivot table, mapping _model-reference_ e.g. `/data/pivot`\n| `alias`:string           | json-pointer **within** model tupel to related tupel, e.g. `/services`\n| `move`:boolean           | Removes associated relationships and pivots from model. defaults to `true`.\n| `referenceId`:string     | **optional** change foreign key of a related tupel (property **within** tupel), e.g. `/id`\n\n#### move-option\n\n\u003e Set this option to `true`, to keep the original data and additionally construct the target-models.\n\nPer default `move = true`, which will remove\n\n- all `pivot` and `reference` objects on a `join` operation\n- the `alias` property and all its contained `references` on a `normalize` operation\n\n\n### methods\n\n| method                                | description\n| ------------------------------------- | -------------------------------------------------------------\n| `normalize`(data, rel):object           | build a unlinked json-data model containing pivot-table and references\n| `join`(data, rel):object                | build a linked json-data, resolving pivot and reference-model\n| `invertPivot`(data, from, to):object    | invert a pivot table (1:1, 1:n). May change relationtype from 1:1 to 1:n\n\n#### normalize\n\n\u003e normalize(data:object, relationship:object) : object\n\n\n#### join\n\n\u003e join(data:object, relationship:object) : object\n\n\n#### invertPivot\n\n\u003e invertPivot(data:object, from:string, to:string = from) : object\n\nA **1:1 pivot** is a simple map between a parent- and a target-property\n\n```js\n{\n  pivot: {\n    server: \"serviceOfServer\"\n  }\n}\n```\n\n`invertPivot(data, \"/pivot\")` will reverse the mapping, resulting in\n\n```js\n{\n  pivot: {\n    serviceOfServer: \"server\"\n  }\n}\n```\n\n`invertPivot(data, \"/pivot\", \"/service_server\")` will move the pivot location to the path specified in the `to`-argument\n\n```js\n{\n  service_server: {\n    serviceOfServer: \"server\"\n  }\n}\n```\n\nA **1:n pivot** is a map between a parent-property and multiple target-properties\n\n```js\n{\n  pivot: {\n    server: [\"serviceA\", \"serviceB\"]\n  }\n}\n```\n\n`invertPivot(data, \"/pivot\", \"/service_servers\")` will reverse the mapping, resulting in\n\n```js\n{\n  service_servers: {\n    serviceA: [\"server\"],\n    serviceB: [\"server\"]\n  }\n}\n```\n\nA **1:1 pivot may be inverted to 1:n pivot**. If the original pivot maps multiple tupels to the same target, the\ninvertion a pivot results in a 1:n map, which may be recogniced by an array for each model key:\n\n```js\n{\n  pivot: {\n    serviceA: \"server\"\n    serviceB: \"server\"\n  }\n}\n```\n\n`invertPivot(data, \"/pivot\")` will create the following `1:n` pivot\n\n```js\n{\n  pivot: {\n    server: [\"serviceA\", \"serviceB\"]\n  }\n}\n```\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsagold%2Fjson-relationship","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsagold%2Fjson-relationship","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsagold%2Fjson-relationship/lists"}