dynamoose-polytree
v3.2.1
Published
Multi table tree implementation for dynamodb
Downloads
93
Readme
Install
To install this Lib we need to install dynamoose
npm install --save dynamoose
then install the lib
npm i dynamoose-polytree
Using this Lib
Preparing Model
Extend exisiting Model
file. Model
already have string id no need to add one and it is already extending dynamoose Item
model so it can still be used with regular dynamoose API.
//building.entity.ts
import { Model } from 'dynamoose-polytree';
export class Building extends Model {
name: string;
logo: JSON;
// ...
}
Prepare Schema
Create a regular dynamoose schema, please refer to this documentation from dynamoose please set id
and type of string as your hash key as this property will be used for tree.
Type to Schema Dictionary
Since to create a dynamoose model we need the table name and the schema, our lib need a dictionary to map those table name and schema. An example of those schema is here.
import { Schema } from 'dynamoose/dist/Schema';
export const TypeScheme: Record<string, Schema> = {
Building: BuildingSchema,
Site: SiteSchema,
Amenity: AmenitySchema,
};
Create the repository
Extend the repository class from this lib, please make sure that the Table name is present in the type to schema dictionary.
import { Repository } from 'dynamoose-polytree';
import { TypeScheme } from './path/to/dictionary';
@Injectable() // decorator for nest.js usage
export class BuildingRepository extends Repository {
constructor() {
super(dynamoose.model('Building', BuildingSchema), TypeScheme);
}
}
Repository has property called instance
that holds dynamoose model that you can interact directly with. Please note that this instance is the model instance that is passed in super()
method. example is
let result = await this.instsance.batchGet(arrayOfIds)
other than that example please refer to dynamoose implementation.
Using the repository
Current version of the repository only support vor v4 uuid for the entity id (partition key) that the value is automatically added by the repository.
There are a few methods that are available in this repository. Please note that you have to use given create
method if you want to add the entity to the tree.
let result = await this.repository.create(createBuildingDto as Building);
other methods that are avilable are as follows
- getInstance() // returns dynamoose model
- create()
- update()
- get()
- findRoots()
- findTree() // find children
- findRoot()
- findParent()
- findSiblings()