Lens Databases
Lens uses the database and data synchronization systems from Meteor.
Types of Databases
Lens has six types of databases, representing different level of sharing.
In-Memory
Lens.DB.inMemory
creates a database that exists only in-memory in the
browser. It is not persisted across page loads, and is not synced to the
backend database server. See also Lens.Session
, a simple, reactive,
in-memory key-value store that's simpler than a full database.
App-Local
Lens.DB.app
creates a persistent database this is only accessible to
this app. It is synchronized with the backend database server and persists
across page loads.
Machine-Local
Lens.DB.machine
creates a persistent database that is accessible to
all apps running on this LuminAR device, but not to any other machines. It is synchronized with the backend database server and persists across page loads.
Published
Lens.DB.published
creates a persistent database that can be accessed
by other machines. It is synchronized with the backend database server on
this device. Other machines can discover the database using
Lens.Machines.findByDB
or Machine#dbs
. Changes to collections
on a published database are propagated in real-time: changes made by one
machine are immediately visible to all other machines connected to the database.
See Reactivity for information on leveraging reactive programming
to respond effectively to database changes. You can find out who is connected
to a published database using Lens.DB#peers
.
Shared
Published databases are owned by one machine, and other machines connect to
that machine. If you need to share a database but don't want to have to pick
a specific owner, use Lens.DB.shared
. Shared databases are temporary;
they are destroyed when the first machine that connected to it disconnects.
However, there is no need to pick an owner; multiple machines can simply
request the same shared database and Lens will handle picking which machine
will act as owner for that session. Because negotiating which machine will
act as owner takes some time, shared database are returned asynchronously
using a callback function. You can find out who is connected to a shared
database using Lens.DB#peers
.
Database Servers
In some cases, you may want to use a central database server rather than
the database servers on the LuminAR devices. Lens.DB.server
connects
to a central database server, which may be either a Lens DB server, or an
arbitrary Meteor application. Only Lens DB servers support creating collections
from a Lens app; if you're using your own Meteor app, you'll need to create
collections in the Meteor app, and then connect to them with
Lens.DB#Collection
. In addition, if you're not connected to a Lens
DB server, Lens.DB#peers
will not work correctly.
Using Databases
Once you have a database, you'll need to create a collection using
Lens.DB#Collection
. Then, you can use the
Meteor Collection API to query the
collection and insert new records:
var blogDB = Lens.DB.inMemory();
var Posts = blogDB.Collection("posts");
Posts.insert({title: "hello", body: "world"});
Posts.find().fetch() // -> [{title: "hello", body: "world"}]
Databases support reactivity, which means you can use Lens.autorun
and Lens.Templates
to automatically react to changes in a database
collection. See Reactivity for more information on reative
programming in Lens.