@megaorm/utils
Version:
This package provides utility functions to validate whether a value is a MegaORM driver or a connection.
113 lines (72 loc) • 2.98 kB
Markdown
# MegaORM Utils
This package provides utility functions to validate whether a value is a MegaORM driver or a connection.
## Installation
To install this package, run the following command:
```bash
npm install /utils
```
## Functions
`isDriver(driver)`: Ensures that the given value is one of MegaORM's supported drivers: `/mysql`, `@megaorm/sqlite`, or `/pg`.
```js
const { isDriver } = require('/utils');
console.log(isDriver({})); // false
```
> Returns `true` if the value is a valid MegaORM driver, `false` otherwise.
`isMySQL(driver)`: Ensures that the given value is a `/mysql` driver.
```js
const { isMySQL } = require('/utils');
console.log(isMySQL({})); // false
```
> Returns `true` if the value is the MySQL driver, `false` otherwise.
`isPostgreSQL(driver)`: Ensures that the given value is a `/pg` driver.
```js
const { isPostgreSQL } = require('/utils');
console.log(isPostgreSQL({})); // false
```
> Returns `true` if the value is the PostgreSQL driver, `false` otherwise.
`isSQLite(driver)`: Ensures that the given value is a `/sqlite` driver.
```js
const { isSQLite } = require('/utils');
console.log(isSQLite({})); // false
```
> Returns `true` if the value is the SQLite driver, `false` otherwise.
`isCon(connection)`: Ensures that the given value is a `MegaConnection` — a connection created by any MegaORM driver.
```js
const { isCon } = require('/utils');
console.log(isCon({})); // false
```
> Returns `true` if the value is a `MegaConnection`, `false` otherwise.
`isPendingCon(connection)`: Ensures that the given value is a `MegaPendingConnection` — a connection that could not be closed by `MegaPool`.
```js
const { isPendingCon } = require('/utils');
console.log(isPendingCon({})); // false
```
> Returns `true` if the value is a `MegaPendingConnection`, `false` otherwise.
`isPoolCon(connection)`: Ensures that the given value is a `MegaPoolConnection` — a connection created by `MegaPool`.
```js
const { isPoolCon } = require('/utils');
console.log(isPoolCon({})); // false
```
> Returns `true` if the value is a `MegaPoolConnection`, `false` otherwise.
## Usage Examples
```js
const { isDriver, isSQLite, isCon } = require('/utils');
const { SQLite } = require('/sqlite');
// Create a driver instance
const driver = new SQLite(':memory:');
// Ensure it's an SQLite driver instance
if (isSQLite(driver)) {
console.log('This is an SQLite driver!');
}
// Ensure it's a MegaORM driver instance (SQLite, PostgreSQL, MySQL)
if (isDriver(driver)) {
console.log('This is a MegaORM driver!');
}
// Create a connection
driver.create().then((connection) => {
// Ensure it's a MegaConnection
if (isCon(connection)) {
console.log('This is a MegaConnection!');
}
});
```