Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Fantom objects to MongoDB mapper

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 fanlink
Octocat-spinner-32 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.