|
34 | 34 | (defn- feature-row->feature |
35 | 35 | "Flatten an {:fkey :metadata} row into a flat map with expires-at parsed. |
36 | 36 |
|
37 | | - When `serialize?` is true, decodes JSON manually (instead of setting up |
38 | | - auto-convert via next.jdbc) so we don't mess with library consumers. When |
39 | | - false, assumes the auto-convert is in effect." |
40 | | - [serialize? feature-row] |
41 | | - (let [raw-meta (.getValue (:metadata feature-row)) |
42 | | - metadata (if serialize? |
| 37 | + Just decodes JSON manually (instead of setting up auto-convert via |
| 38 | + next.jdbc) so we don't mess with library consumers. " |
| 39 | + [feature-row] |
| 40 | + ;; need to re-eval this, base might have futzed w the connection to de-jsonify |
| 41 | + (let [raw-meta (if (map? (:metadata feature-row)) |
| 42 | + (:metadata feature-row) |
| 43 | + (.getValue (:metadata feature-row))) |
| 44 | + metadata (if (string? raw-meta) |
43 | 45 | (json/read-str raw-meta :key-fn keyword) |
44 | 46 | raw-meta) |
45 | 47 | ?expires-at (:expires-at metadata)] |
|
177 | 179 | [f ttl-threshold-msec] |
178 | 180 | (if ttl-threshold-msec (memo/ttl f :ttl/threshold ttl-threshold-msec) f)) |
179 | 181 |
|
180 | | -(defrecord FeatureStore |
181 | | - [table-prefix datasource enabled-pred serialize? *group-registry] |
| 182 | +(defrecord FeatureStore [table-prefix datasource enabled-pred *group-registry] |
182 | 183 | ha/IFStore |
183 | 184 | (-features [_] |
184 | 185 | (let [params (make-names table-prefix)] |
185 | | - (map (partial feature-row->feature serialize?) |
186 | | - (hug:select-features datasource params)))) |
| 186 | + (map feature-row->feature (hug:select-features datasource params)))) |
187 | 187 |
|
188 | 188 | (-add! [_ fkey expires-at author] |
189 | 189 | (let [row {:fkey fkey |
190 | | - :metadata (cond-> {:expires-at (ldt->string expires-at) |
191 | | - :author author} |
192 | | - serialize? json/write-str)} |
| 190 | + :metadata (json/write-str {:expires-at (ldt->string expires-at) |
| 191 | + :author author})} |
193 | 192 | params (merge (make-names table-prefix) row)] |
194 | 193 | (hug:upsert-feature datasource params))) |
195 | 194 |
|
|
199 | 198 |
|
200 | 199 | (-expired? [_ fkey now] |
201 | 200 | (let [params (merge (make-names table-prefix) {:fkey fkey}) |
202 | | - feature (some-> (hug:select-feature datasource params) |
203 | | - (partial feature-row->feature serialize?))] |
| 201 | + feature (some-> (hug:select-feature datasource params) feature-row->feature)] |
204 | 202 | (hp/before? (:expires-at feature) now))) |
205 | 203 |
|
206 | 204 | (-disable! [_ fkey] |
|
293 | 291 | Supply opts `{:clear-tables? true}` to reuse existing tables but clear |
294 | 292 | existing table data. |
295 | 293 |
|
296 | | - Supply opts `{:disable-serialization? true}` to opt out of JSON |
297 | | - serialization if the jdbc protocols have been extended to handle it |
298 | | - automatically, eg. as outlined in https://cljdoc.org/d/seancorfield/next.jdbc/1.2.659/doc/getting-started/tips-tricks#working-with-json-and-jsonb |
299 | | -
|
300 | 294 | Supply opts `{:ttl/threshold msec}` to memoize the `enabled?` call to |
301 | 295 | reduce db load, at the cost of the TTL's delay in a feature's activation." |
302 | 296 | ([table-prefix jdbc-database-url] |
|
310 | 304 | (->FeatureStore table-prefix |
311 | 305 | jdbc-database-url |
312 | 306 | (memoize-fn enabled? (:ttl/threshold opts)) |
313 | | - (boolean (not (:disable-serialization? opts))) |
314 | 307 | (atom {})))) |
315 | 308 |
|
316 | 309 | (defn destroy-fstore! [{:keys [table-prefix datasource] :as _fstore}] |
|
0 commit comments