Skip to content

Latest commit

 

History

History
975 lines (734 loc) · 24.4 KB

File metadata and controls

975 lines (734 loc) · 24.4 KB

Classes

Application

Application

containment
Controller

Controller

Loader

Loader

Model

Model

ModelManager

ModelManager

Route

Route

RouteSegment

RouteSegment

Router

Router

Functions

listen(port)Promise.<void>

Starts the server

runProjectInitializers()Promise.<void>

Runs all consumer initializers

start(port)Promise.<undefined, Error>

starts listening on the defined port

getOwner(child)Object

Get the owner of an object

setOwner(child, parent)

Set the owner object of the child object

getModelName(model)String

Returns the name of the model that is associated with this controller. If options.model is passed to controller it will take precedence, otherwise the controller will attempt to lookup a model matching the controller's name

get(name)Object

Returns the loaded module object by name

define(DataTypes)

Model definition

addModel(Model)

Adds a model to internal cache

class FooModel extends parch.Model {
  constructor(options) {
    super(options);
  }

associate(Foo, models) { }

define(DataTypes) { } }

modelManager.addModel(Model);

inject(context, lookup, propertyName)Object

Inject an object into another object

lookup(name)Object

Find an object in the registry. If the object isn't found in the registry, lookup will attempt to find it by requiring it in. If the require fails the lookup fails

register(name, Obj, options)Object

Register an object in the registry by name. If the name exists and it was registered with the { singleton: true } option, an error will be thrown.

hasLeadingSlash()Boolean

Determines if a path segment contains a leading slash /

hasTrailingSlash()Boolean

Determines if a path segment contains a trailing slash /

_buildSegment()String

Builds a consistent path segment, regardless of slashes

namespace(namespace, routes)

Bind a set of routes to a namespace. Uses {{#crossLink "Router/_buildRoute:method"}}_buildRoute{{/crossLink}} to normalize the path

resource(name, options)

Register a resource and wire up restful endpoints. Uses {{#crossLink "Router/_buildRoute:method"}}_buildRoute{{/crossLink}} to normalize the path and builds your 5 basic CRUD endpoints

route(path, options)

Register a single route. Uses {{#crossLink "Router/_buildRoute:method"}}_buildRoute{{/crossLink}} to normalize the path

createRecord(name)Object

Creates a record

findAll(name)Array | Object

Returns all records. Passing an optional query will query those records.

findOne(name)Array | Object

Returns a single record by id

queryRecord(name)Object

Returns the first record matching the passed query

updateRecord(name)Object

Updates a record by id

Application

Application

Kind: global class

new Application(options)

Param Type Description
options Object Application config options See configuration

application.app : Object

Restify application instance

Kind: instance property of Application
Properties

Name
app

application.projectDirectory : String

The consuming project's directory

Kind: instance property of Application
Properties

Name
projectDirectory

containment

Kind: global class

Controller

Controller

Kind: global class
Todo

  • add default restfull methods (index, show, etc)

new Controller(registry, options)

Param Type Description
registry Object module registry
options Object configuration options
options.model String override the default model name

controller.errors : Object

Restify errors map restify-errors

Kind: instance property of Controller
Properties

Name
errors

controller.models : Array

Object containing all models registered

Kind: instance property of Controller
Properties

Name
models

controller.modelName : String

The name of the model that belongs to this controller. If one cannot be found this will be undefined

Kind: instance property of Controller
Properties

Name
modelName

controller.modelNameLookup : String

Pluralized version of modelName

Kind: instance property of Controller
Properties

Name
modelNameLookup

controller.STATUS_CODES : Object

An object mapping of status codes and their corresponding value

Kind: instance property of Controller
Properties

Name
STATUS_CODES

Loader

Loader

Kind: global class

new Loader(settings)

Param Type Description
settings Object
settings.type String the loader type (controller, model, etc)
settings.filter RegExp loader filter
settings.path String module loader path

loader.type : String

Loader type

Kind: instance property of Loader
Properties

Name
type

loader.filter : String

Loader filter

Kind: instance property of Loader
Properties

Name
filter

loader.loadPath : String

Module load path

Kind: instance property of Loader
Properties

Name
loadPath

Model

Model

Kind: global class

new Model(options)

Param Type Description
options Object sequelize model options Sequelize Configuration

model.options : Object

Model options get passed directly to sequelize model definition. The main difference is the separation of model constructor options and model attribute definitions.

see http://docs.sequelizejs.com/en/v3/docs/models-definition/

Kind: instance property of Model
Properties

Name
options

ModelManager

ModelManager

Kind: global class

new ModelManager(settings)

Param Type
settings Object
settings.connection Object

modelManager.Sequelize : Object

Sequelize class

Kind: instance property of ModelManager
Properties

Name
Sequelize

modelManager.sequelize : Object

Sequelize instance

Kind: instance property of ModelManager
Properties

Name
sequelize

Route

Route

Kind: global class

new Route()

Returns: Object - path object

Param Type
...segment String

Example

new Route("foo", "/bar", "baz/");
/**
 * {
 *   path: "/foo/bar/baz"
 *   segments: [
 *     {{#crossLink "RouteSegment"}}RouteSegment{{/crossLink}},
 *     {{#crossLink "RouteSegment"}}RouteSegment{{/crossLink}},
 *     {{#crossLink "RouteSegment"}}RouteSegment{{/crossLink}}
 *   ]
 * }
 *

route.path : String

The path is the fully built path from segments e.g. /foo/bar/baz

Kind: instance property of Route
Properties

Name
path

route.segments : Array

All segments that make up this route. Consists of an array of {{#crossLink "RouteSegment"}}RouteSegments{{/crossLink}}

Kind: instance property of Route
Properties

Name
segments

RouteSegment

RouteSegment

Kind: global class

new RouteSegment(segment)

Param Type Description
segment String A single route segment

Example

new RouteSegment("/foo"); => { path: "/foo", segment: "/foo" }
new RouteSegment("foo"); => { path: "/foo", segment: "foo" }
new RouteSegment("/foo/"); => { path: "/foo", segment: "/foo/" }
new RouteSegment("foo/"); => { path: "/foo", segment: "foo/" }

routeSegment.segment : String

Segment represents the original path segment that was passed in

Kind: instance property of RouteSegment
Properties

Name
segment

routeSegment.path : String

The normalized path segment after removing/adding slashes

Kind: instance property of RouteSegment
Properties

Name
path

Router

Router

Kind: global class

new Router(registry)

Param Type Description
registry Object {{#crossLink "Registry"}}module registry{{/crossLink}}

router.loader : Object

Contains the model and controller {{#crossLink "Loader"}}loaders{{/crossLink}}

Kind: instance property of Router
Properties

Name
loader

router.namespacePrefix : String

An optional namespace to place before all routes (e.g. /v1)

Kind: instance property of Router
Properties

Name
namespacePrefix

Router.map(settings, callback) ⇒ undefined

configures router resources

Kind: static method of Router

Param Type Description
settings Object
callback function called with the router instance

listen(port) ⇒ Promise.<void>

Starts the server

Kind: global function

Param Type
port Number

runProjectInitializers() ⇒ Promise.<void>

Runs all consumer initializers

Kind: global function

start(port) ⇒ Promise.<undefined, Error>

starts listening on the defined port

Kind: global function

Param Type Description
port Number the port to listen on. Default: 3000

getOwner(child) ⇒ Object

Get the owner of an object

Kind: global function
Returns: Object - parent (owner)

Param Type Description
child Object object from which to fetch the parent

Example

class UserController extends parch.Controller {
  doStuff(req, res, next) {
    const store = getOwner(this).lookup("service:store");

    return store.findAll();
  }
}

setOwner(child, parent)

Set the owner object of the child object

Kind: global function

Param Type Description
child Object child object
parent Object parent (owner) object

Example

setOwner({}, "service:store", "store");

{}.store

getModelName(model) ⇒ String

Returns the name of the model that is associated with this controller. If options.model is passed to controller it will take precedence, otherwise the controller will attempt to lookup a model matching the controller's name

Kind: global function
Returns: String - modelName

Param Type Description
model String name of the model to use

get(name) ⇒ Object

Returns the loaded module object by name

Kind: global function
Returns: Object - module object

Param Type
name String

define(DataTypes)

Model definition

Kind: global function

Param Type Description
DataTypes Object sequelize DataTypes Object See Sequelize DataTypes

Example

define(DataTypes) {
  const user = {
    username: {
      allowNull: false
      type: DataTypes.STRING
    }
  };

  return user;
}

addModel(Model)

Adds a model to internal cache

class FooModel extends parch.Model {
  constructor(options) {
    super(options);
  }

  associate(Foo, models) {
  }

  define(DataTypes) {
  }
}

modelManager.addModel(Model);

Kind: global function

Param Type Description
Model Object parch model class

inject(context, lookup, propertyName) ⇒ Object

Inject an object into another object

Kind: global function
Returns: Object - context

Param Type Description
context Object the object to inject onto
lookup String name by which to look search for the injection in the registry
propertyName String optional property name of the newly injected object

Example

registry.inject(object, "service:store");
// object.store

registry.inject(object, "service:model-manager", "modelManager");
// object.modelManager

lookup(name) ⇒ Object

Find an object in the registry. If the object isn't found in the registry, lookup will attempt to find it by requiring it in. If the require fails the lookup fails

Kind: global function

Param Type Description
name String colon delimited lookup string "service:foo"

Example

registry.lookup("service:foo");

register(name, Obj, options) ⇒ Object

Register an object in the registry by name. If the name exists and it was registered with the { singleton: true } option, an error will be thrown.

Kind: global function
Returns: Object - Obj

Param Type Description
name String the name by which to register the object
Obj Object the object to store in the registry
options Object register options
options.instantiate Boolean instantiate the object when registering it
options.singleton Boolean only allow one registration of this name/object

Example

registry.register("service:foo", { foo: "bar" });

hasLeadingSlash() ⇒ Boolean

Determines if a path segment contains a leading slash /

Kind: global function
Example

new RouteSegment("foo").hasLeadingSlash(); => false
new RouteSegment("foo/").hasLeadingSlash(); => false
new RouteSegment("/foo/").hasLeadingSlash(); => true
new RouteSegment("/foo").hasLeadingSlash(); => true

hasTrailingSlash() ⇒ Boolean

Determines if a path segment contains a trailing slash /

Kind: global function
Example

new RouteSegment("foo").hasTrailingSlash(); => false
new RouteSegment("/foo").hasTrailingSlash(); => false
new RouteSegment("/foo/").hasTrailingSlash(); => true
new RouteSegment("foo/").hasTrailingSlash(); => true

_buildSegment() ⇒ String

Builds a consistent path segment, regardless of slashes

Kind: global function
Returns: String - path segment

namespace(namespace, routes)

Bind a set of routes to a namespace. Uses {{#crossLink "Router/_buildRoute:method"}}_buildRoute{{/crossLink}} to normalize the path

Kind: global function
Since: 0.9.0

Param Type Description
namespace String the namespace to bind to, with or without leading slash
routes Array.<Object> array of routes to bind to the namespace

Example

Router.map(function () {
  this.namespace("/users/:userId", [
    { path: "/setProfileImage", using: "user:setImage", method: "post" }
  ])
});

resource(name, options)

Register a resource and wire up restful endpoints. Uses {{#crossLink "Router/_buildRoute:method"}}_buildRoute{{/crossLink}} to normalize the path and builds your 5 basic CRUD endpoints

Kind: global function

Param Type Description
name String the resource name in singular form
options Object resource mapping options
options.namespace String mount the resource endpoint under a namespace

Example

Router.map(function () {
  this.resource("user");

  // Optionally prefix this resource with a namespace
  this.resource("user", { namespace: "api" })
});

route(path, options)

Register a single route. Uses {{#crossLink "Router/_buildRoute:method"}}_buildRoute{{/crossLink}} to normalize the path

Kind: global function

Param Type Description
path String the route path (e.g. /foo/bar)
options Object
options.using String colon delimited controller method identifier
options.method String http method

Example

Router.map(function () {
  this.route("/user/foo", { using: "users:foo", method: "get" });
});

createRecord(name) ⇒ Object

Creates a record

Kind: global function
Returns: Object - seralized record instance

Param
name

Example

return store.createRecord("user", {
  firstName: "hank",
  lastName: "hill"
});

findAll(name) ⇒ Array | Object

Returns all records. Passing an optional query will query those records.

Kind: global function
Returns: Array | Object - serialized record arry

Param
name

Example

return store.findAll("user");

return store.findAll("user", {
  firstName: "Jon"
})

findOne(name) ⇒ Array | Object

Returns a single record by id

Kind: global function
Returns: Array | Object - serialized record array

Param
name

Example

return store.findOne("user", 1);

queryRecord(name) ⇒ Object

Returns the first record matching the passed query

Kind: global function
Returns: Object - serialized record instance

Param
name

Example

return store.queryRecord("user", {
  firstName: "jon"
})

updateRecord(name) ⇒ Object

Updates a record by id

Kind: global function
Returns: Object - seralized record instance

Param
name

Example

return store.updateRecord("user", 1, { firstName: "Jane" });