Skip to content

Instantly share code, notes, and snippets.

View kevboutin's full-sized avatar

Kevin Boutin kevboutin

View GitHub Profile
@kevboutin
kevboutin / databaseHealth.js
Last active August 26, 2024 03:13
Determines if mongoose connection is good
// As stated before "readyState" is good. "ping" is also good admin utility for doing so as well.
// It will return { ok: 1 } if it can accept commands.
const mongoose = require('mongoose');
// Create the database connection.
const connection = await mongoose.createConnection(
CONNECT_URI,
CONNECT_OPTS
);
{
"Horizontal Spacing" : 1,
"Tags" : [
],
"Ansi 12 Color" : {
"Red Component" : 0.50197118520736694,
"Color Space" : "sRGB",
"Blue Component" : 0.9097973108291626,
"Alpha Component" : 1,
@kevboutin
kevboutin / .vimrc
Created August 14, 2024 21:59
Vim configuration using vim-plug
" Specify Vim-Plug installation folder
call plug#begin('~/.vim/plugged')
Plug 'tpope/vim-sensible'
" Install the Nightfly color scheme plugin
Plug 'zacanger/angr.vim'
Plug 'jacoborus/tender.vim'
Plug 'vim-airline/vim-airline'
Plug 'vim-airline/vim-airline-themes'
@kevboutin
kevboutin / generators.js
Last active July 14, 2024 03:54
Generators are useful as custom iterators...
/*
* Provides a powerful tool for implementing custom iterators and
* simplifying asynchronous workflows. Generators make it easier to
* handle complex iteration logic and asynchronous processes, leading
* to more readable and maintainable code. They can also be used for
* tasks like managing asynchronous operations in a more
* straightforward, linear fashion.
*/
function* objectEntries(obj) {
for (let key of Object.keys(obj)) {
@kevboutin
kevboutin / proxy.js
Created July 14, 2024 03:43
Enforce type constraints and log access attempts
/* The Proxy object allows you to create a proxy for another object,
* enabling you to intercept and redefine fundamental operations such
* as property lookup, assignment, enumeration, function invocation,
* etc. This provides a powerful way to add custom behavior to objects.
*/
const user = {
name: 'John',
age: 30
};
@kevboutin
kevboutin / clusterMode.js
Created July 11, 2024 02:47
Cluster mode example
/* This code utilizes the “cluster” module to create multiple worker processes,
* each handling incoming requests. This approach allows you to scale your
* application horizontally to handle increased traffic. Note that they all use
* the same port.
*/
const cluster = require('cluster');
if (cluster.isMaster) {
// Master process forks worker processes
for (let i = 0; i < numCPUs; i++) {
@kevboutin
kevboutin / cacheRequest.js
Created July 11, 2024 02:43
Cachable request to memory
/* This code utilizes “cacheable-request” to cache the response from the
* specified URL for 60 seconds. Subsequent requests within that timeframe
* will retrieve the data from the cache, reducing API calls and improving
* responsiveness.
*/
const cacheableRequest = require('cacheable-request');
const cachedRequest = cacheableRequest(
requestFn,
{ ttl: 60000 } // Cache for 60 seconds
@kevboutin
kevboutin / workerThread.js
Created July 11, 2024 02:39
Worker thread example
/* This code creates a worker thread from the “long_calculation.js” file. The main
* thread sends data to the worker, which performs the calculation and returns the
* result via the “ message ” event. This approach keeps your main thread responsive
* while intensive tasks run in the background.
*/
const { Worker } = require('worker_threads');
const worker = new Worker('./long_calculation.js');
worker.on('message', (result) => {
@kevboutin
kevboutin / streamlinedOperator.js
Created July 11, 2024 02:37
Streamlined operator example
/* The streamlined operator (“ ?. ”) acts as a nullish coalescing operator. It checks if
* the preceding value is “ null ”or “ undefined ” before attempting to access the next
* property. This simplifies conditional checks and streamlines your code, making it more
* readable and less error-prone.
*/
async function getUser(userId) {
const user = await db.getUser(userId);
// Traditional approach (prone to errors with undefined values)
if (user && user.profile && user.profile.avatarUrl) {
return user.profile.avatarUrl;
@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);
}
}