DynamooseModel 클래스의 생성자입니다.
의존성 주입된 Dynamoose 인스턴스입니다. @Inject("DYNAMOOSE") 데코레이터를 통해 주입됩니다.
This method is the basic entry point for creating a model in Dynamoose. When you call this method a new model is created, and it returns an item initializer that you can use to create instances of the given model.
The name
parameter is a string representing the table name that will be used to store items created by this model.
The schema
parameter can either be an object OR a Schema instance. If you pass in an object for the schema
parameter it will create a Schema instance for you automatically.
const dynamoose = require("dynamoose");
const Cat = dynamoose.model("Cat", {"name": String});
const Cat = dynamoose.model("Cat", new dynamoose.Schema({"name": String}));
An optional TypeScript class which extends Item
can be provided right before the function bracket. This provides type checking when using operations like Model.create()
.
import * as dynamoose from "dynamoose";
import {Item} from "dynamoose/dist/Item";
// Strongly typed model
class Cat extends Item {
id: number;
name: string;
}
const CatModel = dynamoose.model<Cat>("Cat", {"id": Number, "name": String});
// Will raise type checking error as random is not a valid field.
CatModel.create({"id": 1, "random": "string"});
// Will return the correct type of Cat
const cat = await CatModel.get(1);
You can also pass in an array of Schema instances or schema objects into the schema
parameter. This is useful for cases of single table design where you want one model to have multiple options for a schema. Behind the scenes Dynamoose will automatically pick the closest schema to match to your item, and use that schema for all operations pertaining to that item. If no matching schema can be found, it will default to the first schema in the array.
:::note If you use multiple schemas in one model, the hash & range keys must match for all schemas. :::
const Cat = dynamoose.model("Cat", [
new dynamoose.Schema({"id": String, "name": String}),
{"id": String, "age": Number}
]);
If you don't pass the schema
parameter it is required that you have an existing model already registered with that name. This will use the existing model already registered.
const Cat = dynamoose.model("Cat"); // Will reference existing model, or if no model exists already with name `Cat` it will throw an error.
DynamooseModel 클래스는 DynamoDB와 상호작용하기 위한 Dynamoose 모델을 생성하고 관리합니다.
@@Injectable 데코레이터를 사용하여 의존성 주입이 가능하도록 설정됩니다.