// UserModel.ts
import { BaseModel, IModel } from "dynamite-orm";
export interface IUser extends IModel {
username: string;
age: number;
user_email: string;
}
export class UserModel extends BaseModel implements IUser {
constructor(data?: Partial<IUser>) {
super();
Object.assign(this, data);
}
public username!: string;
public age!: number;
public user_email!: string;
}
// UserEntity.ts
import { Entity } from "dynamite-orm";
import { IUser } from "./UserModel.ts";
// just a reflection of your dynamodb GSI definitions
type IndexOptions = "UsernameIndex" | "UserEmailIndex";
class UserEntity extends Entity<IndexOptions, IUser> {
constructor(tableName: string) {
super(tableName);
}
}
export const User = new UserEntity("UserTableName");
import { User } from "./UserEntity";
import { UserModel } from "./UserModel";
(async () => {
const { Attributes } = await User.Insert({ username: "FooBar", age: 18, user_email: "foo@bar.com" });
const user = new UserModel(Attributes);
console.log(user.toJSON());
})()
{
id: "98c79a5a-dbc4-4711-9ade-d6c2d6623f33",
username: "FooBar",
age: 18,
user_email: "foo@bar.com",
is_deleted: false,
created_at: "2021-12-18T20:42:49.449Z",
updated_at: "2021-12-18T20:42:49.449Z"
}
Reading Document By Primary Key
import { User } from "./UserEntity";
(async () => {
const item = await User.FindOne({
id: "98c79a5a-dbc4-4711-9ade-d6c2d6623f33"
});
console.log(item);
})()
{
id: "98c79a5a-dbc4-4711-9ade-d6c2d6623f33",
username: "FooBar",
age: 18,
user_email: "foo@bar.com",
is_deleted: false,
created_at: "2021-12-18T20:42:49.449Z",
updated_at: "2021-12-18T20:42:49.449Z"
}
Reading / Filtering Document By Global Secondary Index
import { User } from "./UserEntity";
(async () => {
const { Items, Count } = await User.Find("UsernameIndex", {
// Key Condition
username: "FooBar"
}, {
// Filter Expression
age: 18
}, { order: "DESC", limit: 1 });
console.log(Count, Items);
})()
// Count
1,
// Items Array
[
{
id: "98c79a5a-dbc4-4711-9ade-d6c2d6623f33",
username: "FooBar",
age: 18,
user_email: "foo@bar.com",
is_deleted: false,
created_at: "2021-12-18T20:42:49.449Z",
updated_at: "2021-12-18T20:42:49.449Z"
}
]
import { User } from "./UserEntity";
(async () => {
const { Attributes } = await User.SoftDelete("98c79a5a-dbc4-4711-9ade-d6c2d6623f33");
console.log(Attributes);
})()
// this will not actually remove the document
{
is_deleted: true,
updated_at: "2021-12-18T21:01:55.102Z"
}
import { User } from "./UserEntity";
(async () => {
const { Attributes } = await User.SoftRecover("98c79a5a-dbc4-4711-9ade-d6c2d6623f33");
console.log(Attributes);
})()
// this will not actually remove the document
{
is_deleted: false,
updated_at: "2021-12-18T21:01:55.102Z"
}
import { User } from "./UserEntity";
(async () => {
const { Attributes } = await User.HardDelete("98c79a5a-dbc4-4711-9ade-d6c2d6623f33");
console.log(Attributes);
})()
// this will remove the document from the database, regardless of is_deleted
{
id: "98c79a5a-dbc4-4711-9ade-d6c2d6623f33",
username: "FooBar",
age: 18,
user_email: "foo@bar.com",
is_deleted: false,
created_at: "2021-12-18T20:42:49.449Z",
updated_at: "2021-12-18T20:42:49.449Z"
}
import { User } from "./UserEntity";
(async () => {
const { Attributes } = await User.UpdateOne({
id: "98c79a5a-dbc4-4711-9ade-d6c2d6623f33"
}, {
age: 19,
username: "BarBas"
}, "UPDATED_NEW");
console.log(Attributes);
})()
{
age: 19,
username: "BarBas",
updated_at: "2021-12-18T21:01:55.102Z"
}