Blueprint
  • Blueprint Developer Guide
  • Quick Start
    • Getting Started
    • My First Application
      • Creating Your Application
      • Controllers
      • Routers & Routes
      • Services
      • Resources & Resource Controllers
      • Validating & Sanitizing Input
      • Unit Testing Your Application
      • Policies
  • Developer Guide
    • The Object Model
      • Introduction
      • Classes and Instances
      • Computed Properties
      • Aggregated Properties
      • Mixins
    • Routers and Controllers
      • Introduction
      • Routers
      • Controllers
      • Resources
    • Models
    • The Server
    • Policy Framework
    • Services
    • Messaging Framework
    • Configuration Management
    • Application and Resources
      • Lookup Operation
      • Views
      • Assets
    • Blueprint Modules
    • Blueprint Cluster
      • What is a Blueprint Cluster?
      • Running a Blueprint Cluster
      • Technical Details
    • Testing Framework
    • Command-line Interface (Coming Soon)
Powered by GitBook
On this page
  • Defining the Service
  • Implementing the Service
  • Using the Service
  1. Quick Start
  2. My First Application

Services

PreviousRouters & RoutesNextResources & Resource Controllers

Last updated 6 years ago

A service in Blueprint is a software component that operate in the background of the application, and outside of the , , and . Services can also be references by other software components, including services themselves. For example, you could recreate a service that performs periodic background tasks, or a service that manages connections to a database.

In our example, we want to migrate the data stored in the to a service. This will allow different software entities in the Blueprint application, such as another controller, to reference the same data as the .

Defining the Service

First, lets generate the service using the Blueprint cli.

blueprint generate service rentals

This command will generate an empty service.

app/services/rentals.js
const { Service, computed } = require ('@onehilltech/blueprint');

/**
 * @class rentals
 */
module.exports = Service.extend ({

});

The service is automatically loaded by the Blueprint application when started.

Implementing the Service

For the service, we need a method for adding rentals, deleting rentals, getting all the rentals, and getting a single rental. We are also going to initialize the service with the original data.

app/services/rentals.js
const { Service, computed } = require ('@onehilltech/blueprint');

/**
 * @class rentals
 */
module.exports = Service.extend ({
  _rentals: null,

  rentals: computed ({
    get () { return this._rentals; }
  }),

  init () {
    this._super.call (this, ...arguments);

    this._rentals = [
      {
        type: 'rentals',
        id: 'grand-old-mansion',
        attributes: {
          title: 'Grand Old Mansion',
          owner: 'Veruca Salt',
          city: 'San Francisco',
          category: 'Estate',
          bedrooms: 15,
          image: 'https://upload.wikimedia.org/wikipedia/commons/c/cb/Crane_estate_(5).jpg'
        }
      },
      {
        type: 'rentals',
        id: 'urban-living',
        attributes: {
          title: 'Urban Living',
          owner: 'Mike Teavee',
          city: 'Seattle',
          category: 'Condo',
          bedrooms: 1,
          image: 'https://upload.wikimedia.org/wikipedia/commons/0/0e/Alfonso_13_Highrise_Tegucigalpa.jpg'
        }
      },
      {
        type: 'rentals',
        id: 'downtown-charm',
        attributes: {
          title: 'Downtown Charm',
          owner: 'Violet Beauregarde',
          city: 'Portland',
          category: 'Apartment',
          bedrooms: 3,
          image: 'https://upload.wikimedia.org/wikipedia/commons/f/f7/Wheeldon_Apartment_Building_-_Portland_Oregon.jpg'
        }
      }];
  },

  // get a single rental
  get (id) {
    return this._rentals.find (rental => rental.id === id);
  },

  // add a rental to the list.
  add (rental) {
    return this._rentals.push (rental);
  },

  // remove the rental from the list.
  remove (id) {
    let index = this._rentals.findIndex (rental => rental.id === id);

    if (index === -1)
      return false;

    this._rentals.splice (index, 1);
    return true;
  }
});

Using the Service

app/controllers/rental.js
const {
  Controller,
  Action,
  service          // computed property for loading a service
} = require ('@onehilltech/blueprint');

/**
 * @class rental
 */
module.exports = Controller.extend ({
  // load the rentals service, name parameter is not needed
  rentals: service (),

  get () {
    return Action.extend ({
      execute (req, res) {
        // get the data from the rentals service
        const data = this.controller.rentals.rentals;
        res.status (200).json ({data});
      }
    })
  }
});

Let's break down the example above since we have made some changes to the original controller code. First, we load the service into the controller using the service() computed property function, which is available via the rentals property on the controller. Next, we updated the get() action implementation to retrieve the rental data from the service.

All actions can access to the parent controller using thethis.controller property.

Let's go back to our controller and re-implement the using the rentals service from above. To load a service into the controller, just use the computed property.

Now, when you restart the application and open the browser to , you will get the same response as before. The only difference this time is the data is pulled from the rentals service, and not from local data in the controller.

http://localhost:5000/api/rentals
controllers
routers
listeners
rental controller
rental controller
rental
rental.get
rental
service