 
 ;; basic table schema shared by primary, secondary
 ;; unique-key and lookup tables
-(s/defschema TableSchema
+(s/defschema BaseTableSchema
   {:name s/Keyword
    :key KeySchema
    (s/optional-key :relationships) [RelationshipSchema]})

     callbacks :- CallbacksSchema
     versioned? :- s/Bool])

(defn ^:private force-key-seq
  [table]
  (assoc table :key (v/coerce (:key table))))

(defn ^:private force-key-seqs
  [entity-schema table-seq-key]
  (assoc entity-schema
         table-seq-key
         (mapv force-key-seq
               (get entity-schema table-seq-key))))

(defn ^:private force-all-key-seqs
  "ensure all keys are given as sequences of components"
  [entity-schema]
  (reduce force-key-seqs
          (assoc entity-schema
                 :primary-table
                 (force-key-seq (:primary-table entity-schema)))
          [:unique-key-tables
           :secondary-tables
           :lookup-key-tables]))

(s/defn ^:always-validate create-entity :- Entity
  "create an entity record from a spec"
  [entity-spec :- EntitySchema]
  (map->Entity (merge {:unique-key-tables []
                       :secondary-tables []
                       :lookup-key-tables []
                       :callbacks {}
                       :versioned? false}
                      (force-all-key-seqs entity-spec))))

(defmacro defentity
  [name entity-spec]

;; delete with a secondary index from seocondary and lookup tables
;; TODO use the :entity-key to delete stale secondary and lookup records
(s/defschema PrimaryTableSchema
  (merge BaseTableSchema
         {(s/optional-key :entity-key) SecondaryKeySchema}
         {:type (s/eq :primary)}))

;; some of the (non-partition) columns in a lookup-table
;; key may be collections... these will be expanded to the
