Create many to many links between objects using joining dataset through type script function

Hello,
I have primarykey information of object A and primarykey information of object B in form of array[x,y,z].

I am trying to create many to many links between objects where I will call typescript through action and that typescript function will take primarykey information of object A and primarykey information of object in form of array and create a joining dataset. this joining dataset will act as many to many links between two object.

For example
object A - 123, object B -array[x,y,z]
joining dataset
pkA, pkB
123,x
123,y
123,z

is it possible to work with dataset in type script. I tried it but did not get succeed

Thank you

Hey!

Are these two objects still in a regular Foundry dataset or are they already synced to the Ontology as two different Object Types?

Hi Josh,
Yes, these two objects are synced to the Ontology as two different object types.

Got it!

Assuming you already have a configured Link Type between the two Object Types (if it’s a many-to-many relationship, this would require a join table/dataset with two columns representing the primary keys for each Object Type), you can write a TypeScript OntologyEditFunction to update links. You can view the official Palantir docs here for reference.

Typically, however, if you’re creating the join table (in Repos or PB) by using the backing datasets of the Object Types, the links will automatically be reflected in the join table the next time it’s built.

Are you trying to do this in the context of when a new object of Object Type A is created via an Action Type and its foreign key property also gets populated via a form?

Hello Josh,

Yes, I have configured the many-to-many link type between two objects and created a join table that contains the primary key values for each object type.

I am using the pipeline builder to perform exact matching between the two objects and populate the data into the join table. However, whenever there is no exact match, I use AIP logic (leveraging an LLM) to find the best match and create a link between them.

To achieve this, I am trying to use a TypeScript function that takes both object inputs and populates the data in the join table.
I would like to understand if it is possible to work with a dataset using a TypeScript function.

I see, that makes sense!

In that case, yes you can still use a TS function for this purpose. Based on your original message on the thread, if one Object Type has an Array property that contains the foreign key values which will link to another Object Type, you can write code similar to below to update (or create) links:

import { Edits, OntologyEditFunction } from "@foundry/functions-api";
import { Objects, ApiNameObjectTypeA, ApiNameObjectTypeB } from "@foundry/ontology-api";

export class MyFunctions {
    @Edits(ApiNameObjectTypeA, ApiNameObjectTypeB)
    @OntologyEditFunction()
    public updateLinks(singleObject: ApiNameObjectTypeA): void {
        const arrayForeignKeys = singleObject.nameOfFKProperty;

        // Loop through the N foreign keys you want to traverse and add links for
        arrayForeignKeys.forEach((item) => {
            const objectToLink = Objects.search().apiNameObjectTypeB()
                 .filter((obj) => obj.primaryKeyPropertyName.exactMatch(item))
                 .all()       // Convert from Object Set to Array with one object
                 [0]          // Store just the first (and only) element

            // Set links
            singleObject.objectTypeBLinkType.set(objectToLink)
        })
    }
}

You can modify the code as needed, especially to use the correct Object Type API names (and Link Type API name for the final line in the for-loop). You may also need to add error handling for the Objects.search() call to ensure that AIP Logic gave you the correct foreign keys. Then you’d create an Action Type that’s backed by this function. You may need to go into your Link Type’s configuration in OMA to enable edits.

Once you start using the function-backed action, you will not see the new links as rows in the original join table you created in Pipeline Builder, but if you create a new Materialization, the original rows from PB + the new links created by your TypeScript function will appear there.

Thank you Josh for the help :slightly_smiling_face:. I will work on your suggestion