Permalink
Browse files

Add appengine-clj.datastore/create

  • Loading branch information...
1 parent 7fef393 commit ef2928e224e55ab485933b8e342afa4de791e6c9 @duelinmarkers committed Apr 7, 2009
Showing with 20 additions and 0 deletions.
  1. +12 −0 src/appengine_clj/datastore.clj
  2. +8 −0 test/appengine_clj/datastore_test.clj
@@ -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)))
+
@@ -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.