[MonkeyScript] [TitleIndex] [WordIndex

   1 // Connect
   2 var s = new Sedna(connectionData);
   3 
   4 s.loadDocument(
   5     'library',
   6     <books>
   7         <book id="aaa">
   8             <title>The great beyond</title>
   9         </book>
  10     </books>
  11 )
  12 
  13 // Raw execute
  14 s.execute("UPDATE insert " + <book><title>In the wild</title></book> + " into doc('foo')/books");
  15 
  16 // Querying
  17 s.query("doc('library')/books/book[@id='aaa']"); => [ <book id="aaa"><title>The great beyond</title></book> ]
  18 s.query("doc('library')/books/book[@id='aaa']")[0].title.text();
  19 s.query("doc('library')/books/book/title/text()"); => ["The great beyond", "In the wild"]
  20 /// Use data substitution (this securely escapes string data when not using xml nodes)
  21 s.query("UPDATE insert <content>$content</content> into doc('library')/books/book[@id='aaa']", {content: contentText});
  22 
  23 // Async
  24 s.query("doc('library')", function(library) {
  25     library.books.(@id == 'aaa').title.text()
  26 });
  27 
  28 // Transactions
  29 s.transaction(function() {
  30     this.query("UPDATE delete doc('index')/indexes/index[@by=title]");
  31     var books = this.query("doc('library')/books/book");
  32     for each ( let book in books ) {
  33         this.query("UPDATE insert " + <book id={book.@id}>{book.title.text()}</book>
  34     }
  35 });
  36 
  37 // Shortcuts & Tips
  38 
  39 /// XML.escape and XML.unescape are defined by MonkeyScript
  40 s.execute("doc('library')/books/book[@id='"+XML.escape(id)+"']");
  41 /// You can use data substitution inside of .query
  42 s.execute("doc('library')/books/book[@id='$id']", {id:id});
  43 
  44 // Connection stuff
  45 s.connected; // bool
  46 s.close();


2010-07-23 04:25