pyramida是否支持GeoSpatial查询

时间:2018-06-24 12:35:13

标签: prisma

我发现到目前为止,您已经使用MySQL和PostgreSQL,并且它们都支持地理空间类型,我该如何对我的pyramida实施地理空间查询。

假设我要在纽约附近获得所有活动?

2 个答案:

答案 0 :(得分:2)

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

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

首先,让我们获取 env var

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连接到我们的数据库:

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

您的表中需要有一个空间列,该列上还应带有一个空间索引。可以通过以下方式以编程方式进行操作(表必须为空):

/**
* 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查询的参数您自己

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

const query = `SELECT ${sqlSelect} FROM ${sqlFrom} WHERE 
MBRContains(ST_GeomFromText("${polygon}"), GeoPoint) ${sqlWhere} LIMIT 
${toSkip},${batchSize}`;

const selectedRows = await connection.query(query);
  

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

答案 1 :(得分:1)

Prisma当前不支持地理查询。您可以将Prisma用于大多数查询,并直接查询基础数据库以进行地理空间查询。

遵循此功能要求以在该功能可用时得到通知:https://github.com/prismagraphql/prisma/issues/28