fremail / sequelize-nested-set

Library to store and manage nested set trees using Sequelize
MIT License
40 stars 10 forks source link

How to add child node to root? #33

Closed xuanngo2001 closed 2 years ago

xuanngo2001 commented 2 years ago

How to add child node to root node? I got error message "TypeError: root.addChild is not a function". Here is my code.

const { Sequelize, DataTypes, BulkRecordError } = require('sequelize');
const ns = require('sequelize-nested-set');

// Connect to database.
const sequelize = new Sequelize({
    dialect: 'sqlite',
    storage: 'treeorg.sqlite'
});

// Define Tree model.
const Tree = ns(sequelize
                , DataTypes
                , 'Tree'
                , { label: DataTypes.STRING }
                , { //tableName: 'Tree'
                    timestamps: false
                    , hasManyRoots: true
                    , levelColumnName: 'level'
                }
);

// Create root record.
sequelize.sync()
    .then(() => {

        let root = new Tree();
        root.label = 'Root record';
        root = Tree.createRoot(root);

        let childNode = new Tree();
        childNode.label = 'child node';
        root.addChild(childNode);
    });
xuanngo2001 commented 2 years ago

Closed. Need to wrap code around (async () => { .... })();