dynamite-orm
TypeScript icon, indicating that this package has built-in type declarations

2.1.0 • Public • Published

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

Readme

Keywords

none

Package Sidebar

Install

npm i dynamite-orm

Weekly Downloads

2

Version

2.1.0

License

MIT

Unpacked Size

34.6 kB

Total Files

23

Last publish

Collaborators

  • judeboy