Prismatik / dendritic

The framework you use when you're not using a framework.
10 stars 1 forks source link

Dendritic

Build Status

Dendritic is a scaffolder and light framework for HTTP APIs. It will give you the bones of an API using JSON Schema, Express and RethinkDB.

Dendritic is designed to work in the Pirate stack.

If you're unfamiliar, the Pirate stack is made up of:

(Get it? RRRRR)


Development

Requirements

Getting started

npm install -g dendritic
mkdir my_new_project && cd my_new_project
dendritic base project_name_singular

And you'll have a minimal, well thought-out base app scaffold. Next:


Creating Controllers

dendritic resource model_name_singular

And it will add routes, a controller and database bootstrapping. You'll have Create, Read, Update and Delete. You can pass filter params for any property of your model to search the database on GET. All updates will be checked against your JSON schema for validity.

Defining Relationships

When creating controllers you also automatically create relationships to other models using the -s or -m switch.

As with model names, the related model names must be singular.

For example:-

dendritic resource product -s product_category,wholesaler

OR

dendritic resource product -m purchaser,warehouse_location

This will add the necessary properties to your model schema as well as setup tests to ensure the related model exists (endpoint can be accessed via a GET request).

Note: You can also use this switch when creating users since users are effectively just a special type of controller / schema i.e. dendritic user -s drivers_license -m motor_vehicle


Creating Users

dendritic user model_name_singular

This will create a controller (as above) but with additions to cater for a user model. This includes an email and password property on the schema, modified controller logic to ensure when a password is provided this is hashed and saved and also a sign in route which returns a JWT for use in authentication.

Finally providing a model-name when running the command is optional, the model name will default to user, but an alternative can be provided i.e. admin.


Migrations

Dendritic can provide a framework for running migrations against the rethinkdb database. To scaffold the migration framework run...

dendritic migration

You can then use the following commands...

Create a migration

migration create migration_name

This will generate a migration file and a migration test file. The migration file contains an up function to run your migration and a down function to rollback your migration. Migrations are created with the name you specify but prepended with a timestamp which ensures the files are ordered based on their creation date.

Run migrations

migrate up

This will run all migrations (execute the up function) that have not yet been run i.e. files in the /migrations folder. Dendritic tracks which migrations have already been run in the _migrations table in rethinkdb.

Rollback migrations

migrate down file_name

This will rollback all migrations (execute the down function) that have previously been run, up until AND including the file_name that you specify. For example if you had three migrations file1.js, file2.js, file3.js and all of these have been run, if you then use migrate down file2.js, file2.js and file3.js would be rolled back.

List migrations

migrate list

This will list all migrations that exist (based on the files found in /migrations) and inform you if they have yet been run.

Show mutex

migrate show_mutex

This will inform the user if the migrations table is currently locked which means either a migration is currently running OR a prior migration has failed and the lock has not been manually released.

Release mutex

migrate release_mutex

This will release any lock that exists on the migrations table, necessary when a migration has failed and the table has therefore not been automatically unlocked.


Notes

TODO