Multi table tree implementation for dynamodb
npm install dynamoose-polytreenpm install --save dynamoosethen install the libnpm i dynamoose-polytree
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.
``typescript
//building.entity.ts
import { Model } from 'dynamoose-polytree';
export class Building extends Model {
name: string;
logo: JSON;
// ...
}
`
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.`typescript
import { Schema } from 'dynamoose/dist/Schema';
export const TypeScheme: Record = {
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.
`typescript
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`typescript
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.
`typescript
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()