Skip to content

Instantly share code, notes, and snippets.

View rafagsiqueira's full-sized avatar

Rafael Guimaraes Siqueira rafagsiqueira

  • United Nations
  • New York
View GitHub Profile
@rafagsiqueira
rafagsiqueira / sendmail.ts
Created January 16, 2021 16:22
Example of sending emails using nodemailer and observable stream
import nodemailer from 'nodemailer';
export class Emailer {
transporter: nodemailer.Transporter;
constructor() {
this.transporter = nodemailer.createTransport({
host: 'mysmtpserver.com.br',
port: 25
});
@rafagsiqueira
rafagsiqueira / getusers.ts
Created January 16, 2021 16:12
Example mssql query to observable stream
import { ConnectionPool } from 'mssql';
export class DatabaseClient {
pool: ConnectionPool;
dbConfig: {redated};
constructor() {
super();
this.pool = new ConnectionPool(this.dbConfig);
}
@rafagsiqueira
rafagsiqueira / index.ts
Created January 16, 2021 14:55
Asynchronous methods with multiple promises and RxJS
getUsers$ = (): Observable<any> => {
if (someCondition) {
return from(someAsyncOperation())
}
else {
return from(someOtherAsyncOperation())
}
}
getUsers2$ = (): Observable<any> => someCondition ? from(someAsyncOperation()) : from(someOtherAsyncOperation())
@rafagsiqueira
rafagsiqueira / index.ts
Last active January 16, 2021 14:58
An asynchronous method and its corresponding observable returning method
getUsers = async () => new Promise((resolve, reject) => {
if (someCondition) {
someAsyncOperation()
.then((result) => resolve(result))
.catch((err) => reject(err))
}
else {
someOtherAsyncOperation()
.then((result) => resolve(result))
.catch((err) => reject(err))
@rafagsiqueira
rafagsiqueira / index.ts
Created January 16, 2021 14:32
Sending emails in parallel with mergeMap
const users$ = getUsers$();
users$.pipe(
filter(user => user.isActive),
mergeMap(user => sendEmail$(user.email))
).subscribe(
(success) => emailsSent++,
(err) => failedEmails++,
() = > console.log('Emails sent:'+emailsSent+' Emails failed:'+failedEmails)
)
@rafagsiqueira
rafagsiqueira / index.ts
Last active January 16, 2021 03:52
Using mergeMap for parallel asynchronous calls
const EMAIL_PARALLELISM = 5;
const users$ = getUsers$();
users$.pipe(
filter(user => user.isActive),
mergeMap(user => sendEmail$(user.email), EMAIL_PARALLELISM)
).subscribe(
(success) => emailsSent++,
(err) => failedEmails++,
() = > console.log('Emails sent:'+emailsSent+' Emails failed:'+failedEmails)
@rafagsiqueira
rafagsiqueira / index.ts
Last active January 16, 2021 14:20
Example of sending emails using a stream of users
let users$ = from(getUsers());
users$.pipe(
filter(user => user.isActive),
concatMap(user => from(sendEmail(user.email)))
).subscribe(
(success) => emailsSent++,
(err) => failedEmails++,
() = > console.log('Emails sent:'+emailsSent+' Emails failed:'+failedEmails)
)
@rafagsiqueira
rafagsiqueira / index.ts
Last active January 16, 2021 02:58
An array into an observable stream
let someArray = [];
someArray = someOtherArray;
const streamOfItems$ = of(someArray).pipe(
mergeAll()
)
// streamOfItems$ will emit each item of the array as an observable through the stream
data "http" "access_token" {
url = "https://${var.docker_registry.host}/oauth2/token?service=${var.docker_registry.host}&scope=repository:*:pull"
request_headers = {
Authorization = "Basic ${base64encode("${var.docker_registry.user}:${var.docker_registry.password}")}"
}
}
data "http" "tips-admin" {
url = "https://${var.docker_registry.host}/v2/tips-admin/manifests/${local.environment}"
request_headers = {
@rafagsiqueira
rafagsiqueira / deployment.tf
Last active November 13, 2020 21:34
Example Terraform Kubernetes deployment
resource "kubernetes_deployment" "tips-admin" {
metadata {
name = "tips-admin"
labels = {
app = "tips-admin"
}
}
spec {
replicas = 1
selector {