Skip to content

Instantly share code, notes, and snippets.

View Avi-E-Koenig's full-sized avatar
🎯
Focusing

Avi E. Koenig Avi-E-Koenig

🎯
Focusing
View GitHub Profile
@Avi-E-Koenig
Avi-E-Koenig / dynamicPoolCreator.js
Created November 22, 2021 14:16
used in a project where every user had a seperate DB....
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]
@Avi-E-Koenig
Avi-E-Koenig / snippet.mysqljs.js
Last active November 14, 2021 14:59
sample sql js snippet
// 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);
});
@Avi-E-Koenig
Avi-E-Koenig / durrp.js
Created November 1, 2021 20:32
Add or create document in nested array mongoose
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',
})
@Avi-E-Koenig
Avi-E-Koenig / printPHPObject.php
Created September 27, 2021 11:35
print an object in php to screen
function printAll($spaces = 0, $payload)
{
$currentIndentation = str_repeat(" ", $spaces);
if (is_string($payload)) {
echo "&nbsp;$payload<br/>";
}
if (is_array($payload)) {
foreach ($payload as $key => $value) {
echo "<br/> $currentIndentation $key: ";
@Avi-E-Koenig
Avi-E-Koenig / dcConn.js
Last active May 17, 2021 22:20
async await Sqlite connection with db file write (if not exists)
const sqlite3 = require('sqlite3').verbose();
const { open } = require('sqlite');
const path = require('path');
const fs = require('fs').promises;
async function checkFileExists(file) {
try {
return await fs.stat(file);
} catch (error) {
export default {
name: 'color-picker',
template: /*html*/ `
<div class="row" style="margin-bottom:0;">
<div
class="input-field col s12"
@click="openColorPicker">
<i class="material-icons prefix">color</i>
<input
@Avi-E-Koenig
Avi-E-Koenig / Crypto_module.js
Last active January 27, 2021 08:31
Encrypt/decrypt with aes
const CryptoJS = require('crypto-js');
const SECRET_KEYB = process.env.SECRET_KEYB;
class CryptoHandler {
constructor() {
if (!SECRET_KEYB || !SECRET_KEYB.length) {
throw new ErrorHandler(500, 'please see env');
}
}
const Logger = require('./logger.service');
const { parse } = require('json2csv');
// opt contain fieldsc
const jsonTocsv = (data, opt) => {
console.log('jsonTocsv -> opt', opt);
console.log('jsonTocsv -> data', data[0]);
try {
if (!data || !data.length) return null;
const csv = parse(data, opt);
//// -- https://dbdiagram.io/d/5e8f12c739d18f5553fd5b60
//// -- LEVEL 1
//// -- Tables and References
// Creating tables
Table users as U {
id int [pk, increment] // auto-increment
fName varchar
lName varchar
// see: https://davidwalsh.name/javascript-proxy-with-storage
function getStorage() {
return new Proxy(window.localStorage, {
// just a helper to see if value is valid json ie stringified obj or array
set: (obj, prop, value) => {
value !== null && typeof value === 'object'
? obj.setItem(prop, JSON.stringify(value))
: obj.setItem(prop, value);
console.log(`${prop} added to localstorage!😀`);
},