Options
All
  • Public
  • Public/Protected
  • All
Menu

External module Upsert

Index

Type aliases

uid

uid: string

Functions

Const basicEqualityUpsertFn

  • basicEqualityUpsertFn(searchPredicates: string | string[]): (Anonymous function)
  • Return a dgraph query and a node found function

    The basicEqualityUpsertFn below will find any nodes that has skill and level predicates.

    const updateJunior = {
      skill: 'Javascript',
      level: 10,
      x: 'y',
    };
    
    // So if you already had a node in the db that looks like:
    const existingNode = {
      skill: 'Javascript',
      level: 10,
      x: 'foo',
    };
    
    /// update will match the existing node as skill and level match
    const updates = [updateJunior];
    const upsertFn = basicEqualityUpsertFn(['skill', 'level']);

    See xUpsertCommitTxn

    Parameters

    • searchPredicates: string | string[]

    Returns (Anonymous function)

    A function that takes a node and returns dgraphQuery and nodeFoundFn. fn => {dgraphQuery, nodeFoundFn}

xUpsertCommitTxn

  • xUpsertCommitTxn(upsertFn: function, data: object[], dgraphClient: DgraphClient, _dgraph?: any): Promise<uid[]>
  • xUpsertCommitTxn(upsertFn: function, data: object, dgraphClient: DgraphClient, _dgraph?: any): Promise<uid>
  • Return a dgraph query and a node found function

    The basicEqualityUpsertFn below will find any nodes that has skill and level predicates.

    const updateJunior = {
      skill: 'Javascript',
      level: 10,
      x: 'y',
      y: 'y',
      z: 'y'
    };
    
    // So if you already had a node in the db that looks like:
    const existingNode = {
      skill: 'Javascript',
      level: 10,
      x: 'foo',
      y: 'foo',
      z: 'foo'
    };
    
    /// x, y and z would be updated from 'foo' to 'y'
    const updates = [updateJunior];
    const upsertFn = basicEqualityUpsertFn(['skill', 'level']);
    
    // takes a upsertFn as well as an array or single object returns a uid or an array of uids
    await xUpsertCommitTxn(upsertFn, updates, dgraphClient);

    If no node matched both skill 'Javascript' and level '10' a new node would be created

    Parameters

    Returns Promise<uid[]>

  • Parameters

    Returns Promise<uid>

xUpsertEdgeList

  • xUpsertEdgeList(upsertFn: function, __namedParameters: object, nodes: object[], transaction: Txn, _dgraph?: any): Promise<uid[]>

xUpsertEdgeListCommitTxn

  • xUpsertEdgeListCommitTxn(upsertFn: function, upsertNode: IUpsertNode, nodes: object[], dgraphClient: DgraphClient, _dgraph?: any): Promise<uid[]>
  • Replaces an existing list of nodes.

    • Finds or creates new nodes on an edge.
    • Removes any nodes not listed from the edge.
    • No nodes are deleted, the link is just removed.

    Example

    // upsert on streetName and postCode
    const addressUpsert = basicEqualityUpsertFn(['streetName', 'postCode']);
    
    const newAddresses = [
      { streetName: 'Clarence', postCode: 2444 },
      { streetName: 'New Street', postCode: 2444 },
      { streetName: 'William', postCode: 2444 },
    ];
    
    const upsertNode: IUpsertNode = {
      uid: 0x2345, // the node you want to update a list on
      predicate: 'addresses' // the list predicate
    };
    
    await xUpsertEdgeListCommitTxn(addressUpsert, upsertNode, newAddresses, dgraphClient);

    Parameters

    Returns Promise<uid[]>

xUpsertMapCommitTxn

  • xUpsertMapCommitTxn(queryFn: any, data: IObjectMap, dgraphClient: DgraphClient, _dgraph?: any): Promise<IUidMap>
  • Upsert an object which has keys for the upsert objects

    const map = {
       howard: { // howard is an upsert key
          name: 'Howard',
          email: 'hb@gmail.com'
       },
       cameron: { // so is cameron
          name: 'Cameron B',
          email: 'cam@gmail.com'
       }
    };
    
    const resultMap = await xUpsertMapCommitTxn(basicEqualityUpsertFn('email'), map, dgraphClient);
    
    // returns an object that has the uid of the upserted values
    const expectedResult: IUidMap = {
       cameron: cameronUid,
       howard: howardUid
    };

    Not sure if this function adds enough value to be kept in the lib. Please do comment in the issues

    Parameters

    • queryFn: any
    • data: IObjectMap
    • dgraphClient: DgraphClient
    • Default value _dgraph: any = dgraph

    Returns Promise<IUidMap>

Generated using TypeDoc