REST-api structure when creating associated records during update - database

In my database, I have two tables, User and Location. Each user should have a location. If I have a user and want to update it, I use PUT on "users/:id", and could add locationId if I want to. What if location does not already exist? I am aware that I could first create a location and then update with the new locationId, but I would like to be able to do it in one request.
I have implemented a way to do this, where I can send a location-object under key: "location" in the body when doing the request in "users/:id". This works, but I understand that this means I am creating in my database, when the requests in itself is a PUT-request. I this allowed in REST, or do I need to do two requests to follow the best practices of REST? The two requests seems pretty annoying for client if I have multiple associations to the user, and would be a lot of requests.

Related

FaunaDB, make connection between different users, I.e. become friends

I have a use case where I want to connect two different user roles, and if they accept and want to connect, new features will open up. It is very similar to how friend requests work at Facebook or LinkedIn, opening up and showing more content. Let's call them role1 and role2.
All users are stored within a "users" collection with an id. Depending on their provided role within the document attached to the "users" collection, they can store additional data in their respective role-collection, i.e., role1 collection or role2 collection.
What is the best approach and structure to connect the two users, i.e., become "friends"? Should I have the connection stored in a new collection, named perhaps connections-collection, or multiple collections?
I'm using Next.js, NextAuth for user authentication, and FaunaDB as a database. I'm using Fauna's query language, FQL.
Have you perhaps seen fireship's video RE: fauna db? I think it covers what you want to do and how you can proceed.
Edit: There are many ways to implement this. Based on my understanding, perhaps you can have "Friends" and "Requests" arrays stored under a user document. That way you can differentiate between confirmed friends or a just request.
Example: When user1 initiates a friend request with user2, you store user1's ref under "Requests" of user2's document. When user2 confirms, you move user1's ref to the "Friends" array.
This is just a overly simplified idea and you may need to consider your options and the implications. You would need to plan and define the predicate in both roles so you would only see what is necessary.

big query how to hide people's email address when we want to share dataset

I have a big query project for multiple clients, they all have READER (and will ever only have READER) permission. When the client go to big query, they are able to go and click share dataset.
share dataset
The client will not able to actually share it (when they click save changes, they will be prompt with an ERROR saying that they are not allowed).
However, they can see whoever has the access to the dataset, including other clients' email address.
Is there a way to "hide" other people's email address?
I was thinking to create a google group and add those clients to the group and assign the READER permission to the group. One thing that troubles me is that individual client can actually leave the group.
Is there any better solution?
Thanks
Groups should works fine.
If you have g-suite - so google groups controlled by organization - you potentially can have audit on changes in group - so you can catch moment client leave the group. https://developers.google.com/admin-sdk/directory/v1/guides/manage-group-members

URL to open Access Web DB form at specific record

I would like to create an email workflow using an Access Web DB form to manage holiday requests. The user will create a request from the form, save it - which triggers the SendMail data macro, ideally with a link in it for the approver to click and go to the approval form filtered to the correct request ID.
Does anybody know:
a) If it is possible to load the form in the browser filtered at the specific record ID. I have tried appending ?ID=1 to the form URL and so on, without any joy so far.
https://mysharepointserver.com/sites/mywebapp/default.aspx?ID=1
b) If possible, what steps do I need to go through to get it working?
This is an Access 2010 web database hosted on a heavily bespoke Sharepoint platform. Any help appreciated.
I have worked it out, using:
?Page={ID}
However, the one caveat with this is that if you delete a record then the page number refers to the records position in the table, not specifically the ID. If you can ensure that records are never deleted from the table it will remain aligned with Page number and ID number.
Its a bit of a pain, but if anyone finds a way of referencing ID directly that would be the ideal solution.
You can also use something along the lines of:
http://...sp_site_path.../default.aspx#Type=Form&Path=NavForm.Subform&Name=ActualForm&DataMode=Edit&Where=%3DID%3D1000
I believe the key part here is...
&Where=%3DID%3D1000
...which in this case refers to the database field [ID] and record 1000, ie:
[ID]=1000
The rest of the URL is refers to a specfic form within your site, however is you only have one, it is potentially not important for your purpose, and you would be able to get by with something like:
http://...sp_site_path.../default.aspx#Where=%3DID%3D1000
Since all my sites use navigation pages, I haven't tested this out

AngularJS: combine REST with Socket.IO

In the single page webapp I've recently built I'm getting data for my models using Restangular module. I'd like to add real-time updates to the app so whenever any model has been changed or added on the server I can update my model list.
I've seen this working very well in webapps like Trello where you can see the updates without refreshing the web page. I'm sure Trello webclient uses REST API.
What is a proper way to architect both server and client to archive this?
First of all, your question is too general and can have a lot of solutions that depend
on your needs and conditions.
I'll give you a brief overview for a single case when you want to leave REST APIs
and add some realtime with web sockets.
Get all data from the REST -- Sokets for notifications only.
Pros: Easy to implement both server side and client side. You only need to emit events on the server with
info about modified resource (like resource name and ID), and catch these events on the client side and fetch
data with REST APIs.
Cons: One more request to the server on every notification. That can increase traffic dramaticaly when you have a lot of active clients for a single resource (they will generate a lot of reverse requests to the server).
Get initial load from the REST -- Sockets for notifications with data payload.
Pros: All info comes with the notification and will not cause new requests to the server, so we have less traffic.
Cons: Harder to implement both server side and client side. You will need to add data to all the events on the server. You will need to fetch data from all the events on the client side.
Updated according to the comment
As for handling different types of models (just a way to go).
Client side.
Keep a factory for each model.
Keep in mind that you need realtime updates only for displayed data (in most cases), so you can easily
use memory caching (so you can find any entity by its ID).
Add listener for every type of changes (Created, Updated, Deleted).
In any listener you should call some initObject function, that will find entity in the cache by ID and extend it, if there is no entity with such ID, just create a new one and add it to cache.
Any Delete just removes an entity from the cache.
Any time you need this resource, you should return the link to cache object in order to keep two way databinding (that is why I use extend and not =). Of course, you need to handle the cases like: "User is editing the resource while notification about deleting comes".
Server side.
It is easier to send all the model then just modified fields (in both cases you must send the ID of resource).
For any Create, Update, Delete event push event to all engaged users.
Event name should contain action name (like New, Update, Delete) and the name of resource (like User, Task etc.). So, you will have NewTask, UpdateTask events.
Event payload should contain the model or just modified fields with the ID.
Collection changes can be handled in two ways: with add/update/remove items in collection or changing all the collection as a whole.
All modifications like PUT, POST, DELETE are made with REST of course.
I've made a super simple pseudo gist for the case 1). https://gist.github.com/gpstmp/9868760 but it can be updated for case 2) like so https://gist.github.com/gpstmp/9900454
Hope this helps.

How to synchronize DB reading and writing from a servlet

I have a Servlet when a request comes it checks for the user id and then if id is not there it creates a new user id in the database. But if I get multiple requests with a very short delay then all those request tend to see that there is not user at the moment and create multiple users with the same name. I just don't want to make the user id field unique to solve this problem. Other than the user id I store some related data as well.
I need to know how to keep a DB locked until one Servlet request is finished processing.
You need to make your servlet code synchronized.
Easy way is to make your servlet implement SingleThreadModel.
http://www.javatpoint.com/SingleThreadModel-interface
But this is not a good approach as your servlet will handle only one thread/request at a time. Good solution is to synchronize the part where you check and generate the uid.

Resources