Couch ignores filters - couchdb

Following this guide I create a design document with one view in it:
"views":{
"user":{
"map":"function(doc){emit(doc.id,doc)}"
}
}
If I then make a curl request like so:
curl .../_view/user
I get a result set, that looks like this:
{"total_rows":5,"offset":0,"rows":[{... value:{"_id":"...","login":"admin"}},...]}
If, however, I want to filter results by login field (now following this guide), like so:
curl ... /_view/user?login="test_login"
or ... /_view/user?login=test_login
I still get the very same result-set. I wonder what I'm doing wrong.
EDIT
I change the view a little bit, so that login attribute is now a key:
"map":"function(doc){emit(doc.login,doc)}"
However, even in this case filtering stil does not work. Whether I do:
/_view/user?login="root"
or
/_view/user?login="blahblahblah"
Taking all this into account, I guess, my final question should be: Does anybody in the world use CouchDB, if it is not working at all? I played around with dozens of databases, and all of them work as I expect it. CouchDB is a major exception.

Related

Jooq database/schema name mapping

I use jooq to generate objects against a local database, but when running "for real" later in production the actual databases will have different names. To remedy this I use the <outputSchemaToDefault>true</outputSchemaToDefault> config option (maven).
At the same time, we have multiple databases (schemas), and are using a connection pool to the server like "jdbc:mysql://localhost:3306/" (without specifying a database here).
How do I tell jooq which database to use when running queries?
I have tried all config I can think of:
new Settings()
.withRenderSchema(true) // true/false seems to make no difference.
.withRenderCatalog(true) // true/false seems to make no difference.
.withRenderMapping(new RenderMapping()
.withDefaultSchema("my_database") // Seems to have no effect.
// The above 3 configs always give me an error saying "no database selected".
// Adding this gives me 'my_database.my_table' does not exist - while it actually does.
.withSchemata(new MappedSchema()
.withInputExpression(Pattern.compile(".*"))
.withOutput("my_database")
));
I have also tried using a database/schema name, as in not configuring outputSchemaToDefault. But then, adding the MappedSchema code above, but that gives me errors with "'my_databasemy_database.my_table' does not exist", which is correct. I have no clue why that code gives me the database/schema name twice?
Edit:
When jooq tells me that the db.table does not exist, if I put a break point in a good place and get the sql from jooq and run exactly that against my database it does work. But jooq fails to run it.
Also, I'm using version 3.15.3 of jooq.
I solved it. Instead of using .withInputExpression(Pattern.compile(".*")), it seems to work with .withInput("").
I'm still not sure why it works, or if this is the "correct" way of solving it. But at least it is a way forward.
No clue why using the pattern, I got the name twice though. But that one I'll leave alone.

Issue NestJS with oracle TypeORM

I'm trying to connect my NestJs Project with a Oracle db and I'm using TypeORM and the status of connection is ok but I don't know how I can connect with a function. This function is into of a package and at the same time this package is into of a schema. The structure is like:
mySchema:
--------->myPackage:
-------------------->myFunction(id)
In the NestJS code I'm define this import in the AppModule file:
When I try to use the entity I don't know what method can I use to connect with my function. With the ESLint I get the next methods:
I hope to be clear and thanks for all!
In you last picture you’re trying to make a request to the database and as you can see, when you mentioned the testRepository and dots(.) now you have to tell him what you want to do in your database and in the suggested list you have all the possible possibilities.
So if you want to get or fetch data from you database, you will use testRepository.find() this will give you everything in that particular entity. To do that, you have to do something like below, before that you code has something that I have never seen in Nest, (public); if it does exist but I won’t use that in my Example since I don’t know it in Nest and also; you have started writing without (return) I don’t know how you’re expecting to return what you will get from your database.
Here is my example:
in your controller:
#Get() AnyThing(): Promise<TestEntity[]> {return this.DaobscsService.whatEver(); }
And in your service:
#InjectRepository(TestEntity) private readonlytestRepository: Repository<TestEntity>, ) {}
async whatEver(): Promise<TestEntity[]> {return await this.testRepository.find();}
What ever name you gonna use instead (whatEver()) in the service that have used, remember to use the same name in your controller pointing to service (this.Boa...Service.(The name here) OOP system’s you know it I hope
This example is to get or fetch so if you don’t have any thing in your database then you will get nothing! if that’s not what u want then command with a full version of what exactly is your issue, what u expected and code from controller, service, and module.

Get couchdb views to process design-docs as well as other docs

I am trying to get a view in couchdb to include design docs. I have done it in the past, but can not get it to work today.
In a past couchapp there is a file called options.json that contains the text:
{
"include_design": "true"
}
This results in the design doc containing
"options": {
"include_design": "true"
},
I added this to the new project, but still the design doc is not processed by my views. Is there something that I missed?
CouchDB 1.7.1
According to this documentation, include_design option is a boolean.
I double-checked CouchDB to see how it saves Boolean values by adding a document to a sample database with a Boolean value for one of the keys:
$ cat doc--0000
{"time":"2011", "address":"CT", "include":true}
$ curl -k -X PUT https://admin:**#192.168.1.106:6984/sample/doc--0000 -d #doc--0000
{"ok":true,"id":"doc--0000","rev":"1-e269c17275e2d21ba9100cd65b304d70"}
$ curl -k -X GET https://admin:**#192.168.1.106:6984/sample/doc--0000
{"_id":"doc--0000","_rev":"1-e269c17275e2d21ba9100cd65b304d70","time":"2011","address":"CT","include":true}
The double-check confirms that the Boolean values are saved as true NOT "true". I'm not sure, maybe that's the cause of the issue.
#user3405291 is correct the problem is with the string "true" instead of boolean true. CouchDB doesn't save this. Your view is run on the server as a javascript script so you should write it like you write javascript anywhere.

Best practice to pass query conditions in ajax request

I'm writing a REST api in node js that will execute a sql query and send the results;
in the request I need to send the WHERE conditions; ex:
GET 127.0.0.1:5007/users //gets the list of users
GET 127.0.0.1:5007/users
id = 1 //gets the user with id 1
Right now the conditions are passed from the client to the rest api in the request's headers.
In the API I'm using sequelize, an ORM that needs to receive WHERE conditions in a particular form (an object); ex: having the condition:
(x=1 AND (y=2 OR z=3)) OR (x=3 AND y=1)
this needs to be formatted as a nested object:
-- x=1
-- AND -| -- y=2
| -- OR ----|
| -- z=3
-- OR -|
|
| -- x=3
-- AND -|
-- y=1
so the object would be:
Sequelize.or (
Sequelize.and (
{x=1},
Sequelize.or(
{y=2},
{z=3}
)
),
Sequelize.and (
{x=3},
{y=1}
)
)
Now I'm trying to pass a simple string (like "(x=1 AND (y=2 OR z=3)) OR (x=3 AND y=1)"), but then I will need a function on the server that can convert the string in the needed object (this method in my opinion has the advantage that the developer writing the client, can pass the where conditions in a simple way, like using sql, and this method is also indipendent from the used ORM, with no need to change the client if we need to change the server or use a different ORM);
The function to read and convert the conditions' string into an object is giving me headache (I'm trying to write one without success, so if you have some examples about how to do something like this...)
What I would like to get is a route capable of executing almost any kind of sql query and give the results:
now I have a different route for everything:
127.0.0.1:5007/users //to get all users
127.0.0.1:5007/users/1 //to get a single user
127.0.0.1:5007/lastusers //to get user registered in the last month
and so on for the other tables i need to query (one route for every kind of request I need in the client);
instead I would like to have only one route, something like:
127.0.0.1:5007/request
(when calling this route I will pass the table name and the conditions' string)
Do you think this solution would be a good solution or you generally use other ways to handle this kind of things?
Do you have any idea on how to write a function to convert the conditions' string into the desired object?
Any suggestion would be appreciated ;)
I would strongly advise you not to expose any part of your database model to your clients. Doing so means you can't change anything you expose without the risk of breaking the clients. One suggestion as far as what you've supplied is that you can and should use query parameters to cut down on the number of endpoints you've got.
GET /users //to get all users
GET /users?registeredInPastDays=30 //to get user registered in the last month
GET /users/1 //to get a single user
Obviously "registeredInPastDays" should be renamed to something less clumsy .. it's just an example.
As far as the conditions string, there ought to be plenty of parsers available online. The grammar looks very straightforward.
IMHO the main disadvantage of your solution is that you are creating just another API for quering data. Why create sthm from scratch if it is already created? You should use existing mature query API and focus on your business logic rather then inventing sthm new.
For example, you can take query syntax from Odata. Many people have been developing that standard for a long time. They have already considered different use cases and obstacles for query API.
Resources are located with a URI. You can use or mix three ways to address them:
Hierarchically with a sequence of path segments:
/users/john/posts/4711
Non hierarchically with query parameters:
/users/john/posts?minVotes=10&minViews=1000&tags=java
With matrix parameters which affect only one path segment:
/users;country=ukraine/posts
This is normally sufficient enough but it has limitations like the maximum length. In your case a problem is that you can't easily describe and and or conjunctions with query parameters. But you can use a custom or standard query syntax. For instance if you want to find all cars or vehicles from Ford except the Capri with a price between $10000 and $20000 Google uses the search parameter
q=cars+OR+vehicles+%22ford%22+-capri+%2410000..%2420000
(the %22 is a escaped ", the %24 a escaped $).
If this does not work for your case and you want to pass data outside of the URI the format is just a matter of your taste. Adding a custom header like X-Filter may be a valid approach. I would tend to use a POST. Although you just want to query data this is still RESTful if you treat your request as the creation of a search result resource:
POST /search HTTP/1.1
your query-data
Your server should return the newly created resource in the Location header:
HTTP/1.1 201 Created
Location: /search/3
The result can still be cached and you can bookmark it or send the link. The downside is that you need an additional POST.

alter date - postgreSQL and websockets

I am using websockets , nodejs v0.10.12 and also PostgreSQL 9.1, with PostGIS 2.0.
Now, on websockets, on the server side, in order to gather textual data and send them to the client I perform a query using node's pg plugin.
I have something like
var query = client.query('SELECT p_name,p_date FROM pins WHERE p_id ='+ja)
//send them and render in client as html
query.on("row", function (row, result) {result.addRow(row);});
query.on("end", function (result) {
for (var i=0; i<result.rows.length; i++){
connection.send(
'Name</br>'
+result.rows[i].p_name+
'</br>Date</br>'
+result.rows[i].p_date+
'</br>'
}
client.end();
});
Now, here is the tricky part. I want to render the date like 25/02/2012.
With the above code, I get Sat Feb 02 2002 02:00:00 GMT+0200 (Χειμερινή ώρα GTB)
To get DD/MM/YYYY I have to put a line of code like
SET datestyle = "SQL, DMY";
This is apparently PHP and I am using Javascript because I work with websockets.
The only thing I could think of is editing the above query like so
var query = client.query('SET datestyle = "SQL, DMY"; SELECT p_name,p_date FROM pins WHERE p_id ='+ja)
I dont get any errors, but on the client the date renders null.
How can I fix this?
Thanks
OK. Where to start?
This:
var query = client.query('SELECT p_name,p_date FROM pins WHERE p_id ='+ja)
is not the correct way to build a query. Used a parameterised query and protect yourself from SQL injection.
SET datestyle = "SQL, DMY";
This is apparently PHP and I am using Javascript because I work with websockets.
What? I'm trying to think of something constructive about this sentence, but the best I can think of is "What?". It is far from apparent that the above is PHP, because it isn't. The fact that you are sending it to the database ought to give you a hint that it's SQL. Also, you're not using javascript because you work with websockets. You're using javascript because you're using javascript - websockets are nothing to do with anything here.
The only thing I could think of...
Doesn't include looking in the manuals.
Go to the PostgreSQL website, click through to the documentation and manuals, and on the contents page click "Functions and Operators" and then "Data type formatting functions". Here is the link for you:
http://www.postgresql.org/docs/current/static/functions-formatting.html
You'll notice that the PostgreSQL developers not only produce extensive and detailed manuals, but they keep multiple versions online and make it simple to switch back and fore to see what's changed.
There is a whole section on this page on how to format date-times in different ways, with clear descriptions of each effect. I didn't find this using the documentation search or anything clever like that - just the obvious links on each page.
If you did a search you would find plenty on the datestyle parameter, and a little further digging would show that you can set it per-session or as a default for a given user or database.
Finally though, don't do it that way at all. Return ISO-standard date formats like #mu said (YYYY-MM-DD etc). and format them in your javascript client code.
Oh - while I'm no expert, I'm not sure that </br> is valid HTML, XHTML or XML either. Did you perhaps mean <br/>?

Resources