What is the best practise of using same Model on several projects? - node.js

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

Related

express js folder structure

I am learning express and i want to find a good folder structure for my project.
i found folder by feature and chose it to my project so I searched some articles to learn this structure.
Now, I have some questions:
where should I put validations? i use Joi for body requests validator and I am confused about where to put the validation.
i can put it into controller and i can create middleware for validated that endpoint, which one is better ? is another way better for that ?
where to put the models, because some models like the user.model used in auth feature and user feature and etc, can I put the models in the feature folder or create general folder for models ?

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 :)

Node Js, Separating DB models and Proxy

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?

What folder structure should I follow in Hapijs?

I am having all the routes, methods, strategies, plugins and db connection in a single file 'server.js'. And this is working fine. But I want modular structure where the controllers, route, db are seperately defined. I used to modulate it, but stucking that how to call my multiple strategies in some of the routes and also how to call my db connection, the connection is established but I am unable to call it in the controllers. I am using hapi-mongodb plugin fo db connection. Please could anybody tell me how do I structure my files?
I use in my personal project the structure of start-hapiness project.
On this branch, have a simple example of a TODO list using Hapi + Mongoose and some cool plugins, modular and easily extensive!
https://github.com/thebergamo/start-hapiness/tree/dev-2.0
I think for modular MEAN application, angular-fullstack is best.

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