Class: Session

Session()

new Session()

Session represent a active connection to a specific database

All your operation over database happen here

pass following flags to observe sql and params

$ node bin/www --show-sql --shoq-values
Source:

Methods

all(entityName, optionsopt) → {Promise.<Array.<Model>>}

select rows from table

Parameters:
Name Type Attributes Description
entityName string
options SelectOptions <optional>
Source:
Returns:
Type
Promise.<Array.<Model>>

array()

same as all but return first cells as array.

Source:

count(entityName, whereopt) → {Promise.<number>}

count

Parameters:
Name Type Attributes Description
entityName string
where Condition <optional>
Source:
Returns:
Type
Promise.<number>

delete(entityName, whereopt)

Delete rows

Parameters:
Name Type Attributes Description
entityName string
where Condition <optional>
Source:

expired(entityName, data, array:) → {Promise.<boolean>}

Check if data different from database, return true when row exists and different

Parameters:
Name Type Description
entityName string
data object
array: Condition | array

pick those keys from data as Condition

Source:
Returns:
Type
Promise.<boolean>

find(entityName, where) → {Promise.<Model>}

find row from table

Parameters:
Name Type Description
entityName string
where Condition | any

you can pass primary key values directly

Source:
Returns:
Type
Promise.<Model>
Example
User.find(1)
UserRole.find(1, 2)
User.find({ age: 20, id: 2 })

insert(entityName, data) → {Promise.<object>}

Insert a new row into database

Parameters:
Name Type Description
entityName string
data object

data should be well formated

Source:
Returns:

the inserted id will be added to data instance.

Type
Promise.<object>

one()

same as all but return first row of the result.

Source:

paginate(entityName, options)

convert options's page/size to offset/limit for you

Parameters:
Name Type Description
entityName string
options SelectOptions

accept extra settings page and size and convert to offset/limit

Source:

query(sqlScript, sqlParams) → {Promise.<Result>}

Simple as its name, run query and return result as an Array(with a extra affectedRows property).

Parameters:
Name Type Description
sqlScript string

sql statement

sqlParams array
Source:
Returns:
Type
Promise.<Result>

refresh(entityName, data, conditionopt, propertyopt) → {Promise}

update row data with timestamp if data is different from database note that you need to provide primary key values in data object

Parameters:
Name Type Attributes Description
entityName string
data object
condition Condition | array <optional>
property string <optional>

updatedAt

Source:
Returns:
Type
Promise
Example
// assume we have a updatedAt column for User
User.refresh({ id: 10, firstName: 'new name' });

// or you have a different columnName(modifiedAt):
User.refresh({ firstName: 'new guy' }, { id: 10, siteId: 1 })

// or just compare only certain properties, this would not update:
User.refresh({ id: 10, remark: 'new remark' }, [ 'firstName', 'lastName', 'age' ] )

scalar(entityName, options) → {Promise.<any>}

return the value of first cell of the query result.

Parameters:
Name Type Description
entityName string
options SelectOptions
Source:
Returns:
Type
Promise.<any>

update(entityName, data, whereopt) → {Promise.<number>}

Update a row

Parameters:
Name Type Attributes Description
entityName string
data object
where Condition | array | string <optional>

perform a batch update against rows match conditions

omitted: treated as normal row updating by primary key

array: update rows whose has same values for that array of keys of data

string: as an one element array

Where: @see Where

Source:
Returns:

affectedRows

Type
Promise.<number>
Example
// normally:
update('User', { id: 1, age: 23 })

// batch
update('User', { rank: 100 }, { id: [ 'in', [1,2,3] ] } )
update('User', { age: 90, remark: 'old' }, 'age');

// incremental
update('User', { age: em.o('age').plus(10) })

upsert(entityName, data, whereopt)

Update a row or Insert one if affectedRows equals 0

Parameters:
Name Type Attributes Description
entityName string
data object
where Condition <optional>
Source: