AlaSQL.js在Feld上的WHERE子句是一个对象数组

时间:2016-08-18 23:45:25

标签: sql alasql

我尝试做的是SELECT metadata->name, metadata->namespace,但前提是spec.volumes包含来自对象列表中secretName = 'test-secret'的对象,如下所示:

{
  "kind": "PodList",
  "apiVersion": "v1",
  "metadata": {
    "selfLink": "/api/v1/pods",
  },
  "items": [
    {
      "metadata": {
        "name": "pod1",
        "namespace": "namespace1"
      },
      "spec": {
        "volumes": [
          {
            "name": "default-token",
            "secret": {
              "secretName": "default-token"
            }
          }
        ],
        "nodeName": "node-1"
      }
    }
    {
      "metadata": {
        "name": "pod2",
        "namespace": "namespace2"        
      },
      "spec": {
        "volumes": [
          {
            "name": "default-token",
            "secret": {
              "secretName": "default-token"
            }
          }
        ],
        "nodeName": "node-2"
      }
    },
    {
      "metadata": {
        "name": "chosen-pod",
        "namespace": "namespace3",
      },
      "spec": {
        "volumes": [
          {
            "name": "pod-storage",
          },
          {
            "name": "test-data",
            "secret": {
              "secretName": "test-secret"
            }
          }
        ],
        "nodeName": "node-2",
      }
    }
  ]
}

此列表将插入表格中;

CREATE TABLE pods (node TEXT, metadata Object, spec Object)

我认为这是我最接近的;

SELECT metadata->name, metadata->namespace, spec->volumes AS volumes FROM pods WHERE (SELECT COUNT(*) FROM ? WHERE secret->secretName = 'test-data') > 0

但这会引发错误:

TypeError: Cannot read property '0' of undefined
    at Object.eval [as datafn] (eval at <anonymous> (~/Documents/myproject/node_modules/alasql/dist/alasql.fs.js:7487:20), <anonymous>:3:40)
    at ~/Documents/myproject/node_modules/alasql/dist/alasql.fs.js:6100:19
    at Array.forEach (native)
    at queryfn (~/Documents/myproject/node_modules/alasql/dist/alasql.fs.js:6097:16)
    at Array.statement (~/Documents/myproject/node_modules/alasql/dist/alasql.fs.js:7121:14)
    at eval [as wherefn] (eval at <anonymous> (~/Documents/myproject/node_modules/alasql/dist/alasql.fs.js:7899:11), <anonymous>:3:56)
    at doJoin (~/Documents/myproject/node_modules/alasql/dist/alasql.fs.js:6615:12)
    at doJoin (~/Documents/myproject/node_modules/alasql/dist/alasql.fs.js:6687:8)
    at queryfn3 (~/Documents/myproject/node_modules/alasql/dist/alasql.fs.js:6174:2)
    at queryfn2 (~/Documents/myproject/node_modules/alasql/dist/alasql.fs.js:6149:9)

嗯,我的WHERE条款中有错误吗?不,从结尾删除> 0并抛出相同的异常。

this question/ answer向后工作我也尝试过了;

SEARCH / AS @a
  UNION ALL(
    spec->volumes AS @v
    RETURN(@a->metadata->name AS name, 
      @a->metadata->namespace AS namespace, 
      @v->secret->secretName AS [secretNames]
    )
  ) 
FROM pods

@v->secret->secretName AS [secretNames]只会产生undefined;

[ 'pod1', 'namespace1', undefined ]
[ 'pod2', 'namespace2', undefined ]
[ 'chosen-pod', 'namespace3', undefined ]

返回裸@v数组会让我回到原来的位置 - 有一个我无法过滤的数组的行。

1 个答案:

答案 0 :(得分:1)

问题出在spec->volumes。它是一个数组,因此您需要将查询更改为@v->0->secret->secretName

SEARCH / AS @a
  UNION ALL(
    spec->volumes AS @v
    RETURN(@a->metadata->name AS name, 
      @a->metadata->namespace AS namespace, 
      @v->0->secret->secretName AS [secretNames]
    )
  ) 
FROM pods

要提取所需元素,请尝试以下代码:

const data = {...data...};
let res = alasql('SEARCH items / AS @a     \
    spec volumes / WHERE(name="test-data") \
    RETURN(@a->metadata->name AS name,     \
      @a->metadata->namespace AS namespace \
    ) FROM ?',[data]);

此处spec volumes /循环遍历数组spec.volumes[]的所有元素。