mysql - 使用 ObjectionJS 插入/选择 Mysql 空间列

标签 mysql node.js spatial objection.js

我在 MySQL 中有一个包含“点”类型列的位置表。我可以使用 Knex 定义它并且它工作正常。

knex.schema.createTable('locations',function(table){
  table.increments('locid').primary();
  table.string('address').notNullable();
  table.string('nick_name');
  table.specificType('loc_gis','Point').nullable();
  table.enu('loc_type',['home','office','college','other']).notNullable();
  table.timestamps();
})

但我不知道如何使用 ObjectionJS 选择/插入 GIS 空间数据。我是否必须每次都使用原始查询,或者无论如何都要为 ObjectionJS 编写一个插件来处理空间列。

最佳答案

找到了答案。我的 ObjectionJS 模型定义中的以下代码解决了这个问题。

$formatDatabaseJson(json) {
 // Remember to call the super class's implementation.
  if(json.locGis){
    json.locGis=knex.raw('point(?, ?)', 
      [json.locGis.x, json.locGis.y]);
  }
  json = super.$formatDatabaseJson(json);
  return json;
}

关于mysql - 使用 ObjectionJS 插入/选择 Mysql 空间列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49901769/

相关文章:

php - 改进查询/表 : Long "Copying to tmp table"

mysql - 通过shell脚本连接mysql数据库服务器

php - jQuery 将复选框值设置为 URL 并获取 PHP 中的值作为数组并使用 NOT IN 查询它

python - 如何从Python将空小数/日期时间插入MYSQL?

javascript - 有没有办法让 Apache Bench 测试提供给 URL 的所有资源的响应时间?

sql - 如何获取 oracle 多边形集合的一小部分

asp.net-mvc - Fluent NHibernate 中的地理空间点映射

node.js - 逗号代替换行符表达nodejs

node.js - 对 AWS SQS 执行 5000 多个 promise 的 AWS Lambda 函数极其不可靠

php - 使用 PHP 和 MySQL 处理空间数据时出现的问题