Home Reference Source Repository

schema-mapper-serializer

build status Docs status

Base serializer class for storage drivers.

usage

To serialize one of the ColumnTypes, Override / implement the method name which is translated the following way: the method name starts with "serialize", then the type follows (first letter is capitalized) and [] is replaced with "Array". Some examples:

string becomes: serializeString(item, column)
string[] becomes: serializeStringArray(item, column)
point[] becomes: serializePointArray(item, column)

The PostgreSQL serializer implementation:

var knex = require('knex')({dialect: 'postgres'});
var st = require('knex-postgis')(knex);

import Serializer from 'schema-mapper-serializer';

class PostgresqlSerializer extends Serializer {
  /**
   * Serialize a point.
   *
   * @protected
   * @param  {string} value
   */
  serializePoint(value, column) {
    return this.serializeGeoJSON(value, column);
  }

  /**
   * Serialize a point array.
   *
   * @protected
   * @param  {string} value
   */
  serializePointArray(value, column) {
    return this.serializeGeoJSON(value, column);
  }

  /**
   * Serialize a linestring.
   *
   * @protected
   * @param  {string} value
   */
  serializeLinestring(value, column) {
    return this.serializeGeoJSON(value, column);
  }

  /**
   * Serialize a linestring array.
   *
   * @protected
   * @param  {string} value
   */
  serializeLinestringArray(value, column) {
    return this.serializeGeoJSON(value, column);
  }

  /**
   * Serialize a polygon.
   *
   * @protected
   * @param  {string} value
   */
  serializePolygon(value, column) {
    return this.serializeGeoJSON(value, column);
  }

  /**
   * Serialize a polygon array.
   *
   * @protected
   * @param  {string} value
   */
  serializePolygonArray(value, column) {
    return this.serializeGeoJSON(value, column);
  }

  /**
   * Serialize to geojson.
   *
   * @protected
   * @param  {string} value
   */
  serializeGeoJSON(value, column) {
    if ( ! value.crs) {
      value.crs = {
        type: 'name',
        properties: {name: 'EPSG:' + (column.srid || 4326)}
      };
    }

    return st.geomFromGeoJSON(value);
  }
}