Nxs db - xkp/Doc GitHub Wiki

##Overview

node.xs supports database access via a sql dsl. We currently support mysql databases with more on its way.

##Usage

The general syntax of our sql dsl is:

sql(connection = <Connection Object>)
{
    <optional assigment> <sql text with xs parameters>
}

####Connection

A connection object encapsulates the data needed to establish a sql connection:

  • hostname: the address where the sql server resides, defaults to 'localhost'
  • user: sql user
  • password: sql password
  • database: the name of the db this call will be connecting to

####Assignments For queries returning data the dsl allows in query assignments, the returning row(s) will be assigned to the variable specified.

####Parameters The syntax of the sql dsl allows for external parameters to be embedded on the query by using the @ symbol followed by an expression. This expression must be resolvable in the current context.

Note: the current implementation uses string substitution for the parameters,meaning string parameters must be enclosed in single quotes, see examples.

####Examples

1- declaring a connection as an application property:

property myConnection = 
{
    hostname: myHost, 
    user: myUser, 
    password: myPass, 
    database: myDB
};

on myPage.render()
{
    sql(connection = myConnection)
    {
        ...
    }
}

2- Reading results:

var results; 
sql(connection = myConnection)
{
    results = select * from myTable 
}

3- Using parameters

var results; 
var limit         = get_limit(); //your logic
var myStringValue = get_string();
sql(connection = myConnection)
{
    results = select * from myTable 
              where myField > @limit and
                    myStringColumn = '@myStringValue' 
}