Model Service

The model service is the main entry point into all of the functionality around models and real time data. Once connected to a domain, the model service can easily be obtained from the domain as follows:

const modelService = domain.models();

Create a Model

To create a new model you can simply call the create(collection, model, data) method on the model service. For example, to create a new Employee:

modelService.create("employee", "1834-4", {
  "id": "1834-4",
  fname: "John",
  lname: "Doe",
  username: "jdoe"
}).then(() => {
  console.log("model created!");
}).catch((err) => {
  console.error("no dice, buddy:", err);
});

Open a Model

To work with a model in real time you must open it. To open a model use the open(collection, model) method:

modelService.open("employee", "1834-4").then((model) => {
  console.log("model", model.modelId(), "opened");
});

This assumes that the model has already been created. We also provide a kind of "create and open" option: you can provide an optional third argument which is an initializer function that will be called if the model does not exist. This will create the model before opening it:

var initializer = () => {
  return {foo: "bar"}
};

modelService.open("employee", "1834-4", initializer).then((model) => {
  console.log("model opened!");
});

Remove a Model

To remove an existing model you can call the remove(collection, model) method on the model service. For example:

modelService.remove("employee", "1834-4").then(() => {
  console.log("John was canned :(");
});

results matching ""

    No results matching ""