Express js uses qs parser to parse query strings into objects. Is there a lib out there for use on front-end applications that does the reverse? I would love to be able to write a mongo db query on the front end in object form and have it parsed automatically into the format qs parser is expecting right before the request is made.
I would love to be able to write a mongo db query on the front end in object form
You should absolutely not do this unless you're OK with essentially giving any user of your web app an open shell to do whatever they want to your database.
But with that said, I found this in bower.io:
https://github.com/fernandofleury/query-object
More here:
http://bower.io/search/?q=qs
Stumbled upon this question, I suppose you don't need it anymore, but I hope this helps other users who end up here:
Meet graphQL. It's an open standard, which lets you query backend api's in a flexible manner.
Mongodb even supports it out of the box and it is permission based, so no worries about frontend users hacking into your database (you should of course properly configure those permissions to limit access to specific resources).
Read more on https://docs.mongodb.com/realm/graphql
Related
This is possibly a broad question:
I've been teaching myself how to program for the last year. I've learned how to create simple websites using HTML, CSS, and JavaScript. I've also learned how to create simple databases using PostgreSQL. I need help figuring out how to connect (API) the front end and the back end.
A friend suggested I learn how to use AWS RDS so I've already created a PostgreSQL DB instance there (tutorial). I've also connected to my DB using pgAdmin 4 using this tutorial.
What I would like to do is have my front end take in a number as the user's input and "submit" (POST) that value in the DB. Every time the user submits a new value, the new value is saved in the DB and the cumulative values of everything POSTed is returned to the front end for the user to see.
I know this project is a bit asinine but its a start. I can't find any more links on the internet to teach myself how to do this. If you have tips or resources (web links) to help me accomplish this, I would be really grateful.
Side note - I've also been studying python for the last year if that helps.
First, connect to the database like so:
$c1 = new PDO('pgsql:host=THERDSURL;user=THEDBUSERNAME;dbname=THEDATABASENAME;password=THEPASSWORD;connect_timeout=500',null,null);
Then start a new query like so:
$statement=$c1->prepare("select * from mytable");
Then execute it:
$statement->execute();
then loop through the results like so:
while($row=$statement->fetch());
{
var_dump($row)
}
it will vary a little depending on whether you're getting data or inserting data, but begin by learning how to get (select) data. Create a table using a GUI of some kind and learn how to query it programatically.
NOTE: Most people use a library of some kind to make the querying both more powerful and less complicated.. This code is in PHP, but a very similar process is done in many languages.
Note2: do not try to do any of this in javascript that's embedded in html or that's running on a client machine. Connecting to postgres using embeded javascript is not the normal way to go about doing this. Usually people use a language like PHP, Python, Java, etc.. and that's usually running on the server-- and although you can use javascript that runs on the server, I wanted to warn you about this because newer developers might not realize that javascript running on the server vs the browser has completely different capabilities where this is concerned.
I have setup my application for REST access as per documentation. The default routes are working well. I am able to retrieve, update and delete records, however, I am not sure how I could filter data sending parameters to the controller. I wonder if I can do that using querystring or if there is a better way to accomplish that. Please can someone give me directions?
Reads about the Request object in the manual. And use the Search Plugin for filtering.
The search plugin comes with a lot of documentation that explains how to use it as well.
Your question is so generic that a proper answer would end up in a whole article - which I'm obviously not going to write, there is enough information available on HTTP requests and query params. Use Google or read these links:
https://developer.mozilla.org/en-US/docs/Web/HTTP/Messages
https://www.w3.org/Protocols/HTTP/Request.html
A little backstory
I have to develop a web application for college. This web application has to do with managing different locations using google maps like pinning new locations adding custom descriptions and so on. The login part is done using facebook (login with facebook). The more interesting part would be that the queries (client-server) would have to be done by using REST.
The part that i try to understand
If i use a database to store my user's unique ID, their online status (online/offline) and somehow (didn't settle actually on the idea) to keep a JSON on the server that would contain each user's pinned locations, would all this actually be ok with the REST paradigm ?
I find mixed answers on the internet and i don't know how to think of the statelessness of the application correctly. A session would not be created but the credentials from the database would be necessary for the users to communicate with each other.
The other side of the question
Considering that i'm mistaken and i shouldn't use the database to store the credentials and locations like that, how am i supposed to keep all that data ? I'm thinking something like JSON cached client-side but what if my client changes the computer, wouldn't this mean that he loses all his data? (Also wouldn't this make MVC handicapped by not having a model?) How do i really keep track of all things.
You're making this way too hard on yourself, try to keep it simple since you probably have a deadline. REST is a way of using APIs with HTTP verbs like GET, POST, PUT, and DELETE. It says nothing about how to store the data behind your APIs.
As for storing the data, a database should be fine. Storing it as JSON in the db could work, but in the end you'll have to parse the json every time that you want to use it, so I would suggest that you store it in a DB in such a way that it can be read easily.
For a beginner (especially if you're doing this for a school project), I would definitely suggest that you set up a relational database like Microsoft SQL Database (Microsoft Stack), or a MySQL/PosGres Database (I think this is what they'd use in linux), but if you wanna skip the relational db approach (because it might not be all that "easy" to get going), you can always try a NoSQL database like MongoDB.
Relevant links to help:
http://rest.elkstein.org/ (REST explained)
http://www.restapitutorial.com/lessons/httpmethods.html (REST verbs)
http://en.wikipedia.org/wiki/Relational_database (what is a relational db)
http://en.wikipedia.org/wiki/Database_normalization (Kinda the goal of relational db.. but note you can go too far...http://lemire.me/blog/archives/2010/12/02/over-normalization-is-bad-for-you/)
http://www.mongodb.com/nosql-explained (NoSQL explanation)
Im in the start up phase of creating an internal system based on PHP and MongoDB. The users of this system are Javascript programmers and would like to be able to make custom queries to the Mongo database from a frontend gui with arbitrary Mongo shell queries. Of course this would not be a problem at all if I forced them to to write the queries with proper PHP arrays etc, but i would definitely like to avoid this.
I am not quite sure how to approach a feature like this without writing some advanced methods being able to restructure the queries to proper formated arrays that can be used in MongoClient PHP. One approach would be making use of the i.e. MongoDB::execute() method and run the javascript on the database server - a method i don't fancy at all.
Im kindly asking if you have any ideas on how to achieve the requested functionalities to some extend.
Thank you in advance.
Are you looking for something like this : http://rockmongo.com/ ?
I was looking at trying to generate a form based on a Mongoose schema definition. I was having trouble finding where the schema info is tucked away. Where is the path type info etc kept in the object?
Or better, has anyone tried to do this already? I'm using Jade but something that pumps out HTML would also be good.
My little project creates complete CRUD for a mongoose schema. Its a little rough but
might be useful.
https://github.com/jspears/bobamo
It is very client side though, all the forms are generated (they can be overriden with static versions if it is not what you need). That is it doesn't use jade, but it does use underscore template on the client and jqtpl on the server to generate the javascript that makes the form.
Wow that was really unclear... The browser talks to the server via JSON/REST it loads this into an all javascript front end. This front end is generated on the node server on demand, to the client. So it easy to modify, and relatively cleanly sepeartes the data from the view.
I can recommend checking out Formage (npm: formage)
https://github.com/Empeeric/formage
You can get good ideas from this project.
If you're exporting out your models, ie
module.exports = mongoose.model("ModelName", Model)
Check in your module's .schema. That should have all the info you need.