 ;; 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 TableSchema
-         {(s/optional-key :entity-key) SecondaryKeySchema}))
+  (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

  (s/conditional

   (fn [k] (-> k first sequential?))
   [(s/optional [s/Keyword] :compound-parition-key)
    s/Keyword]

   :else
   [s/Keyword]))

(s/defschema KeyValueSchema
  [(s/one s/Any :key-component) s/Any])

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

(defn ^:private conform-table
  [table-type table]
  (assoc table
         :type table-type
         :key (v/coerce (:key table))))

(defn ^:private conform-tables
  [entity-schema [table-type table-seq-key]]
  (assoc entity-schema
         table-seq-key
         (mapv (partial conform-table table-type)
               (get entity-schema table-seq-key))))

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

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

(defmacro defentity
  [name entity-spec]
