1

mongo_dart の blog.dart のサンプルから取得しました。データベースにレコードを追加するときに強力な型付けを行いたいと思います。どんな助けでも大歓迎です。

  Db db = new Db("mongodb://127.0.0.1/mongo_dart-blog");
  DbCollection collection;
  DbCollection articlesCollection;
  Map<String,Map> authors = new Map<String,Map>();
  db.open().chain((o){
    db.drop();
    collection = db.collection('authors');
    collection.insertAll( //would like strongly typed List here instead
      [{'name':'William Shakespeare', 'email':'william@shakespeare.com', 'age':587},
      {'name':'Jorge Luis Borges', 'email':'jorge@borges.com', 'age':123}]
    );
    return collection.find().each((v){authors[v["name"]] = v;});
  }).chain((v){
    print(">> Authors ordered by age ascending");
    db.ensureIndex('authors', key: 'age');
    return collection.find(where.sortBy('age')).each(
      (auth)=>print("[${auth['name']}]:[${auth['email']}]:[${auth['age']}]"));
  }).then((dummy){
    db.close();
  });
4

1 に答える 1

2

Maybe Objectory would suit you.

From readme:

Objectory - object document mapper for server-side and client side Dart applications. Objectory provides typed, checked environment to model, save and query data persisted on MongoDb.

Corresponding fragment from blog_console.dart sample in objectory looks like:

 objectory = new ObjectoryDirectConnectionImpl(Uri,registerClasses,true);
  var authors = new Map<String,Author>();
  var users = new Map<String,User>();  
  objectory.initDomainModel().chain((_) {
    print("===================================================================================");
    print(">> Adding Authors");
    var author = new Author();
    author.name = 'William Shakespeare';
    author.email = 'william@shakespeare.com';
    author.age = 587;
    author.save();
    author = new Author();
    author.name = 'Jorge Luis Borges';
    author.email = 'jorge@borges.com';
    author.age = 123;
    author.save();    
    return objectory.find($Author.sortBy('age'));
  }).chain((auths){  
    print("============================================");
    print(">> Authors ordered by age ascending");
    for (var auth in auths) {
      authors[auth.name] = auth;
      print("[${auth.name}]:[${auth.email}]:[${auth.age}]");
    }
........

And class Author is defined as:

class Author extends PersistentObject  {
  String get name => getProperty('name');
  set name(String value) => setProperty('name',value);

  String get email => getProperty('email');
  set email(String value) => setProperty('email',value);

  int get age => getProperty('age');
  set age(int value) => setProperty('age',value);

}

Look at Quick tour, samples and tests for futher information

于 2012-12-24T06:46:57.123 に答える