Skip to content

Commit

Permalink
Add appengine-clj.datastore/create
Browse files Browse the repository at this point in the history
  • Loading branch information
duelinmarkers committed Apr 7, 2009
1 parent 7fef393 commit ef2928e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/appengine_clj/datastore.clj
Expand Up @@ -22,3 +22,15 @@
results (.asIterable (.prepare data-service query))]
(map entity-to-map results)))

(defn create
"Takes a map of keyword-value pairs and puts a new Entity in the Datastore.
The map must include a :kind String.
Returns the saved Entity converted with entity-to-map (which will include the assigned :key)."
[item]
(let [kind (item :kind)
properties (dissoc item :kind)
entity (Entity. kind)]
(doseq [[prop-name value] properties] (.setProperty entity (name prop-name) value))
(.put (DatastoreServiceFactory/getDatastoreService) entity)
(entity-to-map entity)))

8 changes: 8 additions & 0 deletions test/appengine_clj/datastore_test.clj
Expand Up @@ -29,3 +29,11 @@
(is (= ["jim"] (map :name (ds/find-all (doto (Query. "A") (.addFilter "code" Query$FilterOperator/EQUAL 1))))))
(is (= ["jan"] (map :name (ds/find-all (doto (Query. "B") (.addFilter "code" Query$FilterOperator/EQUAL 1)))))))

(dstest create-saves-and-returns-item-with-a-key
(let [created-item (ds/create {:kind "MyKind" :name "hume" :age 31})]
(is (not (nil? (created-item :key))))
(let [created-entity (.get (DatastoreServiceFactory/getDatastoreService) (created-item :key))]
(is (= "MyKind" (.getKind created-entity)))
(is (= "hume" (.getProperty created-entity "name")))
(is (= 31 (.getProperty created-entity "age"))))))

0 comments on commit ef2928e

Please sign in to comment.