From feb4bdfd9b46e87aadfa7c0d5338cde887d1f58c Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Sun, 11 Dec 2016 21:50:51 +0100 Subject: First version with PostgreSQL --- server/models/author.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 server/models/author.js (limited to 'server/models/author.js') diff --git a/server/models/author.js b/server/models/author.js new file mode 100644 index 000000000..493c2ca11 --- /dev/null +++ b/server/models/author.js @@ -0,0 +1,28 @@ +module.exports = function (sequelize, DataTypes) { + const Author = sequelize.define('Author', + { + name: { + type: DataTypes.STRING + } + }, + { + classMethods: { + associate + } + } + ) + + return Author +} + +// --------------------------------------------------------------------------- + +function associate (models) { + this.belongsTo(models.Pod, { + foreignKey: { + name: 'podId', + allowNull: true + }, + onDelete: 'cascade' + }) +} -- cgit v1.2.3 From 67bf9b96bbcd92b069fe86d9223fe0f8b9c6e677 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 28 Dec 2016 15:49:23 +0100 Subject: Server: add database field validations --- server/models/author.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'server/models/author.js') diff --git a/server/models/author.js b/server/models/author.js index 493c2ca11..e0ac868ea 100644 --- a/server/models/author.js +++ b/server/models/author.js @@ -1,8 +1,19 @@ +'use strict' + +const customUsersValidators = require('../helpers/custom-validators').users + module.exports = function (sequelize, DataTypes) { const Author = sequelize.define('Author', { name: { - type: DataTypes.STRING + type: DataTypes.STRING, + allowNull: false, + validate: { + usernameValid: function (value) { + const res = customUsersValidators.isUserUsernameValid(value) + if (res === false) throw new Error('Username is not valid.') + } + } } }, { -- cgit v1.2.3 From 319d072e8eb7266cd8d33e0bb2fb5ebe76c487d1 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 29 Dec 2016 09:33:28 +0100 Subject: Server: Add postgresql indexes --- server/models/author.js | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'server/models/author.js') diff --git a/server/models/author.js b/server/models/author.js index e0ac868ea..8f5b598c8 100644 --- a/server/models/author.js +++ b/server/models/author.js @@ -17,6 +17,14 @@ module.exports = function (sequelize, DataTypes) { } }, { + indexes: [ + { + fields: [ 'name' ] + }, + { + fields: [ 'podId' ] + } + ], classMethods: { associate } -- cgit v1.2.3 From 4712081f2a5f48749cf125d729e78b926ab28d6d Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 29 Dec 2016 10:33:36 +0100 Subject: Server: add association between author and user --- server/models/author.js | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'server/models/author.js') diff --git a/server/models/author.js b/server/models/author.js index 8f5b598c8..5835ada99 100644 --- a/server/models/author.js +++ b/server/models/author.js @@ -23,6 +23,9 @@ module.exports = function (sequelize, DataTypes) { }, { fields: [ 'podId' ] + }, + { + fields: [ 'userId' ] } ], classMethods: { @@ -44,4 +47,12 @@ function associate (models) { }, onDelete: 'cascade' }) + + this.belongsTo(models.User, { + foreignKey: { + name: 'userId', + allowNull: true + }, + onDelete: 'cascade' + }) } -- cgit v1.2.3 From 4ff0d86208dafbdd07beb6286fd93c795db8a95f Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 29 Dec 2016 18:02:03 +0100 Subject: Server: little refractoring --- server/models/author.js | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) (limited to 'server/models/author.js') diff --git a/server/models/author.js b/server/models/author.js index 5835ada99..7d15fb6ec 100644 --- a/server/models/author.js +++ b/server/models/author.js @@ -29,7 +29,9 @@ module.exports = function (sequelize, DataTypes) { } ], classMethods: { - associate + associate, + + findOrCreateAuthor } } ) @@ -56,3 +58,28 @@ function associate (models) { onDelete: 'cascade' }) } + +function findOrCreateAuthor (name, podId, userId, transaction, callback) { + if (!callback) { + callback = transaction + transaction = null + } + + const author = { + name, + podId, + userId + } + + const query = { + where: author, + defaults: author + } + + if (transaction) query.transaction = transaction + + this.findOrCreate(query).asCallback(function (err, result) { + // [ instance, wasCreated ] + return callback(err, result[0]) + }) +} -- cgit v1.2.3