From 2e63831b90c7457e0e322719ebb1193fd6181cc3 Mon Sep 17 00:00:00 2001 From: Saad Jutt Date: Tue, 19 Apr 2022 23:25:05 +0500 Subject: [PATCH] fix: added db seed at server startup --- api/src/app.ts | 2 +- api/src/utils/connectDB.ts | 5 ++++- api/src/utils/file.ts | 2 +- api/src/utils/index.ts | 1 + api/src/utils/seedDB.ts | 35 +++++++++++++++++++++++++++++++++++ 5 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 api/src/utils/seedDB.ts diff --git a/api/src/app.ts b/api/src/app.ts index 100b529..77c3380 100644 --- a/api/src/app.ts +++ b/api/src/app.ts @@ -59,6 +59,6 @@ export default setProcessVariables().then(async () => { app.use(onError) - await connectDB() + connectDB() return app }) diff --git a/api/src/utils/connectDB.ts b/api/src/utils/connectDB.ts index 92b2673..7709b49 100644 --- a/api/src/utils/connectDB.ts +++ b/api/src/utils/connectDB.ts @@ -1,7 +1,8 @@ import mongoose from 'mongoose' import { populateClients } from '../routes/api/auth' +import { seedDB } from './seedDB' -export const connectDB = async () => { +export const connectDB = () => { // NOTE: when exporting app.js as agent for supertest // we should exclude connecting to the real database if (process.env.NODE_ENV === 'test') { @@ -19,6 +20,8 @@ export const connectDB = async () => { console.log('Connected to db!') + await seedDB() + await populateClients() }) } diff --git a/api/src/utils/file.ts b/api/src/utils/file.ts index 8013ce1..764be4a 100644 --- a/api/src/utils/file.ts +++ b/api/src/utils/file.ts @@ -9,7 +9,7 @@ export const sysInitCompiledPath = path.join( ) export const sasJSCoreMacros = path.join(apiRoot, 'sasjscore') -export const sasJSCoreMacrosInfo = path.join(apiRoot, 'sasjscore', '.macrolist') +export const sasJSCoreMacrosInfo = path.join(sasJSCoreMacros, '.macrolist') export const getWebBuildFolderPath = () => path.join(codebaseRoot, 'web', 'build') diff --git a/api/src/utils/index.ts b/api/src/utils/index.ts index b99c7bc..76a646e 100644 --- a/api/src/utils/index.ts +++ b/api/src/utils/index.ts @@ -12,6 +12,7 @@ export * from './isDebugOn' export * from './parseLogToArray' export * from './removeTokensInDB' export * from './saveTokensInDB' +export * from './seedDB' export * from './setProcessVariables' export * from './setupFolders' export * from './upload' diff --git a/api/src/utils/seedDB.ts b/api/src/utils/seedDB.ts new file mode 100644 index 0000000..f63233f --- /dev/null +++ b/api/src/utils/seedDB.ts @@ -0,0 +1,35 @@ +import Client from '../model/Client' +import User from '../model/User' + +const CLIENT = { + clientId: 'clientID1', + clientSecret: 'clientSecret' +} +const ADMIN_USER = { + id: 1, + displayName: 'Super Admin', + username: 'secretuser', + password: '$2a$10$hKvcVEZdhEQZCcxt6npazO6mY4jJkrzWvfQ5stdBZi8VTTwVMCVXO', + isAdmin: true, + isActive: true +} + +export const seedDB = async () => { + // Checking if client is already in the database + const clientExist = await Client.findOne({ clientId: CLIENT.clientId }) + if (!clientExist) { + const client = new Client(CLIENT) + await client.save() + + console.log(`DB Seed - client created: ${CLIENT.clientId}`) + } + + // Checking if user is already in the database + const usernameExist = await User.findOne({ username: ADMIN_USER.username }) + if (!usernameExist) { + const user = new User(ADMIN_USER) + await user.save() + + console.log(`DB Seed - admin account created: ${ADMIN_USER.username}`) + } +}