diff --git a/server/api/atlases.js b/server/api/atlases.js index c1fca939..b34e871c 100644 --- a/server/api/atlases.js +++ b/server/api/atlases.js @@ -56,4 +56,4 @@ router.post('atlases.list', auth(), pagination(), async (ctx) => { }; }); -export default router; \ No newline at end of file +export default router; diff --git a/server/migrations/20160619080644-initial.js b/server/migrations/20160619080644-initial.js index cc6902ad..5e559283 100644 --- a/server/migrations/20160619080644-initial.js +++ b/server/migrations/20160619080644-initial.js @@ -3,252 +3,193 @@ module.exports = { up: function (queryInterface, Sequelize) { queryInterface.createTable('atlases', { - id: - { type: 'UUID', - allowNull: false, - defaultValue: null, - special: [], - primaryKey: true }, - name: - { type: 'CHARACTER VARYING', - allowNull: true, - defaultValue: null, - special: [], - primaryKey: false }, - description: - { type: 'CHARACTER VARYING', - allowNull: true, - defaultValue: null, - special: [], - primaryKey: false }, - type: - { type: 'CHARACTER VARYING', - allowNull: true, - defaultValue: null, - special: [], - primaryKey: false }, - atlasStructure: - { type: 'JSONB', - allowNull: true, - defaultValue: null, - special: [], - primaryKey: false }, - createdAt: - { type: 'TIMESTAMP WITH TIME ZONE', - allowNull: false, - defaultValue: null, - special: [], - primaryKey: false }, - updatedAt: - { type: 'TIMESTAMP WITH TIME ZONE', - allowNull: false, - defaultValue: null, - special: [], - primaryKey: false }, - teamId: - { type: 'UUID', - allowNull: true, - defaultValue: null, - special: [], - primaryKey: false } } - ); + id: { + type: 'UUID', + allowNull: false, + primaryKey: true + }, + name: { + type: 'CHARACTER VARYING', + allowNull: true, + }, + description: { + type: 'CHARACTER VARYING', + allowNull: true, + }, + type: { + type: 'CHARACTER VARYING', + allowNull: true, + }, + atlasStructure: { + type: 'JSONB', + allowNull: true, + }, + createdAt: { + type: 'TIMESTAMP WITH TIME ZONE', + allowNull: false, + }, + updatedAt: { + type: 'TIMESTAMP WITH TIME ZONE', + allowNull: false, + }, + teamId: { + type: 'UUID', + allowNull: false, + references: { + model: "teams", + key: "id", + } + } + }); - // documents queryInterface.createTable('documents', { id: { type: 'UUID', allowNull: false, - defaultValue: null, - special: [], primaryKey: true }, urlId: { type: 'CHARACTER VARYING', allowNull: false, - unique: true, - defaultValue: null, - special: [], - primaryKey: false }, + unique: true, }, private: { type: 'BOOLEAN', allowNull: false, defaultValue: true, - special: [], - primaryKey: false }, + }, title: { type: 'CHARACTER VARYING', allowNull: false, - defaultValue: null, - special: [], - primaryKey: false }, + }, text: { type: 'TEXT', allowNull: true, - defaultValue: null, - special: [], - primaryKey: false }, + }, html: { type: 'TEXT', allowNull: true, - defaultValue: null, - special: [], - primaryKey: false }, + }, preview: { type: 'TEXT', allowNull: true, - defaultValue: null, - special: [], - primaryKey: false }, + }, createdAt: { type: 'TIMESTAMP WITH TIME ZONE', allowNull: false, - defaultValue: null, - special: [], - primaryKey: false }, + }, updatedAt: { type: 'TIMESTAMP WITH TIME ZONE', allowNull: false, - defaultValue: null, - special: [], - primaryKey: false }, - userId: - { type: 'UUID', - allowNull: true, - defaultValue: null, - special: [], - primaryKey: false }, - atlasId: - { type: 'UUID', - allowNull: true, - defaultValue: null, - special: [], - primaryKey: false }, - parentDocumentForId: - { type: 'UUID', - allowNull: true, - defaultValue: null, - special: [], - primaryKey: false }, - teamId: - { type: 'UUID', - allowNull: true, - defaultValue: null, - special: [], - primaryKey: false } + }, + userId: { + type: 'UUID', + allowNull: true, + references: { + model: "users", + key: "id", + } + }, + atlasId: { + type: 'UUID', + allowNull: true, + references: { + model: "atlases", + key: "id", + } + }, + teamId: { + type: 'UUID', + allowNull: true, + references: { + model: "teams", + key: "id", + } + } }); queryInterface.createTable('teams', { - id: - { type: 'UUID', - allowNull: false, - defaultValue: null, - special: [], - primaryKey: true }, - name: - { type: 'CHARACTER VARYING', - allowNull: true, - defaultValue: null, - special: [], - primaryKey: false }, - slackId: - { type: 'CHARACTER VARYING', - allowNull: true, - defaultValue: null, - special: [], - primaryKey: true }, - slackData: - { type: 'JSONB', - allowNull: true, - defaultValue: null, - special: [], - primaryKey: false }, - createdAt: - { type: 'TIMESTAMP WITH TIME ZONE', - allowNull: false, - defaultValue: null, - special: [], - primaryKey: false }, - updatedAt: - { type: 'TIMESTAMP WITH TIME ZONE', - allowNull: false, - defaultValue: null, - special: [], - primaryKey: false } } - ); + id: { + type: 'UUID', + allowNull: false, + primaryKey: true + }, + name: { + type: 'CHARACTER VARYING', + allowNull: true, + }, + slackId: { + type: 'CHARACTER VARYING', + allowNull: true, + unique: true + }, + slackData: { + type: 'JSONB', + allowNull: true, + }, + createdAt: { + type: 'TIMESTAMP WITH TIME ZONE', + allowNull: false, + }, + updatedAt: { + type: 'TIMESTAMP WITH TIME ZONE', + allowNull: false, + } + }); queryInterface.createTable('users', { - id: - { type: 'UUID', - allowNull: false, - defaultValue: null, - special: [], - primaryKey: true }, - email: - { type: 'CHARACTER VARYING', - allowNull: false, - defaultValue: null, - special: [], - primaryKey: false }, - username: - { type: 'CHARACTER VARYING', - allowNull: false, - defaultValue: null, - special: [], - primaryKey: false }, - name: - { type: 'CHARACTER VARYING', - allowNull: false, - defaultValue: null, - special: [], - primaryKey: false }, - isAdmin: - { type: 'BOOLEAN', - allowNull: true, - defaultValue: false, - special: [], - primaryKey: false }, - slackAccessToken: - { type: 'bytea', - allowNull: true, - defaultValue: null, - special: [], - primaryKey: false }, - slackId: - { type: 'CHARACTER VARYING', - allowNull: false, - defaultValue: null, - unique: true, - special: [], - primaryKey: false }, - slackData: - { type: 'JSONB', - allowNull: true, - defaultValue: null, - special: [], - primaryKey: false }, - jwtSecret: - { type: 'bytea', - allowNull: true, - defaultValue: null, - special: [], - primaryKey: false }, - createdAt: - { type: 'TIMESTAMP WITH TIME ZONE', - allowNull: false, - defaultValue: null, - special: [], - primaryKey: false }, - updatedAt: - { type: 'TIMESTAMP WITH TIME ZONE', - allowNull: false, - defaultValue: null, - special: [], - primaryKey: false }, - teamId: - { type: 'UUID', - allowNull: true, - defaultValue: null, - special: [], - primaryKey: false } + id: { + type: 'UUID', + allowNull: false, + primaryKey: true + }, + email: { + type: 'CHARACTER VARYING', + allowNull: false, + }, + username: { + type: 'CHARACTER VARYING', + allowNull: false, + }, + name: { + type: 'CHARACTER VARYING', + allowNull: false, + }, + isAdmin: { + type: 'BOOLEAN', + allowNull: true, + defaultValue: false, + }, + slackAccessToken: { + type: 'bytea', + allowNull: true, }, + slackId: { + type: 'CHARACTER VARYING', + unique: true, + allowNull: false, + }, + slackData: { + type: 'JSONB', + allowNull: true, + }, + jwtSecret: { + type: 'bytea', + allowNull: true, + }, + createdAt: { + type: 'TIMESTAMP WITH TIME ZONE', + allowNull: false, + }, + updatedAt: { + type: 'TIMESTAMP WITH TIME ZONE', + allowNull: false, + }, + teamId: { + type: 'UUID', + allowNull: true, + references: { + model: "teams", + key: "id", + } + } }); }, diff --git a/server/migrations/20160622043741-add-parent-document.js b/server/migrations/20160622043741-add-parent-document.js new file mode 100644 index 00000000..eda6ae28 --- /dev/null +++ b/server/migrations/20160622043741-add-parent-document.js @@ -0,0 +1,22 @@ +'use strict'; + +module.exports = { + up: function (queryInterface, Sequelize) { + queryInterface.addColumn( + 'documents', + 'parentDocumentId', + { + type: Sequelize.UUID, + allowNull: true, + references: { + model: "documents", + key: "id", + } + } + ); + }, + + down: function (queryInterface, Sequelize) { + queryInterface.removeColumn('documents', 'parentDocumentId'); + } +}; diff --git a/server/models/Atlas.js b/server/models/Atlas.js index bc7784b8..1550a1e6 100644 --- a/server/models/Atlas.js +++ b/server/models/Atlas.js @@ -30,28 +30,37 @@ const Atlas = sequelize.define('atlas', { // }, }, instanceMethods: { - // buildUrl() { - // const slugifiedTitle = slug(this.title); - // return `${slugifiedTitle}-${this.urlId}`; - // } async buildStructure() { - // TODO + const getNodeForDocument = async (document) => { + const children = await Document.findAll({ where: { + parentDocumentId: document.id, + atlasId: this.id, + }}); + + let childNodes = [] + await Promise.all(children.map(async (child) => { + console.log(child.id) + childNodes.push(await getNodeForDocument(child)); + })); + + return { + name: document.title, + id: document.id, + url: document.getUrl(), + children: childNodes, + }; + } + const rootDocument = await Document.findOne({ where: { - parentDocumentForId: null, + parentDocumentId: null, atlasId: this.id, }}); - return { - name: rootDocument.title, - id: rootDocument.id, - url: rootDocument.getUrl(), - children: null, - } + return await getNodeForDocument(rootDocument); } } }); Atlas.hasMany(Document, { as: 'documents', foreignKey: 'atlasId' }); -Atlas.hasOne(Document, { as: 'parentDocument', foreignKey: 'parentDocumentForId', constraints: false }); export default Atlas; diff --git a/server/models/Document.js b/server/models/Document.js index e0829927..db3e323b 100644 --- a/server/models/Document.js +++ b/server/models/Document.js @@ -27,6 +27,8 @@ const Document = sequelize.define('document', { text: DataTypes.TEXT, html: DataTypes.TEXT, preview: DataTypes.TEXT, + + parentDocumentId: DataTypes.UUID, }, { hooks: { beforeValidate: (doc) => {