Skip to content

Commit

Permalink
#285 init-db (#286)
Browse files Browse the repository at this point in the history
  • Loading branch information
tariqksoliman authored Dec 8, 2022
1 parent c36eddd commit c039c2e
Show file tree
Hide file tree
Showing 11 changed files with 249 additions and 140 deletions.
10 changes: 5 additions & 5 deletions API/Backend/Config/models/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@ var Config = sequelize.define(
mission: {
type: Sequelize.STRING,
unique: false,
allowNull: false
allowNull: false,
},
config: {
type: Sequelize.JSON,
allowNull: true,
defaultValue: {}
defaultValue: {},
},
version: {
type: Sequelize.DataTypes.INTEGER,
unique: false,
allowNull: false
}
allowNull: false,
},
},
{
timestamps: true,
updatedAt: false
updatedAt: false,
}
);

Expand Down
50 changes: 25 additions & 25 deletions API/Backend/Datasets/models/datasets.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ const attributes = {
name: {
type: Sequelize.STRING,
unique: true,
allowNull: false
allowNull: false,
},
table: {
type: Sequelize.STRING,
unique: true,
allowNull: false
}
allowNull: false,
},
};

const options = {
timestamps: true
timestamps: true,
};

// setup User model and its fields.
Expand All @@ -30,20 +30,20 @@ function makeNewDatasetTable(name, columns, success, failure) {

let attributes = {};

columns.forEach(element => {
columns.forEach((element) => {
attributes[element] = {
type: Sequelize.STRING,
unique: false,
allowNull: true
allowNull: true,
};
});

const options = {
timestamps: false
timestamps: false,
};

Datasets.findOne({ where: { name: name } })
.then(result => {
.then((result) => {
if (result) {
let DatasetTable = sequelize.define(
result.dataValues.table,
Expand All @@ -54,16 +54,16 @@ function makeNewDatasetTable(name, columns, success, failure) {
{ updatedAt: new Date().toISOString() },
{ where: { name: name }, silent: true }
)
.then(r => {
.then((r) => {
success({
name: result.dataValues.name,
table: result.dataValues.table,
tableObj: DatasetTable
tableObj: DatasetTable,
});

return null;
})
.catch(err => {
.catch((err) => {
logger(
"error",
"Failed to update datasets.",
Expand All @@ -73,19 +73,19 @@ function makeNewDatasetTable(name, columns, success, failure) {
);
failure({
status: "failure",
message: "Failed to update datasets"
message: "Failed to update datasets",
});
});
} else {
sequelize
.query("SELECT COUNT(*) FROM datasets")
.spread(results => {
.spread((results) => {
let newTable = "d" + (parseInt(results[0].count) + 1) + "_datasets";
Datasets.create({
name: name,
table: newTable
table: newTable,
})
.then(created => {
.then((created) => {
let DatasetTable = sequelize.define(
newTable,
attributes,
Expand All @@ -97,11 +97,11 @@ function makeNewDatasetTable(name, columns, success, failure) {
success({
name: name,
table: newTable,
tableObj: DatasetTable
tableObj: DatasetTable,
});
return null;
})
.catch(err => {
.catch((err) => {
logger(
"error",
"Failed to sync dataset table.",
Expand All @@ -111,13 +111,13 @@ function makeNewDatasetTable(name, columns, success, failure) {
);
failure({
status: "failure",
message: "Failed to sync"
message: "Failed to sync",
});
});

return null;
})
.catch(err => {
.catch((err) => {
logger(
"error",
"Failed to create dataset table.",
Expand All @@ -127,12 +127,12 @@ function makeNewDatasetTable(name, columns, success, failure) {
);
failure({
status: "failure",
message: "Failed to create"
message: "Failed to create",
});
});
return null;
})
.catch(err => {
.catch((err) => {
logger(
"error",
"Failed to count existing datasets.",
Expand All @@ -142,14 +142,14 @@ function makeNewDatasetTable(name, columns, success, failure) {
);
failure({
status: "failure",
message: "Failed to count existing datasets"
message: "Failed to count existing datasets",
});
});
}

return null;
})
.catch(err => {
.catch((err) => {
logger(
"error",
"Failed to find existing datasets.",
Expand All @@ -161,13 +161,13 @@ function makeNewDatasetTable(name, columns, success, failure) {
status: "failure",
message: "Failed to find existing datasets",
error: error,
name: name
name: name,
});
});
}

// export User model for use in other files.
module.exports = {
Datasets: Datasets,
makeNewDatasetTable: makeNewDatasetTable
makeNewDatasetTable: makeNewDatasetTable,
};
Loading

0 comments on commit c039c2e

Please sign in to comment.