This package is an open source extension for MikroORM, which enables Nested Set Tree for your needs

Overview

MikroORM nested set

This package is an open source extension for MikroORM, which enables Nested Set Tree for your needs

Disclaimer

For now, this package doesn't support multiple trees, but it may change in near future

Installation

npm install mikroorm-nested-set

Setup

At first, you have to create a new entity-type class and extend NestedSetSubjectAbstract (Keep in mind that you have to pass your entity for generic type)

import {NestedSetSubjectAbstract} from "mikroorm-nested-set";
import {Cascade, Collection, Entity, ManyToOne, OneToMany, PrimaryKey, Property} from "@mikro-orm/core";

@Entity()
export class CategoryFake extends NestedSetSubjectAbstract<CategoryFake>{

    @PrimaryKey({ type: 'number' })
    id: number;

    @Property({ type: 'number' })
    left: number;

    @Property({ type: 'number' })
    right: number;

    @Property({ type: 'number' })
    depth: number;

    @OneToMany({
        entity: () => CategoryFake,
        mappedBy: category => category.parent,
        cascade: [Cascade.ALL],
        eager: true,
        orphanRemoval: true
    })
    children = new Collection<CategoryFake>(this)

    @ManyToOne({
        entity: () => CategoryFake
    })
    parent: CategoryFake

    getIdentifier(): number | string {
        return this.id
    }

}

After that, you have to create a repository for your new entity. (It's required for extra tree methods)

import {NestedSetSubjectRepository} from "mikroorm-nested-set";
import {CategoryFake} from "./category.fake";

export class CategoryFakeRepository extends NestedSetSubjectRepository<CategoryFake> {}

Don't forget about migration file if you chose migration files way

Examples

Create new tree

const nestedSetNodeOperator = new NestedSetNodeOperator<Category>()

const mainCategory = new Category()
mainCategory.name = 'Main'
mainCategory.parent = null

const manCategory = new Category()
manCategory.name = 'man'

const womanCategory = new Category()
womanCategory.name = 'Woman'

nestedSetNodeOperator.addNode(manCategory, mainCategory, mainCategory)
nestedSetNodeOperator.addNode(womanCategory, mainCategory, mainCategory)

const famousShoesCategory = new Category()
famousShoesCategory.name = 'Famous shoes category'

const manJeans = new Category()
manJeans.name = 'Jeans'

nestedSetNodeOperator.addNode(manJeans, manCategory, mainCategory)
nestedSetNodeOperator.addNode(famousShoesCategory, womanCategory, mainCategory)

await this.categoryRepository.persistAndFlushTree(mainCategory)

Update existing tree (Be aware of the difference between readableTree and writableTree)

const writableTree = await this.categoryRepository.findWritableTree()
const nestedSetNodeOperator = new NestedSetNodeOperator<Category>()

const manShirtsCategory = new Category()
manShirtsCategory.name = 'man shirts'

const manShoesCategory = new Category()
manShirtsCategory.name = 'man shoes Category'


const nodeToRemove = nestedSetNodeOperator.findNodeInTree(writableTree, {
    getIdentifier(): number | string {
        return 124 // Just some random unneccessary category
    }
})

nestedSetNodeOperator.removeNode(nodeToRemove, writableTree)

const manCategory = nestedSetNodeOperator.findNodeInTree(writableTree, {
  getIdentifier(): number | string {
    return 2 // Man category
  }
})

nestedSetNodeOperator.addNode(manShirtsCategory, manCategory, writableTree)
nestedSetNodeOperator.addNode(manShoesCategory, manCategory, writableTree)

await this.categoryRepository.persistAndFlushTree(writableTree)

Find and edit node property

const writableTree = await this.categoryRepository.findWritableTree()
const nestedSetNodeOperator = new NestedSetNodeOperator<Category>()

// You can pass either whole object or object with getIdentifier implemanation
const nodeToEdit = nestedSetNodeOperator.findNodeInTree(writableTree, {
  getIdentifier(): number | string {
    return 124
  }
})

nodeToEdit.customProperty = '123'

await this.categoryRepository.persistAndFlushTree(writableTree)

Tests

npm run test

TODO

  • More unit tests, especially with operator API
  • Support for multiple trees

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

License

The MIT License (MIT). Please see License File for more information.

You might also like...

This package enables you to mount your Remix app at a different path than root

This package enables you to mount your Remix app at a different path than root

Remix Mount Routes This package enables you to mount your Remix app at a different path than root. 🛠 Installation npm install -D remix-mount-routes

Dec 17, 2022

This package enables you to define your routes using the flat-routes convention.

This package enables you to define your routes using the flat-routes convention.

Remix Flat Routes This package enables you to define your routes using the flat-routes convention. This is based on the gist by Ryan Florence 🛠 Insta

Jan 3, 2023

🍎 A simple application which enables you to sync readings from your Withings scale to a Notion page.

🍎 A simple application which enables you to sync readings from your Withings scale to a Notion page.

weight-logger weight-logger is a simple application which enables you to sync readings from your Withings scale to a Notion page. Preview Installation

Jan 14, 2022

A refined tool for exploring open-source projects on GitHub with a file tree, rich Markdown and image previews, multi-pane multi-tab layouts and first-class support for Ink syntax highlighting.

A refined tool for exploring open-source projects on GitHub with a file tree, rich Markdown and image previews, multi-pane multi-tab layouts and first-class support for Ink syntax highlighting.

Ink codebase browser, "Kin" 🔍 The Ink codebase browser is a tool to explore open-source code on GitHub, especially my side projects written in the In

Oct 30, 2022

(🔗, 🌲) Web3 Link Tree is a free & open-source alternative to Linktree built with React.js, Next.js, Tailwind and Web3-React

Getting Started Read the detailed guide here Customize Add your name, wallet address, social media links and more in config.ts Images Save images to t

Sep 20, 2022

The open-source design system of Tree.ly

🌳 Boemly Boemly is the open source component library maintained by Tree.ly based on ChakraUI. 🏃‍♀️ Getting started Install Boemly: npm i boemly --sa

Oct 7, 2022

Chrome Extension that enables you to read 30% more efficiently and easily!

Chrome Extension that enables you to read 30% more efficiently and easily!

Bionic Reading Extension that enables you to read 30% more efficiently and easily! About The Project All credits for this method go to https://bionic-

Jan 1, 2023

A Chromium extension that enables users to use customizable :emoji: on Facebook/Facebook Messenger.

:emoji: for Messenger A Chromium extension that enables users to use customizable :emoji: on Facebook/Facebook Messenger. Install Download the extensi

Aug 31, 2022

Browser extension that enables you to Log-in as ANY address on ALL dapps

Browser extension that enables you to Log-in as ANY address on ALL dapps

Impersonator Extension Log-in as ANY address on ALL dapps. Impersonator injects into the dapps just like Metamask, but gives you the freedom to set cu

Dec 14, 2022
Releases(1.0.7)
Owner
Kamil Fronczak
NodeJS/TS, Go, PHP, DevOps
Kamil Fronczak
Mekna'7, a subsidiary of the ONCF group, which provides bus services to cities not served by train, needs to set up a computer system by creating a database for managing customer reservations.

Online-bus-ticket-reservation Introduction Hello everyone, this is a project that I have done for assignment. This project is a simple online bus tick

Hala Ziani 5 Oct 25, 2022
Fancytree - JavaScript tree view / tree grid plugin with support for keyboard, inline editing, filtering, checkboxes, drag'n'drop, and lazy loading

Fancytree Fancytree (sequel of DynaTree 1.x) is a JavaScript tree view / tree grid plugin with support for keyboard, inline editing, filtering, checkb

Martin Wendt 2.6k Jan 9, 2023
Package fetcher is a bot messenger which gather npm packages by uploading either a json file (package.json) or a picture representing package.json. To continue...

package-fetcher Ce projet contient un boilerplate pour un bot messenger et l'executable Windows ngrok qui va permettre de créer un tunnel https pour c

AILI Fida Aliotti Christino 2 Mar 29, 2022
Flight is a universal package manager for your needs, no matter what language you may want to write your code in.

Flight Swift, reliable, multi-language package manager. ⚡ Installation We don't have an official release of Flight yet, however, if you would like to

null 26 Dec 25, 2022
Shikhar 4 Oct 9, 2022
An open-source Typing-effect Library, That enables potential users to add a typing effect to mere DOM Elements.

Typing Effect Library An open-source Typing-effect Library I created. That enables potential users to add a typing effect to mere DOM Elements. Tool P

Okoye Charles 14 Oct 3, 2022
Grupprojekt för kurserna 'Javascript med Ramverk' och 'Agil Utveckling'

JavaScript-med-Ramverk-Laboration-3 Grupprojektet för kurserna Javascript med Ramverk och Agil Utveckling. Utvecklingsguide För information om hur utv

Svante Jonsson IT-Högskolan 3 May 18, 2022
Hemsida för personer i Sverige som kan och vill erbjuda boende till människor på flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 4 May 3, 2022
Kurs-repo för kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

null 14 Jan 3, 2023