Table Class Script

Class for database tables.

This is an extension of Base Class that adds functionality for database tables. Most methods are defined in Base Class - the documentation below lists additional or overridden method for the Table Class.

rows = get(filter,query)

Get one or more row objects.

The filter identifies a method that will return a list of nodes.

The query is an object containing parameters for the filter.

Available filters are 'all', 'row' and 'boolean'. These are documented in filters.all(), filters.row() and filters.boolean() below.

filters.all()

Returns all the row objects in the table. Do not call directly, use get('all').

filters.row(query)

Looks up a row object based on the values in query. Returns a single row or an empty array.

If query contains a key property, this will be assumed to be a list of the keys for the table, in order.

If query does not contain a key property, the query object will be resolved like any other node, looking first for a reference field, then for key fields by name.

Do not call directly, use get('row',query)

filters.boolean(query)

Returns all the row objects in the table which have a boolean value set. The boolean field is identified by the boolean property in the query parameter. Do not call, use get('boolean',query).

For example, to return all the rows which have the 'active' boolean set to true, use something like.

var activeRows = get('boolean',{field:'active'});
rows = getByKey(key, ...)

Return a single item array containing the row identified by one or more key fields, passed in order.

If the row is not found, return an empty array.

row = getRowByKey(key, ...)

Get a single row by key fields, passed in order. If the row is not found, return null.

node = getNodeByKey(key, ...)

Return the row node by key fields, passed in order. Return null if not found.

rows = getNodesWhereLinked(fieldReference, target)

Return a list of row objects which link to the given target node using the field with the given field reference. For example, if you wanted to find all the range fields that pointed to a range node, you would use:

var rangeRows = getNodesWhereLinked('range', rangeNode);
var localReference = getReference(key, ...)

Given one or more key fields, passed in order, return a local reference to use for the row node. The rules for this are:

  • Convert each key value to its string equivalent.
  • Replace any characters other than A-Z, a-z and 0-9 with _xx_, where xx is the hexadecimal code for the character, without leading zeroes.
  • Concatenate all the key values with __ (a double underscore).
  • If the concatanated key begins with 0-9, prefix it with an underscore.

For example, if a table were keyed on a numeric product_id and a string version_id, with a product id of 123 and a version of "A11.2", we would get:

  • Convert to strings: "123", "A11.2"
  • Replace non alpha characters: "123", "A11_46_2"
  • Concatenate: "123__A11_46_2"
  • Prefix if first character is number: "_123__A11_46_2"

Child classes may override this to provide a different rule for building local references.

var keyArray = getKeys(localReference)

Given a local reference, return an array of key items. The key items are all strings, even if the original keys were numeric or null.

If a child class overrides getReference(), this should also be overridden to convert a local reference back to an array of key items.

var keyString = getKey(localReference)

Given a local reference, return a delimited keys string. The delimiter defaults to "/".

var separator = getKeySeparator()

Return the key separator. Returns "/".

Child classes may override this.

var row = new(data)

Given a data record, create a new row. Return the row object.

var node = newNode(data)

Given a data record, create a new row. Return the row node.

var rowDefinition = getRowDefinition()

Overrides the Base Class method to provide a row definition. The row definition is documented in Derive Row Definition.

put(rowArray)

Puts an array of rows. If rows with the same key fields exist, they will be replaced with the new data. Fields not passed in the row data will be deleted.

put(rowData)

Put a single row, from row data.

delete(rowArray)

Deletes each of an array of rows. If rows cannot be found, no error is raised.

delete(rowData)

Delete a single row, from row data, if it exists.

node = putRowNode(rowData)

Put a single row, from row data, and return the row node.

patch(rowArray)

Patches an array of rows. If rows with the same key fields exists, the new data will be merged with the rows, but fields not passed in the row data will not be deleted.

patch(rowData)

Patch a single row, from row data.

node = patchRowNode(rowData)

Patch a single row, from row data, and return the row node.

node = resolveNode(rowData)

Return the row that matches the given row data.

setKey(rowData)

Set the key for a new record. The assumes that there is a single key field and that it is numeric, and sets it to the next available number from the auto number field on the table.

If this is not the required functionality, override setKey().

getNextNumber()

Return the next number from the auto number field on the table, and update the auto number field on the table.

rows = services.get()

Implements a service interface for get(). This reads the "filter" parameter and passes this and other query parameters to get() to get the rows, and then uses the "view" parameter to retrieve suitable row objects from the individual rows.

services.put()

Implements a service interface for put. Puts one or more rows of data passed in an array in the request body.

services.patch()

Implements a service interface for patch. Patches one or more rows of data passed in an array in the request body.

services.delete()

Implements a service interface for delete. Deletes one or more rows of data passed in an array in the request body.

 

Tag List