Sailsjs/waterline specify number of decimal places in model - sails.js

How do I tell my sails model that I want some specific number decimal places for a type: 'float' attribute? Like decimalPlaces: 4 or something of that ilk?
The problem is that when i post a value to this entry, the value on disk is truncated to the .00 (hundreds) place. Say I want: 3243.2352362 to be stored just as it is. Currently this is transformed into 3243.24
If it matters I'm using the sails-mysql adapter.

types: {
decimal2: function(number){
return ((number *100)%1 === 0);
}
},
attributes: {
myNumber: {
type: 'float',
decimal2: true
}
}
This is for 2 decimal places though. I cant find a way to make it for dynamically changing N as there is afaik no way to pass a parameter to custom validation.
Workaround for this issue would be to check for custom amount of decimal places in beforeValidation() function.

I would not recommend (and I don't think its possible for float, maybe) adding a constraint in the model.
I'd suggest that you set:
migrate: "safe"
in your model and set the appropriate datatype/decimal Places in your tables.

Related

Prisma 2: Setting Minimum & Maximum Length of a String type

I am creating a model using Prisma 2 and want to set a minimum and maximum length for one of the fields. I.e., something like this:
model Post {
...
title String #min(3) #max(240)
...
}
I just made up the above syntax. I am wondering if something like that exists in Prisma and, if so, how to do it.
Any ideas?
Thanks.
Annotation #db.VarChar
You can set the maximum length using the annotation #db.VarChar:
model Post {
...
title String #db.VarChar(240)
...
}
There is no support for adding the minimum length as for now.

Write a struct into a DICOM header

I created a private DICOM tag and I would like to know if it is possible to use this tag to store a struct in a DICOM file using dicomwrite (or alike), instead of creating a field inside the DICOM header for each struct field.
(Something like saving a Patient's name, but instead of using a char data, I would use double)
Here is an example:
headerdicom = dicominfo('Test.dcm');
a.a = 1; a.b = 2; a.c = 3;
headerdicom.Private_0011_10xx_Creator = a;
img = dicomread('Test.dcm');
dicomwrite(img, 'test_modif.dcm', 'ObjectType', 'MR Image Storage', 'WritePrivate', true, headerdicom)
Undefined function 'fieldnames' for input arguments of type 'double'.
Thank you all in advance,
Depending on what "struct" means, here are your options. As you want to use a private tag which means no application but yours will be able to interpret it, you can choose the solution which is technically most appropriate. Basically your question is "which Value Representation should I assign to my private attribute using the DICOM toolkit of my choice?":
Sequence:
There is a DICOM Value Representation "Sequence" (VR=SQ) which allows you to store a list of attributes of different types. This VR is closest to a struct. A sequence can contain an arbitrary number of items each of which has the same attributes in the same order. Each attribute can have its own VR, so if your struct contains different data types (like string, integer, float), this would be my recommendation
Multi-value attribute:
DICOM supports the concept of "Value Multiplicity". This means that a single attribute can contain multiple values which are separated by backslashes. As the VR is a property of the attribute, all values must have the same type. If I understand you correctly, you have a list of floating point numbers which could be encoded as an array of doubles in one field with VR=FD (=Floating Point Double): 0.001\0.003\1.234...
Most toolkits support an indexed access to the attributes.
"Blob":
You can use an attribute with VR=OB (Other Byte) which is also used for encoding pixel data. It can contain up to 4 GB of binary data. The length of the attribute tells you of how many bytes the attribute's value consists. If you just want to copy the memory from / to the struct, this would be the way to go, but obviously it is the weakest approach in terms of type-safety and correctness of encoding. You are going to lose built in methods of your DICOM toolkit that ensure these properties.
To add a private attribute, you have to
reserve a range for the attribute specifying an odd group number and a prefix (2 hex digits) for the element numbers. (e.g. group = 0x0011, Element = 0x10xx) reserves a range from (0x0011, 0x10xx) - (0x0011, 0x10ff). This is done by specifying a Private Creator DICOM tag which holds a manufacturer name. So I suspect that instead of
headerdicom.Private_0011_10xx_Creator = a;
it should read e.g.
headerdicom.Private_0011_10xx_Creator = "Gabs";
register your private tags in the private dictionary, most of the time by specifying the Private Creator, group, element and VR (one of the options above)
Not sure how this can be done in matlab.

Parse setting explicit type using REST

I know you can set a Date field explicitly like so:
"date_brewed":{
"__type":"Date",
"iso":"2009-10-15T00:00:00.000Z"
}
But is there anyway to explicitly set the column type of 'Number' using REST? For instance, I'd like to set the column 'batch_size' to a Number instead of a string but when POST'ing via rest it keeps getting created as a string type column.
Meh, this was more of a Perl issue than a Parse issue.
What I had to do to tell Perl to treat the number like an actual number was to add a zero to the value. :/

Why Couchbase round numeric values in a json Document?

Is there anyone who had noted that Couchbase changes the numerical value of a property, over a certain limit, when recording a Json document?
Here is an example. For this test, I use the live input via the couchbase web interface.
The property "inputValue" corresponds to the value entered in the property "valueAfterSave" before clicking the save button.
The property "valueAfterSave" corresponds to the value after the save.
To a number with 16 digits, it's good:
{
"inputValue": "1234567890123456",
"valueAfterSave": 1234567890123456
}
But from 17 digits, the system begins to change the value:
{
"inputValue": "12345678901234567",
"valueAfterSave": 12345678901234568
}
or
{
"inputValue": "12345678901234599",
"valueAfterSave": 12345678901234600
}
or
{
"inputValue": "12345678901234567890",
"valueAfterSave": 12345678901234567000
}
Just out of curiosity with 40 digits
{
"inputValue": "1234567890123456789012345678901234567890",
"valueAfterSave": 1.234567890123457e+39
}
This behavior is specified somewhere? Is there a way to change it ?.
There is the solution through String values ​​but I admit that I'm curious.
I use Couchbase Server 2.1.0 on Windows 7 Pro 32-bit platform.
Tugdual Grall, technical evangelist at Couhbase, brought me the answer.
This is due to the behavior of JavaScript when displaying such values ​​as evidenced by the following test with NodeJS:
$ node
> console.log(12345678901234567890)
12345678901234567000
On the other hand, the value returned by the Java API is correct (12345678901234567890 in our example). It is just the console which shows this difference.
If you modify the document, through the web administration console, it is the modified value which will be saved.
So be careful with the use of the administration Console when we handle this type of data.
Tug thank you.

Defining a Mongoose Schema on the fly

I have a model file that gathers together all my Mongoose models. One of the models I would like to initialize with a variable number of fields. Currently I'm defining more fields than I think I am going to require:
TallySchema = new mongoose.Schema
0: Number
1: Number
...
20: Number
Obviously this is not ideal. I see Mongoose will let you specify options outside the Schema definition but can't see how to add new fields (or paths, I guess, in Mongoose).
Based on the mongoose plugin documentation it looks like you can just do:
schema.add({ field: Number })
This would need to be verified, but looking at the source it should be possible:
In the Schema constructor, it simply passes the definition object to this.add() (source).
The actual paths then get created within Schema.prototype.add (source).
So, seems like all you would need to do is something like:
// not sure what this looks like in CoffeeScript
TallySchema.add({ /* new property definition here */ });
I found this in the Mongoose documentation page:
var ToySchema = new Schema;
ToySchema.add({ name: 'string', color: 'string', price: 'number' });
You can use the 'mixed' type to encapsulate your values. It wouldn't be possible to have them be at the top level though but it works great otherwise.
new mongoose.Schema({
average: Number,
countPerRating: mongoose.Schema.Types.Mixed,
});
This is an excerpt from a mapreduce schema. I use the mixed type to store the number of times someone gives a certain rating, so we can say things like "10 1 star ratings, 45 4 star ratings", etc.
mixed type worked great for that.