简体   繁体   English

如何在 dynamoose v3 中从 model 获取模式属性名称?

[英]How to get schema attributes names from model in dynamoose v3?

In dynamoose v2:在发电机 v2 中:

T extends Document; model: ModelType<T>; const attributes: string[] = model.schemas[0].attributes();

In this way I get the attributes names of the schema.通过这种方式,我得到了模式的属性名称。 How I can get attributes names from model in dynamoose v3?如何在 dynamoose v3 中从 model 获取属性名称?

In dynamoose v3:在发电机 v3 中:

T extends Item; model: ModelType<T>; const attributes: string[] = model.schemas[0].attributes();

I have the following error: Property 'schemas' does not exist on type 'ModelType<T>' .我有以下错误: Property 'schemas' does not exist on type 'ModelType<T>'

I encountered a similar issue while working with Dynamoose 3. Here's a working example how to get model attributes:我在使用 Dynamoose 3 时遇到了类似的问题。这是一个如何获取 model 属性的工作示例:

import * as dynamoose from "dynamoose";
import {Item} from "dynamoose/dist/Item";

// Strongly typed model
class Person extends Item {
    id: number;
    name: string;
}
const PersonModel = dynamoose.model<Person>("Person", {"id": Number, "name": String});

If you attempt to create a new record with an undeclared field eg surname , Typescript will yell at you.如果您尝试使用未声明的字段(例如surname )创建新记录,Typescript 将对您大喊大叫。

CatModel.create({"id": 1, "surname": "string"});

After creating or fetching the model, your IDE will correctly provide you the attributes.创建或获取 model 后,您的 IDE 将正确地为您提供属性。

Example 1:示例 1:

const Person = await PersonModel.get(1);
//console.log(Person.id)
//console.log(Person.name)

Example 2:示例 2:

const Person = await PersonModel.create({id: "1", name: "John" })
//console.log(Person.id)
//console.log(Person.name)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 dynamoose 进行排序 - How to sort using dynamoose 如何获取 cognito 用户组(在 AWS JS SDK V3 中) - How to get a cognito users' group (in AWS JS SDK V3) 在 DymanoDB/Dynamoose 中,如何设置 map 字符串键和数值的模式? - In DymanoDB/Dynamoose, how can I set a schema of a map of string keys and number values? 用于 nodejs 的 AWS SDK v3,如何获取 s3 存储桶的标签? - AWS SDK v3 for nodejs, how to get tags of an s3 bucket? 如何将 Dynamoose 与 AWS SAM 结合使用? - How to use Dynamoose combined with AWS SAM? Firebase v3 - 如何重新初始化默认应用程序? - Firebase v3 - How to Reinitialize Default App? dynamoose 如何让多个模型共享一个表? - How can dynamoose have multiple models share a single table? Google 翻译 API V3:如何从文件流中验证服务帐户 - Google translate API V3: How to authenticate service account from file stream 如何从大查询模式错误中获取更好的日志 - How to get better log from big query schema error 如何使用 KEYAPI 运行谷歌翻译 v3? - How to run Google Translate v3 using KEYAPI?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM