Last active
          September 4, 2019 22:49 
        
      - 
      
- 
        Save Just1B/cabd77d78678f6ef65088a1d6e9993b9 to your computer and use it in GitHub Desktop. 
    MongoDB docker
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | const mongoConnect = async () => { | |
| const dbUrl = `mongodb://${process.env.APP_MONGO_USER}:${process.env.APP_MONGO_PASS}@${ | |
| process.env.APP_MONGO_HOST | |
| }:${process.env.APP_MONGO_PORT}/${process.env.APP_MONGO_DB}`; | |
| await mongoose | |
| .connect(dbUrl, { | |
| useNewUrlParser: true, | |
| autoReconnect: true, | |
| }) | |
| .then( | |
| () => { | |
| console.log('\n ๐ Connected to Mongo Instance ๐\n'); | |
| }, | |
| async (err: any) => { | |
| console.log('MongoDB err : ' + err); | |
| process.exit(0); | |
| }, | |
| ); | |
| (await env) === 'development' | |
| ? mongoose.set('debug', (collectionName, method, query, doc) => { | |
| console.log(`\n${collectionName}.${method}`, JSON.stringify(query), doc, '\n'); | |
| }) | |
| : mongoose.set('debug', false); | |
| }; | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | version: "3.1" | |
| services: | |
| mongodb: | |
| image: mongo:latest | |
| restart: always | |
| environment: | |
| MONGO_INITDB_DATABASE: ${APP_MONGO_DB} | |
| MONGO_INITDB_ROOT_USERNAME: ${MONGO_INITDB_ROOT_USERNAME} | |
| MONGO_INITDB_ROOT_PASSWORD: ${MONGO_INITDB_ROOT_PASSWORD} | |
| APP_MONGO_USER: ${APP_MONGO_USER} | |
| APP_MONGO_PASS: ${APP_MONGO_PASS} | |
| APP_MONGO_DB: ${APP_MONGO_DB} | |
| APP_MONGO_PORT: ${APP_MONGO_PORT} | |
| volumes: | |
| - ./data/db:/data/db | |
| - ./mongo/:/docker-entrypoint-initdb.d/ | |
| ports: | |
| - 27017:27017 | |
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | export interface CategoryInterface { | |
| uuid: string; | |
| title: string; | |
| slug: string; | |
| deals: number; | |
| header_description: string; | |
| full_description: string; | |
| thumb_uri: string; | |
| file_name: string; | |
| created_at: Date; | |
| updated_at: Date; | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | import { Document, Schema, Model, model } from 'mongoose'; | |
| import { CategoryInterface } from '../helpers/interface'; | |
| import * as uuid from 'uuid'; | |
| interface CategoryModel extends CategoryInterface, Document {} | |
| const CategorySchema: Schema = new Schema( | |
| { | |
| uuid: { type: String, default: uuid.v4 }, | |
| title: { type: String }, | |
| slug: { type: String, unique: true }, | |
| deals: { type: Number, default: 0 }, | |
| header_description: { type: String }, | |
| // eslint-disable-next-line @typescript-eslint/camelcase | |
| full_description: { type: String }, | |
| // eslint-disable-next-line @typescript-eslint/camelcase | |
| thumb_uri: { type: String }, | |
| // eslint-disable-next-line @typescript-eslint/camelcase | |
| file_name: { type: String }, | |
| }, | |
| { | |
| collection: 'categories', | |
| timestamps: { createdAt: 'created_at', updatedAt: 'updated_at' }, | |
| }, | |
| ); | |
| export const Category: Model<CategoryModel> = model<CategoryModel>('categories', CategorySchema); | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | #!/usr/bin/env bash | |
| echo ' | |
| ************************************************** | |
| CREATION OF THE APPLICATION USER AND DB | |
| ************************************************** | |
| '; | |
| mongo ${APP_MONGO_DB} \ | |
| --host localhost \ | |
| --port ${APP_MONGO_PORT} \ | |
| -u ${MONGO_INITDB_ROOT_USERNAME} \ | |
| -p ${MONGO_INITDB_ROOT_PASSWORD} \ | |
| --authenticationDatabase admin \ | |
| --eval "db.createUser({user: '${APP_MONGO_USER}', pwd: '${APP_MONGO_PASS}', roles:[{role:'dbOwner', db: '${APP_MONGO_DB}'}]});" | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment