Skip to content
aravindet edited this page Oct 10, 2012 · 2 revisions

resql object

resql.connect(options)

Creates a connection to the database and returns a Database object. Options include

  • database (mandatory)
  • username (default "")
  • password (default "")
  • dialect (default "mysql", also allowed "postgresql")
  • hostname (default "127.0.0.1")
  • port (default 3306 if mysql, 5432 if postgre)

Immediately returns a DB object. You can use this object to run queries without worrying about whether the connection has been established or not - queries will be queued up and run when the connection is made.

Column types

These are used when defining table Schema

  • resql.Boolean
  • resql.Integer
  • resql.Float
  • resql.String
  • resql.Text
  • resql.Date
  • resql.Serial
  • resql.Foreign(table)
  • resql.Relation(localColumn | table, foreignColumn | table, foreignColumn, foreignRelation)

Database class

Database#table(name, schema, options)

Defines a table. The schema is a hashmap of column names to column types. Options include indexes (array), softDelete (flag). It returns a Table object, and also adds that table to the Database#tables object.

Database#tables

A hashmap of all the tables defined in that database.

Database#create(force)

Creates all the tables in the database. Returns a Promise.

Database#restHandler()

Returns a REST handler function that can be used as Connect/Express middleware or directly in your HTTP server.

Promise class

See [CommonJS Promises/A] (http://wiki.commonjs.org/wiki/Promises/A) for detailed information. The classes Table and Row extend the Promise class.

Promise#then(callback)

Attach listeners which will be called when the underlying object becomes "ready" - i.e. when data is loaded for a Table (result set) or Row. Returns another Promise object, which will resolve when the callback function in the first promise resolves, and so on.

Table class

Represents a table in the database, and also a result set comprising of multiple rows from the same table. In the pre-resolved state, it stores the details of a query, after the query has been run it contains the results.

Table#column(name, type)

Add columns after the initial definition. Returns a Column object after adding it to Table#columns.

Table#relation(name, targetTable, targetColumn, targetRelation)

In ReSQL, relations are attached to a table object. There are two kinds, outward and inward.

Outward relations are created automatically when a column is added to the table with a name ending in "Id" or "_id" - the name of the relation will be the same as the column except without the trailing "Id". Outward relations refer to a single row from a foreign table.

Inward relations must be created manually by calling this method.

Example 1: Say there are tables Users and Bills, with a foreign key Bills.userId. When this schema is defined, an outbound relation Bills.user is created automatically, so that calling .user() on a Bill object will give the corresponding User object.

To create the Inbound relation Users.bills so that we can get all the bills of a particular user, call

    Users.relate('bills', 'Bills', 'userId');

Example 2: Assume further a table Products and another foreign key Bills.productId. Similarly to above an outbound relation Bills.product is created automatically. Suppose we want to make a relation User.products which will give all the products in all the bills associated with a user. We can do this by:

    Users.relate('products', 'Bills', 'userId', 'product');

Table#relate() returns a Relation object after adding it to Table#relations.

Table#one(filter)

Retrieves one row from the table using the filter criteria provided. Returns a Row object, which will resolve to the actual data when a .then() listener is attached.

Table#all(filter, options)

Retrieves multiple options from the table. The options is a hash which may have keys limits and orderBy. Returns a Table object with the same table name, which will resolve to the actual data when a .then() listener is attached.

Table#add(row)

Adds a new row to the table. Returns a promise.

Table#del(filter)

Delete row(s) from the table. Returns a promise.

Row

A row object represents a single object. It has methods named after the columns and relations in the schema of the table it belongs to.

Column methods

They return a Promise object which resolves to the value of the column.

Relation methods

They return a Row object (for outbound relations) or a Table object (for inbound ones) which can be made to resolve via .then() or chained further by calling their methods.