Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatic DB Collection Initialization #41

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ DB_HOST=mongo

DB_PORT=27017

API_KEY=changeit

#YOUR_MONGO_DB_USER_NAME
DB_USER=afteracademy-blog-db-user

Expand All @@ -39,4 +41,4 @@ DB_ADMIN_PWD=changeit
ACCESS_TOKEN_VALIDITY_DAYS=30
REFRESH_TOKEN_VALIDITY_DAYS=120
TOKEN_ISSUER=afteracademy.com
TOKEN_AUDIENCE=afteracademy.com
TOKEN_AUDIENCE=afteracademy.com
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const db = {
port: process.env.DB_PORT || '',
user: process.env.DB_USER || '',
password: process.env.DB_USER_PWD || '',
apikey: process.env.API_KEY || '',
};

export const corsUrl = process.env.CORS_URL;
Expand Down
48 changes: 48 additions & 0 deletions src/database/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ const dbURI = `mongodb://${db.user}:${encodeURIComponent(db.password)}@${db.host
db.name
}`;

// If above gives problems the following will work:
// const dbURI = `mongodb+srv://${db.user}:${db.password}@${db.host}/${db.name
// }?retryWrites=true&w=majority`;

const options = {
useNewUrlParser: true,
useCreateIndex: true,
Expand All @@ -33,10 +37,54 @@ mongoose
Logger.error(e);
});

// Funtion to Initialize Collections in DB
function initCollection(collName: String) {
if (collName == "api_keys") {
mongoose.connection.createCollection('api_keys')
mongoose.connection.collection("api_keys").insertOne({
metadata: 'To be used by the xyz vendor',
key: db.apikey,
version: 1,
status: true,
createdAt: new Date(),
updatedAt: new Date(),
});
} else if (collName == "roles") {
mongoose.connection.createCollection('roles');
mongoose.connection.collection("roles").insertMany([
{ code: 'LEARNER', status: true, createdAt: new Date(), updatedAt: new Date() },
{ code: 'WRITER', status: true, createdAt: new Date(), updatedAt: new Date() },
{ code: 'EDITOR', status: true, createdAt: new Date(), updatedAt: new Date() },
{ code: 'ADMIN', status: true, createdAt: new Date(), updatedAt: new Date() },
]);
}
}

// CONNECTION EVENTS
// When successfully connected
mongoose.connection.on('connected', () => {
Logger.info('Mongoose default connection open to ' + dbURI);
//trying to get collection names, if they do not exist then create them. this will run only once to inititalize db schema
mongoose.connection.db.listCollections().toArray(function (err, names) {
let keyFlag = false
let roleFlag = false
for (let i = 0; i < names.length; i++) {
if (names[i].name == "api_keys") {
keyFlag = true
}
if (names[i].name == "roles") {
roleFlag = true
}
}
if (!keyFlag) {
console.log("in key")
initCollection("api_keys")
}
if (!roleFlag) {
console.log("in role")
initCollection("roles")
}
});
});

// If the connection throws an error
Expand Down
4 changes: 3 additions & 1 deletion tests/.env.test.example
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ DB_HOST=localhost

DB_PORT=27017

API_KEY=changeit

#YOUR_TEST_MONGO_DB_USER_NAME
DB_USER=afteracademy-blog-test-db-user

Expand All @@ -32,4 +34,4 @@ DB_USER_PWD=changeit
ACCESS_TOKEN_VALIDITY_DAYS=30
REFRESH_TOKEN_VALIDITY_DAYS=120
TOKEN_ISSUER=test.afteracademy.com
TOKEN_AUDIENCE=test.afteracademy.com
TOKEN_AUDIENCE=test.afteracademy.com