es6/utils/Table.js
import Expressive from './Expressive'
import thinky from '../thinky'
const {r} = thinky
/**
* Some useful table extensions for Thinky
*/
export default class Table extends Expressive {
/**
* creates a Thinky model
*
* @param {Array} config The accepts the same arguments as thinky.createModel
*/
static createModel (...args) {
this._table = thinky.createModel.apply(thinky, args)
return this
}
/**
* returns a reference to the raw Table object
*
* @return {Thinky:Table} The table
*/
static get table () {
return this._table
}
/**
* returns what the table name is, useful for raw `r` commands
*/
static get tableName () {
return this.table.getTableName()
}
/**
* creates a new row on this Table
*
* @param {Object} attrs The attributes for the row
*/
static row (attrs) {
return new this.table(attrs)
}
/**
* drops this table on the database, primarily used to clean up after tests
*/
static drop () {
return r.tableDrop( this.tableName ).run()
}
/**
* empties all rows from the table
*/
static empty () {
return r.table( this.tableName ).delete()
}
/**
* emits when the table is online, for any interested Listeners
*/
constructor () {
super()
this.table = this.constructor.table
this.table.ready().then( _ => {
this.is("online")
})
}
}