Show:

Db.Row Class

Module: Db

The class representing database row

Constructor

Db.Row

(
  • fields
  • [retrieved=false]
)

Parameters:

  • fields Object

    Optional object of fields

  • [retrieved=false] Boolean optional

    Optional if object was retrieved from database or created

Methods

beforeRemove

(
  • search_criteria
  • [callback=null]
)

Optional. If defined the method is called before taking actions to remove row. It can be used synchronously and can ignore callback but must return search_criteria object. If used asyncronously shall pass this object to callback

NOTE: if this method is defined but do not return result and do not call callback, the remove() method fails silently without changing database!!!

Parameters:

  • search_criteria Object
  • [callback=null] Function optional

    This function is called when hook completes. Returns error - error object if any and search_criteria as parameters.

beforeRetrieve

(
  • search_criteria
  • [callback=null]
)

Optional. If defined the method is called before taking actions to retrieve row. It can be used synchronously and can ignore callback but must return search_criteria object. If used asyncronously shall pass this object to callback

NOTE: if this method is defined but do not return result and do not call callback, the retrieve() method fails silently!!!

Parameters:

  • search_criteria Object
  • [callback=null] Function optional

    This function is called when hook completes. Returns error - error object if any and search_criteria as parameters.

beforeSave

(
  • modifiedFields
  • [callback=null]
)

Optional. If defined the method is called before taking actions to save row. It can be used synchronously and can ignore callback but must return modifiedFields object. If used asyncronously shall pass this object to callback

NOTE: if this method is defined but do returns null and do not call callback, the save() method fails silently without making any changes in the database!!!

Parameters:

  • modifiedFields Object
  • [callback=null] Function optional

    This function is called when hook completes. Receives "error" - error object if any, and modifiedFields as parameters.

copyFromRow

(
  • row
)
Db_Row

This function copies the members of another row, as well as the primary key, etc. and assigns it to this row.

Parameters:

  • row Db.Row

    The source row. Be careful -- In this case, Db does not check whether the class of the Db_Row matches. It leaves things up to you.

Returns:

Db_Row:

returns this object, for chaining

getFields

()

Get plain object with the fields of the row

remove

(
  • [search_criteria=null]
  • [useIndex=false]
  • [callback=null]
)

Deletes the row from the database. If object has methods beforeRetrieve, beforeRetrieveExecute or afterRetrieveExecute, they may be triggered during this operation.

Parameters:

  • [search_criteria=null] String | Object optional

    You can provide custom search criteria here, such as {"tag.name LIKE ": this.name} If this is left null, and this Db_Row was retrieved, then the db rows corresponding to the primary key are deleted. But if it wasn't retrieved, then the modified fields are used as the search criteria.

  • [useIndex=false] Boolean optional

    If true, the primary key is used in searching for rows to delete. An exception is thrown when some fields of the primary key are not specified

  • [callback=null] Function optional

    This function is called when all queries have completed. The "this" object would be this row, now hydrated with values from the database. It is passed the following arguments: 1) errors: an Object. If there were any errors they will be passed along as documented in query.execute. If there were no errors, this will be null. 2) count: an Integer the number of rows deleted. If there were any errors, it will be passed nothing

retrieve

(
  • [fields='*']
  • [useIndex=false]
  • [modifyQuery=false]
  • [options=array()]
  • [callback=null]
)

Retrieves a row from the database. If object has methods beforeRetrieve, beforeRetrieveExecute or afterRetrieveExecute, they may be triggered during this operation.

Parameters:

  • [fields='*'] String optional

    The fields to retrieve and set in the Db_Row. This gets used if we make a query to the database.

  • [useIndex=false] Boolean optional

    If true, the primary key is used in searching, and an exception is thrown when any fields of the primary key are not specified

  • [modifyQuery=false] Array | Boolean optional

    If an array, the following keys are options for modifying the query. Any other keys will be sent to query.options(modifyQuery); You can call more methods, like limit, offset, where, orderBy, and so forth, on that Db_Query. After you have modified it sufficiently, get the ultimate result of this function, by calling the resume() method on the Db_Query object (via the chainable interface).

    You can also pass true in place of the modifyQuery field to achieve the same effect as {"query": true}

    • [begin] Boolean | String optional

      this will cause the query to have .begin() a transaction which locks the row for update. You should call .save(..., true) to unlock the row, otherwise other database connections trying to access the row will be blocked.

    • [rollbackIfMissing] Boolean optional

      If begin is true, this option determines whether to rollback the transaction if the row we're trying to retrieve is missing. Defaults to false.

    • [ignoreCache] Boolean optional

      If true, then call ignoreCache on the query

    • [caching] Boolean optional

      If provided, then call caching() on the query, passing this value

    • [query] Boolean optional

      If true, it will return a Db_Query that can be modified, rather than the result.

  • [options=array()] Array optional

    Array of options to pass to beforeRetrieve and afterFetch functions.

  • [callback=null] Function optional

    This function is called when all queries have completed. The "this" object would be this row, now hydrated with values from the database. It is passed the following arguments: 1) errors: an Object. If there were any errors they will be passed along as documented in query.execute. If there were no errors, this will be null. 2) result: an array of rows retrieved. If error occured it will be passed nothing

rollback

(
  • [callback=null]
)

Rolls back the transaction

Parameters:

  • [callback=null] Function optional

    This function is called when all queries have completed. The "this" object would be this row, now hydrated with values from the database. It is passed the following argument: errors: an Object. If there were any errors they will be passed along as documented in query.execute. If there were no errors, this will be null.

save

(
  • [onDuplicateKeyUpdate=false]
  • [commit=false]
  • [callback=null]
)

Saves the row in the database. If object has methods beforeRetrieve, beforeRetrieveExecute or afterRetrieveExecute, they may be triggered during this operation.

Parameters:

  • [onDuplicateKeyUpdate=false] Boolean optional

    If MySQL is being used, you can set this to TRUE to add an ON DUPLICATE KEY UPDATE clause to the INSERT statement

  • [commit=false] Boolean optional

    If this is TRUE, then the current transaction is committed right after the save. Use this only if you started a transaction before.

  • [callback=null] Function optional

    This function is called when the queries have all completed. Its this object is the same as the one the save method is called on. It is passed the one optional argument: errors: an Object. If there were any errors, it will be passed error object as returned from query.execute. If successful, it will be passed nothing.

split

(
  • index
)

Set up temporary config for shard split

Parameters:

  • index Object

    Split shard index

    • 'indexes->connection' section of sharding config. Shall contain 'fields' and 'partition' fields
    • 'partition' field shall contain new points mapped to shards
    • 'shards' section of config shall be already filled with new shards config

Properties

_fieldNames

Array private

The fields names

_fieldsModified

Object private

The fields of the row

_pkValue

Object private

The value of the primary key of the row Is set automatically if the Db_Row was fetched from a Db_Result.

_primaryKey

Array private

The names of the fields in primary key

_split

Object private

The temporary config to make shards split

fields

Object

A container for fields values. Used by [G/S]etters to store values of the fields

retrieved

Boolean

Whether this Db_Row was retrieved or not. The save() method uses this to decide whether to insert or update.

retrieved

Boolean

Whether this Db_Row was retrieved or not.