Tutorial: Flitter Core

Flitter Core

libflitter is the core package that runs Flitter. Flitter's initialization structure is broken into individual bits called units, and these units are called one after the other to launch the application.

Each unit contains a function that, when called, initializes that particular unit. This function sets up the Unit's resources. However, the primary work of units are as services. Every unit provides some service that you can require in the rest of the injectable classes.

Injecting Services

You can require the instantiated module:libflitter/Unit~Unit services in other injectable classes (e.g. controllers/models/middleware) by adding their service name to the static services getter on the injectable class. For example, to access an instance of the module:libflitter/config/ConfigUnit~ConfigUnit service (to access app configs), we would request the configs service:

const { Model } = require('flitter-orm')

class MyAppModel extends Model {
	static get services() {
		return [...super.services, 'configs'] // It's important to keep the services required by parent classes in this array
	}

	// ... other code ...

	app_name() {
		return this.configs.get('app.name')
	}
}

Here, we overloaded the services property on the Model, which is provided by module:flitter-di/src/Injectable~Injectable to include the configs service in addition to the other services requested by parent classes.

This is the standard way of injecting services into other classes in Flitter.

Service Name Cheatsheet

Here are the names and service classes for common services provided by first-party Flitter modules. This list is not exhaustive, nor are all of these services installed with Flitter by default, but these are some of the most common ones:

Service Name Service Class Use Enabled by Default?
app module:libflitter/app/FlitterApp~FlitterApp The currently running application. Yes
canon module:libflitter/canon/CanonicalAccessUnit~CanonicalAccessUnit Can resolve any canonical name registered with the application. Yes
services module:libflitter/services/ServicesUnit~ServicesUnit Manages app-defined services. Yes
configs module:libflitter/config/ConfigUnit~ConfigUnit Can be used to resolve config values. Yes
utility module:libflitter/utility/UtilityUnit~UtilityUnit Provides several helpful utility methods. Yes
output module:libflitter/utility/services/Output~Output Can be used to log warnings/errors/info based on the configured logging level. Yes
database module:libflitter/database/DatabaseUnit~DatabaseUnit Manages the open database connection. Yes
scaffold module:flitter-orm/src/services/Connection~Connection Links flitter-orm to the rest of the application. Yes
models module:libflitter/database/DatabaseModelsUnit~DatabaseModelsUnit Can be used to access model classes. Yes
express module:libflitter/express/ExpressUnit~ExpressUnit Can be used to access the under-the-hood Express.js server. Yes
views module:libflitter/views/ViewEngineUnit~ViewEngineUnit Can be used to send views to a response. Yes
upload module:flitter-upload/UploadUnit~UploadUnit Can be used to access different file store backends. Yes
middlewares module:libflitter/middleware/MiddlewareUnit~MiddlewareUnit Can be used to access middleware handlers. Yes
controllers module:libflitter/controller/ControllerUnit~ControllerUnit Can be used to access controllers and their methods. Yes
routers module:libflitter/routing/RoutingUnit~RoutingUnit Loads and manages routing definitions. Yes
static module:libflitter/static/StaticUnit~StaticUnit Creates the server for the static assets endpoint, /assets. Yes
forms module:flitter-forms/FormsUnit~FormsUnit Can be used to access and initialize form validators. Yes
auth module:flitter-auth/AuthUnit~AuthUnit Load and manages auth providers and their configurations. Yes
error_handling module:libflitter/errors/ErrorUnit~ErrorUnit Creates last-resort routes for HTTP 404 and 500 errors. Yes
cli module:flitter-cli/CliUnit~CliUnit Registers CLI commands and can be used to invoke them programmatically. Yes
http module:libflitter/app/AppUnit~AppUnit Starts the HTTP server. Yes
jobs module:flitter-agenda/AgendaUnit~AgendaUnit Registers job classes and starts the job queue. No
crud module:flitter-crud/CrudUnit~CrudUnit Manages templates for the CRUD API generator. No
gridfs module:flitter-gridfs/GridFsUnit~GridFsUnit Registers models and DB connections for MongoDB GridFS support. No
less module:flitter-less/LessUnit~LessUnit Creates the /style-asset endpoint for just-in-time compilation of LESS stylesheets. No
sockets module:flitter-socket/SocketUnit~SocketUnit Registers WebSocket servers and their handlers. No