New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

@megaorm/driver

Package Overview
Dependencies
Maintainers
0
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@megaorm/driver

This package defines the core interfaces and types needed to create custom database drivers for MegaORM.

latest
Source
npmnpm
Version
1.0.0
Version published
Maintainers
0
Created
Source

MegaORM Driver

This package defines the essential interfaces and types for creating custom database drivers for MegaORM. All built-in and custom MegaORM drivers must implement these interfaces, ensuring consistent behavior and compatibility across different databases.

Installation

To install the package, run:

npm install @megaorm/driver

Purpose

This package defines the following:

  • MegaDriver Interface: A blueprint for creating database drivers.
  • MegaConnection Interface: A standardized interface for database connections.
  • Types:
    • Row: Represents a single database row.
    • Rows: Represents multiple database rows.
    • MegaQueryResult: Represents the result of a query.

Types

Row

A type representing a single database row:

export type Row = { [column: string]: number | string | null };

Rows

A type representing multiple rows:

export type Rows = Array<Row>;

MegaQueryResult

Union type representing all possible query results:

export type MegaQueryResult = string | number | Rows | Row | void;

Interfaces

MegaDriver

Represents the database driver interface that all MegaORM drivers must implement:

export interface MegaDriver {
  id: Symbol;
  create(): Promise<MegaConnection>;
}

MegaConnection

A generic interface for database connections. It ensures consistency across different drivers.

Methods

  • query: Executes a SQL query on the database.
  • close: Closes the database connection.
  • beginTransaction: Starts a new transaction.
  • commit: Commits the current transaction.
  • rollback: Rolls back the current transaction.
export interface MegaConnection {
  id: Symbol;
  driver: MegaDriver;

  query(sql: string, values?: Array<string | number>): Promise<MegaQueryResult>;
  close(): Promise<void>;
  beginTransaction(): Promise<void>;
  commit(): Promise<void>;
  rollback(): Promise<void>;
}

Implementing a Custom Driver

Below is an example of how to implement a custom driver for MegaORM:

import {
  MegaDriver,
  MegaConnection,
  Row,
  Rows,
  MegaQueryResult,
} from '@megaorm/driver';

class MariaDB implements MegaDriver {
  id = Symbol('MariaDB');

  async create(): Promise<MegaConnection> {
    return new MariaDBConnection(this);
  }
}

class MariaDBConnection implements MegaConnection {
  id = Symbol('MegaPoolConnection');
  driver: MegaDriver;

  constructor(driver: MegaDriver) {
    this.driver = driver;
  }

  async query(
    sql: string,
    values?: Array<string | number>
  ): Promise<MegaQueryResult> {
    // Implement query logic here
  }

  async close(): Promise<void> {
    // Implement connection close logic
  }

  async beginTransaction(): Promise<void> {
    // Start transaction
  }

  async commit(): Promise<void> {
    // Commit transaction
  }

  async rollback(): Promise<void> {
    // Rollback transaction
  }
}

Keywords

megaorm

FAQs

Package last updated on 20 Dec 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts