Skip to content

Commit

Permalink
Merge pull request #24 from andela/ft-req-table-integration-167891585
Browse files Browse the repository at this point in the history
#167891585: Creates Database Tables and Seeders
  • Loading branch information
daniellamarr committed Sep 2, 2019
2 parents 82e7607 + 774f44a commit 30f2cff
Show file tree
Hide file tree
Showing 20 changed files with 525 additions and 9 deletions.
10 changes: 10 additions & 0 deletions src/database/migrations/20190821105503-create-user.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ module.exports = {
type: Sequelize.ENUM('active', 'inactive', 'unverified'),
defaultValue: 'unverified'
},
favorites: {
allowNull: false,
type: Sequelize.BOOLEAN,
defaultValue: false,
set: (value) => {
if (value === 'true') value = true;
if (value === 'false') value = false;
this.setDataValue('favorites', value);
}
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
Expand Down
57 changes: 57 additions & 0 deletions src/database/migrations/20190828140409-create-trip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@

module.exports = {
up: (queryInterface, Sequelize) => queryInterface.sequelize.query('CREATE EXTENSION IF NOT EXISTS "uuid-ossp";').then(() => queryInterface.createTable('Trips', {
id: {
allowNull: false,
primaryKey: true,
type: Sequelize.DataTypes.UUID,
defaultValue: Sequelize.literal('uuid_generate_v4()'),
},
type: {
type: Sequelize.ENUM('oneway', 'return', 'multiple'),
allowNull: false,
},
userId: {
type: Sequelize.UUID,
allowNull: false,
onDelete: 'CASCADE',
onUpdate: 'CASCADE',
references: {
model: 'Users',
key: 'id',
},
},
status: {
allowNull: false,
type: Sequelize.ENUM('pending', 'approved', 'rejected'),
defaultValue: 'pending',
},
startBranchId: {
type: Sequelize.UUID,
allowNull: false,
},
reason: {
type: Sequelize.STRING,
allowNull: false
},
tripDate: {
type: Sequelize.DATE,
allowNull: false
},
returnDate: {
type: Sequelize.DATE,
allowNull: true
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.fn('now')
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.fn('now')
}
})),
down: queryInterface => queryInterface.dropTable('Trips')
};
40 changes: 40 additions & 0 deletions src/database/migrations/20190828145910-create-location.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

module.exports = {
up: (queryInterface, Sequelize) => queryInterface.sequelize.query('CREATE EXTENSION IF NOT EXISTS "uuid-ossp";').then(() => queryInterface.createTable('Locations', {
id: {
allowNull: false,
primaryKey: true,
type: Sequelize.DataTypes.UUID,
defaultValue: Sequelize.literal('uuid_generate_v4()'),
},
companyId: {
type: Sequelize.UUID,
allowNull: false,
onDelete: 'CASCADE',
onUpdate: 'CASCADE',
references: {
model: 'Companies',
key: 'id'
}
},
country: {
type: Sequelize.STRING,
allowNull: false
},
city: {
type: Sequelize.STRING,
allowNull: false
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.fn('now')
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.fn('now')
}
})),
down: queryInterface => queryInterface.dropTable('Locations')
};
35 changes: 35 additions & 0 deletions src/database/migrations/20190828145911-create-branch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

module.exports = {
up: (queryInterface, Sequelize) => queryInterface.sequelize.query('CREATE EXTENSION IF NOT EXISTS "uuid-ossp";').then(() => queryInterface.createTable('Branches', {
id: {
allowNull: false,
primaryKey: true,
type: Sequelize.DataTypes.UUID,
defaultValue: Sequelize.literal('uuid_generate_v4()'),
},
name: {
type: Sequelize.STRING,
allowNull: false
},
locationId: {
type: Sequelize.UUID,
onDelete: 'CASCADE',
onUpdate: 'CASCADE',
references: {
model: 'Locations',
key: 'id'
}
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.fn('now')
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.fn('now')
}
})),
down: queryInterface => queryInterface.dropTable('Branches')
};
43 changes: 43 additions & 0 deletions src/database/migrations/20190828150143-create-accomodation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

module.exports = {
up: (queryInterface, Sequelize) => queryInterface.sequelize.query('CREATE EXTENSION IF NOT EXISTS "uuid-ossp";').then(() => queryInterface.createTable('Accomodations', {
id: {
allowNull: false,
primaryKey: true,
type: Sequelize.DataTypes.UUID,
defaultValue: Sequelize.literal('uuid_generate_v4()')
},
name: {
type: Sequelize.STRING,
allowNull: false
},
branchId: {
type: Sequelize.UUID,
onDelete: 'CASCADE',
onUpdate: 'CASCADE',
references: {
model: 'Branches',
key: 'id'
}
},
capacity: {
type: Sequelize.INTEGER,
allowNull: false
},
status: {
type: Sequelize.ENUM('available', 'filled'),
allowNull: false
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.fn('now')
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.fn('now')
}
})),
down: queryInterface => queryInterface.dropTable('Accomodations')
};
49 changes: 49 additions & 0 deletions src/database/migrations/20190828150144-create-stop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
module.exports = {
up: (queryInterface, Sequelize) => queryInterface.sequelize.query('CREATE EXTENSION IF NOT EXISTS "uuid-ossp";').then(() => queryInterface.createTable('Stops', {
id: {
allowNull: false,
primaryKey: true,
type: Sequelize.DataTypes.UUID,
defaultValue: Sequelize.literal('uuid_generate_v4()'),
},
destinationBranchId: {
type: Sequelize.UUID,
onDelete: 'RESTRICT',
onUpdate: 'RESTRICT',
references: {
model: 'Branches',
key: 'id',
}
},
accomodationId: {
type: Sequelize.UUID,
onDelete: 'RESTRICT',
onUpdate: 'RESTRICT',
references: {
model: 'Accomodations',
key: 'id'
}
},
tripId: {
type: Sequelize.UUID,
allowNull: false,
onDelete: 'RESTRICT',
onUpdate: 'RESTRICT',
references: {
model: 'Trips',
key: 'id'
}
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.fn('now')
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.fn('now')
}
})),
down: queryInterface => queryInterface.dropTable('Stops')
};
23 changes: 23 additions & 0 deletions src/database/models/accomodation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module.exports = (sequelize, DataTypes) => {
const Accomodation = sequelize.define('Accomodation', {
name: DataTypes.STRING,
branchId: DataTypes.UUID,
capacity: DataTypes.INTEGER,
status: DataTypes.STRING
}, {});
Accomodation.associate = (models) => {
Accomodation.belongsTo(models.Branch, {
foreignKey: 'branchId',
as: 'branch',
onDelete: 'CASCADE',
onUpdate: 'CASCADE'
});
Accomodation.hasMany(models.Stop, {
foreignKey: 'accomodationId',
as: 'stop',
onDelete: 'CASCADE',
onUpdate: 'CASCADE'
});
};
return Accomodation;
};
33 changes: 33 additions & 0 deletions src/database/models/branch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module.exports = (sequelize, DataTypes) => {
const Branch = sequelize.define('Branch', {
name: DataTypes.STRING,
locationId: DataTypes.UUID
}, {});
Branch.associate = (models) => {
Branch.belongsTo(models.Location, {
foreignKey: 'locationId',
as: 'location',
onDelete: 'CASCADE',
onUpdate: 'CASCADE'
});
Branch.hasMany(models.Trip, {
foreignKey: 'startBranchId',
as: 'trip',
onDelete: 'CASCADE',
onUpdate: 'CASCADE'
});
Branch.hasMany(models.Accomodation, {
foreignKey: 'branchId',
as: 'accomodation',
onDelete: 'CASCADE',
onUpdate: 'CASCADE'
});
Branch.hasMany(models.Stop, {
foreignKey: 'destinationBranchId',
as: 'stop',
onDelete: 'CASCADE',
onUpdate: 'CASCADE'
});
};
return Branch;
};
13 changes: 12 additions & 1 deletion src/database/models/company.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,18 @@ module.exports = (sequelize, DataTypes) => {
owner: DataTypes.STRING
}, {});
Company.associate = (models) => {
// associations can be defined here
Company.hasMany(models.Location, {
foreignKey: 'companyId',
as: 'location',
onDelete: 'CASCADE',
onUpdate: 'CASCADE'
});
Company.hasMany(models.User, {
foreignKey: 'companyId',
as: 'user',
onDelete: 'CASCADE',
onUpdate: 'CASCADE'
});
};
return Company;
};
22 changes: 22 additions & 0 deletions src/database/models/location.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module.exports = (sequelize, DataTypes) => {
const Location = sequelize.define('Location', {
companyId: DataTypes.UUID,
country: DataTypes.STRING,
city: DataTypes.STRING
}, {});
Location.associate = (models) => {
Location.belongsTo(models.Company, {
foreignKey: 'companyId',
as: 'company',
onDelete: 'CASCADE',
onUpdate: 'CASCADE'
});
Location.hasMany(models.Branch, {
foreignKey: 'locationId',
as: 'branch',
onDelete: 'CASCADE',
onUpdate: 'CASCADE'
});
};
return Location;
};
28 changes: 28 additions & 0 deletions src/database/models/stop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module.exports = (sequelize, DataTypes) => {
const Stop = sequelize.define('Stop', {
destinationBranchId: DataTypes.UUID,
accomodationId: DataTypes.UUID,
tripId: DataTypes.UUID,
}, {});
Stop.associate = (models) => {
Stop.belongsTo(models.Trip, {
foreignKey: 'tripId',
as: 'trip',
onDelete: 'CASCADE',
onUpdate: 'CASCADE'
});
Stop.belongsTo(models.Accomodation, {
foreignKey: 'accomodationId',
as: 'accomodation',
onDelete: 'CASCADE',
onUpdate: 'CASCADE'
});
Stop.belongsTo(models.Branch, {
foreignKey: 'destinationBranchId',
as: 'branch',
onDelete: 'RESTRICT',
onUpdate: 'RESTRICT'
});
};
return Stop;
};
Loading

0 comments on commit 30f2cff

Please sign in to comment.