Cannot create database with pouchdb in couchdb - javascript

while trying to just create a database with pouchDB for couchDB, the following error is thrown:
{"error":"not_found","reason":"Database does not exist."}.
My code to create a test db is as simple as this:
var db = new PouchDB('http://localhost:5984/testdb');
Also, the couchDB server is running and reachable at http://localhost:5984/ and it says:
{"couchdb":"Welcome","version":"2.1.1","features":["scheduler"],"vendor":{"name":"The Apache Software Foundation"}}
I have even enabled CORS for couchDB.
Thanks in advance.

According to the PouchDB API (especially the info block), it doesn't automatically create the database by creating a new PouchDB object.
You need to call an API function of the database in order to create the database.
For example, you could create your PouchDB object and then call db.info() to create the database.

Related

how we save logs of our application actions in mongodb(node.js) with one schema and limited information

I am working on a node js with MongoDB database we want to logs of our application action with limited information like success, action name, user id, time, etc so how we can do this in node js with MongoDB
You can look in the official website of mongo how to do it Mongo Logs, and if you want a custom one just create a new db/collection to put in your logs and send it as a regular request.

Create Mongo DB database dynamically in authentication mode with Mongo Client

I have an application where the customer creates a company in UI. For every company, I need to create DB in authentication mode with MongoClient in NodeJS.
Databases can be created on the fly. MongoDB will create a database the first time you store data in it (such as when you create a collection in it). See https://docs.mongodb.com/manual/core/databases-and-collections/ for more details.
When using the MongoDB Node.js driver, you can indicate the name of the database and the collection when storing new data. If the database and collection do not exist, MongoDB will create them for you.
Example:
const result = await client.db("sample_airbnb").collection("listingsAndReviews").insertOne(newListing);
The name of the database is "sample_airbnb" and the name of the collection is "listingAndReviews". If those do not exist when this code is executed, MongoDB will create the database and collection. See https://developer.mongodb.com/quickstart/node-crud-tutorial for more examples.

How to query a database from an Azure function in js

I have an IOT hub getting some data. I also have an Azure function app in js that is triggered when an IOT event occurs. In the function app, I want to query the incoming data against a azure sql database.
In the azure function->application settings->connection string, I created a connection string x with value of the azure db connection string. My index.js file is as below.
module.exports = function (context, IoTHubMessages) {
context.log(`JavaScript eventhub trigger function called for message array ${IoTHubMessages}`);
IoTHubMessages.forEach(message => {
context.log(`Processed message ${message}`);
var sqlConnection = x;
});
context.done();
};
I get an error that x is not defined. How can I access x? Also how how to execute a select query from here.
Any help will be greatly appreciated.
Try accessing your app setting using process.env["x"] Here are the docs, and here's a related issue you may face if you're trying to run this locally.
I don't have experience writing Javascript functions to execute a select query, but this documentation seems like a good place to start: Use Node.js to query Azure SQL Database
There is no easy way to access connection strings in Node.js.
Instead, the suggestion is to use app settings for all your secrets and connection strings. You can them access them using process.env.YourAppSetting.
See similar questions one and two.

How to use a same DB with 2 different React applications

I have to make an offline application that sync with another online app when it can.
I developed the offline app using PouchDB. I create this app thanks to the github repo create-react-app. This app is running at localhost:3000. In this app, I create and manage a little DB named "patientDB".
I manage the db with the classical put method like we can see in the documentation:
var db = new PouchDB('patientDB')
db.put({
_id: 'dave#gmail.com',
name: 'David',
age: 69
});
With the development tool for chrome given by PouchDB, I can see that the DB is working like I want (the documents are created):
The other application is another React application with a node server. During the development, this app is running at localhost:8080.
In this app I try to fetch all the docs contained in the "patientDB" with the following code:
const db = new PouchDB('patientDB', { skip_setup: true });
db.info()
.then(() => {
console.log("DBFOUND")
db.allDocs({include_docs: true})
.then(function (result) {
console.log("RESULT" , result)
}).catch(function (err) {
console.log("NOPE")
console.log(err);
});
})
My problem is that I can't get the "patientDB" created with the offline app in the online app. When I do a var db = new PouchDB ('patientDB') it create a new and empty db because it can't find a db which is already present.
I use google chrome to run all my application so I thought that the dbs could be shared.
However I did little and very simple tests with two html files:
First.html which initialize a new db with a doc
Second.html which read the db create in First.html
In this case, I can fetch the doc created with First.html in Second.hmtl even if the are two separated "website".
I think that the application which run at a localhost are like isolated of the rest of the application even if, like I said before, I use the same browser for all my applications...
I don't know what to do or I don't know if it's even possible to do what I want to do. If someone has an idea for me, I would be pleased.
EDIT
I can see why my DBs are not shared:
When I look at all my local DBs after running an html file I can see the following thing :
As we can see, the DBs come from the files _pouch_DB_NAME - file://
When I check my DB from the application running localy (localhost), I can see this :
The DB don't come from file but from localhost:8080
If you know how I can fetch doc from a local db in an app running in a server, it could be really helpful for me!
PouchDB is using IndexedDB in the browser, which adheres to a same-origin policy. MDN says this:
IndexedDB adheres to a same-origin policy. An origin is the domain, application layer protocol, and port of a URL of the document where the script is being executed. Each origin has its own associated set of databases. Every database has a name that identifies it within an origin.
So you have to replicate your local database to a central server in order to share the data. This could be a PouchDB Server together with your node app. You can also access PouchDB Server directly from the browser:
var db = new PouchDB('http://localhost:5984/patientDB')
As an alternative, you can use CouchDB or IBM Cloudant (which is basically hosted CouchDB).

Why is there separate mongo.Server and mongo.Db in mongodb-native driver?

I am just learning mongodb-native driver for nodejs.
I connect like this.
var mongo=require("mongodb")
var serv=mongo.Server("localhost", 27017)
var dbase=mongo.Db("MyDatabase", serv)
And that works. But if I try to create a new database connection using the same server I get an error.
var dbase2=mongo.Db("MyDatabase2", serv)
"Error: A Server or ReplSet instance cannot be shared across multiple Db instances"
But it works if a make a new server connection first.
var serv2=mongo.Server("localhost", 27017)
var dbase2=mongo.Db("MyDatabase2", serv2)
So my question is why there are 2 connection functions, one for Server and one for Db, when it seems like they must always be used together?
Why doesn't it go like this.
var dbase=mongo.Db("localhost", 27017, "MyDatabase")
I want to make my own function that does this, but I wonder if there is some other reason they are separate.
Thanks.
Here is a link to the solution on the mongo docs, for reference. (seems like the same solution the other poster mentioned)
http://mongodb.github.com/node-mongodb-native/markdown-docs/database.html#sharing-the-connections-over-multiple-dbs
The point of separating the connection to the mongo server, and then the DB is for cases like when you want to connect to a ReplSet server, or other custom params. This way, you have a separate process connecting to a mongodb server.
The database connection call is separate simply because of the case you have here: you dont simply want to connect to a mongo server and a single db, but multiple dbs. This separation of connecting to db and server allows this flexibility.
Another Solution: Use node-mongoskin
Mongoskin does what you want to... it allows connecting to server and db all in one command. Not a solution for mongo-native, but worth considering as an alternative library for your future projects.
var mongo = require('mongoskin');
var db = mongo.db('localhost:27017/testDB');
For what it's worth, you can do what you want to do by using Db#db(), which doesn't seem to appear in the official documentation but is listed in the source code of db.js as being a public API:
/**
* Create a new Db instance sharing the current socket connections.
*
* #param {String} dbName the name of the database we want to use.
* #return {Db} a db instance using the new database.
* #api public
*/
so you could do
var serv=mongo.Server("localhost", 27017);
var dbase=mongo.Db("MyDatabase", serv);
var dbase2=dbase.db("MyDatabase2");
Because these are two separate and distinct actions - you have to connect (or already have a connection) to DB server (computer) in order to query any of the databases on that particular server. You can create distinct database query connections for each of the databases that you will want to use, but at the same time you will be using the same connection to the server.
Most of the time you will NOT want to create a separate server connection for each of the databases (if there are many) because the server usually limits the number of connections.

Categories

Resources