Archi Forum

Archi Plug-ins => jArchi => Topic started by: jsandman77 on April 20, 2021, 10:42:06 AM

Title: Upsert capability - Check for existing and update before creating new elements
Post by: jsandman77 on April 20, 2021, 10:42:06 AM
Hi there, is there a code snippet somewhere that shows the best way to check for an existing element by ID to do an upsert rather than create a new element? I went through the examples and couldn't find one that does this. Thoughts?
Title: Re: Upsert capability - Check for existing and update before creating new elements
Post by: Phil Beauvoir on April 20, 2021, 10:47:10 AM
Hi,

you could use the ID selector to get an element:

var element = $("#id-1234")

if "element" is returned or is null then you can proceed on that basis.

https://github.com/archimatetool/archi-scripting-plugin/wiki/jArchi-Collection#selectors
Title: Re: Upsert capability - Check for existing and update before creating new elements
Post by: jsandman77 on April 20, 2021, 11:10:22 AM
Thanks Phil. Doing that already.

I am using the code example to export / import to a csv and have the properties show up as columns. Unfortunately, the import duplicates everything so I was looking to tweak it so that it would update all of the properties when an element id is already found.

Is there a "round trip" set of scripts where you can export to a CSV, do some bulk updates to properties and then re-import to update the model?
Title: Re: Upsert capability - Check for existing and update before creating new elements
Post by: Phil Beauvoir on April 20, 2021, 12:21:15 PM
Sounds like a problem in the logic of the code. When parsing the CSV file, if you are able to get an element by its ID then you can update its name, documentation, properties etc. If the element does not exist then it's up to the code whether to ignore it or create a new element. Perhaps the code example you are using needs more adjustment to suit your case?
Title: Re: Upsert capability - Check for existing and update before creating new elements
Post by: Jean-Baptiste Sarrodie on April 20, 2021, 20:31:47 PM
Hi,

Maybe this script could help you: https://gist.github.com/jbsarrodie/77e1e6779ace738a15c0bdcf551d68ec

Regards,

JB