How to create a new DB table while on plugin development?

Hello.

I’m working on to make a custom plugin for my HollaEx exchange, and I want to know how to create a new DB table for it. Can someone help me?

Also, Do I need to include a model file in db/models folder to do it?

I’ll wait for the reply. Thank you.

1 Like

Hi there.

There is a function called createModel in the tools library. You can access it via toolsLib.database.createModel() .

The parameters of the function are (name , properties , options ).

  • name is the name of the new model to create - properties is an object that contains all the model properties in sequelize format. The ID property is already set in the function so you don’t need to pass it.

  • options are additional sequelize options

This function will return the newly created sequelize model object. You can then use the returned model object as any other sequelize model object.

Example:

 const exampleModel = toolsLib.database.createModel(
    'example',
    properties = {
        created_by: {
            type: 'integer',
            onDelete: 'CASCADE',
            allowNull: false,
            references: {
                model: 'Users',
                key: 'id'
            }
        },
        title: {
            type: 'string',
            allowNull: false
        },
        message: {
            type: 'text',
            allowNull: false
        }
    }
);

exampleModel.create({...});

Let me know if you have any question further :slight_smile: