使用Cosmos DB Core SQL API使用嵌套JSON嵌套JS

时间:2020-09-16 13:53:14

标签: node.js database typescript azure-cosmosdb nestjs

我无法将Nest JS与Cosmos DB Core SQL API集成在一起。我知道有一个用于Cosmos DB(https://github.com/nestjs/azure-database)的模块,但是我需要数据模式来支持嵌套json,如下所示:

{
    "id":"string",
    "name":"string",
    "start_date":2016-06-01T00:00:00.000Z,
    "end_date":2016-06-01T00:00:00.000Z,
    "current_status":{
        "event_part_id":"string",
        "media_type":"string",
        "media_id":"string",
        "media_time":int
    },
    "event_parts":[{
        "id":"string",
        "name":"string",
        "timeline_id":"string",
        "user_voice_chat":boolean,
    "surveys":[{
        "id":"string",
        "name":"string",
        "question":"string",
        "answers":[{
            "id":"string",
            "name":"string"
        }],
    }],
    "images":[{
        "id":"string",
        "name":"string",
        "url":"string"
        }],
    "videos":[{
        "id":"string",
        "name":"string",
            "url":"string"
        }],
        "live_streaming":{            
            "id":"string",
            "name":"string",
            "url":"string"
        }
    }],
    "asset_id":"string",
    "twitter_text":"string",
    "rtc_network_room_id":"string",
    "is_active":boolean
}

我认为(或者我可能错了)@ nestjs / azure-database不支持,因为当我检查他们的示例和快速入门时,没有这样的json模式的示例。

是否可以使用@ nestjs / azure-database或为此创建自定义数据库模块?

我也知道可以使用Cosmos DB MongoDB API并使用TypoORM或Sequelize,但是在内部,我们希望继续使用Cosmos DB SQL API,因为我们已经将它用于多种服务。

谢谢!

1 个答案:

答案 0 :(得分:0)

The docs on the Github Page表明,在定位CosmosDB时可以使用复杂的对象类型。

import { CosmosPartitionKey, CosmosDateTime, Point } from '@nestjs/azure-database';

@CosmosPartitionKey('type')
export class Event {
  id?: string;
  type: string;
  @CosmosDateTime() createdAt: Date;
  location: Point;
}

将自动转换为:

{
  "type": "Meetup",
  "createdAt": "2019-11-15T17:05:25.427Z",
  "position": {
    "type": "Point",
    "coordinates": [2.3522, 48.8566]
  }
}

请注意postition的嵌套对象和Point类型的转换。