Node Js, Separating DB models and Proxy - node.js

I am creating an TODO application (REST API). I am using Node JS, express and sequelize. I have defined my models (db entities) and controllers. now I don't want all the fields in my db to be exposed to the client application. like we normally do in any LOB application, So I need to create an Proxy Object for each model that i have. I searched on net for how to create reusable proxy classes in node js way over the net. but i couldn't find any. I also like to use a mapper like we do in java for copying properties with same name from model to proxy and reverse. can you guys share an example on how to do this?

Related

Use VueJS with sailsjs

How do i use vuejs with sails js. I want to list and create dynamic content using Vue. How do I create route for CRUD operations for objects like posts and comments ? How do i check authorization on every route that the user must be logged in for this route to work. One more thing , how can I test the crud operations using Postman
Thanks
You have two ways of using VueJs in Sails.
The view layer of sails js is built on Vue.js, so you can use the native implementation of Vue.js as described in sails js doc.
Or, you can setup a Vue.js standalone app, and make it dial with you sails.js backend by the way of http requests that can you send with http, axios, ... packages .
In both cases, you're gonna use the entry points that you will define in your routes.js file of sails > config folder which would corresponds to controllers that contains your CRUD requests. That is also the way you have to test your "CRUD" operations with postman, by requesting your entry points.
For authorization, a good practice would be to use the policies of Sails.js :)

What is the best practise of using same Model on several projects?

I create a ReactJS app using Sequelize and PostgreSQL database. I defined my models on my ReactJS API, but I need to use the same database (so the same models) on an other API.
How should I do so without writing again the definition of each model, because if I made a change, I would have to do it everywhere. Is there a better way to handle this king of problem?
Why I don't use the ReactJS API? Because when I built my React app, the API disappears: I'm using proxy.
var myModel = require('./models/myModel');
you can add by require here my model is in root directory inside model folder.
You could place the models in their own separate Node.js package, so they could be reused by both.
Please, refer to this: https://docs.npmjs.com/getting-started/creating-node-modules

Mongoose Sharing Validation/Pre-Save Methods Across NodeJS Apps

If one Nodejs app connects to a Mongo instance, and that app has defined a User schema with pre-save hooks, validation, etc.
And then another Nodejs app connects to the same database, and tries to register a User schema with different properties.
And then the second app saves a User
What happens?
I'm confused with how two Nodejs apps may communicate to the same database.
For example, it's very easy to see how one might want to have V2 of an api on a separate nodejs app developed by a separate team. But they will plug it into the same database and use the same Schema (or will they?), and I'm confused with how things are shared between the two apps.
Any help clarifying this in best-practices would be appreciated
I believe I've found the answer in the Documentation.
This connection object is then used to create and retrieve models. Models are always scoped to a single connection. docs
And
Models are fancy constructors compiled from our Schema definitions. docs
Which explains that a DB Connection 1's Schema Definitions (pre-save, etc), do not affect DB Connection 2's writes/etc.
Essentially, they are completely independent of validation and everything else. They only need to be OK in their own context.

Sail.js - how to structure JSON based live data output with existing static data in the model

In my Angular app, I want to display a table which contains the following
a) URL
b) Social share counts divided by different social networks
Using Sails.js, I already have the api created for the URL when the results show up, I can display the URL now I'm confused how to get the appropriate social counts showing right besides
Here's the API I'm using: https://docs.sharedcount.com/
by itself, I can see the JSON it produces
But here are my questions:
Should I create a new api (model/controller) for social count data or include it in my model where I have the 'url' action defined?
If I create a new api or include the social_counts as an action in the current, what would my JSON query look like? to retrieve the URL's, I'm using default API blueprint that Sails provides, so:
http://www.example.com/url/find?where={"title":{"contains":"mark"}}
Struggling a bit in terms of the thought process, would be great to get input on this
It depends on your app. is your app will store that data or just consume it? If it need to store, of course you need the API. In purpose for modification or aggregating the data for example.
No, you can't do that. That shortcut method only works if you have the data in your database and let the Sails Waterline ORM and Blueprint API served it.
Perhaps, if you only need to consume the data from that Sharedcount API, you didn't need to use Sails as a backend, in this context. Just use Angular as a client of that API. Except if you need to modify the data first and store it in your own database, so Sails will helps with it's Waterline ORM and Blueprint API.

I'm using express js, mongoose, and ember js. Is there a way to write models only once?

I know Integrating Ember with Express JS says that you generally should have two representations of your models (one in backbone, one in express using mongoose schema), but I was wondering if there is some way to define a model once and have it translated into an ember model and a mongoose model on the fly.
I really don't want to repeat myself in code by writing the same model twice.
The only way that you can do this is by building a generator. I am currently looking into this using noeo4js as the back end, you can either read the ember model, and generate it based on its information or generate a ember model based on your backend's model, or generate both of a descriptor json object.

Resources