Skip to content

创建

创建记录

ts
@Table
class User extends Model {
  @Field({ type: FieldType.TEXT })
  Name?: string

  @Field({ type: FieldType.INTEGER })
  Age?: number

  constructor(name: string, age: number) {
    super();
    this.Name = name
    this.Age = age
  }
}

const user = new User("zhangsan", 18);
this.db.Create(user);

我们还可以使用 Create() 创建多项记录:

ts
this.db.Create([user1, user2, user3]);

用指定的字段创建记录

创建记录并为指定字段赋值。

ts
this.db.Select(["name", "age"]).Create(user);
// INSERT INTO `User` (`name`,`age`,`created_at`) VALUES ("jinzhu", 18, "2020-07-04 11:05:21")

用普通对象创建记录

ts
let valueBucket: relationalStore.ValuesBucket = {
  Name: 'ming',
  Age: 18,
};
this.db.Table("User").Insert(valueBucket);