Skip to content

Instantly share code, notes, and snippets.

@Just1B
Last active September 4, 2019 22:49
Show Gist options
  • Save Just1B/cabd77d78678f6ef65088a1d6e9993b9 to your computer and use it in GitHub Desktop.
Save Just1B/cabd77d78678f6ef65088a1d6e9993b9 to your computer and use it in GitHub Desktop.
MongoDB docker
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);
};
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
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;
}
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);
#!/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