5.3.2 Context

Context is the supertype of Session and Batch. It contains functions that are executed immediately if called from a session, or when the batch is executed.

The Mynode implementation does have any concept of a user and does not define any such property.

find(Function constructor, Object keys, Function(Object error, Object instance[, ...]) callback[, ...]);

find(String tableName, Object keys, Function(Object error, Object instance[, ...]) callback[, ...]);

Find a specific instance based on a primary key or unique key value.

You can use either of two versions of this function. In the first version, the constructor parameter is the constructor function of a mapped domain object. Alternatively, you can use the tableName instead, in the second variant of the function.

For both versions of find(), the keys may be of any type. A key must uniquely identify a single row in the database. If keys is a simple type (number or string), then the parameter type must be the same type as or compatible with the primary key type of the mapped object. Otherwise, properties are taken from the parameter and matched against property names in the mapping. Primary key properties are used if all are present, and other properties ignored. If keys cannot be used identify the primary key, property names corresponding to unique key columns are used instead. If no complete primary or unique key properties are found, an error is reported. The returned object is loaded based on the mapping and the current values in the database.

For multi-column primary or unique keys, all key fields must be set.

load(Object instance, Function(Object error) callback);

Load a specific instance by matching its primary or unique key with a database row, without creating a new domain object. (This is unlike find(), which creates a new, mapped domain object.)

The instance must have its primary or unique key value or values set. The mapped values in the object are loaded based on the current values in the database. Unmapped properties in the object are not changed.

Primary key properties are used if all are present, and all other properties are ignored; otherwise, property names corresponding to unique key columns are used. If no complete primary or unique key properties can be found, an error is reported.

The callback function is called with the parameters provided when the operation has completed. The error is the Node.js Error object; see Section 5.3.4, “Errors, for more information.

persist(Object instance, Function(Object error) callback);

persist(Function constructor, Object values, Function(Object error) callback);

persist(String tableName, Object values, Function(Object error) callback);

Insert an instance into the database, unless the instance already exists in the database, in which case an exception is reported to a callback function. Autogenerated values are present in the instance when the callback is executed.

The role of an instance to be persisted can be fulfilled in any of three ways: by an instance object; by a constructor, with parameters, for a mapped domain object; or by table name and values to be inserted.

In all three cases, the callback function is called with the parameters provided, if any, when the operation has completed. The error is the Node.js Error object; see Section 5.3.4, “Errors, for more information.

remove(Object instance, Function(Object error) callback);

remove(Function constructor, Object keys, Function(Object error) callback);

remove(String tableName, Object keys, Function(Object error) callback);

Delete an instance of a class from the database by a primary or unique key.

There are three versions of remove(); these allow you to delete an instance by referring to the instance object, to a constructor function, or by name of the table. The instance object must contain key values that uniquely identify a single row in the database. Otherwise, if the keys supplied with the function constructor or table name is a simple type (Number or String), then the parameter type must be of either the same type as or a type compatible with the primary key type of the mapped object. If keys is not a simple type, properties are taken from the parameter and matched against property names in the mapping. Primary key properties are used if all are present, and other properties ignored. If keys does not identify the primary key, property names corresponding to unique key columnsare used instead. If no complete primary or unique key properties are found, an error is reported to the callback.

All three versions of remove() call the callback function with the parameters provided, if any, when the operation is complete. The error object is a Node.js Error; see Section 5.3.4, “Errors, for error codes.

update(Object instance, Function(Object error) callback);

update(Function constructor, keys, values, Function(Object error) callback);

update(String tableName, keys, values, Function(Object error) callback);

Update an instance in the database with the supplied values without retrieving it. The primary key is used to determine which instance is updated. If the instance does not exist in the database, an exception is reported in the callback.

As with the methods previously shown for persisting instances in and removing them from the database, update() exists in three variations, which allow you to use the instance as an object, an object constructor with keys, or by tableName and keys.

Unique key fields of the keys object determine which instance is to be updated. The values object provides values to be updated. If the keys object contains all fields corresponding to the primary key, the primary key identifies the instance. If not, unique keys are chosen is a nondeterministic manner.

Note

update() cannot be used to change the primary key.

save(Object instance, Function(Object error) callback);

save(Function constructor, Object values, Function(Object error) callback);

save(String tableName, Object values, Function(Object error) callback);

Save an instance in the database without checking for its existence. If the instance already exists, it is updated (as if you had used update()); otherwise, it is created (as if persist() had been used). The instance id property is used to determine which instance should be saved. As with update(), persist(), and remove(), this method allows you to specify the instance using an object, object constructor, or table name.

All three versions of the save() method call the callback function with any parameters provided when the operation has been completed. The error is a Node.js Error object; see Section 5.3.4, “Errors, for error codes and messages.

Boolean isBatch()

Context also exposes an isBatch() instance method, which returns true if this Context is a Batch, and false if it is a Session. isBatch() takes no arguments.