Skip to content

dluksza/FanLink

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 

Repository files navigation

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

About

Fantom objects to MongoDB mapper

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages