Recommendations regarding sharing model code in node.js and browser - javascript

I am using Sequelize as my server side ORM. Is there a recommended approach towards sharing the Model code (especially the validations) with my client application ?
Please don't recommend solutions which require me to move to a NoSQL database. Currently that is not an option for me. While I really Sequelize as an ORM, I am willing to move onto some other model implementation if it is beneficial.

Persistencejs, but it doesn't appear to be as well maintained as Sequelize:
https://github.com/zefhemel/persistencejs

At present there does not seem to be an end to end solution ie. a model library which you could just require in either browser or server and define your models - where methods like save, update would be polymorphic requiring the developer to just extend standard model classes and use them on either client or server.
However, for people looking for a similar solution - I recommend using JSON schema validators, which are quite hassle free and provide a simple means to share your validation logic between client and server.

Related

Connecting to couch db from a webpage

I used to use mysql databases and a php backend for my web applications. Now that I am diving deeper into the the javascript world I am looking for a more compatible way to store my data. I like the idea of writing everything in one language, and javascript seems to be capable to make that job.
I started reading about couchDB and the JSON/javaScipt approach seems to fit perfectly to my needs. My question is now, which would be a convenient way to connect to the database from my pages.
I found this post, which explains how to connect to a couchDB directly from a client-side javascript using a library called couch.js. The post is quite a few years old and the library I cannot find anywhere in the web. So I am asking myself If this approach is still up to date.
I was thought that one important reason for using a server backend is to provide a security mechanism to protect the database authentification process. So: Is it generally secure to save an (encrypted) db pwd in a client side script?
So what I am looking for is a quite simple way to access and store json-like data from webpages. Are there any suggestions?
The hosting of the production solution should also be not that difficult and in the end not expansive, since it's rather meant for smaller projects at the moment.
If the post does not fit to the forum, let me know a better place for it..
I have been using the CouchDB jQuery Plugin for development. At first I was hesitant as well to use it due to most documentation being at least a few years old. So far it has worked really well and been very easy to use, especially if you are familiar with JavaScript/jQuery.
...
You can get the plugin from Github
CouchDB jQuery Plugin
Or you can save it directly from Futon:
http://localhost:5984/_utils/script/jquery.couch.js (where local host is your IP and 5984 is the port CouchDB is on)
Whatever you do, make sure you enable CORS on your database. There are instructions on the Github Readme page, or this script is super useful and easy to use through npm.
...
Documentation can be found here.
This reference has also been very useful for me.
...
Good luck!
I just released Slouch, a JavaScript client for CouchDB that can be used in both the browser and in node. I hope this helps!

Ember.js dynamic model

I'm looking into building an application with Ember.js but I'm not sure about whether its model support is appropriate for my needs. I'm working on a database front-end application which connects to a central server; the main problem here is that the database definition might change (i.e. removing or adding columns).
Editing the code manually to accomodate all changes to the database is something I'd like to avoid at all cost. Therefore, I'd like to know if Ember.js provides a way to generate dynamic models. I've come across the defineProperty method but I haven't been able to figure out how it works, neither have I found anything about it in the Ember.js docs. Besides, it seems to be necessary to run it once for each object; is there a way to run it just once?
A possible approach may be to automate the generation of your client side schema from your database schema.
A similar solution has been demonstrated here (though the implementation is Rails specific) : http://techblog.fundinggates.com/blog/2013/03/automatically-generate-ember-models-from-rails-serializers/

Javascript SPA Multilanguage and LocalStorage

I am developing a JavaScript SPA using DurandalJs and BreezeJs as main technologies in the client side. I would like to know what libraries I can use to do my application support different languages. I have been doing a little of searching and I have found that JED.js (http://slexaxton.github.com/Jed/) can be a good option for this task.
However I am not very sure, can somebody recommend nay library for this task, or give me some opinion about JED.js.
Also I would like to ask about some library for store information in the browser. Can somebody recommend me one? I have thought in amplify.js, it gives good functionality for doing request to the server and caching this information. But in my application I am using breezejs, so this part is not needed. I only need to store some data like the username and some little information.
Very thanks.
MY SOLUTION:
I have implemented my system using amplify.storage for storing information in the browser and i18next (http://i18next.com/) to make my application avaliable in different human-languages.
I am not much help on the first part of your question. I'm quite interested in the response myself!
I do have some thoughts on local storage. AmplifyJS is really three independent libraries. One of them, amplify.store, is dedicated to the local storage issue and is worth looking at. You might look at lawnchair. Finally, look at the Breeze DocCode sample for examples of storing full and partial entity caches in browser local storage; it's primitive but gives you hints.

Does backbone-relational need a backend?

Experimenting with backbone-relational; I'm using Indexeddb to store all my data, is it possible to use backbone-relational without a backend and just let it talk to Indexeddb?
Thanks in advance,
James
Here is link to a similar question.
TL/DR: If you don't need to retrieve or store data on the server side there is no need for backend logic development.
Backend is not required.
Backbone can fully work without any backend if your application
doesn't require one.
That depends on your application. If you want to retrieve value of
some inputs and calculate a result then Backbone won't do that for
you - it will help you structure your code. If you app is simple and
don't need support for models, views and collections or routing,
then there is no point in using Backbone. Hard to answer this
question.

How to use backbone.js with websockets/socket-io/nowjs

I am just getting into backbone.js and am finding progress a little slow. My main problem is working out how to keep my client and server side models in sync using socket-io (technically I am using now.js but the same principal should apply).
I think the best way is to override the sync method but some simple advice would be really welcome.
Simply overwrite Backbone.sync so that it sends messages down socket.io and tells the relevant backbonejs models on the server to alter state.
The interesting part of this solution is setting up the master-master relationship. You need to insure that for any client they can only "update" the state of models on the server that they have "ownership" of to avoid hackers and server-side state corruption.
So for each client they have a set M where that client is the master of all models in M and has a set S where that client has slaves of all the models in S.
It can only force updating on the server of models in M and only one client should have a particular model in M (or you need to implement a solid locking / merging implementation).
Whenever a model on the server is updated you simply push out to any client who has that model in S. (and push to any client who has that model in M if the model is in M for multiple clients).
A lot of thought needs to go into control / permissions and ownership that is normally handled by the MVC controller once a client POST/PUT/DELETE some data.
Check out backbone.iobind: https://github.com/noveogroup/backbone.iobind
It overrides Backbone.sync for you.
A much better approach is event-driven architecture using an event aggregator. Great read on the subject is the following Derick Bailey's article => Decoupling Backbone Apps From WebSockets
It keeps stuff highly decoupled, enables easier testing and changing websockets lib, and on top of it all, it doesn't mess up with overriding Backbone's internals like sync()
Maybe this excellent tuto will help you:
https://blog.andyet.com/2011/02/15/re-using-backbonejs-models-on-the-server-with-node

Categories