Sequelize Model Duplicate value check
up vote
0
down vote
favorite
i am new in sequelize and ORMs and i am trying to create a model. This is what i have:
/* Carrier model definition */
const carrier = sequelize.define('carrier', {
/* The unique carrier id */
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
allowNull: false
},
/* The iata code of the carrier */
iata: {
type: Sequelize.STRING,
validate: {
is: {
args: ["^[A-Z]{1}[0-9]{1}$|^[A-Z]{1}[A-Z]{1}$|^[0-9]{1}[A-Z]{1}$|^s*$"],
msg: "Carrier iata code is invalid."
}
}
},
/* The icao code of the carrier */
icao: {
type: Sequelize.STRING,
validate: {
is: {
args: ["^[A-Z]{3}$|^s*$"],
msg: "Carrier icao code is invalid."
}
}
},
/* The deleted flag of the carrier */
isdeleted: {
type: Sequelize.INTEGER,
validate: {
isIn: {
args: [
[0, 1]
],
msg: "Carrier "is deleted value" can be 0 or 1."
}
}
}
}, {
/* Perform model validations */
validate: {
carrierValidator() {
/* Ensure that at least an iata or an icao code exists */
if ((this.iata == "") && (this.icao == "")) {
throw new Error('Carrier must have at least an iata code or an icao code or both.');
}
}
},
/* Apply hooks */
hooks: {
beforeValidate: (carrier, options) => {
/* Capitalize carrier iata code */
carrier.iata = (typeof carrier.iata === 'undefined') ? "" : carrier.iata.toUpperCase();
/* Capitalize carrier icao code */
carrier.icao = (typeof carrier.icao === 'undefined') ? "" : carrier.icao.toUpperCase();
/* Set default isdeleted value to 0 */
carrier.isdeleted = (typeof carrier.isdeleted === 'undefined') ? 0 : parseInt(carrier.isdeleted);
}
}
})
The isdeleted property is a column which defines if an entry is deleted or not. If it is deleted it's value is 1 and if not 0.
Now, i want to prevent the insert of a new carrier, if one of the following is true:
- If a carrier with the same iata code, and isdeleted = 0 exists
- If a carrier with the same icao code, and isdeleted = 0 exists
I managed to achieve it, by adding the following block inside validate (for iata case):
if (this.iata != ""){
sequelize.models.carrier.findAndCountAll({
where: {
iata: this.iata,
isdeleted:0
}
}).then((result) => {
if (result.rows != 0){
throw new Error("Carrier iata code already exists.");
}
})
}
and when i attempt to insert a duplicate i get a very ugly response:
Is there a way to get an instance of ValidationError in order to handle it and display iteasier?
error-handling orm sqlite3 sequelize.js
add a comment |
up vote
0
down vote
favorite
i am new in sequelize and ORMs and i am trying to create a model. This is what i have:
/* Carrier model definition */
const carrier = sequelize.define('carrier', {
/* The unique carrier id */
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
allowNull: false
},
/* The iata code of the carrier */
iata: {
type: Sequelize.STRING,
validate: {
is: {
args: ["^[A-Z]{1}[0-9]{1}$|^[A-Z]{1}[A-Z]{1}$|^[0-9]{1}[A-Z]{1}$|^s*$"],
msg: "Carrier iata code is invalid."
}
}
},
/* The icao code of the carrier */
icao: {
type: Sequelize.STRING,
validate: {
is: {
args: ["^[A-Z]{3}$|^s*$"],
msg: "Carrier icao code is invalid."
}
}
},
/* The deleted flag of the carrier */
isdeleted: {
type: Sequelize.INTEGER,
validate: {
isIn: {
args: [
[0, 1]
],
msg: "Carrier "is deleted value" can be 0 or 1."
}
}
}
}, {
/* Perform model validations */
validate: {
carrierValidator() {
/* Ensure that at least an iata or an icao code exists */
if ((this.iata == "") && (this.icao == "")) {
throw new Error('Carrier must have at least an iata code or an icao code or both.');
}
}
},
/* Apply hooks */
hooks: {
beforeValidate: (carrier, options) => {
/* Capitalize carrier iata code */
carrier.iata = (typeof carrier.iata === 'undefined') ? "" : carrier.iata.toUpperCase();
/* Capitalize carrier icao code */
carrier.icao = (typeof carrier.icao === 'undefined') ? "" : carrier.icao.toUpperCase();
/* Set default isdeleted value to 0 */
carrier.isdeleted = (typeof carrier.isdeleted === 'undefined') ? 0 : parseInt(carrier.isdeleted);
}
}
})
The isdeleted property is a column which defines if an entry is deleted or not. If it is deleted it's value is 1 and if not 0.
Now, i want to prevent the insert of a new carrier, if one of the following is true:
- If a carrier with the same iata code, and isdeleted = 0 exists
- If a carrier with the same icao code, and isdeleted = 0 exists
I managed to achieve it, by adding the following block inside validate (for iata case):
if (this.iata != ""){
sequelize.models.carrier.findAndCountAll({
where: {
iata: this.iata,
isdeleted:0
}
}).then((result) => {
if (result.rows != 0){
throw new Error("Carrier iata code already exists.");
}
})
}
and when i attempt to insert a duplicate i get a very ugly response:
Is there a way to get an instance of ValidationError in order to handle it and display iteasier?
error-handling orm sqlite3 sequelize.js
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
i am new in sequelize and ORMs and i am trying to create a model. This is what i have:
/* Carrier model definition */
const carrier = sequelize.define('carrier', {
/* The unique carrier id */
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
allowNull: false
},
/* The iata code of the carrier */
iata: {
type: Sequelize.STRING,
validate: {
is: {
args: ["^[A-Z]{1}[0-9]{1}$|^[A-Z]{1}[A-Z]{1}$|^[0-9]{1}[A-Z]{1}$|^s*$"],
msg: "Carrier iata code is invalid."
}
}
},
/* The icao code of the carrier */
icao: {
type: Sequelize.STRING,
validate: {
is: {
args: ["^[A-Z]{3}$|^s*$"],
msg: "Carrier icao code is invalid."
}
}
},
/* The deleted flag of the carrier */
isdeleted: {
type: Sequelize.INTEGER,
validate: {
isIn: {
args: [
[0, 1]
],
msg: "Carrier "is deleted value" can be 0 or 1."
}
}
}
}, {
/* Perform model validations */
validate: {
carrierValidator() {
/* Ensure that at least an iata or an icao code exists */
if ((this.iata == "") && (this.icao == "")) {
throw new Error('Carrier must have at least an iata code or an icao code or both.');
}
}
},
/* Apply hooks */
hooks: {
beforeValidate: (carrier, options) => {
/* Capitalize carrier iata code */
carrier.iata = (typeof carrier.iata === 'undefined') ? "" : carrier.iata.toUpperCase();
/* Capitalize carrier icao code */
carrier.icao = (typeof carrier.icao === 'undefined') ? "" : carrier.icao.toUpperCase();
/* Set default isdeleted value to 0 */
carrier.isdeleted = (typeof carrier.isdeleted === 'undefined') ? 0 : parseInt(carrier.isdeleted);
}
}
})
The isdeleted property is a column which defines if an entry is deleted or not. If it is deleted it's value is 1 and if not 0.
Now, i want to prevent the insert of a new carrier, if one of the following is true:
- If a carrier with the same iata code, and isdeleted = 0 exists
- If a carrier with the same icao code, and isdeleted = 0 exists
I managed to achieve it, by adding the following block inside validate (for iata case):
if (this.iata != ""){
sequelize.models.carrier.findAndCountAll({
where: {
iata: this.iata,
isdeleted:0
}
}).then((result) => {
if (result.rows != 0){
throw new Error("Carrier iata code already exists.");
}
})
}
and when i attempt to insert a duplicate i get a very ugly response:
Is there a way to get an instance of ValidationError in order to handle it and display iteasier?
error-handling orm sqlite3 sequelize.js
i am new in sequelize and ORMs and i am trying to create a model. This is what i have:
/* Carrier model definition */
const carrier = sequelize.define('carrier', {
/* The unique carrier id */
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
allowNull: false
},
/* The iata code of the carrier */
iata: {
type: Sequelize.STRING,
validate: {
is: {
args: ["^[A-Z]{1}[0-9]{1}$|^[A-Z]{1}[A-Z]{1}$|^[0-9]{1}[A-Z]{1}$|^s*$"],
msg: "Carrier iata code is invalid."
}
}
},
/* The icao code of the carrier */
icao: {
type: Sequelize.STRING,
validate: {
is: {
args: ["^[A-Z]{3}$|^s*$"],
msg: "Carrier icao code is invalid."
}
}
},
/* The deleted flag of the carrier */
isdeleted: {
type: Sequelize.INTEGER,
validate: {
isIn: {
args: [
[0, 1]
],
msg: "Carrier "is deleted value" can be 0 or 1."
}
}
}
}, {
/* Perform model validations */
validate: {
carrierValidator() {
/* Ensure that at least an iata or an icao code exists */
if ((this.iata == "") && (this.icao == "")) {
throw new Error('Carrier must have at least an iata code or an icao code or both.');
}
}
},
/* Apply hooks */
hooks: {
beforeValidate: (carrier, options) => {
/* Capitalize carrier iata code */
carrier.iata = (typeof carrier.iata === 'undefined') ? "" : carrier.iata.toUpperCase();
/* Capitalize carrier icao code */
carrier.icao = (typeof carrier.icao === 'undefined') ? "" : carrier.icao.toUpperCase();
/* Set default isdeleted value to 0 */
carrier.isdeleted = (typeof carrier.isdeleted === 'undefined') ? 0 : parseInt(carrier.isdeleted);
}
}
})
The isdeleted property is a column which defines if an entry is deleted or not. If it is deleted it's value is 1 and if not 0.
Now, i want to prevent the insert of a new carrier, if one of the following is true:
- If a carrier with the same iata code, and isdeleted = 0 exists
- If a carrier with the same icao code, and isdeleted = 0 exists
I managed to achieve it, by adding the following block inside validate (for iata case):
if (this.iata != ""){
sequelize.models.carrier.findAndCountAll({
where: {
iata: this.iata,
isdeleted:0
}
}).then((result) => {
if (result.rows != 0){
throw new Error("Carrier iata code already exists.");
}
})
}
and when i attempt to insert a duplicate i get a very ugly response:
Is there a way to get an instance of ValidationError in order to handle it and display iteasier?
error-handling orm sqlite3 sequelize.js
error-handling orm sqlite3 sequelize.js
edited Nov 21 at 18:05
asked Nov 21 at 14:24
George R.
156
156
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
Well it looks like i was a bit tired and i missed a return...
if (this.iata != ""){
return sequelize.models.carrier.findAndCountAll({
where: {
iata: this.iata,
isdeleted:0
}
}).then((result) => {
if (result.rows != 0){
throw new Error("Carrier iata code already exists.");
}
})
}
Thanks to florin from sequelize slack!
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Well it looks like i was a bit tired and i missed a return...
if (this.iata != ""){
return sequelize.models.carrier.findAndCountAll({
where: {
iata: this.iata,
isdeleted:0
}
}).then((result) => {
if (result.rows != 0){
throw new Error("Carrier iata code already exists.");
}
})
}
Thanks to florin from sequelize slack!
add a comment |
up vote
0
down vote
Well it looks like i was a bit tired and i missed a return...
if (this.iata != ""){
return sequelize.models.carrier.findAndCountAll({
where: {
iata: this.iata,
isdeleted:0
}
}).then((result) => {
if (result.rows != 0){
throw new Error("Carrier iata code already exists.");
}
})
}
Thanks to florin from sequelize slack!
add a comment |
up vote
0
down vote
up vote
0
down vote
Well it looks like i was a bit tired and i missed a return...
if (this.iata != ""){
return sequelize.models.carrier.findAndCountAll({
where: {
iata: this.iata,
isdeleted:0
}
}).then((result) => {
if (result.rows != 0){
throw new Error("Carrier iata code already exists.");
}
})
}
Thanks to florin from sequelize slack!
Well it looks like i was a bit tired and i missed a return...
if (this.iata != ""){
return sequelize.models.carrier.findAndCountAll({
where: {
iata: this.iata,
isdeleted:0
}
}).then((result) => {
if (result.rows != 0){
throw new Error("Carrier iata code already exists.");
}
})
}
Thanks to florin from sequelize slack!
answered Nov 21 at 20:48
George R.
156
156
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414213%2fsequelize-model-duplicate-value-check%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown