Ember SPA application with related models - ember.js

Lets say we are working on a recipe app and we have 2 get requests initially.
/ingredients/
/recipes/
These create the recipe and the ingredient model.
Now recipes are made of ingredients and amount of each ingredient.
And ingredients only have names.
Now populating the models are easy using the models and the default serializes and the adapters if we confirm to the https://jsonapi.org/
How do we handle when one of the ingredients is deleted. In the backend I use django if and ingredient is deleted DB also deletes all the recipes that have relation to the that ingredients.
But on the SPA application the let say we ran
ingredient.deleteRecord();
ingredient.get('isDeleted'); // => true
ingredient.save();
or
ingredient.destroyRecord();
How does the recipes model get updated ? So do you manually do what server does on the client(and delete related recipes by custome code)? Or should you just update the list and repopulate it(easier). And can you give dependencies between models so a model in the store would know that it need to be updated when one of it dependencies model is changed so it automatically make a get request and repopulate itself(are routes/models that clever?)
On that note what is the standard way of keeping server and client in sync.
What is the standard for this sort of stuff. Should client app re-get all that data again just to be sure that server and spa are insync and uptodate.
Thanks

In the scenario where an ingredient gets deleted, the save has gone through successfully on the backend and now needs that ingredient needs to be removed from many recipes on the frontend before your UI is truly in sync, I would suggest using this gist to push your delete properly into the store: https://gist.github.com/runspired/96618af26fb1c687a74eb30bf15e58b6

Related

Django: Last modified by and created by user automatic saving

The age-old question: How can I automatically save last_modifed_user in django models?
I found in several places this general process how to do it using thread local. I'm hesitant to simply implement it that way because I'm not entirely sure of the consequences it has and because all these posts are old.
Is using thread local still the "recommended" way of doing this in django 3? Or does django3 have a better options of doing it?
No, this hasn't changed. Simply because separation of concern is an architectural principle of MVC (model-view-controller), which is also how Django (model-view-template) and most web frameworks with ORM are architected. Models know nothing about the request, it's not available (and in many cases there isn't a request at all when a model is saved, think of management commands or regular tasks running in the background).
The alternative to thread local is to make sure you implement it yourself in the controller layer (view layer in Django):
Create a view mixin that you can mix with all the generic views that use the ModelFormMixin to save the user into the model (ModelFormMixin.form_valid()). Or combine it with a form mixin where the user is passed to the form (FormMixin.get_form_kwargs()) and saved when the form is saved (ModelForm.save()).
Create a ModelAdmin mixin that does the same when saving a model in the django admin site.
This of course means someone on your team may forget to do it when creating new views and forms. The link you posted contains an answer as to the advantages and disadvantages of using thread local.

How to dynamically swap default database on the model manager in django?

I am creating a project in django and django rest framework. Its an api for an angular app. The database setup consists of multiple databases. one is default database, all the django tables reside in this database; rest of the databases belong to a type of a user, each user is supposed to have a separate database. So, all the user related data goes to its separate database. To implement the selecting database dynamically, user object has an extra field to store the database to write to.
from django.contrib.auth.models import AbstractUser
class CustomUser(AbstractUser):
"""Custom User model."""
database= models.CharField(max_length=9)
Reason for doing this was performance improvement as each database is separate, ListView and DetailView would work faster than if the data was stored in the one database only.
I know I can choose a database to store by using the using method on the model manager. In the rest api things work fine and data is being stored in their separate databases, but I end up overriding methods that django has defined. Its adding development cost to the project. Foreign keys and ManytoMany keys needs to be resolved with the current database of the user, which is not happening as I have customized the database setup. Also, my code cant be as good as theirs :p , as they have written django over the course of many years.
I have overwritten many querysets already, but django still uses default database many times. If only I could use the request object in the model manager of django models to swap the default database on per request basis, things would be different i think.
My questions are -
Is there a way to access the request object in the model manager? I could do something to the effect of below code.
class CustomManager(models.Manager):
def get_queryset(self, request):
return super(CustomManager, self).using(request.user.database).get_queryset()
Model manager has _db property that could be used to select database. Would overriding it is advised? if yes, how and where in the code?
Is there a better way to implement the separate databases?
Thanks in advance.
Regards
Using a database router is recommended in Django docs, but the problem is it only accesses the model class.
Found a couple of questions related to the problem of switching databases dynamically. This post has a solution that would solve the problem of passing the request.user or any other parameter by using a threading.local instance.
Someone created a reusable plugin even for this - https://github.com/ambitioninc/django-dynamic-db-router
Hope that helps.

Do all tables need to be built as models?

I am new to Django and trying to figure out models. I have a question that I just cannot find the answer to. I am designing an equipment checkout app. I would like to have a data entry page (/Admin) where we can enter the equipment names and types...no problem (I think). However, I also need a Checkout table that will be updated as equipment is checked in and out and an Employee table that would hopefully pull from AD. My question is, do these two tables that will not be updated manually on the Admin page still need to be defined as models? Is this where the 'EDITABLE' field option comes in? Thanks!
Models in Django are the main interface to communicate with data layer. They map directly to database, provide access to it using ORM and also track changes via migrations. They can be integrated with pretty much all Django components: generic views, forms, serializers, etc.
Just because certain piece of data is not being updated manually, doesn't mean that you don't need to define models for it.
If you want to use database tables separately from models, you'd need to manually create DB connection and use raw SQL or create custom data model. Unless you're using a third party external service, I don't think there are many use cases where that would be required.
well i would do it like that
models.py
class Equipment (models.Model):
name = models.CharField(max_length=30)
types = models.CharField(max_length=30) #choice field here would be better
serial = models.CharField(max_length=30)
employee = models.ManyToManyField("Employee")
class = Employee (models.Model):
#here you add all employee data
this way you could create an instance from the admin for every equipment and employee and on every checkout you can update the equipment

Where to collect data from the web in Rails 4 MCV

I'm building a rails application where I parse some html data on the web and then save it in my database, the data is saved in multiple models though. I'm currently doing that in the controller, but I'm not sure where I should do it in the MCV model!
You are doing it well, the work of the controllers is get the data from users, aply some logic and save it on database.
The model work is to ensure that data is correct and understanable and work as gateway between ruby classes and database tables.
Here you can find more information about MVC
http://projectmanagementdud.blogspot.com.es/2013/03/model-view-controller-mvc-simply.html
In your case you're parsing data in your controller and this controller save data on multiple models, this is accepted, one controller is not obligated to interactue only with one model, if isn't, save data in the models with the same action will be impossible, a good practice is to choose this controller with wisdom :).

Ember nested dynamic forms

I'm new to Ember.js but I decided to try it for my task.
The task is to make a dynamic form builder: user should be able to add boxes with names and then insert field configs (which are forms) into that boxes.
Field configs are editable and they should be saved after edit.
In the backend I use Rails+AMS+Mongo and the data structure looks like that:
Company has one UsersProfileConfig
UsersProfileConfig has many BoxConfigs
BoxConfig has many FieldConfigs
In Ember I've already created models for BoxConfigs and FieldConfigs and basic routes for them.
Now I feel stuck and don't know what to do next and whether it was a good idea to use Ember for that.
Could anyone suggest me workflow or next development steps?