xc-data-mapper
v0.3.1
Published
Map database tables with a driver
Downloads
4
Readme
Install & setup
API Reference
Base class for models
Kind: global class
- BaseModelSql
- new BaseModelSql(args)
- .$db ⇒ Object
- .transaction() ⇒ Promise.<Object>
- .commit(trx) ⇒ Promise.<void>
- .rollback(trx) ⇒ Promise.<void>
- .isCompleted(trx) ⇒ Promise.<void>
- .insert(data, [trx]) ⇒ Promise.<Array.<Object>> | Promise.<Array.<Number>>
- .updateByPk(id, data, [trx]) ⇒ Promise.<Number>
- .delByPk(id, [trx]) ⇒ Promise.<Number>
- .insertByFk(args, [trx]) ⇒ Promise.<Array.<Object>> | Promise.<Array.<Object>>
- .updateByFk(args, [trx]) ⇒ Promise.<Number>
- .update(args, [trx]) ⇒ Promise.<Number>
- .delByFk(args, [trx]) ⇒ Promise.<Number>
- .del(args, [trx]) ⇒ Promise.<Number>
- .insertb(data) ⇒ Promise.<Array.<Object>> | Promise.<Array.<Number>>
- .updateb(data) ⇒ Promise.<Array.<Number>>
- .delb(ids) ⇒ Promise.<Array.<Number>>
- .readByPk(id) ⇒ Promise.<Object>
- .readByFk(args) ⇒ Promise.<Object>
- .exists(id) ⇒ Promise.<boolean>
- .existsByFk(id) ⇒ Promise.<boolean>
- .raw(queryString, params) ⇒ Promise
- .hasManyChildren(args) ⇒ Promise.<Array.<Object>>
- .hasManyList(args) ⇒ Promise.<Array.<Object>>
- .belongsTo(args) ⇒ Promise.<Array.<Object>>
- .hasManyListGQL(args, ids) ⇒ Promise.<Object.<string, Array.<Object>>>
- .hasManyListCount(args, ids) ⇒ Promise.<Object.<string, Array.<Object>>>
- .beforeInsert(data, trx)
- .afterInsert(response, trx)
- .errorInsert(err, data, trx)
- .beforeUpdate(data, trx)
- .afterUpdate(response, trx)
- .errorUpdate(err, data, trx)
- .beforeDelete(data, trx)
- .afterDelete(response, trx)
- .errorDelete(err, data, trx)
- .beforeInsertb(data, trx)
- .afterInsertb(response, trx)
- .errorInsertb(err, data, trx)
- .beforeUpdateb(data, trx)
- .afterUpdateb(response, trx)
- .errorUpdateb(err, data, trx)
- .beforeDeleteb(data, trx)
- .afterDeleteb(response, trx)
- .errorDeleteb(err, data, trx)
new BaseModelSql(args)
Returns: BaseModelSql - Returns BaseModelSql reference.
| Param | Type | Description | | --- | --- | --- | | args | Object | | | args.knex | Object | Knex instance | | args.tableName | String | table name | | args.columns | Array.<Object> | columns | | args.pks | Array.<Object> | primary keys | | args.hasMany | Array.<Object> | has many relations | | args.belongsTo | Array.<Object> | belongs to relations | | args.hooks | Object | afterInsert, beforeInsert, errorInsert, afterUpdate, beforeUpdate, errorUpdate, afterDelete, beforeDelete, errorDelete |
baseModelSql.$db ⇒ Object
Kind: instance property of BaseModelSql
Returns: Object - knex instance attached to a table
baseModelSql.transaction() ⇒ Promise.<Object>
Returns a transaction reference
Kind: instance method of BaseModelSql
Returns: Promise.<Object> - Transaction reference
baseModelSql.commit(trx) ⇒ Promise.<void>
Commit transaction
Kind: instance method of BaseModelSql
| Param | Type | Description | | --- | --- | --- | | trx | Object | Transaction reference |
baseModelSql.rollback(trx) ⇒ Promise.<void>
Rollback transaction
Kind: instance method of BaseModelSql
| Param | Type | Description | | --- | --- | --- | | trx | Object | Transaction reference |
baseModelSql.isCompleted(trx) ⇒ Promise.<void>
Transaction completed
Kind: instance method of BaseModelSql
| Param | Type | Description | | --- | --- | --- | | trx | Object | Transaction reference |
baseModelSql.insert(data, [trx]) ⇒ Promise.<Array.<Object>> | Promise.<Array.<Number>>
Creates row in table
Kind: instance method of BaseModelSql
| Param | Type | Default | Description | | --- | --- | --- | --- | | data | Object | | row data | | [trx] | Object | | knex transaction object |
baseModelSql.updateByPk(id, data, [trx]) ⇒ Promise.<Number>
Update table row data by primary key
Kind: instance method of BaseModelSql
Returns: Promise.<Number> - 1 for success, 0 for failure
| Param | Type | Default | Description | | --- | --- | --- | --- | | id | String | | primary key separated by ___ | | data | Object | | table row data | | [trx] | Object | | knex transaction object |
baseModelSql.delByPk(id, [trx]) ⇒ Promise.<Number>
Delete table row data by primary key
Kind: instance method of BaseModelSql
Returns: Promise.<Number> - 1 for success, 0 for failure
| Param | Type | Default | Description | | --- | --- | --- | --- | | id | String | | primary key separated by ___ | | [trx] | Object | | knex transaction object |
baseModelSql.insertByFk(args, [trx]) ⇒ Promise.<Array.<Object>> | Promise.<Array.<Object>>
Creates row in this table under a certain parent
Kind: instance method of BaseModelSql
Todo
- [ ] should return inserted record
| Param | Type | Description | | --- | --- | --- | | args | Object | | | args.data | Object | row data | | args.parentId | String | parent table id | | args.parentTableName | String | parent table name | | [trx] | Object | knex transaction object |
baseModelSql.updateByFk(args, [trx]) ⇒ Promise.<Number>
Update table row data by primary key and foreign key
Kind: instance method of BaseModelSql
Returns: Promise.<Number> - 1 for success, 0 for failure
| Param | Type | Description | | --- | --- | --- | | args | Object | | | args.id | String | primary key separated by ___ | | args.parentId | String | parent table id | | args.parentTableName | String | parent table name | | args.data | Object | table row data | | [trx] | Object | knex transaction object |
baseModelSql.update(args, [trx]) ⇒ Promise.<Number>
Update table row data by using where clause
Kind: instance method of BaseModelSql
Returns: Promise.<Number> - number of rows affected
| Param | Type | Description | | --- | --- | --- | | args | Object | | | args.where | String | update where clause | | args.data | Object | table row data | | [trx] | Object | knex transaction object |
baseModelSql.delByFk(args, [trx]) ⇒ Promise.<Number>
Delete table row data by primary key and foreign key
Kind: instance method of BaseModelSql
Returns: Promise.<Number> - 1 for success, 0 for failure
| Param | Type | Description | | --- | --- | --- | | args | Object | | | args.id | String | primary key separated by ___ | | args.parentId | String | parent table id | | args.parentTableName | String | parent table name | | [trx] | Object | knex transaction object |
baseModelSql.del(args, [trx]) ⇒ Promise.<Number>
Delete table row data by where conditions
Kind: instance method of BaseModelSql
Returns: Promise.<Number> - number of deleted records
| Param | Type | Description | | --- | --- | --- | | args | Object | | | args.where | String | where clause for deleting | | [trx] | Object | knex transaction object |
baseModelSql.insertb(data) ⇒ Promise.<Array.<Object>> | Promise.<Array.<Number>>
Creates multiple rows in table
Kind: instance method of BaseModelSql
| Param | Type | Description | | --- | --- | --- | | data | Array.<Object> | row data |
baseModelSql.updateb(data) ⇒ Promise.<Array.<Number>>
Update bulk - happens within a transaction
Kind: instance method of BaseModelSql
Returns: Promise.<Array.<Number>> - - 1 for success, 0 for failure
| Param | Type | Description | | --- | --- | --- | | data | Array.<Object> | table rows to be updated |
baseModelSql.delb(ids) ⇒ Promise.<Array.<Number>>
Bulk delete happens within a transaction
Kind: instance method of BaseModelSql
Returns: Promise.<Array.<Number>> - - 1 for success, 0 for failure
| Param | Type | Description | | --- | --- | --- | | ids | Array.<Object> | rows to be deleted |
baseModelSql.readByPk(id) ⇒ Promise.<Object>
Reads table row data
Kind: instance method of BaseModelSql
Returns: Promise.<Object> - Table row data
| Param | Type | Description | | --- | --- | --- | | id | String | primary key separated by ___ |
baseModelSql.readByFk(args) ⇒ Promise.<Object>
Reads table row data under a certain parent
Kind: instance method of BaseModelSql
Returns: Promise.<Object> - returns row
| Param | Type | Description | | --- | --- | --- | | args | Object | | | args.id | Object | primary key separated by ___ | | args.parentId | String | parent table id | | args.parentTableName | String | parent table name |
baseModelSql.exists(id) ⇒ Promise.<boolean>
Table row exists
Kind: instance method of BaseModelSql
Returns: Promise.<boolean> - - true for exits and false for none
| Param | Type | Description | | --- | --- | --- | | id | String | ___ separated primary key string |
baseModelSql.existsByFk(id) ⇒ Promise.<boolean>
Table row exists
Kind: instance method of BaseModelSql
Returns: Promise.<boolean> - - true for exits and false for none
| Param | Type | Description | | --- | --- | --- | | id | String | ___ separated primary key string |
baseModelSql.raw(queryString, params) ⇒ Promise
Runs raw query on database
Kind: instance method of BaseModelSql
Returns: Promise - - return raw data from database driver
| Param | Type | Description | | --- | --- | --- | | queryString | String | query string | | params | Array.<Object> | paramaterised values in an array for query |
baseModelSql.hasManyChildren(args) ⇒ Promise.<Array.<Object>>
Gets child rows for a parent row in this table
Kind: instance method of BaseModelSql
Returns: Promise.<Array.<Object>> - return child rows
| Param | Type | Default | Description | | --- | --- | --- | --- | | args | Object | | | | args.child | String | | child table name | | args.parentId | String | | pk | | [args.fields] | String | * | commas separated column names of this table | | [args.where] | String | | where clause with conditions within () | | [args.limit] | String | | number of rows to be limited (has default,min,max values in config) | | [args.offset] | String | | offset from which to get the number of rows | | [args.sort] | String | | comma separated column names where each column name is columnName ascending and -columnName is columnName descending |
baseModelSql.hasManyList(args) ⇒ Promise.<Array.<Object>>
Gets parent list along with children list
Kind: instance method of BaseModelSql
| Param | Type | Default | Description | | --- | --- | --- | --- | | args | Object | | | | args.childs | String | | comma separated child table names | | [args.fields] | String | | commas separated column names of this table | | [args.fields] | String | | commas separated column names of child table( is a natural number 'i' where i is index of child table in comma separated list) | | [args.where] | String | | where clause with conditions within () | | [args.where*] | String | | where clause with conditions within ()(* is a natural number 'i' where i is index of child table in comma separated list) | | [args.limit] | String | | number of rows to be limited (has default,min,max values in config) | | [args.limit*] | String | | number of rows to be limited of child table(* is a natural number 'i' where i is index of child table in comma separated list) | | [args.offset] | String | | offset from which to get the number of rows | | [args.offset*] | String | | offset from which to get the number of rows of child table(* is a natural number 'i' where i is index of child table in comma separated list) | | [args.sort] | String | | comma separated column names where each column name is columnName ascending and -columnName is columnName descending | | [args.sort*] | String | | comma separated column names where each column name is columnName ascending and -columnName is columnName descending(* is a natural number 'i' where i is index of child table in comma separated list) |
baseModelSql.belongsTo(args) ⇒ Promise.<Array.<Object>>
Gets child list along with its parent
Kind: instance method of BaseModelSql
| Param | Type | Default | Description | | --- | --- | --- | --- | | args | Object | | | | args.parents | String | | comma separated parent table names | | [args.fields] | String | | commas separated column names of this table | | [args.fields] | String | | commas separated column names of parent table( is a natural number 'i' where i is index of child table in comma separated list) | | [args.where] | String | | where clause with conditions within () | | [args.limit] | String | | number of rows to be limited (has default,min,max values in config) | | [args.offset] | String | | offset from which to get the number of rows | | [args.sort] | String | | comma separated column names where each column name is columnName ascending and -columnName is columnName descending |
baseModelSql.hasManyListGQL(args, ids) ⇒ Promise.<Object.<string, Array.<Object>>>
Returns key value paired grouped children list
Kind: instance method of BaseModelSql
Returns: Promise.<Object.<string, Array.<Object>>> - key will be parent pk and value will be child list
| Param | Type | Description | | --- | --- | --- | | args | Object | | | args.child | String | child table name | | ids | Array.<String> | array of parent primary keys | | [args.where] | String | where clause with conditions within () | | [args.limit] | String | number of rows to be limited (has default,min,max values in config) | | [args.offset] | String | offset from which to get the number of rows | | [args.sort] | String | comma separated column names where each column name is columnName ascending and -columnName is columnName descending |
baseModelSql.hasManyListCount(args, ids) ⇒ Promise.<Object.<string, Array.<Object>>>
Returns key value paired grouped children list
Kind: instance method of BaseModelSql
Returns: Promise.<Object.<string, Array.<Object>>> - key will be parent pk and value will be child list
| Param | Type | Description | | --- | --- | --- | | args | Object | | | args.child | String | child table name | | ids | Array.<String> | array of parent primary keys | | [args.where] | String | where clause with conditions within () | | [args.limit] | String | number of rows to be limited (has default,min,max values in config) | | [args.offset] | String | offset from which to get the number of rows | | [args.sort] | String | comma separated column names where each column name is columnName ascending and -columnName is columnName descending |
baseModelSql.beforeInsert(data, trx)
Before Insert is a hook which can be override in subclass
Kind: instance abstract method of BaseModelSql
| Param | Type | Description | | --- | --- | --- | | data | Object | insert data | | trx | Object | knex transaction reference |
baseModelSql.afterInsert(response, trx)
After Insert is a hook which can be override in subclass
Kind: instance abstract method of BaseModelSql
| Param | Type | Description | | --- | --- | --- | | response | Object | inserted data | | trx | Object | knex transaction reference |
baseModelSql.errorInsert(err, data, trx)
After Insert is a hook which can be override in subclass
Kind: instance abstract method of BaseModelSql
| Param | Type | Description | | --- | --- | --- | | err | Error | Exception reference | | data | Object | insert data | | trx | Object | knex transaction reference |
baseModelSql.beforeUpdate(data, trx)
Before Update is a hook which can be override in subclass
Kind: instance abstract method of BaseModelSql
| Param | Type | Description | | --- | --- | --- | | data | Object | update data | | trx | Object | knex transaction reference |
baseModelSql.afterUpdate(response, trx)
After Update is a hook which can be override in subclass
Kind: instance abstract method of BaseModelSql
| Param | Type | Description | | --- | --- | --- | | response | Object | updated data | | trx | Object | knex transaction reference |
baseModelSql.errorUpdate(err, data, trx)
Error update is a hook which can be override in subclass
Kind: instance abstract method of BaseModelSql
| Param | Type | Description | | --- | --- | --- | | err | Error | Exception reference | | data | Object | update data | | trx | Object | knex transaction reference |
baseModelSql.beforeDelete(data, trx)
Before delete is a hook which can be override in subclass
Kind: instance abstract method of BaseModelSql
| Param | Type | Description | | --- | --- | --- | | data | Object | delete data | | trx | Object | knex transaction reference |
baseModelSql.afterDelete(response, trx)
After Delete is a hook which can be override in subclass
Kind: instance abstract method of BaseModelSql
| Param | Type | Description | | --- | --- | --- | | response | Object | Deleted data | | trx | Object | knex transaction reference |
baseModelSql.errorDelete(err, data, trx)
Error delete is a hook which can be override in subclass
Kind: instance abstract method of BaseModelSql
| Param | Type | Description | | --- | --- | --- | | err | Error | Exception reference | | data | Object | delete data | | trx | Object | knex transaction reference |
baseModelSql.beforeInsertb(data, trx)
Before insert bulk is a hook which can be override in subclass
Kind: instance abstract method of BaseModelSql
| Param | Type | Description | | --- | --- | --- | | data | Array.<Object> | insert data | | trx | Object | knex transaction reference |
baseModelSql.afterInsertb(response, trx)
After insert bulk is a hook which can be override in subclass
Kind: instance abstract method of BaseModelSql
| Param | Type | Description | | --- | --- | --- | | response | Array.<Object> | inserted data | | trx | Object | knex transaction reference |
baseModelSql.errorInsertb(err, data, trx)
Error insert bulk is a hook which can be override in subclass
Kind: instance abstract method of BaseModelSql
| Param | Type | Description | | --- | --- | --- | | err | Error | Exception reference | | data | Object | delete data | | trx | Object | knex transaction reference |
baseModelSql.beforeUpdateb(data, trx)
Before update bulk is a hook which can be override in subclass
Kind: instance abstract method of BaseModelSql
| Param | Type | Description | | --- | --- | --- | | data | Array.<Object> | update data | | trx | Object | knex transaction reference |
baseModelSql.afterUpdateb(response, trx)
After update bulk is a hook which can be override in subclass
Kind: instance abstract method of BaseModelSql
| Param | Type | Description | | --- | --- | --- | | response | Array.<Object> | updated data | | trx | Object | knex transaction reference |
baseModelSql.errorUpdateb(err, data, trx)
Error update bulk is a hook which can be override in subclass
Kind: instance abstract method of BaseModelSql
| Param | Type | Description | | --- | --- | --- | | err | Error | Exception reference | | data | Array.<Object> | delete data | | trx | Object | knex transaction reference |
baseModelSql.beforeDeleteb(data, trx)
Before delete bulk is a hook which can be override in subclass
Kind: instance abstract method of BaseModelSql
| Param | Type | Description | | --- | --- | --- | | data | Array.<Object> | delete data | | trx | Object | knex transaction reference |
baseModelSql.afterDeleteb(response, trx)
After delete bulk is a hook which can be override in subclass
Kind: instance abstract method of BaseModelSql
| Param | Type | Description | | --- | --- | --- | | response | Array.<Object> | deleted data | | trx | Object | knex transaction reference |
baseModelSql.errorDeleteb(err, data, trx)
Error delete bulk is a hook which can be override in subclass
Kind: instance abstract method of BaseModelSql
| Param | Type | Description | | --- | --- | --- | | err | Error | Exception reference | | data | Array.<Object> | delete data | | trx | Object | knex transaction reference |