We have been running an app on sailsJS 0.12 -
once 1.0 was released ran through the upgrade process and upgraded
previously, some of the models were supporting "array" type, it's no longer supported. what's the alternative to this ? it's not covered in sample app or the documentation
model I have is :
module.exports = {
attributes: {
provider: 'string',
uid: 'string',
email: 'string',
name: 'string',
firstName: 'string',
lastName: 'string',
password: 'string',
projects: {
collection: 'project',
via: 'owner'
},
creditsHistory:{
collection: 'creditsHistory',
via: 'owner'
},
userRoles: {type: 'array', defaultsTo : [roles.USER]}
},
supported types in sails 1.0 are : https://sailsjs.com/documentation/concepts/models-and-orm/attributes
there isn't any example or sample on what to replace the array type with
sails 0.12 supported types:
https://0.12.sailsjs.com/documentation/concepts/models-and-orm/attributes
does anyone has any idea on this ?
You can use it like that:
'coordinate': {
'type': 'json',
'required': true,
},
Or you can use it like this :
'cost_price': {
'type': 'ref',
'columnType': "double"
},
in colunmType you can define database colunm type
Related
on mongo db compass I'd like to use a validation schema but on of my properties is an enum, depending on this field another field can be toggled I was able to do this using JSON schema but it dosen't seems to be working on mongoDB compass why ?
{
type: 'object',
properties: {
type: {
enum: ['teacher', 'student']
},
firstname: {
type: 'string',
},
lastname: {
type: 'string',
},
login: {
type: 'string',
},
pwd: {
type: 'string'
},
"if": {"properties": {"type": {"const": "student"}}},
"then": {"properties": {classes: {type: "array"}}}
},
required: [
'type',
'firstname',
'lastname',
'login',
'pwd',
],
};
No.
The MongoDB documentation indicates that it is using draft 4:
JSON Schema object is formatted according to draft 4 of the JSON Schema standard.
The JSON Schema site says these conditionals are new in draft 7:
New in draft 7 if, then and else keywords
let validator = new Validator(req.body, {
password: "requiredIf:is_login_user,1",
});
this is error return in postman when i try to archive a record in my Sails backend.
UsageError: Invalid initial data for new records.\nDetails:\n Could not use one of the provided new records: Missing value for required attribute id. Expected a string, but instead, got: undefined\n [?] See https://sailsjs.com/support for help
Please somebody know what this error means? Thanks in advance.
Thanks for your response
My code:
- In controller:
try {
await Laboratory.archive({id: inputs.id});
} catch (error) {
console.log('Error-6 CORE-DELETE_LABORATORY:', error)
exits.failed(`Error-6 CORE-DELETE_LABORATORY: ${error}${inputs.id}`)}
In file config/models.js:
module.exports.models = {
migrate: 'alter',
fetchRecordsOnUpdate: true,
fetchRecordsOnCreate: true,
fetchRecordsOnCreateEach: true,
attributes: {
createdAt: { type: 'ref', columnType: 'datetime', autoCreatedAt: true, },
updatedAt: { type: 'ref', columnType: 'datetime', autoUpdatedAt: true, },
id: { type: 'string', columnName: '_id' },
},
dataEncryptionKeys: {
default: 'dRYQHBf8Zpza2vMS5BB3qcSiLspJP4BG37I2JkP2yYw='
},
cascadeOnDestroy: true
};
Laboratory model:
module.exports = {
attributes: {
type: {
type: 'string',
isIn: ['INSIDE', 'OUTSIDE'],
required: true,
},
raisonSocial: {
type: 'string',
required: true,
unique: true,
},
city: {
type: 'string'
},
address: {
type: 'string'
},
email: {
type: 'string'
},
neighborhood: {
type: 'string'
},
contacts: {
type: 'string'
},
logo: {
type: 'string'
},
lat: {
description: 'Latitude',
type: 'number'
},
long: {
description: 'Longitude',
type: 'number'
},
website: {
type: 'string',
defaultsTo: ''
},
subdomain: {
type: 'string',
unique: true,
required: true,
},
mobileMoney: {
type: 'string'
},
bank: {
type: 'string',
defaultsTo: ''
},
bankAccountID: {
type: 'string',
defaultsTo: ''
},
validated : {
type: 'boolean',
defaultsTo : false
},
f_establishment: {
model: 'establishment'
},
director: {
description: 'Chef of laboratory id',
type: 'json',
example: '{name, phone, role}',
required: true,
}
},
customToJSON () {
if (this.logo) {
this.logo = `${process.env.BASE_URL}/avatar/${this.logo}`
}
return this
}
};
Thanks.
Sails throws UsageError when we use a db method in an incorrect way. To be more precise, this is what Sails has in its documentation.
When an error has name: 'UsageError', this indicates that a Waterline
method was used incorrectly, or executed with invalid options (for
example, attempting to create a new record that would violate one of
your model's high-level validation rules.)
https://sailsjs.com/documentation/concepts/models-and-orm/errors#?usage-errors
To give you a simple example, if you have a db query with limit parameter and you start passing string values for that parameter, Sails with start throwing UsageError.
In your case, things should be self explanatory from the error detail itself. Your code flow is getting undefined for inputs.id when it tries to run Laboratory.archive({id: inputs.id}) but id is expected to be a string if you see config/models.js.
Just do a basic debugging to see why inputs.id is coming as undefined and solve that issue, and you should be good to go.
I'm giving a try to the beta version of SailsJS (v1.0.0-32) and I'm having some issues while configuring a custom id. Bellow you'll find my current configuration:
The modelExample.js
module.exports = {
attributes: {
id:{
type: 'string',
columnName: '_id'
},
attr: {
type: 'number'
}
}
}
The model config config/models.js
attributes: {
createdAt: { type: 'number', autoCreatedAt: true, },
updatedAt: { type: 'number', autoUpdatedAt: true, },
id: { type: 'string', columnName: '_id' },
}
The element trying to be inserted:
{id:"600000", attr:40}
The error I get when trying to create a record with an attribute "id" included in the element trying to be created:
AdapterError: Unexpected error from database adapter: Invalid primary key value provided for `id`. Cannot interpret `600000` as a Mongo id.
(Usually, this is the result of a bug in application logic.)
Seems that mongo does not like the string 600000 as an id, but I'm not sure if maybe I'm misunderstanding something related to ids in mongo. In the old version of sails, I never had this issue since the id override was straightforward.
For more information, the sails-mongo adapter version is: "sails-mongo": "^1.0.0-5"
In order to use non-ObjectID primary keys with sails-mongo in Sails 1.0, you have to set dontUseObjectIds: true in your model, for example:
// api/models/User.js
module.exports = {
dontUseObjectIds: true,
attributes: {
id: { type: 'number', columnName: '_id' }, // <-- still need to set `columnName`!
name: { type: 'string' },
...etc...
}
}
This is implemented as of sails-mongo v1.0.0-7.
I'm storing user input into MongoDB with Sails / Waterline and because the type of field is dependent on their setup I'm finding it difficult to figure out the best way to store the data.
The 'surveyField' model is like:
// SURVEY FORM FIELD DEFINITIONS
module.exports = {
attributes: {
name: {
type: 'string',
required: true
},
label: {
type: 'string',
required: true
},
constraints: {
type: 'string',
enum: ['none', 'unique', 'combo', 'same'],
required: true,
defaultsTo: 'none'
},
isRequired: {
type: 'boolean',
required: true,
defaultsTo: false
},
attributeType: {
type: 'string',
enum: ['boolean', 'text', 'localizedText', 'enum', 'localizedEnum', 'number', 'money', 'date', 'time', 'dateTime'],
required: true
}
}
}
The user will have added any number of these fields to their form and so their form will contain a reference to the types of fields they have chosen. When their form is built I know exactly how to handle/display each of the fields based on this information, but saving the info is proving to be somewhat difficult because that Model needs to assume a type for the value field.
The 'surveyData' model looks like:
module.exports = {
attributes: {
value: {
**type: 'string' // THIS IS WHERE THE ISSUE IS**
},
surveyFieldType: {
model: 'surveyFieldType',
required: true
},
survey: {
model: 'survey',
required: true
},
user: {
model: 'user',
required: true
}
}
}
The issue occurs when the value might be a string or it might be json... or any of the other 'standard data types.'
Any help on this would be greatly appreciated.
** EDIT **
I'll also need this value to be searchable as well.
Maybe change it a little. And deal with survery answer as more complex value than just a value. Change type to JSON & build custom validator.
attributes: {
value: {
type: 'json'
}
}
and make it lake that
{
surveyFieldType: something,
value: {
type: 'Array',
value: [1,2,3]
},
survey: survey,
user: user
}
You can search now by that, you have flattened types of all replies. Based on value.type you can create custom validation rules for json.
I just joined a project using SailsJS. I am trying to run a migration on the user model (I want to add a confirmed column boolean). I proceeded this way :
1) I modified the user model in models/User.js adding the confirmed attribute. Here's the model User.js :
attributes: {
id: {
type: 'int',
primaryKey: true,
autoIncrement: true
},
confirmed: {
type: 'boolean',
required: true,
defaultsTo: false
},
facebook_id: {
type: 'int'
},
email: {
type: 'string',
unique:true,
required: true
},
password:{
type: 'string'
// ,
// password: true
},
password_confirmation:{
type:'string'
},
token: {
type:'string',
size: 2000
},
first_name: {
type: 'string',
required: true
},
last_name: {
type: 'string',
required: true
},
full_name: {
type: 'string'
},
poste: {
type: 'string'
},
favorite_club: {
type:'string'
},
picture: {
type:'string',
defaultsTo: 'https://wefoot.s3.amazonaws.com/default.svg'
},
birthday: {
type: 'date'
},
mangoId: {
type: 'integer'
},
telephone: {
type: 'string'
},
password_reset_token:{
type:'string'
},
last_seen:{ //Last opening of notif page
type:'datetime'
},
pending_notif:{
type:'int',
defaultsTo: 0
},
nb_connection:{
type:'int',
defaultsTo: 0
},
last_lat:{
type: 'float'
},
last_long:{
type: 'float'
},
2) In config/env/development.js I changed migrate: safe to migrate alter :
module.exports = {
models: {
connection: 'postgresLocal',
migrate: 'alter'
}
};
3) I ran sails lift and changed back development.js to migrate safe
But When I checked in Pgadmin, the new confirmed column is not showing up and I get this error in my logs
Unhandled rejection Error (E_VALIDATION) :: 1 attribute is invalid
at WLValidationError.WLError (/Users/davidgeismar/wefootpostgres/node_modules/sails/node_modules/waterline/lib/waterline/error/WLError.js:26:15)
at new WLValidationError (/Users/davidgeismar/wefootpostgres/node_modules/sails/node_modules/waterline/lib/waterline/error/WLValidationError.js:20:28)
at /Users/davidgeismar/wefootpostgres/node_modules/sails/node_modules/waterline/lib/waterline/query/validate.js:46:43
at allValidationsChecked (/Users/davidgeismar/wefootpostgres/node_modules/sails/node_modules/waterline/lib/waterline/core/validations.js:210:5)
at /Users/davidgeismar/wefootpostgres/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:49:16
at done (/Users/davidgeismar/wefootpostgres/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:239:19)
at /Users/davidgeismar/wefootpostgres/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:40:16
at /Users/davidgeismar/wefootpostgres/node_modules/sails/node_modules/waterline/lib/waterline/core/validations.js:191:23
at /Users/davidgeismar/wefootpostgres/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:49:16
at done (/Users/davidgeismar/wefootpostgres/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:239:19)
at /Users/davidgeismar/wefootpostgres/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:40:16
at /Users/davidgeismar/wefootpostgres/node_modules/sails/node_modules/waterline/lib/waterline/core/validations.js:164:64
at /Users/davidgeismar/wefootpostgres/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:162:20
at /Users/davidgeismar/wefootpostgres/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:230:13
at _arrayEach (/Users/davidgeismar/wefootpostgres/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:81:9)
at _each (/Users/davidgeismar/wefootpostgres/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:72:13)
What am I doing wrong here ?