This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) { |
NewerOlder