Skip to content

Commit

Permalink
fix: .env + config import
Browse files Browse the repository at this point in the history
  • Loading branch information
saisilinus committed Apr 7, 2022
1 parent 66122f9 commit d0afb24
Show file tree
Hide file tree
Showing 12 changed files with 27 additions and 29 deletions.
7 changes: 3 additions & 4 deletions dist/config/config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import Joi from 'joi';
import path from 'path';
import dotenv from 'dotenv';
import 'dotenv/config';

dotenv.config({ path: path.join(__dirname, '../../.env') });
const envVarsSchema = Joi.object()
.keys({
NODE_ENV: Joi.string().valid('production', 'development', 'test').required(),
Expand All @@ -29,7 +27,7 @@ const { value: envVars, error } = envVarsSchema.prefs({ errors: { label: 'key' }
if (error) {
throw new Error(`Config validation error: ${error.message}`);
}
export default {
const config = {
env: envVars.NODE_ENV,
port: envVars.PORT,
mongoose: {
Expand Down Expand Up @@ -67,4 +65,5 @@ export default {
from: envVars.EMAIL_FROM,
},
};
export default config;
// # sourceMappingURL=config.js.map
2 changes: 1 addition & 1 deletion dist/config/config.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/modules/errors/error.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable no-unused-vars */
import mongoose from 'mongoose';
import httpStatus from 'http-status';
import config from '../../config/config';
Expand All @@ -15,6 +14,7 @@ export const errorConverter = (err, _req, _res, next) => {
}
next(error);
};
// eslint-disable-next-line no-unused-vars
export const errorHandler = (err, _req, res, _next) => {
let { statusCode, message } = err;
if (config.env === 'production' && !err.isOperational) {
Expand Down
6 changes: 3 additions & 3 deletions dist/modules/token/token.model.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Schema, model } from 'mongoose';
import mongoose from 'mongoose';
import tokenTypes from './token.types';
import toJSON from '../toJSON/toJSON.plugin';

const tokenSchema = new Schema(
const tokenSchema = new mongoose.Schema(
{
token: {
type: String,
Expand Down Expand Up @@ -34,6 +34,6 @@ const tokenSchema = new Schema(
);
// add plugin that converts mongoose to json
tokenSchema.plugin(toJSON);
const Token = model('Token', tokenSchema);
const Token = mongoose.model('Token', tokenSchema);
export default Token;
// # sourceMappingURL=token.model.js.map
2 changes: 1 addition & 1 deletion dist/modules/token/token.model.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions dist/modules/user/user.model.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Schema, model } from 'mongoose';
import mongoose from 'mongoose';
import validator from 'validator';
import bcrypt from 'bcryptjs';
import toJSON from '../toJSON/toJSON.plugin';
import paginate from '../paginate/paginate.plugin';
import { roles } from '../../config/roles';

const userSchema = new Schema(
const userSchema = new mongoose.Schema(
{
name: {
type: String,
Expand Down Expand Up @@ -79,6 +79,6 @@ userSchema.pre('save', async function (next) {
}
next();
});
const User = model('User', userSchema);
const User = mongoose.model('User', userSchema);
export default User;
// # sourceMappingURL=user.model.js.map
2 changes: 1 addition & 1 deletion dist/modules/user/user.model.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ecosystem.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"apps": [
{
"name": "app",
"script": "src/index.ts",
"script": "dist/index.js",
"instances": 1,
"autorestart": true,
"watch": false,
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
"name": "node-express-mongoose-typescript-boilerplate",
"version": "0.0.1",
"description": "Node express mongoose typescript boilerplate",
"main": "index.js",
"main": "dist/index.js",
"repository": "https://github.com/saisilinus/node-express-mongoose-typescript-boilerplate.git",
"author": "wafula saisi <waflinus96@gmail.com>",
"license": "MIT",
"type": "module",
"scripts": {
"start": "pm2 start ecosystem.config.json --no-daemon",
"dev": "cross-env NODE_ENV=development nodemon --watch './**/*.ts' --exec 'ts-node' --files src/index.ts",
"dev": "cross-env NODE_ENV=development nodemon --experimental-modules --es-module-specifier-resolution=node dist/index.js",
"test": "jest -i --colors --verbose --detectOpenHandles",
"test:watch": "jest -i --watchAll",
"coverage": "jest -i --coverage",
Expand Down
9 changes: 4 additions & 5 deletions src/config/config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import Joi from 'joi';
import path from 'path';
import dotenv from 'dotenv';

dotenv.config({ path: path.join(__dirname, '../../.env') });
import 'dotenv/config';

const envVarsSchema = Joi.object()
.keys({
Expand Down Expand Up @@ -33,7 +30,7 @@ if (error) {
throw new Error(`Config validation error: ${error.message}`);
}

export default {
const config = {
env: envVars.NODE_ENV,
port: envVars.PORT,
mongoose: {
Expand Down Expand Up @@ -71,3 +68,5 @@ export default {
from: envVars.EMAIL_FROM,
},
};

export default config;
6 changes: 3 additions & 3 deletions src/modules/token/token.model.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Schema, model } from 'mongoose';
import mongoose from 'mongoose';
import tokenTypes from './token.types';
import toJSON from '../toJSON/toJSON.plugin';
import { ITokenDoc, ITokenModel } from './token.interfaces';

const tokenSchema = new Schema<ITokenDoc, ITokenModel>(
const tokenSchema = new mongoose.Schema<ITokenDoc, ITokenModel>(
{
token: {
type: String,
Expand Down Expand Up @@ -37,6 +37,6 @@ const tokenSchema = new Schema<ITokenDoc, ITokenModel>(
// add plugin that converts mongoose to json
tokenSchema.plugin(toJSON);

const Token = model<ITokenDoc, ITokenModel>('Token', tokenSchema);
const Token = mongoose.model<ITokenDoc, ITokenModel>('Token', tokenSchema);

export default Token;
8 changes: 4 additions & 4 deletions src/modules/user/user.model.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Schema, model, ObjectId } from 'mongoose';
import mongoose from 'mongoose';
import validator from 'validator';
import bcrypt from 'bcryptjs';
import toJSON from '../toJSON/toJSON.plugin';
import paginate from '../paginate/paginate.plugin';
import { roles } from '../../config/roles';
import { IUserDoc, IUserModel } from './user.interfaces';

const userSchema = new Schema<IUserDoc, IUserModel>(
const userSchema = new mongoose.Schema<IUserDoc, IUserModel>(
{
name: {
type: String,
Expand Down Expand Up @@ -62,7 +62,7 @@ userSchema.plugin(paginate);
* @param {ObjectId} [excludeUserId] - The id of the user to be excluded
* @returns {Promise<boolean>}
*/
userSchema.static('isEmailTaken', async function (email: string, excludeUserId: ObjectId): Promise<boolean> {
userSchema.static('isEmailTaken', async function (email: string, excludeUserId: mongoose.ObjectId): Promise<boolean> {
const user = await this.findOne({ email, _id: { $ne: excludeUserId } });
return !!user;
});
Expand All @@ -85,6 +85,6 @@ userSchema.pre('save', async function (next) {
next();
});

const User = model<IUserDoc, IUserModel>('User', userSchema);
const User = mongoose.model<IUserDoc, IUserModel>('User', userSchema);

export default User;

0 comments on commit d0afb24

Please sign in to comment.