Last active
August 29, 2015 13:57
-
-
Save k33g/9383626 to your computer and use it in GitHub Desktop.
Golo & MongoDb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module mongodb | |
| import com.mongodb.MongoClient | |
| import com.mongodb.MongoException | |
| import com.mongodb.WriteConcern | |
| import com.mongodb.DB | |
| import com.mongodb.DBCollection | |
| import com.mongodb.BasicDBObject | |
| import com.mongodb.DBObject | |
| import com.mongodb.DBCursor | |
| import com.mongodb.ServerAddress | |
| import org.bson.types.ObjectId | |
| function Mongo = { | |
| let db = DynamicObject() # default values | |
| :host("localhost") # easily change because it's a DynamicObject | |
| :port(27017) | |
| :getDBInstance(|this, dataBaseName| { | |
| this: mongoClient(MongoClient(this: host(), this: port())) | |
| this: db(this: mongoClient(): getDB(dataBaseName)) | |
| return this | |
| }) | |
| :database(|this, dataBaseName| -> this: getDBInstance(dataBaseName)) | |
| :collection(|this, collectionName| { | |
| return this: db(): getCollection(collectionName) | |
| }) | |
| return db | |
| } | |
| function MongoModel = |mongoCollection| { | |
| let mongoModel = DynamicObject() | |
| let collection = mongoCollection | |
| let basicDBObject = BasicDBObject() | |
| mongoModel: collection(collection) | |
| mongoModel: basicDBObject(basicDBObject) | |
| mongoModel: getId(|this| { | |
| return this: basicDBObject(): getObjectId("_id"): toString() | |
| }) | |
| mongoModel: setField(|this, fieldName, lastName| { | |
| this: basicDBObject(): put(fieldName, lastName) | |
| return this | |
| }) | |
| mongoModel: getField(|this, fieldName| { | |
| return this: basicDBObject(): get(fieldName) | |
| }) | |
| mongoModel: insert(|this| { | |
| collection: insert(this: basicDBObject()) | |
| return this | |
| }) | |
| mongoModel: update(|this| { | |
| let id = this: basicDBObject(): get("_id") | |
| this: basicDBObject(): removeField("_id") | |
| let searchQuery = BasicDBObject(): append("_id", ObjectId(id)) | |
| collection: update(searchQuery, this: basicDBObject()) | |
| this: basicDBObject(): put("_id", ObjectId(id)) | |
| return this | |
| }) | |
| mongoModel: fetch(|this, id| { | |
| let searchQuery = BasicDBObject(): append("_id", ObjectId(id)) | |
| this: basicDBObject(): putAll(collection: findOne(searchQuery)) | |
| return this | |
| }) | |
| mongoModel: remove(|this, id| { | |
| let searchQuery = BasicDBObject(): append("_id", ObjectId(id)) | |
| let doc = collection: find(searchQuery): next() | |
| this: basicDBObject(): putAll(doc) | |
| collection: remove(doc) | |
| return this | |
| }) | |
| mongoModel: readable(|this| { # return map | |
| let map = this: basicDBObject(): toMap() | |
| map: put("_id", this: getId()) | |
| return map | |
| }) | |
| return mongoModel | |
| } | |
| function MongoCollection = |mongoModel, mongoCollection|{ | |
| let mongoColl = DynamicObject() | |
| mongoColl: define("model", |this| { | |
| return mongoModel(mongoCollection) | |
| }) | |
| mongoColl: collection(mongoCollection) | |
| # get all models (value objects) | |
| mongoColl: fetch(|this| { # return list of hashMap | |
| let models = list[] | |
| let cursor = this: collection() | |
| : find() | |
| : each(|doc| { | |
| let map = doc: toMap() | |
| let id = doc: getObjectId("_id"): toString() | |
| map: put("_id", id) | |
| models: add(map) | |
| }) | |
| : close() | |
| return models | |
| }) | |
| # find models (value objects) | |
| #coll: find("firstName", "John") | |
| mongoColl: find(|this, fieldName, value| { # return list of hashMap | |
| let models = list[] | |
| let cursor = this: collection() | |
| : find(BasicDBObject(fieldName, value)) | |
| : each(|doc| { | |
| let map = doc: toMap() | |
| let id = doc: getObjectId("_id"): toString() | |
| map: put("_id", id) | |
| models: add(map) | |
| }) | |
| : close() | |
| return models | |
| }) | |
| #coll: like("firstName", ".*o.*") | |
| mongoColl: like(|this, fieldName, value| { # return list of hashMap | |
| let models = list[] | |
| let cursor = this: collection() | |
| : find(BasicDBObject(fieldName, java.util.regex.Pattern.compile(value))) | |
| : each(|doc| { | |
| let map = doc: toMap() | |
| let id = doc: getObjectId("_id"): toString() | |
| map: put("_id", id) | |
| models: add(map) | |
| }) | |
| : close() | |
| return models | |
| }) | |
| return mongoColl | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module demo | |
| import mongodb | |
| # Collection | |
| function Humans = { | |
| let collection = Mongo(): database("demodb"): collection("humans") | |
| # Model | |
| let Human = |collection| -> | |
| MongoModel(collection): | |
| setFirstName(|this, value| { | |
| this: setField("firstName", value) | |
| return this | |
| }): | |
| setLastName(|this, value| { | |
| this: setField("lastName", value) | |
| return this | |
| }) | |
| return MongoCollection(Human , collection) | |
| } | |
| function main = |args| { | |
| let humans = Humans() | |
| let tony = humans: model() | |
| tony | |
| : setFirstName("Tony") | |
| : setLastName("Stark") | |
| : insert() | |
| let jane = humans: model() | |
| jane | |
| : setFirstName("Jane") | |
| : setLastName("Doe") | |
| : insert() | |
| # get all humans | |
| humans: fetch() | |
| # find some humans | |
| humans: find("firstName", "Tony"): getFirst() | |
| humans: find("lastName", "Doe"): getLast() | |
| humans: like("firstName", ".*a.*"): getFirst() | |
| : each(|field, value|-> println(field + " = " + value)) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment