Skip to content

Latest commit

 

History

History
executable file
·
137 lines (108 loc) · 5.05 KB

database-connector.md

File metadata and controls

executable file
·
137 lines (108 loc) · 5.05 KB

A Database Connector

Our first Feathers example resides on the server only. We'll see how easy it is to use a database table.

This example adds some user items to a NeDB database table, reads them back and displays them.

Databases. We're using the NeDB database because it won't distract us from concentrating on Feathers. NeDB resembles the popular MongoDB database but requires neither installation nor configuration.

Feathers supports over 20 different databases. Everything we mention in this guide is applicable to all of them.

Working example

The source code contains lines like /// [dependencies] and //! [dependencies]. Ignore them. They are for automatic display of code snippets in this guide.

Feathers is modular

Feathers embodies the same spirit as the popular HTTP server framework Express. Feathers is comprised of small modules that are all completely optional, and the core weighs in at just a few hundred lines of code. How's that for light weight! Now you can see where Feathers got its name.

We require our dependencies.

import:'dependencies'

We start an instance of Feathers and define its services.

import:'feathers'

users is the only service we need and it's a database table located at examples/step/data/users.db. import:'services'

Create 3 users using Promises.

import:'create' Each create returns a promise which resolves into the item added into the database. NeDB will always add a unique _id property to the user item and the returned item will contain it.

Callbacks and Promises. users.create({ ... }, {}, (err, data) => { ... }) would create a user item using a callback signature. We however will use Promises in this guide because the Feathers team prioritizes them.

** Promise Refresher.** Promise.all([ ... ]).then(results => { ... }); Promise.all takes an array whose elements are JavaScript values or Promises. It returns a single Promise that will resolve if every promise in the array is resolved or reject if any promise in the array is rejected. The elements are resolved in parallel, not sequentially, so Promise.all is a great pattern with which to start independent actions. The then portion is called once all elements are resolved. It receives an array as a parameter. The n-th element of the array is the resolved value of the n-th element in Promise.all.

The 3 user items are now in the database, their values are returned in results. We issue a find for the entire table and print the results. import:'results'

** Promise Refresher.** user.find().then(results => ...); user.find() returns a Promise. .then(results => ...) waits for the Promise to resolve, i.e. for the find to finish. The zero, one or more items found in the table are returned in the results param.

| View the completed file db-connector/1.js.

Service methods

Feathers provides the following service methods:

find(params)
get(id, params)
create(data, params)
update(id, data, params)
patch(id, data, params)
remove(id, params)

Feathers supports a common way for querying, sorting, limiting and selecting find method calls as part of params, e.g. { query: { ... }, ... }. Querying also applies to update, patch and remove method calls if the id is set to null.

** ProTip:** The find method does not guarantee an order for the returned items.

Results

Run the program with node ./examples/step/01/db-connector/1.js. The console displays:

feathers-guide$ node ./examples/step/01/db-connector/1.js
created Jane Doe item
 { email: 'jane.doe@gmail.com',
  password: 'X2y6',
  role: 'admin',
  _id: '6Rq7O4RPYEO2TdAn' }
created John Doe item
 { email: 'john.doe@gmail.com',
  password: 'i6He',
  role: 'user',
  _id: 'Q2bnsBRfO1ScqoqY' }
created Judy Doe item
 { email: 'judy.doe@gmail.com',
  password: '7jHw',
  role: 'user',
  _id: 'Tymf6Nailusd5MZD' }
find all items
 [ { email: 'jane.doe@gmail.com',
    password: 'X2y6',
    role: 'admin',
    _id: '6Rq7O4RPYEO2TdAn' },
  { email: 'john.doe@gmail.com',
    password: 'i6He',
    role: 'user',
    _id: 'Q2bnsBRfO1ScqoqY' },
  { email: 'judy.doe@gmail.com',
    password: '7jHw',
    role: 'user',
    _id: 'Tymf6Nailusd5MZD' } ]

Boilerplate. Feathers requires little boilerplate. It took only 15 lines of code to connect to a database.

Is anything wrong, unclear, missing?

Leave a comment.