Instantly share code, notes, and snippets.

View nodejs-delay.js
const someFunction = async () => {
try {
// wait 5 sec before sending the response
await new Promise(resolve => setTimeout(() => resolve(), 5000));
return res.json({response: 'here'})
} catch (error) {
// return error
}
}
View query-with-count-mongoose.js
Site.aggregate()
.lookup({
from: 'pages',
localField: '_id',
foreignField: 'siteID',
as: 'pages',
})
.addFields({ pageCount: { $size: '$pages' } }) // add pageCount field
.project({ pages: 0 }) // exclude pages array
.exec();
View rounding.java
/** round n down to nearest multiple of m */
long roundDown(long n, long m) {
return n >= 0 ? (n / m) * m : ((n - m + 1) / m) * m;
}
/** round n up to nearest multiple of m */
long roundUp(long n, long m) {
return n >= 0 ? ((n + m - 1) / m) * m : (n / m) * m;
}
View webdev_online_resources.md
View echo-json-problem.go
package main
import (
"net/http"
"github.com/labstack/echo"
"github.com/labstack/echo/engine/fasthttp"
)
func main() {
View Makefile
#
# Makefile to perform "live code reloading" after changes to .go files.
#
# n.b. you must install fswatch (OS X: `brew install fswatch`)
#
# To start live reloading run the following command:
# $ make serve
#
# binary name to kill/restart
View gist:2febd72fc82278a447b7

dokku postgresql rsync nightly backup

This is a rough guide to automating a nightly 7-day rolling remote backup of your PostgreSQL database(s) for your dokku app(s). There is a slight difference if you are using an older version of Dokku.

This will save the comprssed output of pg_dump as well as the contents of the /home directory on your dokku machine (just in case).

You will need to create a shell script in /root and edit the cron file.

View pg_drop_all_tables.sql
-- Schema: public
DROP SCHEMA public CASCADE;
DROP SCHEMA retail CASCADE;
DROP SCHEMA logs CASCADE;
CREATE SCHEMA public;
GRANT ALL ON SCHEMA public TO postgres;
GRANT ALL ON SCHEMA public TO public;
COMMENT ON SCHEMA public