Helma Logo
main list history

Version 8 by hannes on 20. February 2009, 23:00

3A simple demo app for helma.filestore is <a href="https://dev.helma.org/trac/helma/browser/helma-ng/trunk/apps/storage">included in with Helma NG</a>.

Version 7 by hannes on 20. February 2009, 22:59

3A simple demo app for helma.filestore is <a href="https://dev.helma.org/trac/helma/browser/helma-ng/trunk/apps/storage?rev=9205org/trac/helma/browser/helma-ng/trunk/apps/storage">included in Helma NG</a>.

Version 6 by hannes on 20. February 2009, 22:05

7  importModule(include('helma.filestore', 'db');
10  db.var store = db.store || new db.Store("db");
12Next you define a JavaScript constructor for your persistent model class. There are two extra methods need to call. One is the <code>registerType()</code> method in your Store instance, which adds static retrieval methods such as <code>get(id)</code> or <code>all()</code> to the constructor, allows the store to use the constructor when retrieving objects. The other is constructor takes an object containing the <code>makeStorable()</code> method which you call in properties of the constructor to add instance methods like <code>save()</code> and <code>remove()</code> as well as getters and setters for the persistent propertiesargument.
14  function Book(props) Book(properties) {
15    this.properties =  // add instance methods and property accessorsproperties || {};
16      db.makeStorable(this, props);return this;
16  
17  // add static retrieval methods to the Book constructor
18  db.store.registerType(Book);
18Note that To make the persistent properties are kept in a separate object which is here called <code>props</code>.class persistable, The accessors installed by db.makeStorable() allow you pass it to access these properties in the model object itself<code>registerType()</code> method of your Store instance along with a description of the class's perstistent properties. Any properties you set in This adds static retrieval methods such as <code>get(id)</code> or <code>all()</code> to the persistent property object will be storedconstructor and allows the store to use the constructor when retrieving objects. 
20Of course you can add methods to your model object any way you like it, either in the constructor or through the constructor's prototype property. Thusstore.registerType(Book, the full code now looks like this:
21    title: Text(),
22    author: Reference(Author)
23  });
22Of course you can add methods to your model object any way you like it,  importModule(either in the constructor or through the constructor'helmas prototype property.filestore', 
23    
24  // init store instance if doesn't already exist.
25  db.store = db.store || new db.Store("db");
26  
27  function Book(props) {
28      // Define any instance methods you like, accessing persistent
29      // properties using the this prefix.
30      this.getFullTitle = function() {
31          return this.author + ": " + this.title;
32      }
33      
34      this.toString = function() {
35          return "Book[" + this.getFullTitle() + "]";
36      }
37      
38      // add instance methods and property accessors
39      db.makeStorable(this, props);
40  }
41    
42  // add static retrieval methods to the Book constructor
43  db.store.registerType(Book);
24That's it, your first persistent JavaScript class. Store For a full working application using the code above to helma.filestore module have a file called booklook at the *storage demo app|http://dev.js so we helma.org/trac/helma/browser/helma-ng/trunk/apps/storage* that comes with Helma NG. You can play start the application with it using the Helma NG shell:following command.
26  $ java -jar run.jar -i bookapps/storage/main.js
27  helma> var b = new Book({ title: "Die kleine Hexe", author: "Otfried Preußler" });
28  helma> b
29  Book[Otfried Preußler: Die kleine Hexe]
30  helma>
28Go to http://localhost:8080/ to view the app in your browser. You also can work with the application interactively, using the shell (that's what the -i switch was for in the command line above).
29
30  helma> include("model");
31  helma> var b = new Book({ title: "Die kleine Hexe" });
32  helma> b.author = new Author({ name: "Otfried Preußler" });
33
36  helma> new Book({ title: "Der kleine Walfisch", author: "Erwin Moser" }).save()
38  Book[Otfried Preußler: Die kleine Hexe],Book[Erwin Moser: Der kleine Walfisch]
39  helma>
49  Book[Otfried Preußler: Die kleine Hexe],Book[Erwin Moser: Der kleine Walfisch]
50  helma>

Version 5 by hannes on 05. September 2008, 23:15

52  helma> var b = new Book({ title: "Die kleine Hexe", author: "Erich KästnerOtfried Preußler" });
54  Book[Erich Kästner: Book[Otfried Preußler: Die kleine Hexe]
63  Book[Erich Kästner: Book[Otfried Preußler: Die kleine Hexe]
66  Book[Erich Kästner: Book[Otfried Preußler: Die kleine Hexe],Book[Erwin Moser: Der kleine Walfisch]
78  Book[Erich Kästner: Book[Otfried Preußler: Die kleine Hexe],Book[Erwin Moser: Der kleine Walfisch]

Version 4 by hannes on 05. September 2008, 16:33

74      >  filter: function(obj) function(book) { return /klein/.test(objtest(book.title); },

Version 3 by hannes on 05. September 2008, 16:22

49That's it, your first persistent JavaScript class. Store the code above to a file called book.js and start so we can play with it using the Helma NG shell:

Version 2 by hannes on 05. September 2008, 16:19

4
5To use helma.filestore you need to import the module and create a Store instance, passing it the root directory of the data store:
6
7  importModule('helma.filestore', 'db');
8  
9  // init store instance if doesn't already exist.
10  db.store = db.store || new db.Store("db");
11
12Next you define a JavaScript constructor for your persistent model class. There are two extra methods need to call. One is the <code>registerType()</code> method in your Store instance, which adds static retrieval methods such as <code>get(id)</code> or <code>all()</code> to the constructor, allows the store to use the constructor when retrieving objects. The other is the <code>makeStorable()</code> method which you call in the constructor to add instance methods like <code>save()</code> and <code>remove()</code> as well as getters and setters for the persistent properties.
13
14  function Book(props) {
15      // add instance methods and property accessors
16      db.makeStorable(this, props);
17  }
18  
19  // add static retrieval methods to the Book constructor
20  db.store.registerType(Book);
21
22Note that the persistent properties are kept in a separate object which is here called <code>props</code>. The accessors installed by db.makeStorable() allow to access these properties in the model object itself. Any properties you set in the persistent property object will be stored.
23
24Of course you can add methods to your model object any way you like it, either in the constructor or through the constructor's prototype property. Thus, the full code now looks like this:
25
26  importModule('helma.filestore', 'db');
27    
28  // init store instance if doesn't already exist.
29  db.store = db.store || new db.Store("db");
30  
31  function Book(props) {
32      // Define any instance methods you like, accessing persistent
33      // properties using the this prefix.
34      this.getFullTitle = function() {
35          return this.author + ": " + this.title;
36      }
37      
38      this.toString = function() {
39          return "Book[" + this.getFullTitle() + "]";
40      }
41      
42      // add instance methods and property accessors
43      db.makeStorable(this, props);
44  }
45    
46  // add static retrieval methods to the Book constructor
47  db.store.registerType(Book);
48
49That's it, your first persistent JavaScript class. Store the code above to a file called book.js and start the Helma NG shell:
50
51  $ java -jar run.jar -i book.js
52  helma> var b = new Book({ title: "Die kleine Hexe", author: "Erich Kästner" });
53  helma> b
54  Book[Erich Kästner: Die kleine Hexe]
55  helma>
56
57Voila, your first persistent object. Let's store it and see how the data retrieval objects work:
58
59  helma> b.save()
60  helma> b._id
61  1
62  helma> Book.get("1");
63  Book[Erich Kästner: Die kleine Hexe]
64  helma> new Book({ title: "Der kleine Walfisch", author: "Erwin Moser" }).save()
65  helma> Book.all()
66  Book[Erich Kästner: Die kleine Hexe],Book[Erwin Moser: Der kleine Walfisch]
67  helma>
68
69One very powerful method is the <code>list()</code> method in the model's constructor. It allows you to get a filtered, ordered, and sliced view of the stored objects. For example, the following will give you a list of up to 10 books with the word "klein" in the title, ordered by author name and starting with the first book:
70
71  helma> Book.list({
72      >  orderBy: "author",
73      >  order: "asc",
74      >  filter: function(obj) { return /klein/.test(obj.title); },
75      >  start: 0,
76      >  max: 10
77      > });
78  Book[Erich Kästner: Die kleine Hexe],Book[Erwin Moser: Der kleine Walfisch]
79  helma>
80
81If you're done, use a book's <code>remove()</code> method to delete it from the data store.

Version 1 by hannes on 05. September 2008, 15:04

1helma.filestore is a simple, file based storage engine. It uses JSON as object serialization format and offers basic storage, retrieval and querying support for plain JavaScript objects.
3A simple demo app for helma.filestore is <a href="https://dev.helma.org/trac/helma/browser/helma-ng/trunk/apps/storage?rev=9205">included in Helma NG</a>.