*: update redefs of `to_json`
[nit.git] / lib / popcorn / pop_repos.nit
index b4142a4..0a6bd37 100644 (file)
@@ -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 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 server used for data persistence
+       var db_host: String is lazy do return value_or_default("db.host", default_db_host)
+
+       # MongoDB DB used for data persistence
+       var db_name: String is lazy do return value_or_default("db.name", default_db_name)
+
+       # Mongo db client
+       var client = new MongoClient(db_host) is lazy
+
+       # Mongo db instance
+       var db: MongoDb = client.database(db_name) is lazy
+
+       redef init from_options(opts) do
+               super
+               var db_host = opts.opt_db_host.value
+               if db_host != null then self["db.host"] = db_host
+               var db_name = opts.opt_db_name.value
+               if db_name != null then self["db.name"] = db_name
+       end
+end
+
+redef class AppOptions
+
+       # 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")
+
+       init do
+               super
+               add_option(opt_db_host, opt_db_name)
+       end
+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 +192,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
@@ -219,17 +265,14 @@ end
 #
 # ~~~
 # import popcorn
-# import popcorn::pop_routes
+# import popcorn::pop_repos
 #
 # # First, let's create a User abstraction:
 #
 # # Serializable user representation.
 # class User
+#      super RepoObject
 #      serialize
-#      super Jsonable
-#
-#      # User uniq ID
-#      var id: String = (new MongoObjectId).id is serialize_as "_id"
 #
 #      # User login
 #      var login: String
@@ -238,9 +281,6 @@ end
 #      var password: String is writable
 #
 #      redef fun to_s do return login
-#      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 User specific services:
@@ -296,14 +336,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)
@@ -338,6 +382,61 @@ class MongoRepository[E: Serializable]
        end
 end
 
+# Base serializable entity that can go into a JsonRepository
+#
+# Provide boiler plate implementation of all object serializable to json.
+#
+# `id` is used as a primary key for `find_by_id`.
+#
+# Subclassing RepoObject makes it easy to create a serializable class:
+# ~~~
+# import popcorn::pop_repos
+#
+# class Album
+#      super RepoObject
+#      serialize
+#
+#      var title: String
+#      var price: Float
+# end
+# ~~~
+#
+# Do not forget the `serialize` annotation else the fields will not be serialized.
+#
+# It is also possible to redefine the `id` primary key to use your own:
+# ~~~
+# import popcorn::pop_repos
+#
+# class Order
+#      super RepoObject
+#      serialize
+#
+#      redef var id = "order-{get_time}"
+#
+#      # ...
+#
+# end
+# ~~~
+abstract class RepoObject
+       super Jsonable
+       serialize
+
+       # `self` unique id.
+       #
+       # This attribute is serialized under the key `_id` to be used
+       # as primary key by MongoDb
+       var id: String = (new MongoObjectId).id is writable, serialize_as "_id"
+
+       # Base object comparison on ID
+       #
+       # Because multiple deserialization can exists of the same instance,
+       # we use the ID to determine if two object are the same.
+       redef fun ==(o) do return o isa SELF and id == o.id
+
+       redef fun hash do return id.hash
+       redef fun to_s do return id
+end
+
 # JsonObject can be used as a `RepositoryQuery`.
 #
 # See `mongodb` lib.