【问题标题】:Does prisma supports GeoSpatial queriesprisma 是否支持地理空间查询
【发布时间】:2018-12-03 05:37:34
【问题描述】:

我发现你现在使用 MySQL 和 PostgreSQL,并且它们支持地理空间类型,我如何对我的 prisma 实现地理空间查询。

假设我想获取纽约附近的所有活动?

【问题讨论】:

    标签: prisma


    【解决方案1】:

    我使用 Prisma 和 MySQL 数据库在我的项目中“实施”了自定义地理搜索:

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

    首先,让我们获取我们的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);
    

    后记:这些 sn-ps 不是抽象的,因此可能需要修改/改进。我只是提供一个解决这个临时问题的方法的例子。

    【讨论】:

    • 是的,我熟悉这种方法,但我现在正在寻找的是 Prisma 实施的指导方针,因为我知道 prisma 正在快速改变他们的代码,所以我正在寻找准则,因此至少必须与他们的代码保持一致并且面向未来。但是感谢您的输入@Namoz
    【解决方案2】:

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

    当此功能可用时,请按照此功能请求获得通知:https://github.com/prismagraphql/prisma/issues/28

    【讨论】:

    • 有什么我可以做的逃生舱口吗,比如说我的数据库的自定义代码,而我正在等待您的解决方案。你有指导方针吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-17
    • 2013-07-06
    • 1970-01-01
    • 2020-04-27
    • 1970-01-01
    • 2021-07-17
    • 1970-01-01
    相关资源
    最近更新 更多