Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
initial commit
  • Loading branch information
mattinsler committed May 19, 2016
0 parents commit 9b3759e
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .babelrc
@@ -0,0 +1,4 @@
{
"presets": ["es2015", "stage-0"],
"plugins": ["transform-decorators-legacy"]
}
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
node_modules
7 changes: 7 additions & 0 deletions README.md
@@ -0,0 +1,7 @@
# todo-backend-modern-js

This is an implementation of [Todo-Backend](http://www.todobackend.com/).

This implementation uses modern Javascript features like decorators. It combines
a few libraries meant to organize and simplify your node.js stack while taking
advantage of ES6/7 features.
10 changes: 10 additions & 0 deletions app-context.js
@@ -0,0 +1,10 @@
export default function() {
this.runlevel('configured')
.use('connie', 'file', 'config/${environment}.json');

this.runlevel('connected')
.use('access-mongo', '$mongodb')

this.runlevel('running')
.use('express');
}
3 changes: 3 additions & 0 deletions config/development.json
@@ -0,0 +1,3 @@
{
"mongodb": "mongodb://localhost/test"
}
3 changes: 3 additions & 0 deletions config/production.json
@@ -0,0 +1,3 @@
{
"mongodb": "${MONGODB_URL}"
}
27 changes: 27 additions & 0 deletions package.json
@@ -0,0 +1,27 @@
{
"name": "todo-backend",
"version": "1.0.0",
"description": "An implementation of Todo-Backend using modern Javascript features",
"author": "Matt Insler <matt.insler@gmail.com>",
"repository": "mattinsler/todo-backend-modern-js",
"engines": {
"node": "5.x.x",
"npm": "3.x.x"
},
"scripts": {
"start": "app-context start"
},
"license": "MIT",
"dependencies": {
"@mattinsler/garnish": "0.0.4",
"app-context": "0.3.1",
"app-context-access-mongo": "0.0.2",
"app-context-connie": "1.1.0",
"app-context-express": "1.1.0",
"babel-plugin-transform-decorators-legacy": "1.3.4",
"babel-preset-es2015": "6.9.0",
"babel-preset-stage-0": "6.5.0",
"cors": "2.7.1",
"routers": "0.1.4"
}
}
17 changes: 17 additions & 0 deletions router.js
@@ -0,0 +1,17 @@
import cors from 'cors';
import { classApiResolver } from 'routers';

export default function(app) {
const resolve = classApiResolver('routes');

app.use(cors());

app.get('/todos', resolve('todos#index'));
app.post('/todos', resolve('todos#create'));
app.delete('/todos', resolve('todos#clear'));
app.get('/todos/:id', resolve('todos#get'));
app.patch('/todos/:id', resolve('todos#patch'));
app.delete('/todos/:id', resolve('todos#destroy'));

app.use(resolve.errorHandler);
}
43 changes: 43 additions & 0 deletions routes/todos.js
@@ -0,0 +1,43 @@
import { view } from '@mattinsler/garnish';

const { mongodb } = APP;
const Todo = mongodb.createModel('todos');

const TodoView = {
id: '!_id',
url: ({ _id }, req) => `http://${req.get('host')}/todos/${_id}`
};

class TodosRoute {
@view(TodoView)
index(req) {
return Todo.array();
}

@view(TodoView)
create({ body }) {
return Todo.create({ completed: false, ...body });
}

clear() {
return Todo.remove({ override: true });
}

@view(TodoView)
get({ params: { id } }) {
return Todo.where({ _id: Todo.ObjectID(id) }).first();
}

@view(TodoView)
async patch({ body, params: { id } }) {
const query = Todo.where({ _id: Todo.ObjectID(id) });
await query.update({ $set: body });
return query.first();
}

destroy({ params: { id } }) {
return Todo.where({ _id: Todo.ObjectID(id) }).remove();
}
}

export default TodosRoute;

0 comments on commit 9b3759e

Please sign in to comment.