Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Implement user resolvers, db config, and transit config.
- Loading branch information
Showing
17 changed files
with
280 additions
and
33 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
FROM flyway/flyway:6.3.2-alpine | ||
|
||
COPY migrations /flyway/sql | ||
|
||
ENTRYPOINT ["/bin/sh"] | ||
|
||
CMD ["-c", "/flyway/flyway -url=jdbc:postgresql://${POSTGRES_HOSTNAME}:${POSTGRES_PORT}/${POSTGRES_DB} -user=${POSTGRES_USER} -password=${POSTGRES_PASSWORD} -connectRetries=60 migrate"] |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
version: "3.7" | ||
services: | ||
postgres: | ||
image: postgres:12.2-alpine | ||
restart: always | ||
environment: | ||
POSTGRES_PASSWORD: password | ||
ports: | ||
- "15432:5432" | ||
volumes: | ||
- db_data:/var/lib/postgresql/data | ||
volumes: | ||
db_data: |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
CREATE EXTENSION IF NOT EXISTS pgcrypto WITH SCHEMA public; | ||
|
||
CREATE TABLE "user" ( | ||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(), | ||
email text NOT NULL UNIQUE, | ||
auth0_id text NOT NULL UNIQUE, | ||
created_at timestamp with time zone DEFAULT now() NOT NULL | ||
); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,7 @@ | ||
{:port #long #or [#env PORT 3000]} | ||
{:database-spec {:username #or [#env POSTGRES_USER "postgres"] | ||
:password #or [#env POSTGRES_PASSWORD "password"] | ||
:server-name #or [#env POSTGRES_HOSTNAME "localhost"] | ||
:port-number #long #or [#env POSTGRES_PORT 15432] | ||
:database-name #or [#env POSTGRES_DB "postgres"] | ||
:sslmode #or [#env POSTGRES_SSLMODE "disable"]} | ||
:port #long #or [#env PORT 3000]} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#!/bin/bash | ||
|
||
pushd "$(git rev-parse --show-toplevel)" | ||
|
||
docker build \ | ||
-f Dockerfile.flyway \ | ||
-t cpodonnell/mygiftlist-blog:migrate-local . | ||
|
||
docker run --rm \ | ||
--network mygiftlist-blog_default \ | ||
-e POSTGRES_USER=postgres \ | ||
-e POSTGRES_PASSWORD=password \ | ||
-e POSTGRES_HOSTNAME=postgres \ | ||
-e POSTGRES_PORT=5432 \ | ||
-e POSTGRES_DB=postgres \ | ||
cpodonnell/mygiftlist-blog:migrate-local | ||
|
||
popd |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/bin/bash | ||
|
||
docker-compose exec -u postgres postgres psql |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,16 @@ | ||
(ns rocks.mygiftlist.application | ||
(:require [com.fulcrologic.fulcro.application :as app] | ||
[com.fulcrologic.fulcro.rendering.keyframe-render2 :as keyframe-render2] | ||
[com.fulcrologic.fulcro.networking.http-remote :as http-remote])) | ||
[com.fulcrologic.fulcro.networking.http-remote :as http-remote] | ||
[rocks.mygiftlist.transit :as transit])) | ||
|
||
(defonce SPA (app/fulcro-app {:optimized-render! keyframe-render2/render! | ||
:remotes {:remote (http-remote/fulcro-http-remote {})}})) | ||
(defonce SPA | ||
(app/fulcro-app | ||
{:optimized-render! keyframe-render2/render! | ||
:remotes {:remote (http-remote/fulcro-http-remote | ||
{:request-middleware | ||
(http-remote/wrap-fulcro-request | ||
identity transit/write-handlers) | ||
:response-middleware | ||
(http-remote/wrap-fulcro-response | ||
identity transit/read-handlers)})}})) |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
(ns rocks.mygiftlist.db | ||
(:require [rocks.mygiftlist.config :as config] | ||
[mount.core :refer [defstate]] | ||
[hikari-cp.core :as pool] | ||
[next.jdbc :as jdbc] | ||
[next.jdbc.result-set :as result-set] | ||
[next.jdbc.prepare :as p] | ||
[clojure.string :as str] | ||
[honeysql.core :as sql] | ||
honeysql-postgres.format)) | ||
|
||
(def datasource-options | ||
(merge {:auto-commit true | ||
:read-only false | ||
:connection-timeout 30000 | ||
:validation-timeout 5000 | ||
:idle-timeout 600000 | ||
:max-lifetime 1800000 | ||
:minimum-idle 10 | ||
:maximum-pool-size 10 | ||
:pool-name "db-pool" | ||
:adapter "postgresql" | ||
:register-mbeans false} | ||
config/database-spec)) | ||
|
||
(defstate pool | ||
:start (pool/make-datasource datasource-options) | ||
:stop (pool/close-datasource pool)) | ||
|
||
(defn- qualify | ||
"Given a kebab-case database table name, returns the namespace that | ||
attributes coming from that table should have." | ||
[table] | ||
(when (seq table) | ||
(str "rocks.mygiftlist.type." table))) | ||
|
||
(defn- snake->kebab [s] | ||
(str/replace s #"_" "-")) | ||
|
||
(defn- as-qualified-kebab-maps [rs opts] | ||
(result-set/as-modified-maps rs | ||
(assoc opts | ||
:qualifier-fn (comp qualify snake->kebab) | ||
:label-fn snake->kebab))) | ||
|
||
(def ^:private query-opts {:builder-fn as-qualified-kebab-maps}) | ||
|
||
(defn execute! [conn sql-map] | ||
(jdbc/execute! conn | ||
(sql/format sql-map :quoting :ansi) | ||
query-opts)) | ||
|
||
(defn execute-one! [conn sql-map] | ||
(jdbc/execute-one! conn | ||
(sql/format sql-map :quoting :ansi) | ||
query-opts)) | ||
|
||
(extend-protocol result-set/ReadableColumn | ||
|
||
;; Automatically convert java.sql.Array into clojure vector in query | ||
;; results | ||
java.sql.Array | ||
(read-column-by-label ^clojure.lang.PersistentVector | ||
[^java.sql.Array v _] | ||
(vec (.getArray v))) | ||
(read-column-by-index ^clojure.lang.PersistentVector | ||
[^java.sql.Array v _2 _3] | ||
(vec (.getArray v))) | ||
|
||
;; Output java.time.LocalDate instead of java.sql.Date in query | ||
;; results | ||
java.sql.Date | ||
(read-column-by-label ^java.time.LocalDate | ||
[^java.sql.Date v _] | ||
(.toLocalDate v)) | ||
(read-column-by-index ^java.time.LocalDate | ||
[^java.sql.Date v _2 _3] | ||
(.toLocalDate v)) | ||
|
||
;; Output java.time.Instant instead of java.sql.Timestamp in query | ||
;; results | ||
java.sql.Timestamp | ||
(read-column-by-label ^java.time.Instant | ||
[^java.sql.Timestamp v _] | ||
(.toInstant v)) | ||
(read-column-by-index ^java.time.Instant | ||
[^java.sql.Timestamp v _2 _3] | ||
(.toInstant v))) | ||
|
||
|
||
(extend-protocol p/SettableParameter | ||
|
||
;; Accept java.time.Instant as a query param | ||
java.time.Instant | ||
(set-parameter | ||
[^java.time.Instant v ^java.sql.PreparedStatement ps ^long i] | ||
(.setTimestamp ps i (java.sql.Timestamp/from v))) | ||
|
||
;; Accept java.time.LocalDate as a query param | ||
java.time.LocalDate | ||
(set-parameter | ||
[^java.time.LocalDate v ^java.sql.PreparedStatement ps ^long i] | ||
(.setTimestamp ps i (java.sql.Timestamp/valueOf (.atStartOfDay v))))) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,34 @@ | ||
(ns rocks.mygiftlist.model.user | ||
(:require | ||
[com.wsscode.pathom.connect :as pc :refer [defresolver defmutation]] | ||
[rocks.mygiftlist.db :as db] | ||
[rocks.mygiftlist.type.user :as user])) | ||
|
||
(defonce users (atom {})) | ||
|
||
(defresolver user-by-id [env {::user/keys [id]}] | ||
(defresolver user-by-id | ||
[{::db/keys [pool]} {::user/keys [id]}] | ||
{::pc/input #{::user/id} | ||
::pc/output [::user/email]} | ||
(get @users id)) | ||
::pc/output [::user/id ::user/email ::user/auth0-id ::user/created-at]} | ||
(db/execute-one! pool | ||
{:select [:id :email :auth0_id :created_at] | ||
:from [:user] | ||
:where [:= id :id]})) | ||
|
||
(defn- assign-tempid [{::user/keys [id] :as user} tempid] | ||
(assoc user :tempids {tempid id})) | ||
|
||
(defmutation insert-user [env {::user/keys [id] :as user}] | ||
{::pc/params #{::user/id ::user/email} | ||
(defmutation insert-user | ||
[{::db/keys [pool]} {::user/keys [id auth0-id email] :as user}] | ||
{::pc/params #{::user/email ::user/auth0-id} | ||
::pc/output [::user/id]} | ||
(swap! users assoc id user)) | ||
(cond-> (db/execute-one! pool | ||
{:insert-into :user | ||
:values [{:auth0_id auth0-id | ||
:email email}] | ||
:upsert {:on-conflict [:auth0_id] | ||
:do-update-set [:email]} | ||
:returning [:id]}) | ||
id (assign-tempid id))) | ||
|
||
(def user-resolvers | ||
[user-by-id | ||
insert-user]) | ||
|
||
(comment | ||
@users | ||
) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
(ns rocks.mygiftlist.model.user | ||
(:require | ||
[rocks.mygiftlist.type.user :as user] | ||
[edn-query-language.core :as eql] | ||
[com.fulcrologic.fulcro.algorithms.normalized-state :refer [swap!->]] | ||
[com.fulcrologic.fulcro.mutations :as m :refer [defmutation]])) | ||
|
||
(defmutation set-current-user [{::user/keys [id auth0-id email] | ||
:as user}] | ||
(action [{:keys [state]}] | ||
(swap!-> state | ||
(assoc-in [:component/id :current-user] [::user/id id]) | ||
(assoc-in [::user/id id] user))) | ||
(remote [_] | ||
(eql/query->ast1 `[(insert-user | ||
#::user{:id ~id | ||
:auth0-id ~auth0-id | ||
:email ~email})]))) |
This file contains 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
Oops, something went wrong.