Skip to content

Instantly share code, notes, and snippets.

View sikanhe's full-sized avatar
😈
demon time

Sikan He sikanhe

😈
demon time
View GitHub Profile
@sikanhe
sikanhe / rethinkdb-elixir-helpers.ex
Created February 12, 2016 05:17
rethinkdb-elixir helpers
defmodule App.Helpers do
use Timex
import App.Rethink, only: [run: 1]
alias RethinkDB.Query
def get(table, id) when is_bitstring(id) do
Query.table(table)
|> Query.get(id)
@sikanhe
sikanhe / improved_cache.js
Last active November 20, 2017 21:45
Most efficient way to store domain state in Redux (Indexed Key-Value Store)
//After working on the backend for a while, I started to continue working on my front end,
//I revisited my redux “cache” store and I made this improvement that made my life a lot easier.
//In my project for example, some collections i need to get the document by id, some by slug, some by username, and etc.
//If I need to account for those cases I had to make new action creators/types, for different entities.
//This is very frustrating as it makes the code less dry and leads to more boilerplate.
//Then what I realized was, I can just pass a “index” option for my insert action creators!(which defaults to “id")
//Actions
@sikanhe
sikanhe / rethink_query_wrapper.ex
Created January 3, 2016 23:39
Functions to wrap commonly used rehinkdb queries with error handling
defmodule App.Query do
import App.Database, only: [run: 1]
alias RethinkDB.Query
def get(table, id) when is_bitstring(id) do
Query.table(table)
|> Query.get(id)
|> run
|> catch_errors
|> handle_get_response
@sikanhe
sikanhe / bulkinsert.js
Last active March 5, 2016 00:40
bulk insert into minimongo
export function insertBulk(collection, documents) {
if(collection) {
const last = _.last(documents);
_.each(documents, item => {
if(_.isObject(item)) {
if (EJSON.equals(item._id, last._id)) {
collection.upsert({ _id: last._id}, item);
}
else {
if(_.isObject(item._id)) {