首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >prisma是否支持GeoSpatial查询

prisma是否支持GeoSpatial查询
EN

Stack Overflow用户
提问于 2018-06-24 20:35:13
回答 2查看 1.7K关注 0票数 3

我发现您现在使用的是MySQL和PostgreSQL,而且它们支持地理空间类型,我如何才能在我的prisma上实现地理空间查询。

比方说我想把所有的活动都放在纽约市附近?

EN

回答 2

Stack Overflow用户

发布于 2018-06-26 08:07:27

我使用Prisma和MySQL数据库在我的项目中“实现”了一个自定义的geoSearch:

您需要能够以编程方式连接到数据库。

首先,让我们获得env var

代码语言:javascript
复制
const host = process.env.MYSQL_ENDPOINT;
const user = process.env.MYSQL_ROOT_USERNAME;
const password = process.env.MYSQL_ROOT_PASSWORD;
const database = process.env.PRISMA_SERVICE + "@" + process.env.PRISMA_STAGE;

现在尝试使用promise-mysql包连接到我们的数据库:

代码语言:javascript
复制
let connection;
try {
      //Create a connection to the database;
      connection = await mysql.createConnection({
        host,
        user,
        password,
        database
      });
    } catch (e) {
      console.error(e);
      throw new Error("Could not connect to the Database");
    }

表中需要有一个空间列,表上还应该有一个空间索引。可以使用以下命令以编程方式完成此操作(表必须为空):

代码语言:javascript
复制
/**
* Add a spatial column to the table, used for geo-searching
 * @param  {string}  tableName name of the table to alter
 * @param  {string}  columnName name of the spatial column
 * @param  {string}  lonColumnName name of the longitude column
 * @param  {string}  latColumnName name of the latitude column
 * @param  {object}  connection connection to the database
 * @return {Promise} result of the table alteration
 */
 const addSpatialColumn = async (
  tableName,
  columnName,
  lonColumnName,
  latColumnName,
  connection
) => {
  return connection.query(`
  ALTER TABLE
      ${tableName} ADD ${columnName} POINT AS(
          ST_POINTFROMTEXT(
              CONCAT(
                  'POINT(',
                  ${lonColumnName},
                  ' ',
                  ${latColumnName},
                  ')'
              )
          )
      ) STORED NOT NULL;`);
};

/**
 * Add a spatial index to the table
 * @param  {string}  tableName  name of the table
 * @param  {string}  columnName name of the column to create an index on
 * @param  {object}  connection connection to the database
 * @return {Promise} result of the index creation
 */
const addSpatialIndex = async (tableName, columnName, connection) => {
  return connection.query(
    `ALTER TABLE ${tableName} ADD SPATIAL INDEX(${columnName});`
  );
};

现在到了棘手的部分。由于Prisma还没有向我们灌输这方面的信息,因此您需要自己确定sql查询的参数。

然后,您可以执行查询,例如:

代码语言:javascript
复制
const query = `SELECT ${sqlSelect} FROM ${sqlFrom} WHERE 
MBRContains(ST_GeomFromText("${polygon}"), GeoPoint) ${sqlWhere} LIMIT 
${toSkip},${batchSize}`;

const selectedRows = await connection.query(query);

后脚本:这些片段不是抽象的,因此可能需要修改/改进。我只是提供了一个解决这个临时问题的方法的示例。

票数 2
EN

Stack Overflow用户

发布于 2018-06-24 21:33:23

Prisma目前不支持地理查询。您可以使用Prisma进行大多数查询,并直接查询底层数据库以进行地理空间查询。

按照此功能请求在此功能可用时收到通知:https://github.com/prismagraphql/prisma/issues/28

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51010009

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档