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 = {
up: function (queryInterface, Sequelize) {
queryInterface.createTable('atlases', {
id:
{ type: 'UUID',
id: {
type: 'UUID',
allowNull: false,
defaultValue: null,
special: [],
primaryKey: true },
name:
{ type: 'CHARACTER VARYING',
primaryKey: true
},
name: {
type: 'CHARACTER VARYING',
allowNull: true,
defaultValue: null,
special: [],
primaryKey: false },
description:
{ type: 'CHARACTER VARYING',
},
description: {
type: 'CHARACTER VARYING',
allowNull: true,
defaultValue: null,
special: [],
primaryKey: false },
type:
{ type: 'CHARACTER VARYING',
},
type: {
type: 'CHARACTER VARYING',
allowNull: true,
defaultValue: null,
special: [],
primaryKey: false },
atlasStructure:
{ type: 'JSONB',
},
atlasStructure: {
type: 'JSONB',
allowNull: true,
defaultValue: null,
special: [],
primaryKey: false },
createdAt:
{ type: 'TIMESTAMP WITH TIME ZONE',
},
createdAt: {
type: 'TIMESTAMP WITH TIME ZONE',
allowNull: false,
defaultValue: null,
special: [],
primaryKey: false },
updatedAt:
{ type: 'TIMESTAMP WITH TIME ZONE',
},
updatedAt: {
type: 'TIMESTAMP WITH TIME ZONE',
allowNull: false,
defaultValue: null,
special: [],
primaryKey: false },
teamId:
{ type: 'UUID',
allowNull: true,
defaultValue: null,
special: [],
primaryKey: 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',
},
userId: {
type: 'UUID',
allowNull: true,
defaultValue: null,
special: [],
primaryKey: false },
atlasId:
{ type: 'UUID',
references: {
model: "users",
key: "id",
}
},
atlasId: {
type: 'UUID',
allowNull: true,
defaultValue: null,
special: [],
primaryKey: false },
parentDocumentForId:
{ type: 'UUID',
references: {
model: "atlases",
key: "id",
}
},
teamId: {
type: 'UUID',
allowNull: true,
defaultValue: null,
special: [],
primaryKey: false },
teamId:
{ type: 'UUID',
allowNull: true,
defaultValue: null,
special: [],
primaryKey: false }
references: {
model: "teams",
key: "id",
}
}
});
queryInterface.createTable('teams', {
id:
{ type: 'UUID',
id: {
type: 'UUID',
allowNull: false,
defaultValue: null,
special: [],
primaryKey: true },
name:
{ type: 'CHARACTER VARYING',
primaryKey: true
},
name: {
type: 'CHARACTER VARYING',
allowNull: true,
defaultValue: null,
special: [],
primaryKey: false },
slackId:
{ type: 'CHARACTER VARYING',
},
slackId: {
type: 'CHARACTER VARYING',
allowNull: true,
defaultValue: null,
special: [],
primaryKey: true },
slackData:
{ type: 'JSONB',
unique: true
},
slackData: {
type: 'JSONB',
allowNull: true,
defaultValue: null,
special: [],
primaryKey: false },
createdAt:
{ type: 'TIMESTAMP WITH TIME ZONE',
},
createdAt: {
type: 'TIMESTAMP WITH TIME ZONE',
allowNull: false,
defaultValue: null,
special: [],
primaryKey: false },
updatedAt:
{ type: 'TIMESTAMP WITH TIME ZONE',
},
updatedAt: {
type: 'TIMESTAMP WITH TIME ZONE',
allowNull: false,
defaultValue: null,
special: [],
primaryKey: false } }
);
}
});
queryInterface.createTable('users', {
id:
{ type: 'UUID',
id: {
type: 'UUID',
allowNull: false,
defaultValue: null,
special: [],
primaryKey: true },
email:
{ type: 'CHARACTER VARYING',
primaryKey: true
},
email: {
type: 'CHARACTER VARYING',
allowNull: false,
defaultValue: null,
special: [],
primaryKey: false },
username:
{ type: 'CHARACTER VARYING',
},
username: {
type: 'CHARACTER VARYING',
allowNull: false,
defaultValue: null,
special: [],
primaryKey: false },
name:
{ type: 'CHARACTER VARYING',
},
name: {
type: 'CHARACTER VARYING',
allowNull: false,
defaultValue: null,
special: [],
primaryKey: false },
isAdmin:
{ type: 'BOOLEAN',
},
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,
},
slackAccessToken: {
type: 'bytea',
allowNull: true, },
slackId: {
type: 'CHARACTER VARYING',
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',
},
slackData: {
type: 'JSONB',
allowNull: true,
defaultValue: null,
special: [],
primaryKey: false }
},
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",
}
}
});
},

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: {
// buildUrl() {
// const slugifiedTitle = slug(this.title);
// return `${slugifiedTitle}-${this.urlId}`;
// }
async buildStructure() {
// TODO
const rootDocument = await Document.findOne({ where: {
parentDocumentForId: null,
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: rootDocument.title,
id: rootDocument.id,
url: rootDocument.getUrl(),
children: null,
name: document.title,
id: document.id,
url: document.getUrl(),
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.hasOne(Document, { as: 'parentDocument', foreignKey: 'parentDocumentForId', constraints: false });
export default Atlas;

View File

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