Skip to content

Instantly share code, notes, and snippets.

View tdubs42's full-sized avatar
🐘
constantly coding

tdubs tdubs42

🐘
constantly coding
View GitHub Profile
@tdubs42
tdubs42 / Error Catching Block - expressJS
Last active September 8, 2021 21:15
use in express server to feed errors down the pipeline via .catch(next) || next({status: CODE, message: MESSAGE})
/* eslint-disable */
router.use((err, req, res, next) => {
res.status(err.status || 500).json({
fromTheDev: 'mistakes were made',
message: err.message,
stack: err.stack,
})
})
/* eslint-enable */
@tdubs42
tdubs42 / knexfile.js
Last active September 16, 2021 22:02
baseline for knex config file
const sharedConfig = {
client: 'sqlite3',
useNullAsDefault: true,
migrations: {
directory: './data/migrations',
},
seeds: {
directory: './data/seeds',
},
pool: {
@tdubs42
tdubs42 / db-config.js
Last active September 8, 2021 19:12
baseline for db-config file - used with knex
const knex = require('knex')
const configs = require('../knexfile.js')
const environment = process.env.NODE_ENV || 'development'
module.exports = knex(configs[environment])
// always use as first seed file to clean db
const cleaner = require('knex-cleaner')
exports.seed = function (knex) {
return cleaner.clean(knex, {
mode: 'truncate', // resets ids
ignoreTables: [
'knex_migrations',
'knex_migrations_lock'
],
@tdubs42
tdubs42 / package.json
Last active February 19, 2024 01:13
package.json Scripts for Express + Node Backend with Knex and Jest
"scripts": {
"start": "node index.js",
"server": "nodemon index.js",
"migrate": "knex migrate:latest",
"rollback": "knex migrate:rollback",
"seed": "knex seed:run",
"test": "cross-env NODE_ENV=test jest --runInBand --verbose --watch --setTimeout=500"
},
@tdubs42
tdubs42 / js-query.js
Created September 23, 2021 14:44 — forked from samsch/js-query.js
Nested Knex query
knex
.select([
'users.*',
knex.raw('json_agg("posts") as posts')
])
.from('users')
.leftJoin(function () {
this.select(['posts.*', knex.raw('json_agg("comments") as comments')])
.from('posts')
.leftJoin('comments', { 'posts.id': 'comments.post' })
@tdubs42
tdubs42 / reverse-string.js
Last active February 19, 2024 01:31
Reverse String - JavaScript
/*
This challenge is slightly misleading, as it says to reverse a string but inputs an array and expects an
array as output. If you need to return this as a string, use the String.join() method
*/
const reverseString = s => {
const reverseArray = s.reverse()
return reverseArray
};
@tdubs42
tdubs42 / number-of-islands.js
Last active February 19, 2024 01:32
Number of Islands - JavaScript
const numIslands = grid => {
// need variable to count islands
let islands = 0
// create a function to search for 1 and use recursion to search surrounding cells
const findIslands = (row, col, grid) => {
// first check if row and col exist on grid and if grid[row][col] 0
if (
row < 0 ||
col < 0 ||
@tdubs42
tdubs42 / two-sum.js
Last active February 19, 2024 01:32
Two Sum - JavaScript
const twoSum = (nums, target) => {
// given an array, return pair of indices corresponding to numbers in array that equal target
// there is exactly 1 solution per array of nums
// need variable to store indices of solution
const solution = []
// need variable to store count to leverage while loop
let count = 0
@tdubs42
tdubs42 / valid-anagram.js
Last active February 19, 2024 01:31
Valid Anagram - JavaScript
const isAnagram = (s, t) => {
if (s.split('').sort().join('') == t.split('').sort().join('')) {
return true
}
return false
};