0.8.75 • Published 2 years ago

@rafterjs/watcher v0.8.75

Weekly downloads
40
License
GPL-3.0-or-later
Repository
github
Last release
2 years ago

Rafter

Rafter is a lightweight, slightly opinionated Javascript framework for rapid development of web applications.

Rafter:

  • is built using Typescript.
  • is built on top of Expressjs.
  • eliminates the tedious wiring of routes, middleware and services.
  • allows decoupling of services by utilizing dependency injection via the autoloading service container Awilix.
  • is flexible, reusable and testable.

Install

yarn add rafter

Run

yarn bootstrap & yarn build & yarn start:boilerplate

This will build and run the boilerplate project. You can access it via http://localhost:3000.

Building your own Rafter application

Dependency autoloading

Dependency autoloading is at the heart of Rafter, and the most opinionated portion of the framework. Rafter utilizes Awilix under the hood to automatically scan your application directory and register services. So there's no need to maintain configuration or add annotations, as long as the function or constructor arguments are the same name, it will wire everything up automatically.

Logger.ts

class Logger implements ILogger {
  public log(...args: any[]): void {
    console.log(args);
  }
}

MyService.ts

class MyService {
  private logger: ILogger;

  constructor(logger: ILogger) {
    this.logger = logger;
  }
  
  public run(): void {
    this.logger.log('I have been autowired');
  }
}

The Rafter autoloader will look recursively throughout your project for services, functions and config. This means you do not need to statically import all your dependencies, which maintains separation of concerns and improves reusability.

Rafter specific config files

The following configuration files are autoloaded by Rafter.

  • config.ts: a general application or module config.
  • middleware.js: registers services as middleware and loads them into the routes stack.
  • routes.ts: links controller services to route definitions.
  • pre-start-hooks.js: loads defined services before Rafter has started the server.

Config

The config file (config.ts) is a place to define all your application style config.

export default () => ({
  db: {
    connection: 'mongodb://localhost:27000/rafter' || process.env.NODE_DB_CONNECTION,
  },
  server: {
    port: 3000,
  },
  example: {
    message: `Hello Mars`,
  },
});

This config can be referenced within the injected dependencies.

Middleware

The middleware file (middleware.js) exports an array of service name references which will be loaded/registered in the order in which they were defined. eg.

export default (): IMiddlewareConfig[] => [`corsMiddleware`, `authenticationMiddleware`];

Routes

The routes file (routes.ts) exports an array of objects which define the http method, route, controller and action. eg.

export default (): IRouteConfig[] => [
  {
    endpoint: `/`,
    controller: `exampleController`,
    action: `index`,
    method: `get`,
  },
];

This would call exampleController.index(req, res) when the route GET / is hit. exampleController will be the name of the autoloaded service.

Pre start hooks

The routes file (pre-start-hooks.js) exports an array of service references that will be executed before Rafter has started, in the order in which they were defined. This is useful for instantiating DB connections, logging etc.

export default (): IPreStartHookConfig[] => [`connectDbService`];

An example of the connectDbService pre start hook would be:

export default (dbDao: IDBDatabaseDao, logger: ILogger) => {
  return async function connect(): Promise<IDbConnection> {
    logger.info(`Connecting to the database`);
    return dbDao.connect();
  };
};

By adding async to the function, Rafter will wait for it to be successfully returned before continuing to the next pre start hook, or will finish starting up if there are no more hooks.

Starting your Rafter application

Along with the aforementioned configs, all that is required to run Rafter is the following in an index.ts file:

import rafter from 'rafter';

const run = async () => {
  // define the paths you want to autoload
  const paths = [join(__dirname, '/**/!(*.spec).@(ts|js)')];
  
  // instantiate rafter
  const rafterServer = rafter({ paths });
  
  // start rafter server
  await rafterServer.start();
};

run();

Once start() is called, Rafter will:

  1. Scan through all your directories looking for config files and services.
  2. Autoload all your services into the service container.
  3. Run all the pre-start-hooks.
  4. Apply all the middleware.
  5. Register all the routes.
  6. Start the server.

To see an example project, visit the skeleton rafter app repository, or look at the included boilerplate application within packages.

Going deeper

Rafter is slightly opinionated; which means we have outlined specific ways of doing some things. Not as much as say, Sails or Ruby on Rails, but just enough to provide a simple and fast foundation for your project.

The foundations of the Rafter framework are:

  • Dependency injection
  • Autoloading services
  • Configuration

Dependency injection

With the advent of RequireJs, dependency injection (DI) had largely been thrown by the way side in favor of requiring / importing all your dependencies in Node. This meant that your dependencies were hard coded in each file, resulting in code that was not easily unit testable, nor replicable without rewrites.

eg.

With RequireJs

import mongoose from 'mongoose';

const connect = async connectionUrl => {
  await mongoose.connect(connectionUrl);
};

const find = async query => {
  await mongoose.find(query);
};

export { connect };

With DI

export class DbDao {
  private db: IDatabaseDao;
  private config: {connectionUrl: string};

  constructor(db: IDatabaseDao, config: {connectionUrl: string}) {
    this.db = db;
    this.config = config;
  }

  public async connect(): Promise<IDatabaseConnection> {
    return this.db.connect(this.config.connectionUrl);
  }

  public async find<T>(query: any): Promise<T> {
    return this.db.find(query);
  }
}

As you can see with DI, we can substitute any DB service rather than being stuck with mongoose. This insulates services which use a data store from caring what particular store it is. eg. If our DB becomes slow, we can simply substitute a CacheDao instead, and no other services would have to change.

0.8.74

2 years ago

0.8.73

2 years ago

0.8.75

2 years ago

0.8.70

2 years ago

0.8.72

2 years ago

0.8.71

2 years ago

0.8.69

2 years ago

0.8.68

2 years ago

0.8.67

2 years ago

0.8.66

2 years ago

0.8.63

2 years ago

0.8.65

2 years ago

0.8.64

2 years ago

0.8.62

2 years ago

0.8.61

2 years ago

0.8.60

2 years ago

0.8.56

2 years ago

0.8.55

2 years ago

0.8.58

2 years ago

0.8.57

2 years ago

0.8.54

2 years ago

0.8.59

2 years ago

0.8.52

3 years ago

0.8.51

3 years ago

0.8.53

3 years ago

0.8.50

3 years ago

0.8.49

3 years ago

0.8.45

3 years ago

0.8.47

3 years ago

0.8.46

3 years ago

0.8.48

3 years ago

0.8.44

3 years ago

0.8.43

3 years ago

0.8.41

3 years ago

0.8.40

3 years ago

0.8.42

3 years ago

0.8.39

3 years ago

0.8.37

3 years ago

0.8.34

3 years ago

0.8.36

3 years ago

0.8.35

3 years ago

0.8.33

3 years ago

0.8.32

3 years ago

0.8.30

3 years ago

0.8.31

3 years ago

0.8.23

3 years ago

0.8.22

3 years ago

0.8.25

3 years ago

0.8.24

3 years ago

0.8.21

3 years ago

0.8.20

3 years ago

0.8.27

3 years ago

0.8.26

3 years ago

0.8.29

3 years ago

0.8.28

3 years ago

0.8.19

3 years ago

0.8.9

3 years ago

0.8.8

3 years ago

0.8.12

3 years ago

0.8.11

3 years ago

0.8.14

3 years ago

0.8.13

3 years ago

0.8.10

3 years ago

0.8.16

3 years ago

0.8.15

3 years ago

0.8.18

3 years ago

0.8.17

3 years ago

0.8.7

3 years ago

0.8.6

3 years ago

0.8.5

3 years ago

0.8.4

3 years ago

0.8.1

3 years ago

0.8.3

3 years ago

0.8.2

3 years ago

0.7.1

3 years ago

0.7.0

3 years ago

0.7.0-alpha.0

3 years ago

0.6.0

3 years ago

0.5.1-alpha.1

3 years ago

0.5.1-alpha.0

3 years ago

0.5.0

4 years ago

0.4.2-alpha.6

4 years ago

0.4.2-alpha.5

4 years ago

0.4.2-alpha.4

4 years ago