From ee9aefa4fa11799cff399e43c38e5447677d7ee7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Malak?= Date: Mon, 8 Nov 2021 23:39:42 +0100 Subject: [PATCH] Modified models to support resource visibility. Added migration --- db/migrations/02_resource-access.js | 27 +++++++++++++ models/App.js | 59 +++++++++++++++++------------ models/Bookmark.js | 47 +++++++++++++---------- models/Category.js | 41 ++++++++++++-------- 4 files changed, 114 insertions(+), 60 deletions(-) create mode 100644 db/migrations/02_resource-access.js diff --git a/db/migrations/02_resource-access.js b/db/migrations/02_resource-access.js new file mode 100644 index 0000000..717ac38 --- /dev/null +++ b/db/migrations/02_resource-access.js @@ -0,0 +1,27 @@ +const { DataTypes } = require('sequelize'); +const { INTEGER } = DataTypes; + +const tables = ['categories', 'bookmarks', 'apps']; + +const up = async (query) => { + const template = { + type: INTEGER, + allowNull: true, + defaultValue: 0, + }; + + for await (let table of tables) { + await query.addColumn(table, 'isPublic', template); + } +}; + +const down = async (query) => { + for await (let table of tables) { + await query.removeColumn(table, 'isPublic'); + } +}; + +module.exports = { + up, + down, +}; diff --git a/models/App.js b/models/App.js index f521955..c71d350 100644 --- a/models/App.js +++ b/models/App.js @@ -1,31 +1,40 @@ const { DataTypes } = require('sequelize'); const { sequelize } = require('../db'); -const App = sequelize.define('App', { - name: { - type: DataTypes.STRING, - allowNull: false +const App = sequelize.define( + 'App', + { + name: { + type: DataTypes.STRING, + allowNull: false, + }, + url: { + type: DataTypes.STRING, + allowNull: false, + }, + icon: { + type: DataTypes.STRING, + allowNull: false, + defaultValue: 'cancel', + }, + isPinned: { + type: DataTypes.BOOLEAN, + defaultValue: false, + }, + orderId: { + type: DataTypes.INTEGER, + allowNull: true, + defaultValue: null, + }, + isPublic: { + type: DataTypes.INTEGER, + allowNull: true, + defaultValue: 0, + }, }, - url: { - type: DataTypes.STRING, - allowNull: false - }, - icon: { - type: DataTypes.STRING, - allowNull: false, - defaultValue: 'cancel' - }, - isPinned: { - type: DataTypes.BOOLEAN, - defaultValue: false - }, - orderId: { - type: DataTypes.INTEGER, - allowNull: true, - defaultValue: null + { + tableName: 'apps', } -}, { - tableName: 'apps' -}); +); -module.exports = App; \ No newline at end of file +module.exports = App; diff --git a/models/Bookmark.js b/models/Bookmark.js index 6c7d27b..652c119 100644 --- a/models/Bookmark.js +++ b/models/Bookmark.js @@ -1,25 +1,34 @@ const { DataTypes } = require('sequelize'); const { sequelize } = require('../db'); -const Bookmark = sequelize.define('Bookmark', { - name: { - type: DataTypes.STRING, - allowNull: false +const Bookmark = sequelize.define( + 'Bookmark', + { + name: { + type: DataTypes.STRING, + allowNull: false, + }, + url: { + type: DataTypes.STRING, + allowNull: false, + }, + categoryId: { + type: DataTypes.INTEGER, + allowNull: false, + }, + icon: { + type: DataTypes.STRING, + defaultValue: '', + }, + isPublic: { + type: DataTypes.INTEGER, + allowNull: true, + defaultValue: 0, + }, }, - url: { - type: DataTypes.STRING, - allowNull: false - }, - categoryId: { - type: DataTypes.INTEGER, - allowNull: false - }, - icon: { - type: DataTypes.STRING, - defaultValue: '' + { + tableName: 'bookmarks', } -}, { - tableName: 'bookmarks' -}); +); -module.exports = Bookmark; \ No newline at end of file +module.exports = Bookmark; diff --git a/models/Category.js b/models/Category.js index 9c9eda6..582a6c2 100644 --- a/models/Category.js +++ b/models/Category.js @@ -1,22 +1,31 @@ const { DataTypes } = require('sequelize'); const { sequelize } = require('../db'); -const Category = sequelize.define('Category', { - name: { - type: DataTypes.STRING, - allowNull: false +const Category = sequelize.define( + 'Category', + { + name: { + type: DataTypes.STRING, + allowNull: false, + }, + isPinned: { + type: DataTypes.BOOLEAN, + defaultValue: false, + }, + orderId: { + type: DataTypes.INTEGER, + allowNull: true, + defaultValue: null, + }, + isPublic: { + type: DataTypes.INTEGER, + allowNull: true, + defaultValue: 0, + }, }, - isPinned: { - type: DataTypes.BOOLEAN, - defaultValue: false - }, - orderId: { - type: DataTypes.INTEGER, - allowNull: true, - defaultValue: null + { + tableName: 'categories', } -}, { - tableName: 'categories' -}); +); -module.exports = Category; \ No newline at end of file +module.exports = Category;