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 / 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)) {
@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 / 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 / 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 / rethinkdb_helpers.ex
Last active February 26, 2016 17:17
helpers for rethinkdb elixir
defmodule RethinkDB.Helpers do
def run(query) do
query
|> do_run
|> handle_response
end
def handle_response(%RethinkDB.Exception.ConnectionClosed{}) do
raise "Cannot connect to RethinkDB"
defmodule MyApp.RethinkDB.Helpers do
def run(query, opts) do
query
|> MyApp.Database.run(opts)
|> handle_response
end
def run(query) do
run(query, [])
@sikanhe
sikanhe / textarea_autoheight.js
Last active May 16, 2023 04:28
Simple textarea that expand and shrinks depending on the content
import React, { PropTypes } from 'react'
export default class TextareaAutosize extends React.Component {
static propTypes = {
value: PropTypes.string,
className: PropTypes.string,
style: PropTypes.object,
onChange: PropTypes.func
}
componentDidMount() {
defmodule AWS.S3 do
import AWS.Utils
@config Application.get_env(:vino, __MODULE__)
@allowed_file_types ~w(.jpeg .jpg .png)
def base_url(bucket) do
"https://#{bucket}.s3.amazonaws.com/"
defmodule RethinkDB.Response do
@moduledoc false
def parse(raw_data, token, pid) do
d = Poison.decode!(raw_data)
r = RethinkDB.Pseudotypes.convert_reql_pseudotypes(d["r"])
profile = d["p"]
type = d["t"]
resp = case type do
{
id: "123",
store: {
name: "GAP",
id: "123"
},
name: "Winter Coat",
image_url: string,
sizes: ["small", "large"..],
color: ["green", "blue"]