HollaEx®
⚙️ DashboardStart →
  • HollaEx® — The Crypto Exchange Solution
  • ☁️Cloud Operators
    • Launching the Exchange
    • Setting Domain for Cloud Exchanges
    • Easy SMTP for Cloud Exchanges
    • SEO Settings for Cloud Exchanges
      • SEO Advanced Settings
  • ⚙️Operating Your Exchange
    • Operator Control Panel
      • General
      • Users
      • User Profile
      • Assets
      • Markets
      • Stakes
      • Sessions
      • Plugins Apps
      • Trading Fees & Account Tiers
      • Roles
      • Chat
      • Billing
    • Customize Exchange
      • Browser Tools
        • Enter Edit Mode
        • Operator Controls (Visuals)
        • Console
      • Plugins
      • Forked Repo
    • Fiat Controls
      • Initial Setup
      • Setting Up Fiat On/ Off Ramp
      • Editing Deposit & Withdrawal Fees
      • Users Making Fiat Deposit
      • Users Trading With Fiat
      • User Making Fiat Withdrawal
    • Staking
    • OTC Broker
    • P2P
      • P2P Overview
      • P2P Setup
      • P2P Troubleshooting
      • P2P Vendor Flow
    • Smart Chain Trading
    • Assets & Trading Pairs
      • Add New Assets & Trading Pairs
      • Configure Pair Parameters
    • Set up the SMTP Email
      • Set up SMTP with AWS SES
      • Set up SMTP with Mailgun
      • Set up SMTP with SendGrid
      • Test the SMTP with Gmail
    • Enabling reCAPTCHA
    • Email Customization & Audit
    • DeFi Asset Staking Process
  • 🧩Plugins
    • HollaEx Plugins
      • Announcements
      • Bank
      • AWS SNS (Text Messages - SMS)
      • KYC
      • Automatic KYC
      • Messente
      • Advanced Referral
      • CoinMarketCap
      • Guardarian
    • Install Plugins
    • Developing Plugins
      • Development Walkthrough: Hello-Plugin
        • Initialization
        • Configuration
        • Scripting
        • Web View
        • The Final Product & Installation
      • Advanced
        • Initialization
        • Config
        • Server Script
        • Plugin Libraries
        • Web View
        • Final Plugin Product
        • Advanced Tutorial: Using the user meta field
        • Advanced Tutorial: Adding a new database table column
        • Advanced Tutorial: Creating a new database table
      • Simple Wallet Example
      • Web View Development
        • Overview
        • External dependencies
        • Getting started
        • Basic Tutorial: Hello Exchange Plugin web view
        • Advanced Tutorial: KYC Plugin web views
    • Bank Integration
      • Handling Deposits
      • Handling Withdrawals
  • 👷Developers
    • API Guide
      • API Example Scripts
    • Run Dev Mode
    • Build a New Front-end Interface
  • 🧰On-Premise Operators (Advanced Only)
    • On-Premise Exchange Setup
      • Getting Started — Requirements
      • Installation
      • Server Setup
      • Web Setup
      • Production
    • CLI How-Tos
      • Start Exchange
      • Stop Exchange
      • Upgrade Exchange
        • Build and Apply the Code Changes
      • Get Exchange Logs
      • Get a Backup and Restore
      • Exchange Migration
      • Command List
    • Run Exchange on Kubernetes
    • Troubleshooting Guide
  • 🚀Advanced
    • SEO Optimization
    • Nginx
    • Rate Limits
    • Database
      • Upgrade Database
    • Dependencies
    • Contents Delivery Network
      • Cloudflare CDN for HollaEx
      • CloudFront CDN for HollaEx
    • Load Balancer
      • AWS ELB
      • DigitalOcean LB
    • Customize Kubenretes Ingress
    • Exchange Keys
      • Exchange API Keys Troubleshoot
    • HollaEx on non-Linux
      • HollaEx on Windows
      • HollaEx on macOS
    • The Network Tool Library
      • Accessing the Network Tool Library
      • Functions
        • WebSocket
      • Simple Example: Creating a User and Wallet
      • Getting More Interesting: Orders with the Tools
        • Setup: Using the transferAsset function
        • Creating and Monitoring a Sell Order
        • Settling Fees
      • Private HollaEx Network
    • Docker Content Trust (DCT)
    • Revenue Sharing
  • 📦Releases
    • Release Notes
    • Side Notes
  • ➡️External Links
  • Blogs
  • Forum
  • Videos
  • Twitter X
  • Telegram
  • Interactive Demo
  • Discord Community
  • API Documentation
  • Tools Library Documentation
  • Node Library Documentation
  • Plugins Documentation
Powered by GitBook
On this page
  • Script
  • Breakdown
  • Create New Table Migration
  • Add helper functions for accessing the new table
  1. Plugins
  2. Developing Plugins
  3. Advanced

Advanced Tutorial: Creating a new database table

PreviousAdvanced Tutorial: Adding a new database table columnNextSimple Wallet Example

Last updated 3 years ago

In this tutorial, we will go over how to create a new table in our database by creating a new migration. To illustrate this, we are creating a plugin that allows users to add their twitter usernames. The requirements will be stored in a newly created table Twitters in the DB. This table will be created by a migration using the library.

We DO NOT recommend creating a new table in the DB. This can potentially have cause adverse side effects on your exchange.

Script

'use strict';

const {
	app,
	loggerPlugin,
	toolsLib
} = this.pluginLibraries;
const sequelize = require('sequelize');
const umzug = require('umzug');
const { body, validationResult } = require('express-validator');

const init = async () => {
	const umzugInstance = new umzug({
		storage: 'sequelize',
		storageOptions: {
			sequelize: toolsLib.database.getModel('sequelize'),
			modelName: 'PluginMigrations',
			tableName: 'PluginMigrations'
		},
		upName: 'up',
		downName: 'down',
		migrations: umzug.migrationsList(
			[
				{
					name: 'twitter_plugin-create_twitter_table',
					up: (queryInterface, Sequelize) => queryInterface.createTable(
						'Twitters',
						{
							id: {
								allowNull: false,
								autoIncrement: true,
								primaryKey: true,
								type: Sequelize.INTEGER
							},
							user_id: {
								type: Sequelize.INTEGER,
								onDelete: 'CASCADE',
								allowNull: false,
								references: {
									model: 'Users',
									key: 'id'
								}
							},
							username: {
								type: Sequelize.STRING,
								allowNull: false,
								unique: true
							},
							updated_at: {
								allowNull: false,
								type: Sequelize.DATE,
								defaultValue: Sequelize.literal('NOW()')
							},
							created_at: {
								allowNull: false,
								type: Sequelize.DATE,
								defaultValue: Sequelize.literal('NOW()')
							}
						},
						{
							timestamps: true,
							underscored: true
						}
					),
					down: (queryInterface, Sequelize) => queryInterface.dropTable('Twitters')
				}
			],
			[toolsLib.database.getModel('sequelize').getQueryInterface(), sequelize]
		)
	});

	const pending = await umzugInstance.pending();
	if (pending.length > 0) {
		await umzugInstance.up('twitter_plugin-create_twitter_table');
	}
};

const findUserTwitter = async (user_id) => {
	return toolsLib.database.getModel('sequelize').query('SELECT * FROM "Twitters" WHERE user_id = :user_id', {
		plain: true,
		raw: true,
		replacements: {
			user_id
		},
		type: sequelize.QueryTypes.SELECT
	});
};

const createUserTwitter = async (user_id, username) => {
	const twitter = await toolsLib.database.getModel('sequelize').query('INSERT INTO "Twitters" (user_id, username) VALUES (:user_id, :username) RETURNING *', {
		replacements: {
			user_id,
			username
		},
		type: sequelize.QueryTypes.INSERT
	});

	return twitter[0][0];
};

const updateUserTwitter = async (user_id, username) => {
	const twitter = await toolsLib.database.getModel('sequelize').query('UPDATE "Twitters" SET username = :username WHERE user_id = :user_id RETURNING *', {
		replacements: {
			user_id,
			username
		},
		type: sequelize.QueryTypes.UPDATE
	});

	return twitter[0][0];
};

init()
	.then(() => {
		app.get(
			'/plugins/twitter/username',
			[toolsLib.security.verifyBearerTokenExpressMiddleware(['user'])],
			async (req, res) => {
				loggerPlugin.verbose(
					req.uuid,
					'GET /plugins/twitter/username auth',
					req.auth.sub
				);

				const { id } = req.auth.sub;

				try {
					const user = await toolsLib.user.getUserByKitId(id);

					if (!user) {
						throw new Error('User not found');
					}

					const twitter = await findUserTwitter(user.id);

					if (!twitter) {
						throw new Error('User twitter username not found');
					}

					return res.json(twitter);
				} catch (err) {
					loggerPlugin.error(
						req.uuid,
						'GET /plugins/twitter/username err',
						err.message
					);
					return res.status(err.status || 400).json({message: err.message});
				}
			}
		);

		app.post(
			'/plugins/twitter/username',
			[
				toolsLib.security.verifyBearerTokenExpressMiddleware(['user']),
				body('username').isString().notEmpty()
			],
			async (req, res) => {
				const errors = validationResult(req);
				if (!errors.isEmpty()) {
					return res.status(400).json({errors: errors.array()});
				}

				const { id } = req.auth.sub;
				const { username } = req.body;

				loggerPlugin.verbose(
					req.uuid,
					'POST /plugins/twitter/username username:',
					username
				);

				try {
					const user = await toolsLib.user.getUserByKitId(id);

					if (!user) {
						throw new Error('User not found');
					}

					const twitter = await findUserTwitter(user.id);

					if (twitter) {
						throw new Error('User already has twitter username set');
					}

					const result = await createUserTwitter(user.id, username);

					return res.json(result);
				} catch (err) {
					loggerPlugin.error(
						req.uuid,
						'POST /plugins/twitter/username err:',
						err.message
					);
					return res.status(err.status || 400).json({message: err.message});
				}
			}
		);

		app.put(
			'/plugins/twitter/username',
			[
				toolsLib.security.verifyBearerTokenExpressMiddleware(['user']),
				body('username').isString().notEmpty()
			],
			async (req, res) => {
				const errors = validationResult(req);
				if (!errors.isEmpty()) {
					return res.status(400).json({errors: errors.array()});
				}

				const { id } = req.auth.sub;
				const { username } = req.body;

				loggerPlugin.verbose(
					req.uuid,
					'PUT /plugins/twitter/username username:',
					username
				);

				try {
					const user = await toolsLib.user.getUserByKitId(id);

					if (!user) {
						throw new Error('User not found');
					}

					const twitter = await findUserTwitter(user.id);

					if (!twitter) {
						throw new Error('User twitter username not found');
					}

					if (twitter.username === username) {
						throw new Error(`User twitter username is already ${username}`);
					}

					const result = await updateUserTwitter(user.id, username);

					return res.json(result);
				} catch (err) {
					loggerPlugin.error(
						req.uuid,
						'PUT /plugins/twitter/username err:',
						err.message
					);
					return res.status(err.status || 400).json({message: err.message});
				}
			}
		);
	})
	.catch((err) => {
		loggerPlugin.error(
			'TWITTER PLUGIN error during initialization:',
			err.message
		);
	});

Breakdown

For this tutorial, we will only go over how we create and use the newly created DB table Twitters.

Create New Table Migration

const init = async () => {
	const umzugInstance = new umzug({
		storage: 'sequelize',
		storageOptions: {
			sequelize: toolsLib.database.getModel('sequelize'),
			modelName: 'PluginMigrations',
			tableName: 'PluginMigrations'
		},
		upName: 'up',
		downName: 'down',
		migrations: umzug.migrationsList(
			[
				{
					name: 'twitter_plugin-create_twitter_table',
					up: (queryInterface, Sequelize) => queryInterface.createTable(
						'Twitters',
						{
							id: {
								allowNull: false,
								autoIncrement: true,
								primaryKey: true,
								type: Sequelize.INTEGER
							},
							user_id: {
								type: Sequelize.INTEGER,
								onDelete: 'CASCADE',
								allowNull: false,
								references: {
									model: 'Users',
									key: 'id'
								}
							},
							username: {
								type: Sequelize.STRING,
								allowNull: false,
								unique: true
							},
							updated_at: {
								allowNull: false,
								type: Sequelize.DATE,
								defaultValue: Sequelize.literal('NOW()')
							},
							created_at: {
								allowNull: false,
								type: Sequelize.DATE,
								defaultValue: Sequelize.literal('NOW()')
							}
						},
						{
							timestamps: true,
							underscored: true
						}
					),
					down: (queryInterface, Sequelize) => queryInterface.dropTable('Twitters')
				}
			],
			[toolsLib.database.getModel('sequelize').getQueryInterface(), sequelize]
		)
	});

	const pending = await umzugInstance.pending();
	if (pending.length > 0) {
		await umzugInstance.up('twitter_plugin-create_twitter_table');
	}
};

To create our new Twitters table, we need to create a new migration for our database. We can do this using the umzug library. We are first creating an instance of umzug with the following configurations:

  • storage: Should be set to sequelize

  • storageOptions

    • sequelize: This is the sequelize instance being used throughout our exchagne. We can get this instance using the tools library getModel function

    • modelName: The name of the to be used model. Should be set to PluginMigrations

    • tableName: The name of the table that stores migrations in the DB. Should be set to PluginMigrations

  • upName: Should be set to up

  • downName: Should be set to down

  • migrations: This is where we use the umzug migrationsList function to create our migration. Each migration will need a name, up function, and down function.

    • name: The name of this migration. This should be formatted as <PLUGIN_NAME>-<MIGRATION_ACTION> e.g. twitter_plugin-create_twitter_table

    • up: The function to run when running the migration. Should be formatted as a basic sequelize migration.

    • down: The function to run when removing the migration. Should be formatted as a basic sequelize migration.

Once configured, we need to check if the migration has already been ran in the database. To do so, we can use umzugInstance.pending() to check if there are any migrations with the given name that have not been ran. If none are found, we run the migration using umzugInstance.up(<MIGRATION_NAME>).

Add helper functions for accessing the new table

const findUserTwitter = async (user_id) => {
	return toolsLib.database.getModel('sequelize').query('SELECT * FROM "Twitters" WHERE user_id = :user_id', {
		plain: true,
		raw: true,
		replacements: {
			user_id
		},
		type: sequelize.QueryTypes.SELECT
	});
};

const createUserTwitter = async (user_id, username) => {
	const twitter = await toolsLib.database.getModel('sequelize').query('INSERT INTO "Twitters" (user_id, username) VALUES (:user_id, :username) RETURNING *', {
		replacements: {
			user_id,
			username
		},
		type: sequelize.QueryTypes.INSERT
	});

	return twitter[0][0];
};

const updateUserTwitter = async (user_id, username) => {
	const twitter = await toolsLib.database.getModel('sequelize').query('UPDATE "Twitters" SET username = :username WHERE user_id = :user_id RETURNING *', {
		replacements: {
			user_id,
			username
		},
		type: sequelize.QueryTypes.UPDATE
	});

	return twitter[0][0];
};

Newly created tables are not included in the base sequelize instance for the Kit. This means we can't access the column using basic Tools Library functions. Instead, we need to use the Sequelize instance for the exchange to run raw SQL queries. We can do this using the getModel('sequelize').query(...) function. For our plugin, we need to get, create, and update Twitters rows for new users.

The rest of the script is using the newly created table to get, set, and update user Twitter usernames.

🧩
umzug