schema-mapper-serializer

0.4.2 • Public • Published

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

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
0.4.21latest

Version History

VersionDownloads (Last 7 Days)Published
0.4.21
0.4.10
0.4.00
0.3.20
0.3.10
0.3.00
0.2.21
0.2.10
0.2.00
0.1.10
0.1.00
0.0.20
0.0.10

Package Sidebar

Install

npm i schema-mapper-serializer

Weekly Downloads

2

Version

0.4.2

License

MIT

Last publish

Collaborators

  • vespakoen