A DAO, or Data Access Object, is a universal interface to a collection of
objects. foam.dao.EasyDAO configures itself automatically, based on the
configuration flags you specify when creating an EasyDAO.
To store your class in an EasyDAO, just add an id property:
foam.CLASS({
package: 'fun',
name: 'StoreMe',
properties: [ 'id' ],
});
Your id must be unique for separate objects, so either set it carefully or
set EasyDAO.seqNo to true to assign id automatically.
The basic storage mechanism your EasyDAO will provide is configured by setting
daoType:
Choose a default for IndexedDB, browser local storage, or in-memory MDAO:
foam.dao.EasyDAO.create({ daoType: 'IDB' });
foam.dao.EasyDAO.create({ daoType: 'LOCAL' });
foam.dao.EasyDAO.create({ daoType: 'MDAO' });
or specify the actual DAO class to use:
foam.dao.EasyDAO.create({ daoType: foam.dao.NullDAO });
Note that if you enable server-synchronization, the extra DAO created to store sync records will be of the same type.
For IndexedDB, disk or network-based DAO types, in-memory caching significantly
speeds up data access and querying. Add cache: true to automatically
set up an MDAO as an indexed, in-memory cache.
foam.dao.EasyDAO.create({ cache: true });
The addIndex() and addPropertyIndex() methods can be used to manually
add indexes to the cache.
To ensure your objects have unique IDs, you can use sequence numbers or guids
to auto-assign unique ids to the objects put() into the EasyDAO. Specify
the property name in seqProperty if you are not using the default of id as
your primary key.
foam.dao.EasyDAO.create({ seqNo: true, seqProperty: 'id' });
or
foam.dao.EasyDAO.create({ guid: true, seqProperty: 'id' });
You can’t use both at once, and they are optional if your objects already have unique identifiers.
If your object has a lot of String properties, enable dedup to ensure
that duplicate string values don’t eat up a lot of memory.
foam.dao.EasyDAO.create({ dedup: true });
To record a journal of all writes to your EasyDAO, set journal to a
foam.dao.Journal instance. EasyDAO then wraps the DAO in a JDAO, which
replays the journal on load and writes each put() and remove() to it.
On the server (Java), choose a journal with journalType instead, for example
foam.dao.JournalType.SINGLE_JOURNAL.
If your data model has imports, it will need a parent context from which to
find those import values. This can be provided by enabling contextualize
on your EasyDAO, and making sure the EasyDAO itself, or one of its
ancestors in the creation chain, exports those values.
foam.dao.EasyDAO.create({ contextualize: true });
You can enabled console logging of DAO operations by enabling logging:
foam.dao.EasyDAO.create({ logging: true });
And for benchmarking, timing of DAO operations is enabled with timing
foam.dao.EasyDAO.create({ timing: true });
Turn on syncWithServer to activate synchronization with a server.
Specify serverBox and syncProperty as well.
syncProperty indicates the property to synchronize on. This is typically
an integer property indicating the version last seen on the remote server.
Use an actual property reference (such as example.MyClass.MY_PROPERTY)
not the string name of the property.
serverBox is the Box used to contact the server-side DAO. On your
server, use an EasyDAO with isServer: true to provide the other end
of the connection.
syncPolling (true by default) activates polling, periodically checking in
with the server.
foam.dao.EasyDAO.create({
...
syncWithServer: true,
syncProperty: foo.myModel.VERSION,
serverBox: myServerBox,
syncPolling: true
});