This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SampleService { | |
constructor(connection) { | |
this.model = connection.models['sample']; | |
this.connection = connection; | |
} | |
async findById(id) { | |
return this.model.findById(id); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const DatabaseService = require('./databaseService'); | |
/** | |
* Database options. | |
* @typedef {Object.<string, boolean|number|string>} DatabaseOptions | |
*/ | |
/** | |
* A cache entry. | |
* @typedef {Object} CacheEntry |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const twilio = require('twilio'); | |
const TAG = 'smsUtility'; | |
/** | |
* Send an SMS text message. | |
* | |
* Example response from Twilio: | |
* { | |
* "account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const sgMail = require('@sendgrid/mail'); | |
sgMail.setTimeout(40000); | |
const TAG = 'emailUtility'; | |
const DEFAULT_SENDER = '[email protected]'; | |
/** | |
* Send an email. | |
* |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |