Skip to content

Instantly share code, notes, and snippets.

View janherich's full-sized avatar

Jan Herich janherich

View GitHub Profile
(when (get contacts from)
(some->> (:data payload)
(filter (comp discovery-exists? :message-id))
(reduce (fn [fx discover]
...)
{})))
@janherich
janherich / realm.cljs
Last active July 30, 2017 06:04
How to perform realm schema changes with optional database migrations
;; While working on the status-react codebase, from time to time, some properties of persisted objects
;; can be added/removed, their semantics can change, or whole new kind of objects need to be persisted
;; (or deleted).
;; In such situation, realm database schema need to be changed and maybe, even migration/s need to be specified.
;; Lets suppose we need to add boolean property `:unremovable?` to all chat objects persisted in realm.
;; First, we need to add new schema to schemas defined in accounts:
(ns status.im.data-store.realm.schemas.account.core
(:require [status.im.data-store.realm.schemas.account.v1.core :as v1]
;; all the other versions between v1 and v(N)
;; require your v(N+1) schema file, in this case, v11
@janherich
janherich / status-im.db.cljs
Last active July 24, 2017 16:46
Database denormalisation
;; Subset of current db structure
{:chat-animations
{"console"
{:parameter-box
{:height 300
:changes-counter 17}}}
:chat-loaded-callbacks
{"console" nil
"wallet" nil
"mailman" nil
@janherich
janherich / message.edn
Last active July 12, 2017 16:36
duplicate message keys
{:message /confirmation-code ,
:command {:command {:description Confirmation code,
:sequential-params true,
:color #7099e6,
:name confirmation-code,
:params [{:name code, :type number}],
:type :response,
:has-handler false,
:hidden? nil,
:owner-id console},
@janherich
janherich / projects.json
Last active June 28, 2017 10:30
projects.json
[{
"id" : 392,
"title" : "Solved R&D - Internal Project",
"description" : "Developing the Solved Platform & Processes",
"topics" : {
"DISCIPLINE" : [ {
"id" : 1,
"name" : "General",
"avatar" : "",
"topicGroup" : "DISCIPLINE",
@janherich
janherich / gist:11006018
Last active August 29, 2015 14:00
Demystifying Clojure
;; common points of confusion for people new to Clojure
;; why core functions apparently change the type of given collection ?
(map inc [1 2 3 4]) ;; when evaluated, prints (2 3 4 5), not [2 3 4 5]
(take 2 {:a 1 :b 2 :c 3}) ;; when evaluated, prints ([:a 1] [:b 2]), not {:a 1 :b 2}
@janherich
janherich / gist:8905254
Last active August 29, 2015 13:56
SQL helper function
(ns db-util
(:require [clojure.string :as str]))
(def ^:private placeholders-for (comp (partial str/join ",") #(repeat % '?) count))
(defn in-statement
"Takes a prepared SQL statement and variable number of arguments, which may be
also collection values. Replace all occurences of IN (?) with spliced out values
such as IN (?,?,?) where number of placeholder characters is the same as count
of elements in corresponding argument which is assumed to be a collection.
@janherich
janherich / gist:8776271
Last active August 29, 2015 13:55
Input pipe channel
(ns async-util.input-pipe
(:require [clojure.core.async.impl.protocols :as impl]
[clojure.core.async :refer [<! >! close! go-loop]]))
(defprotocol InputPipe
(detach* [p]))
(defn input-pipe
"Creates and return input pipe of supplied channel with attached input channel."
[ch ch-i]
@janherich
janherich / gist:8738585
Last active August 29, 2015 13:55
time bound channel
(defn create-cancel-channel
[channel-to-notify timeout-ms]
(let [cancel-channel (async/chan)
timeout-channel (async/timeout timeout-ms)]
(async/go
(let [[_ c] (async/alts! [cancel-channel timeout-channel])]
(when (= c timeout-channel)
(async/close! cancel-channel)
(async/close! channel-to-notify))))
cancel-channel))
@janherich
janherich / gist:8594342
Last active January 4, 2016 08:19
polymorphic finder
(require '[clojure.set :as set])
(require '[clojure.java.jdbc :as j])
(def mysql-db {:subprotocol "mysql"
:subname "//127.0.0.1:3306/clojure_test"
:user "clojure_test"
:password "clojure_test"})
(defn product-service [key]
(println (str "Finding product with key: " key))