0.4.2 • Published 9 years ago

schema-mapper-serializer v0.4.2

Weekly downloads
6
License
MIT
Repository
github
Last release
9 years ago

schema-mapper-serializer

build status Docs status Code Climate Dependencies License

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);
  }
}
0.4.2

9 years ago

0.4.1

9 years ago

0.4.0

9 years ago

0.3.2

10 years ago

0.3.1

10 years ago

0.3.0

10 years ago

0.2.2

10 years ago

0.2.1

10 years ago

0.2.0

10 years ago

0.1.1

10 years ago

0.1.0

10 years ago

0.0.2

10 years ago

0.0.1

10 years ago