couchdb futon document editor - can I customize the document validation part? - couchdb

A VERY nice to have would be if I could edit object-literals in this editor's text-field instead of JSON expressions.
If I could replace the JSON parse with a simple eval - it will make editing sooooo much easier! (and help me design document structures for my projects soooo much more easily)
I mean, gosh!! it's not a protocol school, it's an editor's tool.
The goal of the tool is not to teach me the protocol and comment me on every petty mistake, but to help me design documents for the software.
Why must it ensist on strict JSON? Can't it live with Object Literals, and do for us the
JSON.stringify( eval(editor_textarea.value))
woulnd't that be cool? LOL :D
(yea yea, catching errors and feeding back to the user)
(and for who ever missed the difference - it is mainly in the quote marks in attribute names.
the dry strict JSON protocol require quotemarks ALWAYS, no question asked, where JS object literal require quote-marks only for attribute names that are not legal JS variable names and accepts also numbers without quotation marks)
Strict dry JSON:
{ "attribute" : "value"
, "mapmap" :
{ "map" :
{ "attr" : "sdss"
, "123" : "ss32332"
, "val" : 23323
, "456" : "ss32332"
}
}
}
Object Literal
{ attribute: "value"
, mapmap :
{ map :
{ attr : "sdss"
, 123 : "ss32332"
, val : 23323
, 456 : "ss32332"
}
}
}
Well, it won't solve me missing commas or mismatching brakets, but it does make life easier, where quote marks are a big part of the scaffold.
If you can point me to where I can change this even as patch on the futon I'll be soooOOO greatful :)
Maybe later we can integrate there an editor helper such as the cool one in github source-editor or the one in jsfiddle, that helps you indent and color things nicely.
But lets start with a simple eval.
it will make life easier... :)
It can also let me generate complicated documents using JS code without any additional test software...
Happy coding :)
P.S
If you know the answer here - you might know the answer to this question:
couchdb futon document editor - can I customize the indentation rules?

I had a quick browse, and I believe this is where you will want to add your eval:
https://github.com/apache/couchdb/blob/master/share/www/script/futon.browse.js#L911
and here:
https://github.com/apache/couchdb/blob/master/share/www/script/futon.browse.js#L902
You can edit your local couchdb instance share/www/script/futon.browse.js if you want to see live changes.

Related

validate.js returning error "unknown validator pattern"

I'm trying to use validate.js to validate input from the front end before entering it in the database with node but I'm getting an error that I can't figure out. I've gone over the docs and believe I setup the constraints correctly. The exact error is:
message:"Unknown validator pattern"
my validator is setup like this:
let alphanumeric = /^[a-zA-Z0-9]*$/;
let constraints = {
clientUsername:{
presence: true,
length: {min:8, max:15},
pattern:alphanumeric,
message: 'make sure client username is between 8-15 characters, is only numbers and letters'
},
tileCategory:{
presence:true,
length:{min:1, max:1},
numericality:{
onlyInteger:true,
lessThanOrEqualTo:tileCategoryNumber,
},
message:'enter a number, 1 char in length, less than or equal to 3' //the current number of tiles
}
};
validate({clientUsername: input.clientUsername},constraints);
At first I thought it was the regex pattern but tried commenting that out and then it said
message:"Unknown validator messsage"
so I'm guessing there is something wrong with my validator in general.
at the very top I of course included const validate = require('validate.js');
Something similar to this just burned me, have a look at the documentation again.
pattern is sort of a sub-validator of format and should look like:
{
format: {
pattern: "[A-Za-z0-9]+"
}
}
You're trying to use pattern at the "top level". I don't see anything in the documentation that implies helper patterns like alphanumeric exist. (I think the language the tool would use is to say "pattern is an option of the format validator" but I'm not sure.)
Your stated error message also implies a misspelling: it tells you it doesn't recognize messsage, which has 3 of the letter 's' but should have 2.
There's (2) things I could see being the issue. Firstly, you're using JS based regexes with the preceding and following /. Try removing these.
Beyond that, I'd recommend trying to remove the alphanumeric parameter & input the regex directly... it may be a type issue as well.
pattern:"^[a-zA-Z0-9]*$",
Hope this helps! :)

Best way to manage internationalization in database

I ' ve some troubles , managing my i18n in my database
For now I ' just two languages available on my application , but in order to be scalable, I would like to do it the "best" way.
I could have duplicated all fields like description_fr, description_en but I was no confortable with this at all. What I've done for now, is a external table , call it content, and its architecture is like this :
id_ref => entity referenced id (2)
type => table name (university)
field => field of the specific table (description)
lang => which lang (fr, en, es…)
content => and finally the appropriate content.
I think it can be important to precise, I use sequelizeJS as ORM. So I can use a usefull hooks as afterFind, afterCreate and afterUpdate. So Each time I wanna to find a resource for example, after find it, my hook retrieve all content for this resource and set definitly my object with goods values. It works, but I'm not in love with this.
But I have some troubles with this :
It's increase considerably my number of requests to the database : If I select 50 rows for example, I have to do 50 requests more.. , and just for a particular model. If I have nested models, it's exponential…
Then, It's complicated to fetch data by content i18ned. Example find a university with a specific name is complicated.
And It's a lot of work for updating etc...
So I wonder, if it would be a good idea , to save as a JSON, directly in the table concerned , the data. Something like
{
fr : { 'name':'Ma super université' },
en : { 'name':'My kick ass university' }
}
And keep on using Sequelize Hooks to build and insert proper data into my object.
What do you think ?
How do you manage this ?
EDIT
I use a mysql database
It concerns around 20 fields (cross models)
I have to set the default value using a my default_lang if there is no content set (e.g, event.description in french will be the same as the english one, if there is no content set)
I used this npm package sequelize-i18n. It worked pretty fine for me using sequelize 3.23.2, unfortunately it seems does not have support for sequelize 4.x yet.

Kibana and groovy scripting

I was looking for a way to calculate a ratio on Kibana. After many researches i found this way :
Using the "JSON Input" feature in a visualisation.
I have all my informations in an index, with 2 types of documents (boots and reboots).
I am looking for the script which count the number of documents with the type boots, same for the reboots type then divide the second by the first.
It sounds really easy, but i do not find any way to get it after my researches, and i am not used to groovy enough yet to do it by myself.
I found many ways to manipulate documents values (doc['mydocname'].values etc), but nothing about the type.
Thanks in advance.
EDIT : I tried this
{
"aggs" : {
"boots_count" : { "value_count" : { "_type" : "boots" } }
}
}
Which is supposed to count the number of fields (here the field _type) in the index. But when i put it into "JSON Input" in a visualisation, that results in an error :
Error: Request to Elasticsearch failed: {"error":"SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[BbXJ0O6tRxa_OcyBfYCGJQ][informationbe][0]: SearchParseException[[informationbe][0]: from[-1],size[0]: Parse Failure [Failed to parse source [{\"size\":0,\"aggs\":{\"2\":{\"terms\":{\"field\":\"#sitePoste\",\"size\":5,\"order\":{\"1\":\"desc\"}},\"aggs\":{\"1\":{\"avg\":{\"script\":\"0\",\"lang\":\"expression\",\"ratio\":{\"boots_count\":{\"value_count\":{\"_type\":\"boots\"}}}}}}}}
I am wrong. But where ?
EDIT2 : In other hand, i am trying scripted fields, with something like this using lucene expression :
doc['_type:boots'].count / doc['_type:reboots'].count
but it doesnt work more, i am pretty confident about the "doc['_type:boots']" part, i guess the problem is on the "XXX.count" part.
After many attempts, i understand better and better how it works. Default scripted fields scope is on the document, not on the whole index, so i cant do a count action of whole values of the index from documents in it.
I am looking for a workaround, i'll post it it if find something interesting.
I finally solved my problem :
I added a scripted field, if the type of the document is boots, the scripted field = 1, else 0. Then i created a search with only boots and reboots documents (filter _type:boots _type:reboots) and calculated the average of the scripted field in a metric.
Everything works well !

What is "sth" with respect to web traffic

Just wondering if anyone could enlighten me to what sth might be. (Seen in Tranalyzer flow files.) Basically it's a web analysis category (ip address, port, sth, etc) but I'm not sure what meant by it and there is no mention in the documentation.
(Also for bonus points what would a value of dir mean for sth?)
I'd appreciate any help.
sth means : STatement Handle
The connection to a database.
See http://perlmeme.org/tutorials/connect_to_db.html and https://stackoverflow.com/a/13208866/465183
Edit :
In perl, if I display the content of the object using Data::Dumper with a DBI script :
$VAR1 = bless( {}, 'DBI::st' );
but that's not very helpful. It's means only that's a DBI::st object.

How to name application specific fields in couchdb

I tried adding my own fields with names like _myappvar and _myotherappvar to documents to distinguish them from data fields. At first it worked but at some point futon starts to complain.
What is the right way to go?
I am using couchdb 0.9.0, this may be old, butI will not be able to upgrade in this iteration.
Edit: I guess _* is reserved for couchdb vars. I could choose something else but is there a best practice or ho are you solving this?
Edit2: This is somehow severe for my application, because it is already live with those fields. I wonder under which circumstances I can keep the parts that work and only apply a new naming for future fields.
You are correct. The CouchDB Document API, Special Fields section explains it.
Top-level fields may not begin with _.
CouchDB is relaxed, so the best way to go is the easiest thing for your application. About your specific edits:
One idea is to use the _ suffix instead of a prefix. Another idea is a .myapp field which is an object(namespace) for your internal data. You could combine them too:
{
"type": "the document type",
"var1": "Normal variable 1",
"var2": true,
"myapp_": {
"var": "Something internal",
"othervar": null,
}
}
Now you can reference doc.myapp_.var in your view maps, reduces, etc.
You have a choice. You can bite the bullet and change all documents right now. I don't know your app however I prefer that because you are playing with fire using a _ prefix.
However, you could also have both types of document and simply teach your map() function how to handle both of them.
function(doc) {
if(doc.type == "the document type") {
if(doc._myappvar) {
emit(doc._id, doc._myappvar); // The old way
} else if(doc.myapp_) {
emit(doc._id, doc.myapp_.var); // The new way
}
}
}
Good luck!

Resources