All Projects → t4t5 → november-cli

t4t5 / november-cli

Licence: MIT License
❄️ Generate a Node.js API for your Ember.js app

Programming Languages

javascript
184084 projects - #8 most used programming language
HTML
75241 projects

Projects that are alternatives of or similar to november-cli

ember-sort-filter-table
A sortable/searchable table addon for ember cli
Stars: ✭ 13 (-74.51%)
Mutual labels:  ember, ember-data
ember-data-contentful
Ember Data adapter for contentful.com
Stars: ✭ 33 (-35.29%)
Mutual labels:  ember, ember-data
ember-airtable
Boilerplate for quickly prototyping apps with Airtable, Node & Ember
Stars: ✭ 21 (-58.82%)
Mutual labels:  ember, ember-data
Data
A data persistence library for Ember.js.
Stars: ✭ 2,998 (+5778.43%)
Mutual labels:  ember, ember-data
saule
JSON API library for ASP.Net Web API 2.
Stars: ✭ 74 (+45.1%)
Mutual labels:  ember, ember-data
ember-links-with-follower
Render a set of links with a "follower" line underneath. The follower moves to the active link, matching size and position on the page.
Stars: ✭ 43 (-15.69%)
Mutual labels:  ember
ember-new-relic
Adds New Relic to your Ember CLI app based on the app's environment
Stars: ✭ 29 (-43.14%)
Mutual labels:  ember
els-component-extraction-addon
Component extraction addon
Stars: ✭ 11 (-78.43%)
Mutual labels:  ember
ember-stripe-elements
A simple Ember wrapper for Stripe Elements.
Stars: ✭ 64 (+25.49%)
Mutual labels:  ember
glimmer-apollo
Ember and Glimmer integration for Apollo Client.
Stars: ✭ 32 (-37.25%)
Mutual labels:  ember
nodejs-with-postgres-api-example
k8s course example - node.js app with Postgres, Hapi.js and Swagger
Stars: ✭ 59 (+15.69%)
Mutual labels:  sequelize
Stackoverflow-Clone-Frontend
Clone project of a famous Q/A website for developers built using MySQL, Express, React, Node, Sequelize 🌐
Stars: ✭ 379 (+643.14%)
Mutual labels:  sequelize
nest-blog-api
Blog Web API with NestJs, Postgres, and Sequelize ORM
Stars: ✭ 69 (+35.29%)
Mutual labels:  sequelize
express-mysql-rest
Building the simple api with sequelize, mysql and express js. this repository contains the code about how to use sequelize with mysql at express js. for example i have provide the crud operation to this repository. You can also testing the api with chai and mocha with chai-http by this repository
Stars: ✭ 25 (-50.98%)
Mutual labels:  sequelize
ember-deep-tracked
Deep auto-tracking for when you just don't care, and want things to work (at the cost of performance in some situtations)
Stars: ✭ 20 (-60.78%)
Mutual labels:  ember
ember-headlessui
gavinjoyce.github.io/ember-headlessui/
Stars: ✭ 76 (+49.02%)
Mutual labels:  ember
ember-rapid-forms
Smart, Intuitive forms for Ember.js styled with Bootstrap, Multi layouts and Validation support.
Stars: ✭ 58 (+13.73%)
Mutual labels:  ember
sequelize-guard
An Authorization library for Sequelize.js
Stars: ✭ 26 (-49.02%)
Mutual labels:  sequelize
Nodejs-Tutorials
Nodejs Tutorial
Stars: ✭ 38 (-25.49%)
Mutual labels:  sequelize
ember-credit-card
"make your credit card form dreamy in one line of code"
Stars: ✭ 89 (+74.51%)
Mutual labels:  ember

November logo

November helps you generate a simple Node.js API tailored for Ember.js apps, with the help of Express and Sequelize.

Installation

$ npm install -g november-cli

Get started

$ november new my-app

This will create a new project with the following structure:

├── app
│   ├── actions
│   ├── controllers
│   ├── middleware
│   ├── models
│   ├── router.js
│
├── config
│   ├── config.json
│
├── lib
├── migrations
├── node_modules
├── public
├── server.js
├── test

By default, MySQL is used as a database, but you can use any relational database supported by Sequelize by changing the values in config/config.json.

To run the app, run npm start (or just nodemon if you have it installed) in your app’s directory and visit localhost:9000.

In your Ember.js app

Make sure you change the host in your Ember.js adapter file so that it can communicate with November:

# In your ember project folder
$ ember generate adapter
// app/adapters/application.js
import DS from "ember-data";

export default DS.RESTAdapter.reopen({ 
  host: 'http://localhost:9000'
});

Models

$ november generate model user

This generates:

  • A model file (app/models/user.js) for the user, which will determine the structure of the database table
  • Routes in app/router.js for creating, reading, updating and deleting users (based on the conventions of Ember Data). Feel free to remove the actions you don't need.
  • Controller files, which hook up the routes to database actions:
    • app/controllers/user/add.js
    • app/controllers/user/list.js
    • app/controllers/user/load.js
    • app/controllers/user/update.js
    • app/controllers/user/remove.js

With the app and your local database running in the background, visit localhost:9000/users, and you should see:

{
  "users": []
}

The table users has automatically been created in your database.

Actions

Actions are for API endpoints which are not specifically tied to any model.

$ november generate action login

This generates:

  • An action file (app/actions/login.js)
  • A route in app/router.js (POST by default)

Render()

The render()-method in your controllers is used for rendering both your models and your error messages. It takes a single argument.

Rendering models

If you pass a valid sequelize model to render(), it will generate that model according to the JSON API conventions used by Ember Data.

The most basic usage:

render(<your-sequelize-model>);

...which can also be typed like this:

render({
  model: <your-sequelize-model> 
});

returns:

{
  "user": {
    <...>
  }
}

If your sequelize model includes associated models, they are sideloaded by default:

{
  "user": {
    <...>
    "tweets": [1, 5]
  },
  "tweets": [
    <...>
  ]
}

However, you can also specify if you want some (or all) associations to be embedded instead.

Here we specify that we want the tweets-association to be embedded. If we wanted all associations to be embedded, we would set embedded: true

render({
  model: <your-sequelize-model>,
  embedded: ['tweets']
});

... which returns:

{
  "user": {
    <...>
    "tweets": [
      {
        id: 1,
        <...>
      },
      {
        id: 5,
        <...>
      }
    ]
  }
}

Rendering errors

Controllers generated by November rely heavily on promises. If they catch an error, they call render(error).

Let's say we accidentally search for a field (name) which doesn't exist in our database table:

// app/controllers/user/list.js
req.models.user
.findAll({
  where: {
    name: "Bob"
  }
})
.then(function(users) {
  // Not gonna happen
})
.catch(function(err) {
  render(err);
});

An error will be catched and render(err) will return this JSON to the client:

{
  "error": {
    "code": 500,
    "message": "Could not load users"
  }
}

... while still showing a more descriptive error to the developer in the console so that you can locate the problem:

A console error

You can also render your own exceptions to the user, by throwing a string with the error message or an array where the first element is the error code and the second is the error message:

// app/controllers/user/update.js
req.models.user.find({
  where: {
    username: req.params.username
  }
})
.then(function(user) {
  if (user.id !== req.user) {
    throw [403, "You are not allowed to edit this user!"]
  }
  return user.save();
})
.then(function(user) {
  // Not gonna happen
})
.catch(function(err) {
  render(err);
});

...what the client will see:

{
  "error": {
    "code": 403,
    "message": "You are not allowed to edit this user!"
  }
}

Todos

TDD is not really my thing, but it would be nice to get some automatic Mocha tests when you generate new models. :)

Contact

If you have any questions, feel free to ping me on Twitter or just open an issue!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].