examples: annotate examples
[nit.git] / lib / popcorn / pop_repos.nit
index f2d4a22..2040c83 100644 (file)
 #
 # import popcorn
 # import popcorn::pop_repos
+# import popcorn::pop_json
 #
 # # Serializable book representation.
 # class Book
 #      serialize
-#      super Jsonable
 #
 #      # Book uniq ID
 #      var id: String = (new MongoObjectId).id is serialize_as "_id"
@@ -54,7 +54,6 @@
 #      redef fun to_s do return title
 #      redef fun ==(o) do return o isa SELF and id == o.id
 #      redef fun hash do return id.hash
-#      redef fun to_json do return serialize_to_json
 # end
 #
 # # We then need to subclass the `MongoRepository` to provide Book specific services.
 # ~~~
 module pop_repos
 
+import popcorn::pop_config
 import serialization
-import json::serialization
+import json
 import mongodb::queries
 
+redef class AppConfig
+
+       # Default database host string for MongoDb
+       var default_db_host = "mongodb://localhost:27017/"
+
+       # Default database hostname
+       var default_db_name = "popcorn"
+
+       # MongoDb host name
+       var opt_db_host = new OptionString("MongoDb host", "--db-host")
+
+       # MongoDb database name
+       var opt_db_name = new OptionString("MongoDb database name", "--db-name")
+
+       # MongoDB server used for data persistence
+       fun db_host: String do return opt_db_host.value or else ini["db.host"] or else default_db_host
+
+       # MongoDB DB used for data persistence
+       fun db_name: String do return opt_db_name.value or else ini["db.name"] or else default_db_name
+
+       init do
+               super
+               add_option(opt_db_host, opt_db_name)
+       end
+
+       # Mongo db client
+       var client = new MongoClient(db_host) is lazy
+
+       # Mongo db instance
+       var db: MongoDb = client.database(db_name) is lazy
+end
+
 # A Repository is an object that can store serialized instances.
 #
 # Repository is the base class of all kind of persistance processes. It offers
@@ -149,7 +181,10 @@ interface Repository[E: Serializable]
        # Find all instances based on `query`
        #
        # Using `query` == null will retrieve all the document in the repository.
-       fun find_all(query: nullable QUERY): Array[E] is abstract
+       fun find_all(query: nullable QUERY, skip, limit: nullable Int): Array[E] is abstract
+
+       # Count instances that matches `query`
+       fun count(query: nullable QUERY): Int is abstract
 
        # Save an `instance`
        fun save(instance: E): Bool is abstract
@@ -160,6 +195,9 @@ interface Repository[E: Serializable]
        # Remove the instance based on `query`
        fun remove(query: nullable QUERY): Bool is abstract
 
+       # Remove all the instances matching on `query`
+       fun remove_all(query: nullable QUERY): Bool is abstract
+
        # Remove all instances
        fun clear: Bool is abstract
 
@@ -217,6 +255,7 @@ end
 # ~~~
 # import popcorn
 # import popcorn::pop_repos
+# import popcorn::pop_json
 #
 # # First, let's create a User abstraction:
 #
@@ -287,14 +326,18 @@ class MongoRepository[E: Serializable]
                return deserialize(res.to_json)
        end
 
-       redef fun find_all(query) do
+       redef fun find_all(query, skip, limit) do
                var res = new Array[E]
-               for e in collection.find_all(query or else new JsonObject) do
+               for e in collection.find_all(query or else new JsonObject, skip, limit) do
                        res.add deserialize(e.to_json).as(E)
                end
                return res
        end
 
+       redef fun count(query) do
+               return collection.count(query or else new JsonObject)
+       end
+
        redef fun save(item) do
                var json = serialize(item).as(String)
                var obj = json.parse_json.as(JsonObject)
@@ -311,6 +354,10 @@ class MongoRepository[E: Serializable]
                return collection.remove(query or else new JsonObject)
        end
 
+       redef fun remove_all(query) do
+               return collection.remove_all(query or else new JsonObject)
+       end
+
        redef fun clear do return collection.drop
 
        # Perform an aggregation query over the repo.
@@ -361,7 +408,6 @@ end
 # end
 # ~~~
 abstract class RepoObject
-       super Jsonable
        serialize
 
        # `self` unique id.
@@ -378,7 +424,6 @@ abstract class RepoObject
 
        redef fun hash do return id.hash
        redef fun to_s do return id
-       redef fun to_json do return serialize_to_json
 end
 
 # JsonObject can be used as a `RepositoryQuery`.