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 Cart = require('../models/cart.model') | |
// returns ObjectId or null | |
const { parseObjectId } = require('../utils/validateObjectId') | |
async function addOrCreateNestedItem(newItem) { | |
try { | |
const cart = await Cart.findOne( | |
{ | |
_id: '618018504bb0eb2bdf6f2955', | |
}) |
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
// server.js | |
process | |
.on('unhandledRejection', (reason, p) => { | |
Logger.error('Unhandled Rejection at Promise', { reason, p }); | |
}) | |
.on('uncaughtException', (err) => { | |
Logger.error('Uncaught Exception thrown', err); | |
process.exit(1); | |
}); |
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 mysql = require('mysql2/promise'); | |
const Logger = require('./SqlLogger.service'); | |
const dbPool = require('./authDb.service') | |
const _DB_POOL_LIST = {} | |
async function _createPool({ db_id, db_host, db_name, db_user, db_password }) { | |
try { | |
let pool = _DB_POOL_LIST[db_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
// the logger to sql class....based on winston-mysql | |
/** | |
* This is a MySQL transport module for winston. | |
* https://github.com/winstonjs/winston | |
* I made some customizations | |
*/ | |
const Transport = require('winston-transport'); | |
const MySql = require('mysql2'); |
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 Joi = require('joi'); | |
const Logger = require('../services/logger.service'); | |
const mysqlEscapeStrings = (value, helper) => { | |
const validMysqlChars = /[0-9,a-z,A-Z$_]/ | |
if (value.match(validMysqlChars)) { | |
return value; | |
} | |
return helper.message(`${value} contains illegal characters`) |
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 arrayMapper = (array, uniqueID) => array.reduce((acc, obj, index) => { | |
if (!obj?.[uniqueID]) { | |
console.error("🚀 ~ file: doodle.js ~ line 6 ~ arrayMapper ~ ", { obj, uniqueID, index }) | |
throw new Error('uniqueId no found') | |
} | |
acc[obj[uniqueID]] = obj | |
return acc; | |
}, {}) | |
function uuidv4() { |
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 express = require('express') | |
const app = express() | |
const port = 3000 | |
const catchErrors = (requestHandler) => { | |
return async (req, res, next) => { | |
try { | |
throw 'oops' | |
return await requestHandler(req, res, next); | |
} catch (error) { |
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 main = async () => { | |
try { | |
const resultSet = fetchYearRange(2022, 2052) | |
console.log("🚀 ~ file: main.js ~ line 24 ~ merged ~ resultSet", JSON.stringify(resultSet)) | |
// copy and do stuff. | |
console.log("🚀 ~ file: main.js ~ line 7 ~ main ~ resultSet", resultSet) | |
// save this to file if you're less lazy then me :) |
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
/** | |
* Takes a string input | |
* @param {String} str | |
* @returns {String} The first not repeated character or empty string | |
*/ | |
const firstNonRepeatingLetter = (str) => { | |
function isUnique(char) { | |
let charCount = 0; | |
for (let i = 0; i < str.length; i++) { |
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 baseArr = [[1, 2], [{ foo: 'bar' }]] | |
const reducer1 = (keepRefs) => (acc, val, i) => { | |
if (keepRefs) { | |
acc[i] = val | |
return acc | |
} | |
try { | |
acc[i] = JSON.parse(JSON.stringify(val)) |