Skip to content

Instantly share code, notes, and snippets.

@alexflav23
Last active December 30, 2015 08:39
Show Gist options
  • Save alexflav23/7804137 to your computer and use it in GitHub Desktop.
Save alexflav23/7804137 to your computer and use it in GitHub Desktop.
Using Lift MongoDB record and Foursquare Rogue.
package com.example.record
/**
* In here you will learn how to define a basic MongoDB record class.
* It's type safe and far more powerful than anything our there.
* First, a few basic imports
*/
import org.bson.types.ObjectId
import org.joda.time.DateTime
import com.foursquare.rogue.LiftRogue._
import net.liftweb.mongodb.record.{ MongoRecord, MongoMetaRecord, ObjectIdPk }
import net.liftweb.record.field.{ StringField, EmailField, IntField, ObjectIdField }
import net.liftweb.mongodb.record.field.{ MongoListField, MongoCaseClassField, MongoCaseClassListField }
import net.liftweb.record.field.joda.JodaTimeField
/**
* For the purpose of this exercise, we will define a very simple record.
* Articles, with the following fields: title, author, description, content, timestamp, publicationId, likes and tags.
*/
sealed class Articles private() extends MongoRecord[Articles] with ObjectIdPk[Articles] {
def meta = Articles;
object title extends StringField(this, 256) // 256 is the maximum length
object author extends StringField(this, 256)
object description extends StringField(this, 1000)
object content extends StringField(this, 500000)
object publicationId extends ObjectIdField(this)
object timestamp extends JodaTimeField(this)
object tags extends BsonRecordListField(this, Tags)
}
/**
* To define the Tags, we need a nested record to list them.
*/
class Tags private() extends BsonRecord[Tags] {
def meta = Tags;
object name extends StringField(this, 200)
object timestamp extends JodaTimeField(this, 200)
}
object Tags extends Tags with BsonMetaRecord[Tags] {
}
/**
* Now we make a singleton object.
* This is the only thing we will ever use to do queries.
* The actual record classes will almost always be sealed.
* sealed means only available in the current file.
* so you won't be able to import it anywhere else.
* instead you will be importing the below companion object.
*/
object Articles extends Articles with MongoMetaRecord[Articles] {
override def collectionName = "articles"
// now lets see Rogue in action.
// Lets define a method that optionally selects an article by it's author.
// This will return Some(Article) if a match is found, or None otherwise.
def getArticlesByAuthor(author: String): Option[Articles] = {
Articles.where(_.author eqs author)
.fetch.headOption
}
// this will match the article by title and remove the tag with the given name from the list.
def removeTagByName(title:String, tag: String): Unit = {
Articles.where(_.title eqs title)
.modify(_.tags pullWhere (_.subfield(_.name) eqs tag))
.updateOne
}
}
@alexflav23
Copy link
Author

I'm using Lift 3.0-SNAPSHOT. That's the latest development edge. You can remove that column if it's not needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment