Skip to content

Instantly share code, notes, and snippets.

@belsrc
belsrc / add-week-days.js
Created May 10, 2018
Simple add week day
View add-week-days.js
const addWeekDays = (date, days) => {
const next = new Date(date);
next.setDate(date.getDate() + days);
return next.getDay() % 6 ?
next :
addWeekDays(next, 1);
}
// const now = new Date(); >> 2018-05-09T21:20:20.082Z
View string-transforms.js
const isString = value => typeof value === 'string' || value instanceof String;
/*
camelCase('camel case'); // camelCase
camelCase('camel_case'); // camelCase
camelCase('camel-case'); // camelCase
*/
const camelCase = str => {
if(!isString(str)) {
return '';
View gist:38cb0207c71f42e962b9865d81f6b76a
suite() - Test group
test() - Individual test
setup() - Called before each test
teardown() - Called after each test
suiteSetup() - Called before all tests in the suite
View extendable-error.js
/**
* ExtendableError class.
* @module
*/
class ExtendableError extends Error {
constructor(message) {
super(message);
// Extending Error is weird and you need to make the props non-enumerable
// or you will get annoying logs
@belsrc
belsrc / gist:8623ba3da74f855ac13b32ee2941b1de
Last active May 24, 2017
Mongo Unique Created At Days
View gist:8623ba3da74f855ac13b32ee2941b1de
db.getCollection('collection').aggregate([
{ $match: { $and: [
{ created_at: { $exists: true } },
{ created_at: { $ne: null } }
] } },
{ $project: {
month: { $month: "$created_at" },
year: { $year: "$created_at" },
day: { $dayOfMonth: "$created_at" } } },
{ $group: { _id: { "year": "$year", "month": "$month", "day": "$day" } } },
@belsrc
belsrc / app.vue
Created Feb 20, 2017
Vue 2 Event Bus
View app.vue
<template>
</template>
<script>
import { mapState, mapGetters, mapActions } from 'vuex';
import * as types from './../store/action_types';
export default {
components: { },
@belsrc
belsrc / performance_check.js
Last active Dec 5, 2016
Check Mongo query performance
View performance_check.js
/*
db.setProfilingLevel(2) Needs to be ran first
0 - Off
1 - Only slow
2 - all
This enables query profiling, after enabling profiling, run the application for a bit to get some data.
*/
db.getCollection('system.profile')
.find({
View postgres_index_sizes.sql
-- Line 9 equals indexes
SELECT nspname || '.' || relname AS "relation",
pg_size_pretty(pg_total_relation_size(C.oid)) AS "total_size"
FROM pg_class C
LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
WHERE nspname NOT IN ('pg_catalog', 'information_schema')
AND C.relkind = 'i'
AND nspname !~ '^pg_toast'
View postgres_table_sizes.sql
-- Line 9 ignores indexes
SELECT nspname || '.' || relname AS "relation",
pg_size_pretty(pg_total_relation_size(C.oid)) AS "total_size"
FROM pg_class C
LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
WHERE nspname NOT IN ('pg_catalog', 'information_schema')
AND C.relkind != 'i'
AND nspname !~ '^pg_toast'
@belsrc
belsrc / postgres_database_size.sql
Created Jan 12, 2016
Size of databases in cluster (Postgres)
View postgres_database_size.sql
SELECT d.datname AS Name, pg_catalog.pg_get_userbyid(d.datdba) AS Owner,
CASE WHEN pg_catalog.has_database_privilege(d.datname, 'CONNECT')
THEN pg_catalog.pg_size_pretty(pg_catalog.pg_database_size(d.datname))
ELSE 'No Access'
END AS SIZE
FROM pg_catalog.pg_database d
ORDER BY
CASE WHEN pg_catalog.has_database_privilege(d.datname, 'CONNECT')
THEN pg_catalog.pg_database_size(d.datname)
ELSE NULL