[英]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.