JSPM

json-relationship

1.0.1
    • ESM via JSPM
    • ES Module Entrypoint
    • Export Map
    • Keywords
    • License
    • Repository URL
    • TypeScript Types
    • README
    • Created
    • Published
    • Downloads 2
    • Score
      100M100P100Q32451F
    • License ISC

    json relationship definition and implementation

    Package Exports

    • json-relationship

    This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (json-relationship) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

    Readme

    Json Relationship

    install via npm i json-relationship

    This is a basic json relationship implementation following the minimal specification. A json relationship is established by

        var Relationship = require("json-library").relation.Relationship;
        var relation = new Relationship(data, relationshipDefinition);

    The main function of a relationship is to

    • load one or all related tupels specified in foreign keys or pivot table to its parent tupel
    • update the pivot table or foreign keys of one or all tupels
    • unload reverse one or all established relations

    A relationship instance also includes helpers to

    • loadAll, updateAll and unloadAll relationships
    • link a new object to a tupel and
    • unlink the given object from a tupel

    Usage

    The following example

        var Relationship = require("json-library").relation.Relationship;
    
        var relation = new Relationship(data, "parent has_one:child through:parent_children as:workload");
        relation.loadAll();

    will change the json object

        {
            parent: {
                p_01: {},
                p_02: {},
            },
            child: {
                c_01: {id: "c_01"},
                c_02: {id: "c_02"}
            },
            parent_children: {
                p_01: ["c_02", "c_01"],
                p_01: ["c_01"]
            }
        }

    to

        {
            parent: {
                p_01: {
                    workload: [
                        {id: "c_02"},
                        {id: "c_01"}
                    ]
                },
                p_02: {
                    workload: [
                        {id: "c_01"}
                    ]
                }
            ...

    For further details and examples check createDefinitionObject and the unit tests

    Relationship Definition

    A relationship object may be also created by: "[model] [[type]:[related] [mapping]:[path] (as:[alias])]". Using createDefinitionObject(string) a valid relationship Object is retrieved. For details see createDefinitionObject