Create entry in related table when inserting an item into Hasura? - graphql

I have two tables, one has a foreign key to the other. For ease of use let's say I have a table named 'Boy' with a foreign key for iceCreamId to a table called 'IceCream'.
In hasura the only way I can see how to create an entry in IceCream when I insert into boy is through the query.
Is there a way I can trigger a default IceCream insert when a 'Boy' is inserted through the backend? Don't like the frontend being relied on to do this.

Hasura builds on top of the functionality of your database and works best when you embrace all the functionality that the underlying DB has to offer.
This can easily be accomplished using a Database Trigger (I am assuming you're using Postgres here)
Triggers allow you to run additional logic in the backend either for validation or to do create, update, delete etc when records are updated, deleted or created

You can simply use Hasura event triggers
Hasura can be used to create event triggers on tables in the Postgres
database. Event triggers reliably capture events on specified tables
and invoke webhooks to carry out any custom logic.
so, in your case, you can set up a simple event trigger on the create/update of the Boy table, you can then create the IceCream row in your webhook.
The one thing cooler about event triggers is that you can manually invoke/retry the same operation in case of failure!
for more info: Hasura Event triggers

Related

Algolia Update Index On Relational Database change with Laravel Scout

I have implemented Aloglia for my Movies table with actors as relational table and it works fine.
Problem:
When I update any movie its also updating algolia index (its good). But how can I update index if I made any change in relational table (for example update an actor of movie).
How to push a specific record manually with laravel scout.
Thanks
The issue itself lies in laravel's events. Whats happening is scout is listening for an 'updated' event which only occurs in laravel when the model object is saved and is dirty (aka value differ from that in the db).
There are two ways you can do this.
The bad lazy way would simply be to add ->touch() to the model prior to save - this will force the updated_at field to update and ultimately trigger the updated event. This is bad as you're wasting a DB query.
The second and preferable way is to register another observer on 'saved' which triggers regardless of whether or not the object is dirty. Likely you either want to check if the model is dirty and only index when its not (to prevent double indexing from the updated event) or just de-register the 'updated' listener that comes in Scout.

parse: query a project to see if a table name exists

I am a relative newbie to parse (and IOS) so I hope that this question is legitimate. I am using parse tables as events with entries into the table being all who have joined the event. I want to be able to query my project to see if an event (table name) exists. Is there a way to query the entire project to see if a PF object exists based on the table name? Thanks in advance for any help provided.
If I understand well, you are creating a table by event, you will end up having as many tables as events, which is not what you want. In my opinion, you should be creating a table with all events, then another table with all the persons joining the events. On this second table, you will use a Pointer to the first table. This way, you can query all events in the first table and see if a particular event exists.

Oracle Database Design View or Table?

I'm new to database design and faced a problem while trying to design one. Here's my system:
There are 3 different softwares (SW_A, SW_B, SW_C) and 500-600 users. Each user can use any of the softwares and do some work. Each work is sent to the database approx. 6 times per minute. (Randomly for users and software)
I want a database to keep all user actions related to the softwares for archive and also want to keep track instant actions for all users related to these softwares. (Only the latest action for distinct users for each software)
So I designed sth. like that:
Table: SW_X_DATA_ARCHIVE
Columns :
UserID
Data_Date
SW_A_ActionID
Table: SW_X_INSTANT_DATA
Columns:
UserID
Data_Date
SW_X_ActionID
When a new data comes, it updates INSTANT_DATA table's related row if that UserID exists, inserts new row with UserID and data if not. Then inserts the same data to DATA_ARCHIVE table.
The question is: Is there a better way for this kind of work? Views maybe?
(I've read sth. about materialized views but not sure how to use it here.)
Thanks
Only the latest action for distinct users for each software
Your INSTANT table should have a unique composite key on (USERID,SOFTWAREID) so that it is not possible to have more than one row relating to a user's use of a software.
The ARCHIVE table does not require such a unique key or constraint.
You would insert into INSTANT if a row for (USERID, SOFTWARE) does not exist, or update the row if it already exists.
Typically, an insert/update trigger would be placed on the INSTANT table, to insert a row into ARCHIVE whenever a change happens to INSTANT.
EDIT: alternatively, you could maintain a single table, and have a view that selected only the latest action per user per software. You'd need to timestamp every action and place a composite index (non-unique) on (USERID,SOFTWAREID). The decision on which approach to take might be based on relative performance. I would tend to favor the trigger if the Latest Action information was needed frequently.

Oracle APEX form based on multiple tables

Can I create a form in APEX which creates/updates records in multiple tables? For example two tables with a one-to-one relationship.
If you are using Automated Row Fetch mechanism, you can use two different ARF for two tables. If you want to have more control, you can define your INSERT/UPDATE statements yourself usign Page Processes.
A simple way would be to create a view that returns the joined tables and base the Apex form on that. You may need to add INSTEAD OF triggers to the view to be able to insert and update successfully.

Oracle Row Level Security in multi-tenant app / default values for new records

Task
Retrofit an existing application to use a multi-tenant approach. It shall be possible to create tenants and each user's session should reference exactly one active tenant. Each tenant should only be able to see and update his partition of the database schema.
Approach
Create an Oracle application context that contains the tenant id
Add a tenant id column to any table that should be scoped
Create a predicate function that returns "tenant_id = sys_context('tenant_context', 'tenant_id')" for SELECT, INSERT, UPDATE and delete
Add an appropiate policy via dbms_rls to register the predicate function
This works like a charm without touching the existing application for SELECT, UPDATE and DELETE
Question
When inserting the tenant_id column doesn't get set and a security exception comes up. Is there any way that is as sleek as the predicate function to always set security related fields? I'd rather not add triggers to 300+ tables.
Sometimes asking a question provides the answer. I wasn't aware that you may use non-constant expressions in column's default values, so
alter table XXX
add column tenant_id default sys_context('tenant_context', 'tenant_id');
actually solves my problem.

Resources