I am new to CouchDB and CouchApp and I am trying to make a couchapp that can be called on any couchdb database and present the user with the available fields so that he can choose one and get all the records for that field.
It would work kind of like this: One calls the app with something like
http://host:5984/couchapp_db/_design/couchapp_name/index.html?host=data_host:5984&db=data_db
This presents the user with the list of keys in the database. The user selects one and the app shows the values for that field.
That requires that there is a view that gets all fields in the database, I already solved that with the post "how could-i-determine-all-possible-keys-of-a-couchdb-database"
In order to get the values the user asks for, I would need a simple view like:
function(doc) {
if(doc.user_selected_field)
emit(null, doc.user_selected_field)
}
How can you check from the couch app if this view exists for the database and if it does not, how can you create it?
Maybe there is a pretty standard solution to this, but I have not found it, and, as I mentioned, I am just starting with couchdb.
Couchapp may not be your best option for this. You can use CouchDB on multitudes of way more powerful platforms very easily, so why not consider something else?
CouchApp is a very simple platform for viewing your database, think of it like phpMyAdmin but like really customizable.
Related
I am working with NodeJS and Cloudant (alternatively the DashDB Warehouse if that works better). I wonder if it is possible to have a function in NodeJS that gets called each time a document has been added to the database? I have checked out indexed views but can't really understand how to do it. Does anyone have any good tips regarding this or what documentation to look at?
You can listen to DB _changes in Cloudant (https://console.bluemix.net/docs/services/Cloudant/api/database.html) in continous mode.
Every document change in the Cloudant DB (create,update,delete) will be notified through this channel.
There are different nodejs libraries you can use with this purpose. This is
one example: https://www.npmjs.com/package/cloudant-follow
Does it create any major problems if we always create and populate a PouchDB database locally first, and then later sync/authenticate with a centralised CouchDB service like Cloudant?
Consider this simplified scenario:
You're building an accommodation booking service such as hotel search or airbnb
You want people to be able to favourite/heart properties without having to create an account, and will use PouchDB to store this list
i.e. the idea is to not break their flow by making them create an account when it isn't strictly necessary
If users wish to opt in, they can later create an account and receive credentials for a "server side" database to sync with
At the point of step 3, once I've created a per-user CouchDB database server-side and assigned credentials to pass back to the browser for sync/replication, how can I link that up with the PouchDB data already created? i.e.
Can PouchDB somehow just reuse the existing database for this sync, therefore pushing all existing data up to the hosted CouchDB database, or..
Instead do we need to create a new PouchDB database and then copy over all docs from the existing (non-replicated) one to this new (replicated) one, and then delete the existing one?
I want to make sure I'm not painting myself into any corner I haven't thought of, before we begin the first stage, which is supporting non-replicated PouchDB.
It depends on what kind of data you want to sync from the server, but in general, you can replicate a pre-existing database into a new one with existing documents, just so long as those document IDs don't conflict.
So probably the best idea for the star-rating model would be to create documents client-side with IDs like 'star_<timestamp>' to ensure they don't conflict with anything. Then you can aggregate them with a map/reduce function.
I am using pouchdb on client side and couchdb on server side. and both are in sync.
I am accessing couchdb from java using client-api lightpouch.
I am storing transaction data, each transaction is stored as document by prefixed _id like
Transaction_1,
Transaction_2
..
..
so on
Now i want to access all the documents where the _id field starts with Transaction on the server.
This is possible in pouchdb and i am able to achieve that.
But i am wondering how can i achieve the same at server side, in java using lightcouch.
Or is there any Java client-API available that provides this kind of functionality. ??
To find all documents whose _ids match a certain prefix, you only need to do:
/_all_docs?startkey="foo"&endkey="foo\uffff"
(For the prefix "foo".)
I wrote up a bit about why this works here.
LightCouch aims at providing a simple API
for communicating with CouchDB databases.
What you need is a CouchDB view server-side which you can request with LightCouch.
I'm working on an app that i being built using Node and Express. All is fine, however the app is currently not asynchronous and I'd like it to be, so I'm currently investigating what would be the best way to do it.
As far as I can tell, socket.io seems to be the preferred choice to go with Node.
My question is, is socket.io's methodology the best way to move data between the server and client or is there a better, more robust way to do it? Maybe something accomplished with Node only?
PS: I think socket.io sounds really nice. Its just that I'm new to Node and though there would be a simpler way to move data back and forth.
Many thanks
EDIT:
Ok, I've seen the term "realtime" used before and was frown upon. The commenter implied that technically there is no "realtime" application, hence me choosing asynchronous, however realtime does describe what I'm after: An app that will be all ajax-like. For instance, in my app, when I need to edit a saved document (mongodb records are called documents), I need to redirect the page passing the document id as argument. I don't want that. I want all through ajax. I can achieve this with jQuery, however behind the scenes the server will still be moving through urls (I'll need to create loads of app.get('product/:id/edit', ...), app.post('/product/:id/edit'. ... and then use $.ajax to get and post stuff ) so I was wondering what's the best way to achieve this.
PS: I might be looking at this completely wrong. Like I said, I'm new to Node and for app development for that matter.
EDIT2: An example: Let's say I have a page with a table in it where I list all products. Each product will have a EDIT/DELETE button. At the moment, when I click edit, I'm redirected to another page where I can edit the product and save it, then I'm redirected to the product listing. I'd prefer to load the product into a modal window, make whatever edits I need, then update the product/listing without leaving the page.
Using $.ajax I can use the product ID, enquiry the db for that particular product, populate the field in the modal with the product details and display to the user. Then allow the user to make the changes and update the products, however the part in which I need to enquiry the db in order to populate the modal is muddy because the id needs to be passed through the url...
I don't know how to pass the id to the application unless is through app.get('/product/:id/edit', ...) then app.post('/product/:id/edit').
I'm using nano to connect to couchDB through node.js; I have read the basic documentation for couch.db and understand it for the most part, but I didn't see a simple query function anywhere. All I would like to do is (from my server), get the value of a field , from a specific document. How would I do this?
Additionally, while looking for the answer to this, I ran across one site that said an html page can directly send a GET to the DB to get values; I thought the database was secure though, so how is this possible? I guess I'm missing something big here.
The simplest way to get a specific document by id is using the get method. This will return you the document itself:
var Nano = require('nano'),
db = new Nano('http://admin:password#localhost:5984').use(yourDatabase),
db.get(yourDocumentId, function (err, yourDocument, headers) {
return console.log(err || yourDocument);
});
Re your second question about a webpage requesting data straight from CouchDB: this can be done securely as long as you have a login specifically for your end user in CouchDB. Your end user would log in to CouchDB and CouchDB would return a cookie which your browser would send on each subsequent request to CouchDB as a way of authenticating itself.
see http://guide.couchdb.org/editions/1/en/security.html#cookies for more info.
Personally I would not use this approach as it makes it too easy to couple your UI to your data making future changes harder. I would recommend creating a REST api in express and call that from your UI. It will keep your UI code more focused on UI logic and your api implementation focused on dealing with CouchDB document and view operations.