Home

Awesome

<p align="center"> <a href="https://github.com/base/base"> <img height="250" width="250" src="https://raw.githubusercontent.com/base/base/master/docs/logo.png"> </a> </p>

base

NPM version NPM monthly downloads Build Status Gitter

<details> <summary><strong>Table of contents</strong></summary> </details> <details> <summary><strong>About</strong></summary>

Why use Base?

Base is a foundation for creating modular, unit testable and highly pluggable server-side node.js APIs.

Most importantly, once you learn Base, you will be familiar with the core API of all applications built on Base. This means you will not only benefit as a developer, but as a user as well.

Guiding principles

The core team follows these principles to help guide API decisions:

Minimal API surface

The API was designed to provide only the minimum necessary functionality for creating a useful application, with or without plugins.

Base core

Base itself ships with only a handful of useful methods, such as:

Be generic

When deciding on method to add or remove, we try to answer these questions:

  1. Will all or most Base applications need this method?
  2. Will this method encourage practices or enforce conventions that are beneficial to implementors?
  3. Can or should this be done in a plugin instead?

Composability

Plugin system

It couldn't be easier to extend Base with any features or custom functionality you can think of.

Base plugins are just functions that take an instance of Base:

var base = new Base();

function plugin(base) {
  // do plugin stuff, in pure JavaScript
}
// use the plugin
base.use(plugin);

Add "smart plugin" functionality with the base-plugins plugin.

Inheritance

Easily inherit Base using .extend:

var Base = require('base');

function MyApp() {
  Base.call(this);
}
Base.extend(MyApp);

var app = new MyApp();
app.set('a', 'b');
app.get('a');
//=> 'b';

Inherit or instantiate with a namespace

By default, the .get, .set and .has methods set and get values from the root of the base instance. You can customize this using the .namespace method exposed on the exported function. For example:

var Base = require('base');
// get and set values on the `base.cache` object
var base = Base.namespace('cache');

var app = base();
app.set('foo', 'bar');
console.log(app.cache.foo);
//=> 'bar'
</details>

Install

NPM

Install

Install with npm:

$ npm install --save base

yarn

Install with yarn:

$ yarn add base && yarn upgrade

Usage

var Base = require('base');
var app = new Base();

// set a value
app.set('foo', 'bar');
console.log(app.foo);
//=> 'bar'

// register a plugin
app.use(function() {
  // do stuff (see API docs for ".use")
});

API

Base

Create an instance of Base with the given cache and options. Learn about the cache object.

Params

Example

// initialize with `cache` and `options`
const app = new Base({isApp: true}, {abc: true});
app.set('foo', 'bar');

// values defined with the given `cache` object will be on the root of the instance
console.log(app.baz); //=> undefined
console.log(app.foo); //=> 'bar'
// or use `.get`
console.log(app.get('isApp')); //=> true
console.log(app.get('foo')); //=> 'bar'

// values defined with the given `options` object will be on `app.options
console.log(app.options.abc); //=> true

.is

Set the given name on app._name and app.is* properties. Used for doing lookups in plugins.

Params

Example

app.is('collection');
console.log(app.type);
//=> 'collection'
console.log(app.isCollection);
//=> true

.isRegistered

Returns true if a plugin has already been registered on an instance.

Plugin implementors are encouraged to use this first thing in a plugin to prevent the plugin from being called more than once on the same instance.

Params

Events

Example

const base = new Base();
base.use(function(app) {
  if (app.isRegistered('myPlugin')) return;
  // do stuff to `app`
});

// to also record the plugin as being registered
base.use(function(app) {
  if (app.isRegistered('myPlugin', true)) return;
  // do stuff to `app`
});

.use

Call a plugin function or array of plugin functions on the instance. Plugins are called with an instance of base, and options (if defined).

Params

Example

const app = new Base()
  .use([foo, bar])
  .use(baz)

.define

The .define method is used for adding non-enumerable property on the instance. Dot-notation is not supported with define.

Params

Example

// example of a custom arbitrary `render` function created with lodash's `template` method
app.define('render', (str, locals) => _.template(str)(locals));

.base

Getter/setter used when creating nested instances of Base, for storing a reference to the first ancestor instance. This works by setting an instance of Base on the parent property of a "child" instance. The base property defaults to the current instance if no parent property is defined.

Example

// create an instance of `Base`, this is our first ("base") instance
const first = new Base();
first.foo = 'bar'; // arbitrary property, to make it easier to see what's happening later

// create another instance
const second = new Base();
// create a reference to the first instance (`first`)
second.parent = first;

// create another instance
const third = new Base();
// create a reference to the previous instance (`second`)
// repeat this pattern every time a "child" instance is created
third.parent = second;

// we can always access the first instance using the `base` property
console.log(first.base.foo);
//=> 'bar'
console.log(second.base.foo);
//=> 'bar'
console.log(third.base.foo);
//=> 'bar'

Base.use

Static method for adding global plugin functions that will be added to an instance when created.

Params

Example

Base.use(function(app) {
  app.foo = 'bar';
});
const app = new Base();
console.log(app.foo);
//=> 'bar'

cache object

Cache

User-defined properties go on the cache object. This keeps the root of the instance clean, so that only reserved methods and properties on the root.

Base { cache: {} }

You can pass a custom object to use as the cache as the first argument to the Base class when instantiating.

const myObject = {};
const Base = require('base');
const base = new Base(myObject);

Toolkit suite

Base is part of the Toolkit suite of applications.

What is Toolkit?

Toolkit is a collection of node.js libraries, applications and frameworks for helping developers quickly create high quality node.js applications, web projects, and command-line experiences. There are many other libraries on NPM for handling specific tasks, Toolkit provides the systems and building blocks for creating higher level workflows and processes around those libraries.

Toolkit can be used to create a static site generator, blog framework, documentaton system, command line, task or plugin runner, and more!

Building Blocks

The following libraries can be used as "building blocks" for creating modular applications.

Lifecycle Applications

The following applications provide workflows and automation for common phases of the software development lifecycle. Each of these tools can be used entirely standalone or bundled together.

About

Related projects

Tests

Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:

$ npm install && npm test

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

If Base doesn't do what you need, please let us know.

Release History

See the changelog;

Authors

Jon Schlinkert

Brian Woodward

License

Copyright © 2018, Jon Schlinkert. MIT


This file was generated by verb-generate-readme, v0.6.0, on March 29, 2018.