Skip to content

Instantly share code, notes, and snippets.

View Kostanos's full-sized avatar

Kostya Kostyushko Kostanos

View GitHub Profile
@Kostanos
Kostanos / useRawParams.js
Created July 16, 2022 17:56
Getting RAW (without URL decode) params from react-router-dom.
import { useContext, useMemo } from 'react';
// eslint-disable-next-line camelcase
import { UNSAFE_RouteContext, useParams } from 'react-router-dom';
/**
* Temporary workaround to get RAW params from react-router-dom v.6
* Should hanndle most of the simple cases like /:myVar/someVar/:mySecondVar
* @returns {Object} The same as useParams from react-router-dom but returns RAW (not URL decoded) paramters
*/
export default function useRawParams() {
@Kostanos
Kostanos / GravyQeury.sql
Created July 20, 2022 12:02
Select Group with Limit 1 Speed
-- slitely adapted to PostgresQL
SELECT
id,
category_title,
(array_agg(product_title))[1]
FROM
(SELECT c.id, c.title AS category_title, p.id AS product_id, p.title AS product_title
FROM categories AS c
JOIN products AS p ON c.id = p.category_id
ORDER BY c.id ASC) AS a
@Kostanos
Kostanos / insertOrUpdate.js
Created May 23, 2021 09:37
Knex Postgress ON CONFLICT (ON DUPLICATE KEY) insert
// Inspired by https://github.com/knex/knex/issues/701#issuecomment-314818463
// Will converts also CamelCase fields to SnakeCase
const knex = require('./pgAdaptor');
const camelToSnakeCase = str => str.replace(/[A-Z]/g, letter => `_${letter.toLowerCase()}`);
function insertOrUpdate(tableName, data, conflictKey) {
const updateTxt = Object.getOwnPropertyNames(data)
.filter(f => f !== conflictKey)
.map(field => `${camelToSnakeCase(field)}=:${field}`).join(', ');
@Kostanos
Kostanos / OpenWRT.OpenVPN.md
Last active January 21, 2021 05:16
OpenWRT PandoraBox OpenVPN
@Kostanos
Kostanos / Dockerfile
Last active June 20, 2020 11:25
Creates multiple databases with postgresql docker image
FROM postgres:12-alpine
LABEL owner="Kostya Kostyushko"
# Custom initialization scripts
COPY ./create_user.sh /docker-entrypoint-initdb.d/10-create_user.sh
COPY ./create_db.sh /docker-entrypoint-initdb.d/20-create_db.sh
COPY ./dbInit.sql /dbInit.sql
COPY ./create_db_schemas.sh /docker-entrypoint-initdb.d/20-create_db_schemas.sh
@Kostanos
Kostanos / json-to-csv.php
Created May 24, 2013 03:28
Convert JSON array file to CSV. Use the array keys as the first row. Command line using: json-to-csv.php json.filename > csv.filename
#!/usr/bin/php
<?php
/*
* Convert JSON file to CSV and output it.
*
* JSON should be an array of objects, dictionaries with simple data structure
* and the same keys in each object.
* The order of keys it took from the first element.
*
* Example:
@Kostanos
Kostanos / __init___test.py
Created December 30, 2017 16:37
python Settings class test
from . import Settings, cleanCache
import unittest
from unittest.mock import patch
import os
from pyfakefs import fake_filesystem, fake_filesystem_unittest
from dotmap import DotMap
# import pyfakefs.fake_filesystem_ as fake_glob
# Faking filesystem to test .yaml files
@Kostanos
Kostanos / Dockerfile
Last active September 17, 2017 21:48
debugColorsTest
FROM node:8-alpine
WORKDIR /root/app
CMD npm start
#!/usr/bin/php
<?php
/*
* Converts CSV to JSON
* Example uses Google Spreadsheet CSV feed
* csvToArray function I think I found on php.net
*/
/*
* Using of script in command line:
* ./csv-to-json.php csv.file.name.or.url > json.file.name