5.3.3 Converter

Converter classes convert between JavaScript types and MySQL types. If the user supplies a JavaScript converter, it used to read and write to the database.

Converters have several purposes, including the following:

  • To convert between MySQL DECIMAL types and a user's preferred JavaScript fixed-precision utility library

  • To convert between MySQL BIGINT types and a user's preferred JavaScript big number utility library

  • To serialize arbitrary application objects into character or binary columns

The ndb back end also uses converters to support SET and ENUM columns. (The mysql back end does not use these.)

A Converter class has the interface defined here:

function Converter() {}:

Converter.prototype = {
  "toDB"    : function(obj) {  },
  "fromDB"  : function(val) {  }
};

The Converter must implement the following two functions:

  1. toDB(obj): Convert an application object obj into a form that can be stored in the database.

  2. fromDB(val): Convert a value val read from the database into application object format.

Each function returns the result of the conversion.

Converter invocations are chained in the following ways:

  • When writing to the database, first the registered FieldConverter, if any, is invoked. Later, any registered TypeConverter is invoked.

  • When reading from the database, first the registered TypeConverter, if any, is invoked. Later, any registered FieldConverter is invoked.