The instructions and the sample-data file were moved to https://github.com/bajtos/loopback-workshop.
This is an extra exercise for the workshop Building a full-stack application with LoopBack and AngularJS.
Let's write a method Whiskey.prototype.ratingStats
that will provide
a breakdown of rating data for a given whiskey. This method should
be defined in common/models/whiskey.js
. Below is a template, it's
up to the reader to implement the statistical computation.
This is an extra exercise for the workshop Building a full-stack application with LoopBack and AngularJS.
Now that we have the first version of our application ready, let's switch the storage backend from an in-memory database to MySQL.
- Install
loopback-connector-mysql
var jdb = require('./'); | |
var ModelBuilder = jdb.ModelBuilder; | |
var DataSource = jdb.DataSource; | |
var Memory = require('./lib/connectors/memory'); | |
var assert = require('assert'); | |
var ID = 'custom-generated-id'; | |
var memory = new DataSource({connector: Memory}); | |
var modelBuilder = memory.modelBuilder; |
function createLoopBackApp() { | |
var app = express(); | |
// add loopback stuff | |
app.phase('routes').use(function(req, res, next) { | |
if (app._router) { | |
app._router.handle(res, res, next); | |
} else { | |
next(); | |
} |
Imagine a chat server persisting the messages, a simplified Slack chat. The domain is designed in the object-orientated style. In order to get realtime updates, we need to transport events emitted on the model constructor ("static" events) and on a model instance ("instance" events).
While it may not be immediately clear, the example is covering few other important LoopBack concepts too:
- authorization (
loopback.token
in REST) - current context (
loopback.context
in REST)
What is not covered:
- file uploads and downloads
-
API to group test cases into test suites, e.g.
describe
andit
-
setup/teardown hooks (
before
,beforeEach
,after
,afterEach
) -
Support for promises: test case returns a promise, the test runner waits until the promise is resolved
-
Output formatters: junit/xunit for CI, spec-like for humans (a list of failed tests is printed again at the end)
-
Run all tests in a single file, run a single test case, run a single test suite.
// Call fs.stat over and over again really fast. | |
// Then see how many times it got called. | |
// Yes, this is a silly benchmark. Most benchmarks are silly. | |
var path = require('path'); | |
var common = require('../common.js'); | |
var fs = require('fs'); | |
var FILES = [ | |
require.resolve('../../lib/assert.js'), |
THE STATUS IS NO LONGER TRUE, WE HAVE ADDED OPERATION HOOKS SINCE THEN.
SEE http://docs.strongloop.com/display/LB/Operation+hooks
At the moment, LoopBack provides two kinds of hooks: