Loopback: conditional value on upsert - loopback

so I am having this mass import of data to my DB and conditional whether a record exists or not I am inserting or updating (upsert)...
now, what I need is when it is inserting (new record) to populate the "created" property's value and 'modified' value with the current datetime... but only modified one when it is getting updated...
so if I would use operational hook of before save.... how do I know it is for an inserting record or for an updating record?
any ideas?

I solved it in different manner (would still be good to know the original answer)
in my case I upserted them all normaly with updated to current date, after, I find all with created:null and update just those with the created value.

Related

DB Architecture question: Would using a JSONB column for storing row changes be an efficient solution?

I have an app which allows for dynamic table and column generation. One feature we are looking to implement is change tracking for each row. Anytime any changes occur, we want to keep track of the field and what the new value is.
One potential solution we are looking at is adding a "history" JSONB column to the table which would contain an ongoing array of every change. When a field is updated or multiple fields are updated, we could append a new element to the history field by using the || append syntax which will append the element to the existing jsonb vlaue:
update table set history = history || '{new node with changes}'
When viewing the "history changes", it would all be contained in this JSONB field and we'd have logic to parse out /display the changes over time.
Note: We won't need to query this JSONB column. It's simply the place to store all the row level changes. It seems like this should be an efficient way of saving updates.
My question is, is this a viable solution in regards to performance over time? It's possible this history field could become large over time, so would making any row changes become slower the larger the field? Or does the fact we are using the || operator to append data to the field mitigate any performance issues?
FYI, I am currently on Postgres 11, but could upgrade to newer versions if that had an impact.
Thanks for any feedback you can provide.

How to search on a field that may be empty in zapier

Trying to sync up a postgres record to airtable on create/update. The field has a couple of ids that I would like to check for in airtable to determine whether I should create a new record or update an existing one. The first id (optional_id) I need to search on can possibly be null. This causes the search to fail before it can get to the other id(required_id) that should always be populated. Is there any way I can skip the initial search for optional_id if it turns out to be null in postgres?
My current outline is as follows:
I would use a Formatter > Text > Default Value step in case the input value can be null and then make sure the fallback value is from a record that does not exist.
If further help is needed, feel free to reach out to us here:
https://zapier.com/app/get-help

iSQLOutput - Update only Selected columns

My flow is simple and I am just reading a raw file into a SQL table.
At times the raw file contains data corresponding to existing records. I do not want to insert a new record in that case and would only want to update the existing record in the SQL table. The challenge is, there is a 'record creation date' column which I initialize at the time of record creation. The update operation overwrites that column too. I just want to avoid overwriting that column, while updating the other columns from the information coming from the raw file.
So far I am having no idea about how to do that. Could someone make a recommendation?
I defaulted the creation column to auto-populate in the SQL database itself. And I changed my flow to just update the remaining records. Talend job is now not touching that column. Problem solved.
Yet another reminder of 'Simplification is underrated'. :)

TSQL - force update of persisted computed column

I have a persisted computed column in one table with the value calculated using a user function. How can I force that column to be updated without updating any other column in that table?
UPDATE: So as it turns out, this will not work as I imagined it.
I wanted to have user function that contains sub-query in it, gets me some data and stores it in computed column. But SQL Server won't allow this...
It looks like I will have to do something similar with insert/update triggers.
If you persist the value by adding the PERSISTED keyword, the value is both retained on insert and will be synchronized when the referenced column is updated.

HOW TO: squeryl full update

I'm new to squeryl and I have a question in squeryl full updates..
Can anybody please explain what is actually a full update and how it is done ?
I couldn't really understand full update in squeryl guide.
Thanx...
A partial update is similar to calling Update in SQL. You give values for some fields, and a where clause determines on which row the update happens.
With a full update, you simply give an object of the type that is mapped to the table, it means update the row with the same primary key as the object, set all fields (hence "full" update) to the value they have in the object. You simply call the update method on the table, passing the object (you can also pass a collection (Iterable) of them, updating them all).