Structures for atlases

This commit is contained in:
Jori Lallo
2016-06-22 00:01:57 -07:00
parent cbdf6b517d
commit db16e71c5e
5 changed files with 198 additions and 224 deletions

View File

@ -3,252 +3,193 @@
module.exports = { module.exports = {
up: function (queryInterface, Sequelize) { up: function (queryInterface, Sequelize) {
queryInterface.createTable('atlases', { queryInterface.createTable('atlases', {
id: id: {
{ type: 'UUID', type: 'UUID',
allowNull: false, allowNull: false,
defaultValue: null, primaryKey: true
special: [], },
primaryKey: true }, name: {
name: type: 'CHARACTER VARYING',
{ type: 'CHARACTER VARYING',
allowNull: true, allowNull: true,
defaultValue: null, },
special: [], description: {
primaryKey: false }, type: 'CHARACTER VARYING',
description:
{ type: 'CHARACTER VARYING',
allowNull: true, allowNull: true,
defaultValue: null, },
special: [], type: {
primaryKey: false }, type: 'CHARACTER VARYING',
type:
{ type: 'CHARACTER VARYING',
allowNull: true, allowNull: true,
defaultValue: null, },
special: [], atlasStructure: {
primaryKey: false }, type: 'JSONB',
atlasStructure:
{ type: 'JSONB',
allowNull: true, allowNull: true,
defaultValue: null, },
special: [], createdAt: {
primaryKey: false }, type: 'TIMESTAMP WITH TIME ZONE',
createdAt:
{ type: 'TIMESTAMP WITH TIME ZONE',
allowNull: false, allowNull: false,
defaultValue: null, },
special: [], updatedAt: {
primaryKey: false }, type: 'TIMESTAMP WITH TIME ZONE',
updatedAt:
{ type: 'TIMESTAMP WITH TIME ZONE',
allowNull: false, allowNull: false,
defaultValue: null, },
special: [], teamId: {
primaryKey: false }, type: 'UUID',
teamId: allowNull: false,
{ type: 'UUID', references: {
allowNull: true, model: "teams",
defaultValue: null, key: "id",
special: [], }
primaryKey: false } } }
); });
// documents
queryInterface.createTable('documents', { queryInterface.createTable('documents', {
id: id:
{ type: 'UUID', { type: 'UUID',
allowNull: false, allowNull: false,
defaultValue: null,
special: [],
primaryKey: true }, primaryKey: true },
urlId: urlId:
{ type: 'CHARACTER VARYING', { type: 'CHARACTER VARYING',
allowNull: false, allowNull: false,
unique: true, unique: true, },
defaultValue: null,
special: [],
primaryKey: false },
private: private:
{ type: 'BOOLEAN', { type: 'BOOLEAN',
allowNull: false, allowNull: false,
defaultValue: true, defaultValue: true,
special: [], },
primaryKey: false },
title: title:
{ type: 'CHARACTER VARYING', { type: 'CHARACTER VARYING',
allowNull: false, allowNull: false,
defaultValue: null, },
special: [],
primaryKey: false },
text: text:
{ type: 'TEXT', { type: 'TEXT',
allowNull: true, allowNull: true,
defaultValue: null, },
special: [],
primaryKey: false },
html: html:
{ type: 'TEXT', { type: 'TEXT',
allowNull: true, allowNull: true,
defaultValue: null, },
special: [],
primaryKey: false },
preview: preview:
{ type: 'TEXT', { type: 'TEXT',
allowNull: true, allowNull: true,
defaultValue: null, },
special: [],
primaryKey: false },
createdAt: createdAt:
{ type: 'TIMESTAMP WITH TIME ZONE', { type: 'TIMESTAMP WITH TIME ZONE',
allowNull: false, allowNull: false,
defaultValue: null, },
special: [],
primaryKey: false },
updatedAt: updatedAt:
{ type: 'TIMESTAMP WITH TIME ZONE', { type: 'TIMESTAMP WITH TIME ZONE',
allowNull: false, allowNull: false,
defaultValue: null, },
special: [], userId: {
primaryKey: false }, type: 'UUID',
userId:
{ type: 'UUID',
allowNull: true, allowNull: true,
defaultValue: null, references: {
special: [], model: "users",
primaryKey: false }, key: "id",
atlasId: }
{ type: 'UUID', },
atlasId: {
type: 'UUID',
allowNull: true, allowNull: true,
defaultValue: null, references: {
special: [], model: "atlases",
primaryKey: false }, key: "id",
parentDocumentForId: }
{ type: 'UUID', },
teamId: {
type: 'UUID',
allowNull: true, allowNull: true,
defaultValue: null, references: {
special: [], model: "teams",
primaryKey: false }, key: "id",
teamId: }
{ type: 'UUID', }
allowNull: true,
defaultValue: null,
special: [],
primaryKey: false }
}); });
queryInterface.createTable('teams', { queryInterface.createTable('teams', {
id: id: {
{ type: 'UUID', type: 'UUID',
allowNull: false, allowNull: false,
defaultValue: null, primaryKey: true
special: [], },
primaryKey: true }, name: {
name: type: 'CHARACTER VARYING',
{ type: 'CHARACTER VARYING',
allowNull: true, allowNull: true,
defaultValue: null, },
special: [], slackId: {
primaryKey: false }, type: 'CHARACTER VARYING',
slackId:
{ type: 'CHARACTER VARYING',
allowNull: true, allowNull: true,
defaultValue: null, unique: true
special: [], },
primaryKey: true }, slackData: {
slackData: type: 'JSONB',
{ type: 'JSONB',
allowNull: true, allowNull: true,
defaultValue: null, },
special: [], createdAt: {
primaryKey: false }, type: 'TIMESTAMP WITH TIME ZONE',
createdAt:
{ type: 'TIMESTAMP WITH TIME ZONE',
allowNull: false, allowNull: false,
defaultValue: null, },
special: [], updatedAt: {
primaryKey: false }, type: 'TIMESTAMP WITH TIME ZONE',
updatedAt:
{ type: 'TIMESTAMP WITH TIME ZONE',
allowNull: false, allowNull: false,
defaultValue: null, }
special: [], });
primaryKey: false } }
);
queryInterface.createTable('users', { queryInterface.createTable('users', {
id: id: {
{ type: 'UUID', type: 'UUID',
allowNull: false, allowNull: false,
defaultValue: null, primaryKey: true
special: [], },
primaryKey: true }, email: {
email: type: 'CHARACTER VARYING',
{ type: 'CHARACTER VARYING',
allowNull: false, allowNull: false,
defaultValue: null, },
special: [], username: {
primaryKey: false }, type: 'CHARACTER VARYING',
username:
{ type: 'CHARACTER VARYING',
allowNull: false, allowNull: false,
defaultValue: null, },
special: [], name: {
primaryKey: false }, type: 'CHARACTER VARYING',
name:
{ type: 'CHARACTER VARYING',
allowNull: false, allowNull: false,
defaultValue: null, },
special: [], isAdmin: {
primaryKey: false }, type: 'BOOLEAN',
isAdmin:
{ type: 'BOOLEAN',
allowNull: true, allowNull: true,
defaultValue: false, defaultValue: false,
special: [], },
primaryKey: false }, slackAccessToken: {
slackAccessToken: type: 'bytea',
{ type: 'bytea', allowNull: true, },
allowNull: true, slackId: {
defaultValue: null, type: 'CHARACTER VARYING',
special: [],
primaryKey: false },
slackId:
{ type: 'CHARACTER VARYING',
allowNull: false,
defaultValue: null,
unique: true, 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, allowNull: false,
defaultValue: null, },
special: [], slackData: {
primaryKey: false }, type: 'JSONB',
updatedAt:
{ type: 'TIMESTAMP WITH TIME ZONE',
allowNull: false,
defaultValue: null,
special: [],
primaryKey: false },
teamId:
{ type: 'UUID',
allowNull: true, allowNull: true,
defaultValue: null, },
special: [], jwtSecret: {
primaryKey: false } 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",
}
}
}); });
}, },

View File

@ -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');
}
};

View File

@ -30,28 +30,37 @@ const Atlas = sequelize.define('atlas', {
// }, // },
}, },
instanceMethods: { instanceMethods: {
// buildUrl() {
// const slugifiedTitle = slug(this.title);
// return `${slugifiedTitle}-${this.urlId}`;
// }
async buildStructure() { async buildStructure() {
// TODO const getNodeForDocument = async (document) => {
const rootDocument = await Document.findOne({ where: { const children = await Document.findAll({ where: {
parentDocumentForId: null, parentDocumentId: document.id,
atlasId: this.id, atlasId: this.id,
}}); }});
let childNodes = []
await Promise.all(children.map(async (child) => {
console.log(child.id)
childNodes.push(await getNodeForDocument(child));
}));
return { return {
name: rootDocument.title, name: document.title,
id: rootDocument.id, id: document.id,
url: rootDocument.getUrl(), url: document.getUrl(),
children: null, children: childNodes,
};
} }
const rootDocument = await Document.findOne({ where: {
parentDocumentId: null,
atlasId: this.id,
}});
return await getNodeForDocument(rootDocument);
} }
} }
}); });
Atlas.hasMany(Document, { as: 'documents', foreignKey: 'atlasId' }); Atlas.hasMany(Document, { as: 'documents', foreignKey: 'atlasId' });
Atlas.hasOne(Document, { as: 'parentDocument', foreignKey: 'parentDocumentForId', constraints: false });
export default Atlas; export default Atlas;

View File

@ -27,6 +27,8 @@ const Document = sequelize.define('document', {
text: DataTypes.TEXT, text: DataTypes.TEXT,
html: DataTypes.TEXT, html: DataTypes.TEXT,
preview: DataTypes.TEXT, preview: DataTypes.TEXT,
parentDocumentId: DataTypes.UUID,
}, { }, {
hooks: { hooks: {
beforeValidate: (doc) => { beforeValidate: (doc) => {