Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Fantom objects to MongoDB mapper
Fantom
Branch: master

Refactor Operations.find and Operations.update

Extract common part from find and update operations
latest commit 654fd7b0d0
@dluksza authored
Failed to load latest commit information.
fanlink Refactor Operations.find and Operations.update
README.md Updated documentation in README.md

README.md

FanLink

FanLink allows you easily save and find Fantom objects in MongoDB. It uses Fantom reflection API to convert MongoDoc object to Str:Obj? map then fantomongo to persist this map in MongoDB. De serialization works other way around.

Mongo collection name is automatically created based on pod name and object name eg. class User in pod example will be saved in example_User collection.

Nested objects are converted into nested maps, FanLink doesn't support DBRef.

Examples

Each persist able object must:

  • be a const class,
  • extend MongoDoc mixin,
  • define it-block-constructor and
  • define storage for _id eg:
using fanlink
using fantomongo

const class SimpleMongoObj : MongoDoc {
  const Str name
  const Str surname
  const Decimal number?
  override const ObjectID? _id

  new make(|This f| f) {
    f(this)
  }
}

All simple types like:

  • Str,
  • Bool,
  • Decimal,
  • Float,
  • Int,
  • Date,
  • Buf

are supported. Also nested List and Map are supported, same as nested instances of MongoDoc, List[MongoDoc] and Map[x, MongoDoc].

To persist object simply call:

db := Mongo().db("test")
Operations.insert(db, SimpleMongoObj{ name = "John"; surname = "Doe" })

When you want get all documents of given type call just:

allDocuments := Operations.findAll(db, SimpleMongoObj#)

In case of finding all documents where name attribute has value "John" run:

filterObj := SimpleMongoObj {
  name = "John"
  surname = "required by language syntax"
}
findFilter := FindFilter {
  filter = filterObj
  interestingFields = [SimpleMongoObj#name]
}
allJohnes := Operations.find(db, findFilter)

If you want find first 5 SimpleMongoObj documents with number field set to 8 call:

filterObj := SimpleMongoObj {
  name = "uninteresting"
  surname = "uninteresting"
  number = 8d
}
findFilter := FindFilter {
  filter = filterObj
  interestingFields = [SimpleMongoObj#number]
}
number8 := Operations.find(db, findFilter, FindOpts { limit = 5 })

For more examples see Operations*Test.fan inside test directory

Something went wrong with that request. Please try again.