Skip to content

Instantly share code, notes, and snippets.

View crizstian's full-sized avatar

Cristian Ramirez crizstian

View GitHub Profile
@crizstian
crizstian / solid.js
Last active March 15, 2023 02:40
Code examples of SOLID principles for JavaScript
/*
Code examples from the article: S.O.L.I.D The first 5 priciples of Object Oriented Design with JavaScript
https://medium.com/@cramirez92/s-o-l-i-d-the-first-5-priciples-of-object-oriented-design-with-javascript-790f6ac9b9fa#.7uj4n7rsa
*/
const shapeInterface = (state) => ({
type: 'shapeInterface',
area: () => state.area(state)
})
@crizstian
crizstian / movies.raml
Last active January 23, 2017 16:09
Movie API service example
#%RAML 1.0
title: cinema
version: v1
baseUri: /
types:
Movie:
properties:
id: string
title: string
@crizstian
crizstian / movie-service-repo.js
Last active January 23, 2017 01:38
Example of a repository
// repository.js
'use strict'
// factory function, that holds an open connection to the db,
// and exposes some functions for accessing the data.
const repository = (db) => {
// since this is the movies-service, we already know
// that we are going to query the `movies` collection
// in all of our functions.
const collection = db.collection('movies')
@crizstian
crizstian / movie-service-server.js
Last active January 21, 2017 21:25
Example of express server app
'use strict'
const express = require('express')
const morgan = require('morgan')
const helmet = require('helmet')
const movieAPI = require('../api/movies')
const start = (options) => {
return new Promise((resolve, reject) => {
// we need to verify if we have a repository added and a server port
if (!options.repo) {
@crizstian
crizstian / movies-service-movies.js
Last active January 22, 2017 20:46
Example of movie service API
'use strict'
const status = require('http-status')
module.exports = (app, options) => {
const {repo} = options
// here we get all the movies
app.get('/movies', (req, res, next) => {
repo.getAllMovies().then(movies => {
res.status(status.OK).json(movies)
@crizstian
crizstian / movie-service-movie.spec.js
Created January 22, 2017 20:52
Example of unit test with mocha
/* eslint-env mocha */
const request = require('supertest')
const server = require('../server/server')
describe('Movies API', () => {
let app = null
let testMovies = [{
'id': '3',
'title': 'xXx: Reactivado',
'format': 'IMAX',
@crizstian
crizstian / mongodb-replicaset.js
Last active January 22, 2017 21:35
Example of how to connect to a mongodb 3.4 replica set from nodejs
const MongoClient = require('mongodb')
// here we create the url connection string that the driver needs
const getMongoURL = (options) => {
const url = options.servers
.reduce((prev, cur) => prev + `${cur.ip}:${cur.port},`, 'mongodb://')
return `${url.substr(0, url.length - 1)}/${options.db}`
}
// simple configuration file
// database parameters
const dbSettings = {
db: process.env.DB || 'movies',
user: process.env.DB_USER || 'cristian',
pass: process.env.DB_PASS || 'cristianPassword2017',
repl: process.env.DB_REPLS || 'rs1',
servers: (process.env.DB_SERVERS) ? process.env.DB_SERVERS.split(' ') : [
'192.168.99.100:27017',
@crizstian
crizstian / movie-service-index.js
Created January 22, 2017 23:17
Example for booting movie-service
'use strict'
// we load all the depencies we need
const {EventEmitter} = require('events')
const server = require('./server/server')
const repository = require('./repository/repository')
const config = require('./config/')
const mediator = new EventEmitter()
// verbose logging when we are starting the server
console.log('--- Movies Service ---')
@crizstian
crizstian / movies-service-integration.test.js
Last active February 11, 2017 04:59
Example of an integration test
/* eslint-env mocha */
const supertest = require('supertest')
describe('movies-service', () => {
const api = supertest('http://192.168.99.100:3000')
it('returns a 200 for a collection of movies', (done) => {
api.get('/movies/premiers')