How to render example source code from noSql database? - javascript

I've googled this loads and am sure there is an obvious way that I'm missing, but..does anyone know how to approach rendering example source code that is kept in a noSql database?
I am writing a javaScript blog site and want to show example code rendered from backbone templates whose models are populated from a mongoDB database.
Most of the google results offer a variety of syntax highlighting pulg-ins, but how would I format the code to preserve indentation etc as the code will be flat in a JSON object.
I'm thinking tables may be a key but I'm unsure if I'm reinventing the wheel here.
As always, any help would be greatly appreciated. Thanks in advance.

Strings in MongoDB can store any special characters like line-breaks, tabs or multiple spaces. When you store some program sourcecode as a string in a document, it should look the same when you retrieve the document.

Well you'll need something to access the database and returns back that to the backbone.js client.
You can use a Node.JS server with the MongoDB Driver if you prefer javascript.
You may want to try some node.js packages such as Express and Mongoose.
If this is a small project, just to learn how everything works, that's ok. Otherwise you might want to consider looking into how to display only the Blog Text by backbone.js, and separating the View from the Data. So if you want to change the CSS or they style of the blog you don't need to update (every single blog post document on mongo)

Related

Store users information in a single file on the root of the web server JavaScript/HTML/CSS

I'm trying to do an authentication system for a website that is supposed to be stored on an intranet. The guy told me he wanted the credentials of the users (max 10 users) to be stored on a single file with some kind of protection. Do you have suggestions.? I really tried to find information, maybe I am not searching for the right thing but everybody uses mondoDB in this kind of situation. Which is not stored locally.
be gentle please... I trying to do it in JavaScript.
When you mentioned a single file on the server, I thought of SQLite. As per the projects description:
The complete state of an SQLite database is usually contained in a single file on disk called the "main database file".
But passwords - even on an intranet server - should never be stored in plain text. And one-way hashing where the source code is also present does not sound like a good idea.
However, sqlcipher seems to solve both those issues. As a starter, you might want to setup a brief tester with plain SQLite.(Here is one simple tutorial).
From there you can look at a sample like this one which shows how sqlcipher could be used with a JavaScript engine like Node.js.
I've tried like that to make it easy
The Javascript Code
SOOOOO, i ended up using nedb... VOILA

AngularJS: Load all data at once as JSON vs. using a database and only load parts. What is better?

I have to display products which are stored in an ERP on a webpage. The ERP can produce an XML or JSON file which would include all products. The webpage needs functions like pagination, sorting or filtering by attributes. At the moment I think the easiest way would be to just load the entire file in AngularJS and then iterate over all items and work with that. The number of products is limited to ~500. The reason why I think this is the easiest is because the client changes the information on a daily basis in this way I don't have to write an import / synchronization process for a database.
But I am a bit worried about performance. Sorting, filtering, pagination etc. are all things that would be very fast with a database (probably MongoDB since the datastructure is quite simple).
Can I expect serious performance problems? Is this doable? Or should I put a database between ERP and frontend that does the heavy lifting?
The only way to be sure is to test it out yourself, that is what I would do. The answer is very often, "it depends.. ". You say its around ~500 products, if each product only has a product name, then a database would be overkill. Angular is perfectly comfortable with that amount. But if each product got tons of properties and nested data, the file itself could be very large to even load on every pageload. So, it depends..
I would to this:
Export the file from the ERP as JSON
Create a boilerplate angular app
Put the JSON file as a resource file
Create a simple repeater and throw those objects out into the DOM
Now you can easily experiment with filtering, sorting, pagination and so on. Test if the browser perfomance and load time is what you are looking for.
I think there will be no performance problem on data transfer since there are only 500 elements in the JSON file. But maybe you'll experiment performance problems in showing those 500 elements with AngularJS.
Instead of pagination, you can checkout 'Infinite Scroll' with AngularJS, a good solution for result showing performance. Check this article: http://www.williambrownstreet.net/blog/2013/07/angularjs-my-solution-to-the-ng-repeat-performance-problem/
And no, I don't agree having a database in the middle. You are trying to use it as a cache, but you'll have more problems than solutions, because since you have only 500 elements, you would not gain performance at all. And another added problem: database maintenance ;)
Cheers

How does HTML templating fit in with the backend language and database?

I apologize if this question is not specific enough, but I do need some help understanding this concept. I've been researching many Javascript libraries including JQuery, MooTools, Backbone, Underscore, Handlebars, Mustache, etc - also Node.js and Meteor (I know all those serve different purposes). I have a basic idea of what each does, but my question is mainly focused on the templating libraries.
I think the general idea is that the template will be filled by a JSON object that's retrieved from the server. However, i'm confused by how that JSON object is formed, and if it can go the other way to the backend to update the database. Please correct me if this is incorrect.
For a more solid example, let's say I have Apache running on Linux, and am using MongoDB as the database and python as my primary language. How do all these components interact with the templating library and each other?
For example, if I have an HTML file with a form in it and the action will be set to some python script; will that script have to retrieve the fields, validate them, and then update them in the DB? If it's MySQL I'd have to write a SQL statement to update it, but with Mongo wouldn't it be different/easier since it's BSON/JSON based?
And for the other example, let's say I have a view-account.html page that will need to pull up user information from the DB, in what form will it pull the information out and how will it fill it into the template? I'm guessing i'd have to have a python script that pulls the information from the DB, create a JSON object, and use it to populate the fields in the html template.
I am aware there are web frameworks that will ease this process, and please suggest any that you would recommend; however, I'm really interested in understanding the concepts of how these components interact.
Thanks!!
There are obviously many ways this can all work together, but it sounds like you have the (a) right idea. Generally the frontend deals with JSON, and the server provides JSON. What's consuming or providing those responses is irrelevant; you shouldn't need to worry that Mongo is your database, or that underscore is handling your templates.
Think of your frontend and backend as two totally separate applications (this is pretty much true). Ignore the fact that your frontend code and templates are probably delivered from the same machine that's handling the backend. Your backend is in the business of persisting data, and your frontend in the business of displaying it.
RE: Mongo using JSON/BSON; The fact it uses the same language as your frontend to communicate is a red herring. Your DB layer should abstract this away anyway, so you're just using Python dicts/tuples/etc to talk to the database.
I'm guessing i'd have to have a python script that pulls the information from the DB, create a JSON object, and use it to populate the fields in the html template.
Spot on :)

Querying data with JavaScript

I am currently looking for an intelligent best practice solution for the following problem. Within a web page, I want to make use of data that is currently organised in some MySQL tables.
Problem: I cannot access the database from the webserver, so I am not able to query the data using SQL. Therefore, I thought to transform the data into something JavaScript can handle (e.g. JSON) and perform all the operations on the data on client-side.
Doing SQL-like queries on an object will probably be kind of hard, even if those I need will probably be easy (simple SELECTs). Is there a elegant way to do that? Some nice little javascript library?
Thanks in advance for your ideas.
Best practice would (of course) be to put some kind of link (even if not a direct one) between the database and the web server.
For SQL in the browser, though, there's the TrimQuery library, basically a mini-SQL engine written in JavaScript for use on the browser. You would output the "tables" as arays of objects using JSON (as you indicated), and then query it via TrimQuery's SQL support.
But if you can't access the database from the web server, I assume you'll be making a mostly-static copy of the data as an admin procedure and placing it on the webserver. That being the case, you may well be better off determining your usage patterns in your web app and then formatting the data into useful objects as part of the DB->web server copy operation. But if you really need ad-hoc queries into the data, TrimQuery may be a route you could use.
You can try JSINQ:
http://jsinq.codeplex.com/
which is a Javsascript LINQ implementation. It allows to "query" json object with linq expressions (then in a sql-like way).

Any dynamic database frontend tool from which you can update directly?

This is with reference to this question where I got one tabular report format.
I need to update the user entered values correctly back to the same table rows. I am in the process of doing this by using general form post data methods by using some logics which I think will not be easy to maintain. So, just out of curiosity, Is there a front end creator javascript libraries or frameworks which can create the front end for any query's resultset and updates the corresponding rows when the user updates them from front end. This need not have all the full functionality, any one usable, customizable thing will reduce the code maintenance problems. I have googled for some javascript libraris for this but not able to get which will be suitable. Please suggest any useful tools. My environment is Mysql, PHP, JQuery, XAMPP server on Windows. Is JQuery provides one.
Thanks in advance.
"...from any query's resultset..." I suspect there is no tool that can do that. Imagine a resultset of data from a table with one (or more) columns which has a foreign key to another table(s). A tool would need to find the relationship between the tables, update the "lookup table", and then update the dependent table. Now imagine a more complicated resultset from a variety of tables - maybe some of those tables are views. Tall order...
I use phpMyAdmin to update tables in my schema, but I perform these updates as an admin rather than a user.
I dont know the nature of the app nor do i know of any JS libraries with this type of functionality. I think i would look at PHP libraries/components as opposed to JS. For example Symfony could lend you this functionality through the use of its admin generator features. While converting something youre well along on to Symfony for the sake of admin generation probably isnt the best idea, i think youre bound to find alo tmore resources on the server side than the client side. :-)

Categories