Sequelize (4.2.0): Custom validation messages not working with migrations - mysql

I am learning how to use sequelize, so far it is a great ORM but I am stuck with the custom validation messages.
I am using the sequelize-cli library to handle migration and I found an issue, custom validation messages don't work if you use the sequelize-cli to create tables, I tried the sequelize.sync method to create the tables and it worked.
Code
This is how I create a field with a custom validation message
Wallet.js
userId: {
type: DataTypes.UUID,
unique: {
name: 'Wallets_userId_unique',
msg: 'This user already have a wallet'
}
},
WalletMigration.js (Not actual migration file name)
userId: {
allowNull: false,
type: Sequelize.UUID,
unique: true,
},
When I tried to create a Wallet with the same userId, I get a Validation error but I should get This user already have a wallet.
The message I am getting is the default message provided by the database because I used unique: true in the migration, If I remove that option the model validation doesn't work.
I want to know what can I do to change this behavior or maybe I am missing something?

Related

How to create public Forge Design Automation Activity and Package?

I would like to create a public Design Automation Activity and Package so other Forge apps with unknown ClientId & Secret can use our corporate DA tools. So I am setting the isPublic: true flag on the payload but, to my great despair, isn't working.
Take a look at my POST activity payload below:
const activityParams = {
id: DA_ACTIVITY_ID,
commandLine: [
`$(engine.path)\\accoreconsole.exe ` +
`/i \"$(args[inputDwg].path)\" ` +
`/al \"$(appbundles[${DA_APP_BUNDLE_ID}].path)\" ` +
`/s \"$(settings[script].path)\"`
],
parameters: {
inputDwg: {
description: "input .dwg",
localName: "input.dwg",
ondemand: false,
required: true,
verb: "get",
zip: false
},
result: {
description: "result .json",
localName: "result.json",
ondemand: false,
required: false,
verb: "put",
zip: false
}
},
settings: {
script: "(command \"EXTRACTGEOBIMDATA\")\n"
},
description: "GeoBIM Extract Data",
engine: DA_ENGINE,
appbundles: [
fullAppBundleId
],
isPublic: true
}
The first thing I notice is that the response after successful creation of activity doesn't contain the isPublic field:
The second thing I'm forced to notice is that attempting to execute a workitem against that activity result in an error:
Note that when run with the same credentials the activity and workitem are running perfectly fine.
Design Automation V3 let you use individual alias to share an Activity or AppBundle with a specific forge app.
If you look at the documentation when creating an alias for activity and AppBundle, there is an optional parameter you can set in the request body named "receiver". You can specify a forge app client id or nickname if the forge app you want to share with have one setup in Design Automation.
Note that if the Forge app you want to set as receiver use a nickname, you must use that nickname instead of the app client id.
https://forge.autodesk.com/en/docs/design-automation/v3/reference/http/activities-id-aliases-POST/
https://forge.autodesk.com/en/docs/design-automation/v3/reference/http/appbundles-id-aliases-POST/
We don't currently allow people to create public activities. This is because it is not clear how the parties could establish a trust relationship necessary. It sounds like in your scenario the sharing would happen within the same org. Would they have the same email domain (eg. xyz#somecompany.com would share with bla#somecompany.com)?

Sails.js Can't create data in another model from other model's lifecycle

Hey I'm kinda new to Sails. I've enabled REST api in sails and no I just simply create data using the body of the post request body without the intervention of a controller.
I have defined two models - users, and call_log. Every time an event occurs in my app, I want that to trigger a change in the value of a user in the users table but I also want to create a log in the call log table.
I'm using an afterCreate() lifecycle method and try to use the create() in order to write my data to the second table. I don't seem to get any error, but nothing is written to the dB.
my User.js (model)
module.exports = {
attributes: {
username: {type: 'string', required: true},
country: {type: 'string'},
},
afterCreate: function (valuesToSet, proceed) {
Talk_log.create({
uid: 'myuid',
partner_uid: 'mypartnerid',
action: 'yeahhh'
}).fetch();
valuesToSet.password = 1244;
return proceed();
},
};
my Talk_log.js
module.exports = {
attributes: {
uid: { type: 'string', required: true},
partner_uid: {type: 'string'},
action: {type: 'string'}
},
};
Documentation say "The afterCreate lifecycle callback will only be run on queries that have the fetch meta flag set to true"
So use:
User.create().fetch();
Fetch tell Waterline (and the underlying database adapter) to send back records that were updated/destroyed/created when performing an .update(), .create(), .createEach() or .destroy() query.
U say afterCreate() but in your code is beforeCreate(). Fix that.
I forgot the .then() that handles the bluebird promises and only then I could write to dB.
Talk_log.update({ uid: valuesToSet.uid})
.set({ joined: true})
.fetch()
.then(() => {return proceed();});

sailsjs 1.0 model attribute type date columntype datetime error out in sailsjs

In sailsjs 1.0#beta I have a model linked to a view in mysql table with mysql-sails#beta adapter.
The model is configured the following way:
attributes: {
id: {type:'number', required: true},
'release_date': { type: 'string', columnType: 'datetime' }
}
Although when ever i query the model, sails display a wall of errors per date column:
Warning: After transforming columnNames back to attribute names, a record
in the result has a value with an unexpected data type for property `release_date`.
The corresponding attribute declares `type: 'string'` but instead
of that, the actual value is:
```
2016-04-07T05:00:00.000Z
```
I have tried to set the type to "datetime" although its no longer supported.
The type "datetime" is no longer supported. To use this type in your model, change
`type` to one of the supported types and set the `columnType` property to a column
type supported by the model's adapter, e.g. { type: 'string', columnType: 'datetime' }
I was not able to find any documentation on what is the proper way to tell sailsjs model that its a date so that sailsjs 1.0b doesn't error out, and is there a way to tell sails its a read only model view?
After more digging, found a ticket about this on https://github.com/balderdashy/waterline/issues/1497
The fix for the datetime issue to use ref instead of string:
attributes: {
id: {type:'number', required: true},
'release_date': { type: 'ref', columnType: 'datetime' }
}
a workaround could be the approach currently found in the SailsJS default web app in the User model:
lastSeenAt: {
type: 'number',
description: 'A JS timestamp (epoch ms) representing the moment at which this user most recently interacted with the backend while logged in (or 0 if they have not interacted with the backend at all yet).',
example: 1502844074211
},
then in the UI, you can easily transform this into a human-readable format by doing, for example, the following:
new Date(user.lastSeenAt)

Sails.js - Waterline Model - Association to itself

I want to create a Tree like storage structure to be used with my app, but can't find enough documentation on how to create a tree model using Waterline attributes.
Case is simple. I do need to have a set of folders, that can have multiple levels of subfolders and in the end, files. What you usually do in mySQL for this kind of data is to add a parent_id field to your model as a foreign key to the model itself.
How can this be done using attributes in sailsjs/waterline model?
I've tried doing something like the following, which generates quite a bit of redundant and orphaned data:
--
attributes: {
name: {
type: 'string'
},
parentFolder: {
model: 'Folder'
},
childFolders: {
model: 'Folder',
via: 'parentItem'
}
}
--
Any ideas?
And by the way, if this is possible, let's say for example using mySQL as a backend. How will it replicate to say, mongoDB?
This seemed to work:
name: {
type: 'string',
maxLength: 255,
required: true
},
parent: {
model: 'folder'
},
childs: {
collection: 'folder',
via: 'parent'
}
I do believe duplicates were being generated by posting data directly via GET in the browser. I'm posting data with a client via POST and it seems to work as expected. (At least from what I see in mySQL)

ydn-db issue with ie9: SCRIPT5007: Unable to get value of the property 'XMLDocument': object is null or undefined

I'm using ydn-db to build a test app that can work offline in mutliple borwsers and devices.
My first option was to use indexedDB but then I realised that not all browsers and devices support it.
So, after some research I decided to move to ydn-db.
I'm initializing the db by doing this:
var dbSchema = {
version: DB_VERSION,
//autoSchema: true,
stores: [{
name: DB_STORE_USERS_NAME,
autoIncrement: false, // optional.
indexes: [{
name: 'login', // optional
keyPath: 'login',
unique: true,
multiEntry: false
}]
}, {
name: DB_STORE_REPOS_NAME,
autoIncrement: false, // optional.
indexes: [{
name: 'userid', // optional
keyPath: 'owner.id',
unique: false
}]
}]
};
db = new ydn.db.Storage(DB_NAME, dbSchema);
This works fine in chrome, but when I test it in ie9 I get the following error:
SCRIPT5007: Unable to get value of the property 'XMLDocument': object is null or undefined.
Do you have any clue of what can be the issue here?
I'm using the last version of ydn-db as it is in the site http://dev.yathit.com/ydn-db/downloads.html.
I'm downloading the following modules:
IndexedDb, WebSql, Webstorage, Userdata, Query
XMLDocument, which is available only in older IE (with compatibility mode in IE9 ?) is used to persist data with UserData storage mechanism on DOM node. See more detail on UserData and XMLDocument.
IE9 should use WebStorage (localStorage) instead. Also try out with IE6 or IE7. You will get better picture.
So somewhere mechanisms detection is screwed up. If you have more detail info, I could help out.