Application-friendly RethinkDB client
Creates a new Db
object where:
name
- name of the database to use. Default is'test'
.options
- optional configuration:host
- string containing the host to connect to. Default is'localhost'
port
- port to connect on. Default is28015
.authKey
- string containing an established authorization keyuser
- string containing the user you want to perform actions as. Default is'admin'
password
- string containing the users passwordtimeout
- number indicating the number of seconds to wait for a connection to be established before timing out. Default is20
seconds.ssl
- ssl settings when RethinkDB requires a secure connection. This setting is passed to the RethinkDB module.ca
- certificate as a Buffer used for establishing a secure connection to rethink, see RethinkDB connect documentation
test
- boolean indicating if theDb
actions should take place. Useful for testing scenarios, do not use in production.extended
- object that is used to extend the members of the table objects.onConnect
- function to execute when a connection is established.onError
- function to execute when an error occurs. Signature isfunction(err)
whereerr
is an error object.onDisconnect
- function to execute when a disconnection occurs. Signature isfunction(willReconnect)
wherewillReconnect
is a boolean indicating if the connection will be reestablished.reconnect
- boolean indicating if the connection should be reestablished when interruptedreconnectTimeout
- number of milliseconds to wait between reconnect attempts, whenfalse
there is no timeout. Defaults to100
Create a connection to the database. Throws connection errors.
Close all database connections.
Note that this can alter data and indexes, not intended of production use.
Establish a connection if one doesn't exist and create the database and tables if they don't already exist. This function also decorates the db
object with helper properties to for using each of the tables.
[tables]
- array of strings with the name of each table to create
Only available when Db
is constructed in test mode. Disable a specific method from being performed on a table, used for testing.
table
- name of tablemethod
- name of method on table to disableoptions
- optional object with the following propertiesvalue
- value to return when the method is called. Can be an error object to simulate errors.
Only available when Db
is constructed in test mode. Enable a disabled method on a table, used for testing.
table
- name of tablemethod
- name of method on table to enable
Property that contains the RethinkDB module.
After a database connection exists and tables are established then the Db
object is decorated with properties for each table name containing various helper function. Below are the available functions for each table.
Retrieve a record in the table
with the given id
. id
itself can be an array of id
values if you want to retrieve multiple records.
id
- unique identifier of record to retrieve. Can be an array with values for each ID to retrieve.options
- optional object with the following propertiessort
- table key to sort results usingorder
-'descending'
or'ascending'
for sort order, Defaults to'ascending'
from
- index in result set to selectcount
- number of records to return in resultsfilter
- properties to pluck from the results
Retrieve all records for a table.
Determine if a record in the table
exists with the provided ID
id
- unique identifier of record to retrieve. Can be an array with values for each ID to retrieve.
Perform a query on the table using the provided criteria. Criteria is available on the Db
object and is listed in the criteria section below.
criteria
- db criteria functions chained together
Retrieve a single record from the provided criteria.
criteria
- db criteria functions chained together
Retrieve the number of records in the table that match the given criteria.
criteria
- db criteria functions chained together
Create new record(s) in the table. Each item can specify a unique id
property or allow rethink to generate one for them.
items
- item object or array of items to insert into the tableoptions
- optional object with the following propertiesmerge
- boolean, when true any conflicts with existing items will result in an update, when false an error is returnedchunks
- maximum number of updates to send to the database at the same time
Update an existing record with the provided changes.
ids
- an identifier or array of identifiers of records to update in the tablechanges
- the parts of the record to change and the values to change the parts to
Update an existing record with the provided changes.
updates
- an array of records to update (each must include an existing primary key)options
- optional settings where:chunks
- maximum number of updates to send to the database at the same time
Remove the records in the table that match the given criteria.
criteria
- db criteria functions chained together
Remove all records in the table.
Wait until all operations are complete and all data is persisted on permanent storage. Note that this function shouldn't be necessary for normal conditions.
Create the secondary indexes
on the table.
indexes
- a string or array of strings for each index to create
Subscribe to changes matching the given criteria for the table.
criteria
- db criteria functions chained togetheroptions
- optional object with the following propertieshandler
- handler function to execute when changes occur.reconnect
- boolean, reconnect if the connection to the feed is interruptedinitial
- boolean, include the initial results in the change feed