Modify: relay-todolist/data/database.js

Open relay-todolist/data/database.js in your favorite editor and replace it with the following code:


import pmongo from 'promised-mongo';

const db = pmongo('relaytest', ['users', 'todos']);

export function getUser() {
  return db.users.findOne({ name: 'admin' });
}

export function getTodos() {
  return db.todos.find({ user: 'admin' }).toArray().then((docs) => docs);
}

export function getTodo(id) {
  return db.todos.findOne({ _id: pmongo.ObjectId(id), user: 'admin' });
}

export function updateTodo(id, todo, completed) {
  if (!id || !todo || typeof completed !== 'boolean') {
    return new Promise((resolve, reject) => {
      let message = "";
      if (!id) {
        message += '"id" required to update Todo Item\n';
      }
      if (!todo) {
        message += '"todo" required to update Todo Item\n';
      }
      if (typeof completed !== 'boolean') {
        message += '"completed" should be a boolean\n';
      }

      reject(message);
    });
  }
  return db.todos.update({ _id: pmongo.ObjectId(id), user: 'admin' },
                           { $set: { todo, completed } })
                  .then(() => { return { todo: id }; });
}

export function addTodo(todo) {
  if (!todo) {
    return new Promise((resolve, reject) => {
      let message = '"todo" cannot be empty';
      reject(message);
    });
  }
  return db.todos.insert({ todo, completed: false, user: 'admin' });
}

export function removeTodo(id) {
  if (!id) {
    return new Promise((resolve, reject) => {
      reject('"id" required to remove a Todo item');
    });
  }
  return db.todos.remove({ _id: pmongo.ObjectId(id), user: 'admin' })
                 .then(() => { return { todo: id }; });
}
          
  • We start by creating a connection to our Mongo database relaytest along with access to the "users" and "todos" collection in Lines 1 - 3.
  • We then define a getUser() function that returns a promise with the user details. Since we are developing a single-user app for the purpose of this tutorial we are going to assume that all Todos are created for the admin user.
  • We then define a set of functions that create, update, delete and fetch Todos from our database in Lines 9 - 57.