2.1.0 • Published 2 years ago

dynamite-orm v2.1.0

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

Dynamite-Orm 🧨

MIT License


Usage

// 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");

Inserting Document

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"
	}
]

Soft Delete / Recover

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"
}

Recover

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"
}

Hard Delete

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"
}

Update Item

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"
}
2.1.0

2 years ago

2.0.0

2 years ago

1.9.0

2 years ago

1.8.0

2 years ago

1.7.0

2 years ago

1.6.0

2 years ago

1.5.0

2 years ago

1.4.1

2 years ago

1.4.0

2 years ago

1.3.0

2 years ago

1.2.0

2 years ago

1.1.4

2 years ago

1.1.2

2 years ago