2012-12-23 35 views
1

從mongo_dart的blog.dart中抽取樣本在將記錄添加到數據庫時,我希望打出一些強類型。任何幫助表示讚賞。Dart使用MongoDB強打字

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':'[email protected]', 'age':587}, 
     {'name':'Jorge Luis Borges', 'email':'[email protected]', '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(); 
    }); 

回答

2

也許Objectory會適合你。

從自述:

對象工廠 - 目標文件映射爲服務器端和客戶端應用程序達特。 Objectory提供了類型化的檢查環境,用於對保存在MongoDb上的數據進行建模,保存和查詢。

從對象工廠blog_console.dart樣本的對應片段是這樣的:

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 = '[email protected]'; 
    author.age = 587; 
    author.save(); 
    author = new Author(); 
    author.name = 'Jorge Luis Borges'; 
    author.email = '[email protected]'; 
    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}]"); 
    } 
........ 

和類作者的定義爲:

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); 

} 

Quick tour,樣品和測試獲得進一步的信息

+0

這很棒!讓我想起了Linq。 – basheps