Skip to content

Instantly share code, notes, and snippets.

View kevboutin's full-sized avatar

Kevin Boutin kevboutin

View GitHub Profile
@kevboutin
kevboutin / sampleService.js
Last active June 13, 2024 18:18
The SampleService class
class SampleService {
constructor(connection) {
this.model = connection.models['sample'];
this.connection = connection;
}
async findById(id) {
return this.model.findById(id);
}
}
@kevboutin
kevboutin / sampleFunction.js
Last active June 20, 2024 04:55
This is just a sample Azure function
const mongoose = require('mongoose').set('debug', process.env.NODE_ENV !== 'production');
const SampleService = require('./sampleService');
const DatabaseCache = require('./databaseCache');
// The time-to-live in seconds for each cache entry.
const cacheTtl = process.env.CACHE_TTL || 3600;
// The cache must live in global scope so it remains in memory between invocations.
let databaseCache = new DatabaseCache(cacheTtl);
const sampleFunctionHandler = async (context, req) => {
@kevboutin
kevboutin / databaseCache.js
Last active June 20, 2024 05:16
The DatabaseCache class
const DatabaseService = require('./databaseService');
/**
* Database options.
* @typedef {Object.<string, boolean|number|string>} DatabaseOptions
*/
/**
* A cache entry.
* @typedef {Object} CacheEntry
@kevboutin
kevboutin / databaseService.js
Last active September 8, 2024 16:42
The DatabaseService class
const mongoose = require('mongoose').set('debug', process.env.NODE_ENV !== 'production');
/** @constant {boolean} */
const DB_BUFFER_COMMANDS = process.env.DB_BUFFER_COMMANDS === 'true';
/**
* Database options.
* @typedef {Object.<string, boolean|number|string>} DatabaseOptions
*/
@kevboutin
kevboutin / smsUtility.js
Created April 8, 2024 17:02
SMS Utility using Twilio
const twilio = require('twilio');
const TAG = 'smsUtility';
/**
* Send an SMS text message.
*
* Example response from Twilio:
* {
* "account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
@kevboutin
kevboutin / emailUtility.js
Created April 8, 2024 16:57
SendGrid email utility
const sgMail = require('@sendgrid/mail');
sgMail.setTimeout(40000);
const TAG = 'emailUtility';
const DEFAULT_SENDER = '[email protected]';
/**
* Send an email.
*
@kevboutin
kevboutin / groupArray.js
Created October 17, 2023 15:43
Group an array by object property
const groupBy = (arr, groupFn) => {
const grouped = {};
for (const obj of arr) {
const groupName = groupFn(obj);
if (!grouped[groupName]) {
grouped[groupName] = [];
}
grouped[groupName].push(obj);
}
return grouped;
@kevboutin
kevboutin / reverseString.js
Created October 17, 2023 15:40
Reverse a string
/* The old way:
const reverseString = (str) => {
let reversed = '';
for (let i = str.length - 1; i >= 0; i--) {
const ch = str[i];
reversed += ch;
}
return reversed;
};
const reverse = reverseString('javascript');
@kevboutin
kevboutin / compareArrays.js
Created October 17, 2023 15:38
Compare Arrays
const areEqual = (arr1, arr2) =>
arr1.sort().join(',') === arr2.sort().join(',');
const arr1 = [1, 2, 3, 4];
const arr2 = [3, 4, 1, 2];
const arr3 = [1, 2, 3];
console.log(areEqual(arr1, arr2)); // true
console.log(areEqual(arr1, arr3)); // false
@kevboutin
kevboutin / removeDuplicates.js
Created October 17, 2023 15:37
Remove duplicates from an array
const removeDuplicates = (arr) => [...new Set(arr)];
const arr = [1, 2, 3, 4, 5, 3, 1, 2, 5];
const distinct = removeDuplicates(arr);
console.log(distinct); // [1, 2, 3, 4, 5]