Skip to content

Instantly share code, notes, and snippets.

View SamanShafigh's full-sized avatar

Saman Shafigh SamanShafigh

View GitHub Profile
import Kernel from './kernel';
import * as ServiceBuilder from './service';
(async () => {
const kernel = new Kernel(ServiceBuilder);
await kernel.boot();
await kernel.container.get('db').save('product', {}, {id: 1, name: 'ssssscar'});
})();
export default class Container {
constructor(builder) {
this.services = new Map();
if (builder !== undefined) {
Object.keys(builder).forEach(name => this.register(name, builder[name]));
}
}
/**
* Register a service
import Container from './container';
export default class Kernel {
constructor(builder) {
this.container = new Container(builder);
}
/**
* Boot the kernel, load all services
*/
import {MongoClient} from 'mongodb';
import UserController from './controller/user-controller';
import UserService from './service/user-service'
import MongoDriver from './service/db-service';
import Config from './service/config-service';
export const config = {
constructor: () => new Config(process.env),
tags: ['boot']
}
@SamanShafigh
SamanShafigh / async-await.js
Created July 19, 2018 03:06
ES6 async/await
// We can also use async/await which is a Syntactic Sugar For Promises
var addC = function (a, b) {
return new Promise((resolve, reject) => {
setTimeout (() => {
resolve(a + b);
}, 0);
});
};
// Async using promise
var addB = function (a, b) {
return new Promise((resolve, reject) => {
setTimeout (() => {
resolve(a + b);
}, 0);
});
};
// How to use it :D
@SamanShafigh
SamanShafigh / async-callback.js
Created July 19, 2018 03:01
async using callback
// Async using call back
var addA = function (a, b, cb) {
setTimeout (() => {
cb(a + b);
}, 0);
};
// How to use it :D
addA(2, 3, (result) => {
console.log(result)
@SamanShafigh
SamanShafigh / anti-patterns.js
Last active October 30, 2018 23:45
Asynchronous AntiPatterns
// :)
// Let say you can perform these 3 actions in parallel, then you can run them
// all at once and aggregate the result which is good
var p1 = addB(2, 3)
var p2 = addB(2, 3)
var p3 = addB(2, 3)
Promise.all([p1, p2, p3]).then(([r1, r2, r3]) => {
console.log(r1, r2, r3)
})
@SamanShafigh
SamanShafigh / main.go
Created June 14, 2018 02:07
Sample hello world Lambda GO
package main
import "github.com/aws/aws-lambda-go/lambda"
type Event struct {
Payload string `json:"payload"`
}
func HandleRequest(e Event) (Event, error) {
return Event{"Hello World"}, nil
@SamanShafigh
SamanShafigh / step-function
Created June 10, 2018 02:25
step-function config
{
"Comment": "A step function to test some lambda functions in Go",
"StartAt": "Step1",
"States": {
"Step1": {
"Type": "Task",
"Resource": "arn:aws:lambda:ap-southeast-2:xyz:function:lambda-f1",
"Next": "Step2"
},
"Step2": {