import * as parse from 'co-body';
import { config } from '../../config';
import * as _ from 'lodash';
import * as Router from 'koa-router';
import * as rethinkdb from 'rethinkdb';
import * as rethinkdbdash from 'rethinkdbdash';

// RethinkDB table & reQl instance
const TABLE_NAME = 'todos';
const r: any = rethinkdbdash(config.rethinkdb);

// Retrieve all todo items
export const getAll = async (ctx: Router.IRouterContext, next: () => void) => {
  try {
    const userId = ctx.state.userId;
    if (!userId) {
      ctx.throw(400, 'userId required');
    }
    const result = await r.table(TABLE_NAME).filter({ userId }).orderBy('createdAt');
    ctx.body = JSON.stringify(result);
  } catch (e) {
    ctx.status = e.status || 500;
    ctx.body = e.message;
  }
};

// Retrieve all todo items non-archived
export const getAllNonArchived = async (ctx: Router.IRouterContext, next: () => void) => {
  try {
    const userId = ctx.state.userId;
    if (!userId) {
Example #2
0
import * as RDash from "rethinkdbdash";

const r = RDash({
    db: "send2pocket",
    host: process.env.RETHINKDB_HOST || "localhsot"
});
export default r;
export function connect(connectionOptions: ConnectionOptions = config.rethinkdb) {
    return r = rethinkdbdash(connectionOptions);
}
/// <reference path="rethinkdbdash.d.ts" />

import * as rConnect from 'rethinkdbdash';
var r = rConnect({db: 'test'});

async function test() {
  let connection = await r.connect('host');
  
  r.db('something').table<{ id:string, name:string }>('great').get('a').run(connection, (err, result) => {
    let id = result.id;
  });
  
  let instance = await r.db('something').table<{ id:string, name:string }>('great').get('a');
  
  let changes = await r.db('something').table<{ id:string, name:string }>('great').get('a').changes();
  changes.each((err, el) => console.log(el));
  
  let eqJoin = await r.db('something').table<{ id:string, name:string }>('great').eqJoin('left', r.db('that').table<{ num:number }>('other'));
  eqJoin.eachAsync(el => {
    el.left.id;
    el.right.num;
  });
  
  let a = 123;
  let b = await r.db('something').table<{ id:string, name:string }>('great').get('123')<{ something:boolean }>('subObject');
  let del = await r.db('something').table('a').delete({durability: 'hard'});
  let del2 = await r.db('something').table('a').get('123').replace((current) => {  });
}