Skip to content

Commit

Permalink
feat: add dotenv and prod and dev environments
Browse files Browse the repository at this point in the history
  • Loading branch information
joxpulp committed Dec 2, 2021
1 parent 104927e commit 671d5a7
Show file tree
Hide file tree
Showing 11 changed files with 29 additions and 22 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "",
"main": "index.js",
"scripts": {
"dev": "nodemon --DAO=MEMORIA",
"dev": "export NODE_ENV=development && nodemon",
"prod": "export NODE_ENV=production && node ./dist/index.js --P=8082",
"start": "node ./dist/index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
Expand Down
4 changes: 2 additions & 2 deletions src/apis/cartapi.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { flags } from '../config/config';
import { CONFIG, flags } from '../config/config';
import { CartFactoryDAO } from '../models/cart/cartfactory';
import { Cart, Products } from '../models/interfaces';

class CartAPI {
private cart;

constructor() {
this.cart = CartFactoryDAO.get(flags.D);
this.cart = CartFactoryDAO.get(CONFIG.DAO);
}

async getProducts(id?: string): Promise<Cart[] | Products[]> {
Expand Down
4 changes: 2 additions & 2 deletions src/apis/messagesapi.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Messages } from '../models/interfaces';
import { flags } from '../config/config';
import { CONFIG, flags } from '../config/config';
import { MessagesFactoryDAO } from '../models/messages/messagesFactory';

class messagesAPI {
private messages;

constructor() {
this.messages = MessagesFactoryDAO.get(flags.D);
this.messages = MessagesFactoryDAO.get(CONFIG.DAO);
}

async getMessages(): Promise<Messages[]> {
Expand Down
4 changes: 2 additions & 2 deletions src/apis/productsapi.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { FactoryDAO } from '../models/products/productfactory';
import { Products, newProductI, ProductQuery } from '../models/interfaces';
import { flags } from '../config/config';
import { CONFIG, flags } from '../config/config';

class prodAPI {
private products;

constructor() {
this.products = FactoryDAO.get(flags.D);
this.products = FactoryDAO.get(CONFIG.DAO);
}

async getProducts(id?: string): Promise<Products[]> {
Expand Down
4 changes: 2 additions & 2 deletions src/baseRepository/mongodb.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Schema, model, connect } from 'mongoose';
import { IRead, IWrite, Messages } from '../models/interfaces';
import CONFIG from '../config/config';
import { CONFIG } from '../config/config';

const messagesCollection = 'mensajes';

Expand All @@ -27,7 +27,7 @@ export abstract class BaseMongo<T> implements IRead<T>, IWrite<T> {
async find(): Promise<Messages[]> {
let outputGet: Messages[] = [];
const product = await this.messages.find();
outputGet.push(...product)
outputGet.push(...product);
return outputGet;
}

Expand Down
11 changes: 7 additions & 4 deletions src/config/config.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import dotenv from 'dotenv';
import args from 'args';
import path from 'path';

args.option('DAO', 'Sets the DB to use')
args.option('PORT', 'SETS THE PORT USING CLI', 8080)
export const flags = args.parse(process.argv);

dotenv.config();
dotenv.config({
path: path.resolve(__dirname, '../../' + process.env.NODE_ENV + '.env')
});

const env = {
export const CONFIG = {
MYSQL_HOST: process.env.MYSQL_HOST || 'urlhost',
MYSQL_USER: process.env.MYSQL_USER || 'user',
MYSQL_PASSWORD: process.env.MYSQL_PASSWORD || 'password',
Expand All @@ -18,6 +21,6 @@ const env = {
FIREBASE_CLIENTEMAIL: process.env.FIREBASE_CLIENTEMAIL || 'clientemail',
FIREBASE_PROJECTID: process.env.FIREBASE_PROJECTID || 'projectid',
FIREBASE_DBURL: process.env.FIREBASE_DBURL || 'dbUrl',
DAO: process.env.DAO || 'ANYTHING',
};

export default env;
5 changes: 2 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { flags } from './config/config';
import Server from './services/server';
import { ioServer } from './services/socket';

const port = process.env.PORT || 8080;

ioServer(Server);
Server.listen(port, () => console.log(`Server running in port: ${port}`));
Server.listen(flags.P, () => console.log(`Server running in port: ${flags.P}`));
Server.on('error', (error) => console.error(`There was an error: ${error}`));
2 changes: 1 addition & 1 deletion src/knexfile.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Update with your config settings.

import CONFIG from './config/config'
import { CONFIG } from './config/config';

export default {
servermysql: {
Expand Down
2 changes: 1 addition & 1 deletion src/models/cart/DAO/mongodb.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Schema, model, connect, Types } from 'mongoose';
import { Products, Cart } from '../../interfaces';
import { productsAPI } from '../../../apis/productsapi';
import CONFIG from '../../../config/config';
import { CONFIG } from '../../../config/config';

const cartSchema = new Schema<Cart>(
{
Expand Down
2 changes: 1 addition & 1 deletion src/models/products/DAO/firebase.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import firebase from 'firebase-admin';
import { newProductI, Products, ProductQuery } from '../../interfaces';
import CONFIG from '../../../config/config';
import { CONFIG } from '../../../config/config';

export class ProductDAOFirebase {
// Private instance of the class to use singleton pattern
Expand Down
10 changes: 7 additions & 3 deletions src/models/products/DAO/mongodb.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Schema, model, connect } from 'mongoose';
import { Products, newProductI, ProductQuery } from '../../interfaces';
import CONFIG from '../../../config/config';
import { CONFIG } from '../../../config/config';

const productsSchema = new Schema<Products>(
{
Expand Down Expand Up @@ -43,7 +43,9 @@ export class ProductDAOMONGO {
);
return this._instanceLocal;
} else {
console.log('Intancia MONGODB LOCAL PRODUCT inicializada por primera vez');
console.log(
'Intancia MONGODB LOCAL PRODUCT inicializada por primera vez'
);
return (this._instanceLocal = new this());
}
}
Expand All @@ -56,7 +58,9 @@ export class ProductDAOMONGO {
);
return this._instanceAtlas;
} else {
console.log('Intancia MONGODB ATLAS PRODUCT inicializada por primera vez');
console.log(
'Intancia MONGODB ATLAS PRODUCT inicializada por primera vez'
);
return (this._instanceAtlas = new this(false));
}
}
Expand Down

0 comments on commit 671d5a7

Please sign in to comment.