1.0.0 • Published 3 months ago

flow-server-plugin-mariadb-raw v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

Flow Server Plugin - MariaDB Raw

A MariaDB plugin for Flow Server Framework that provides direct, raw SQL query capabilities.

Features

  • Execute raw SQL queries directly
  • Support for both synchronous and asynchronous query execution
  • Connection pooling for improved performance
  • Prepared statements for security
  • Transaction support

Installation

npm install flow-server-plugin-mariadb-raw

Usage

Basic Setup

const FlowServer = require('flow-server-framework');
const MariaDBRawPlugin = require('flow-server-plugin-mariadb-raw');

const app = new FlowServer();

// Register the plugin
app.use(new MariaDBRawPlugin({
  host: 'localhost',
  user: 'root',
  password: 'password',
  database: 'my_database'
}));

// Now you can use the plugin in your services
app.start();

Query Examples

Asynchronous Queries

// In your service
async function getUserById(id) {
  const query = 'SELECT * FROM users WHERE id = ?';
  const params = [id];
  
  const [rows] = await this.app.db.query(query, params);
  return rows[0];
}

async function createUser(userData) {
  const query = 'INSERT INTO users (name, email, active) VALUES (?, ?, ?)';
  const params = [userData.name, userData.email, userData.active];
  
  const [result] = await this.app.db.query(query, params);
  return result.insertId;
}

Synchronous Queries

// In your service
function getAllActiveUsers() {
  const query = 'SELECT * FROM users WHERE active = ?';
  const params = [true];
  
  return this.app.db.querySync(query, params);
}

Transactions

// In your service
async function transferFunds(fromAccountId, toAccountId, amount) {
  const connection = await this.app.db.getConnection();
  
  try {
    await connection.beginTransaction();
    
    await connection.query(
      'UPDATE accounts SET balance = balance - ? WHERE id = ?',
      [amount, fromAccountId]
    );
    
    await connection.query(
      'UPDATE accounts SET balance = balance + ? WHERE id = ?',
      [amount, toAccountId]
    );
    
    await connection.commit();
    return true;
  } catch (error) {
    await connection.rollback();
    throw error;
  } finally {
    connection.release();
  }
}

License

MIT

1.0.0

3 months ago