@fxjs/db-driver
Advanced tools
Comparing version 0.1.0 to 0.2.0
/// <reference types="fib-pool" /> | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.MongoDriver = exports.RedisDriver = exports.SQLiteDriver = exports.MySQLDriver = exports.SQLDriver = exports.Driver = void 0; | ||
const db = require("db"); | ||
exports.SQLDriver = exports.Driver = void 0; | ||
const url = require("url"); | ||
@@ -28,12 +27,18 @@ const assert = require("assert"); | ||
} | ||
// function getDriver (name: 'mongodb'): typeof import('./driver-mongodb').default | ||
function getDriver(name) { | ||
switch (name) { | ||
case 'mysql': | ||
return MySQLDriver; | ||
return require('./driver-mysql').default; | ||
case 'postgresql': | ||
case 'postgres': | ||
case 'pg': | ||
case 'psql': | ||
return require('./driver-postgresql').default; | ||
case 'sqlite': | ||
return SQLiteDriver; | ||
return require('./driver-sqlite').default; | ||
case 'redis': | ||
return RedisDriver; | ||
case 'mongodb': | ||
return MongoDriver; | ||
return require('./driver-redis').default; | ||
// case 'mongodb': | ||
// return require('./driver-mongodb').default | ||
default: | ||
@@ -45,2 +50,3 @@ if (name) { | ||
} | ||
// throw new Error(`[feature] driver type ${name} is not supported`) | ||
return Driver; | ||
@@ -55,2 +61,3 @@ } | ||
}; | ||
// options would be replaced, `Utils.parseConnectionString` return a fresh object | ||
options = Utils.parseConnectionString(options); | ||
@@ -60,3 +67,10 @@ Object.defineProperty(this, 'config', { get() { return options; } }); | ||
// some db has no host | ||
// assert.ok(!!this.config.host, '[driver.config] invalid host') | ||
switch (options.protocol) { | ||
default: break; | ||
case 'mysql:': | ||
case 'mssql:': | ||
case 'postgresql:': | ||
assert.ok(!!this.config.host || !!this.config.hostname, '[driver.config] host or hostname required'); | ||
break; | ||
} | ||
this.type = Utils.filterDriverType(this.config.protocol); | ||
@@ -74,4 +88,16 @@ const extend_config = {}; | ||
} | ||
/** | ||
* @descritpin there's a bug in fibjs <= 0.35.x, only string type `field` would be | ||
* used by `url.format` | ||
*/ | ||
static formatUrl(input) { | ||
let protocol = input.protocol; | ||
// some user would like add // after valid protocol like `http:`, `mysql:` | ||
if (protocol === null || protocol === void 0 ? void 0 : protocol.endsWith('//')) | ||
protocol = protocol.slice(0, -2); | ||
return url.format(Object.assign(Object.assign(Object.assign({}, input), { protocol }), !!input.port && { port: input.port + '' })); | ||
} | ||
get uri() { | ||
return url.format(Object.assign(Object.assign({}, this.config), { slashes: this.config.protocol === 'sqlite:' ? false : this.config.slashes, query: this.config.protocol === 'sqlite:' ? {} : this.config.query })); | ||
const isSQLite = this.config.protocol === 'sqlite:'; | ||
return Driver.formatUrl(Object.assign(Object.assign(Object.assign({}, this.config), this.type === 'psql' && { protocol: 'psql:' }), { slashes: isSQLite ? false : this.config.slashes, query: isSQLite ? {} : this.config.query })); | ||
} | ||
@@ -84,2 +110,4 @@ get isPool() { | ||
return ((p === 'mysql:') | ||
|| (p === 'mssql:') | ||
|| (p === 'psql:') | ||
|| (p.startsWith('sqlite:'))); | ||
@@ -97,2 +125,8 @@ } | ||
/** | ||
* @description switch to another database, pointless for some databases such as sqlite | ||
* @param targetDb | ||
*/ | ||
switchDb(targetDb) { } | ||
; | ||
/** | ||
* @description re open db connection | ||
@@ -157,4 +191,2 @@ */ | ||
} | ||
switchDb(targetDb) { } | ||
; | ||
/** | ||
@@ -179,118 +211,1 @@ * @override | ||
exports.SQLDriver = SQLDriver; | ||
class MySQLDriver extends SQLDriver { | ||
constructor(conn) { | ||
super(conn); | ||
this.connection = null; | ||
} | ||
switchDb(targetDb) { | ||
this.execute(db.formatMySQL("use `" + db.escape(targetDb) + "`")); | ||
} | ||
open() { return super.open(); } | ||
close() { | ||
if (this.connection) | ||
this.connection.close(); | ||
} | ||
ping() { return; } | ||
begin() { return this.connection.begin(); } | ||
commit() { return this.connection.commit(); } | ||
trans(cb) { return this.connection.trans(cb); } | ||
rollback() { return this.connection.rollback(); } | ||
getConnection() { return db.openMySQL(this.uri); } | ||
execute(sql) { | ||
if (this.isPool) | ||
return this.pool(conn => conn.execute(sql)); | ||
if (!this.connection) | ||
this.open(); | ||
return this.connection.execute(sql); | ||
} | ||
} | ||
exports.MySQLDriver = MySQLDriver; | ||
class SQLiteDriver extends SQLDriver { | ||
constructor(conn) { | ||
super(conn); | ||
this.connection = null; | ||
} | ||
open() { return super.open(); } | ||
close() { | ||
if (this.connection) | ||
this.connection.close(); | ||
} | ||
ping() { return; } | ||
begin() { return this.connection.begin(); } | ||
commit() { return this.connection.commit(); } | ||
trans(cb) { return this.connection.trans(cb); } | ||
rollback() { return this.connection.rollback(); } | ||
getConnection() { return db.openSQLite(this.uri); } | ||
execute(sql) { | ||
if (this.isPool) | ||
return this.pool(conn => conn.execute(sql)); | ||
if (!this.connection) | ||
this.open(); | ||
return this.connection.execute(sql); | ||
} | ||
} | ||
exports.SQLiteDriver = SQLiteDriver; | ||
class RedisDriver extends Driver { | ||
constructor(conn) { | ||
super(conn); | ||
this.connection = null; | ||
} | ||
open() { return super.open(); } | ||
close() { | ||
if (this.connection) | ||
this.connection.close(); | ||
} | ||
ping() { } | ||
command(cmd, ...args) { | ||
if (this.isPool) | ||
return this.pool(conn => conn.command(cmd, ...args)); | ||
if (!this.connection) | ||
this.open(); | ||
return this.connection.command(cmd, ...args); | ||
} | ||
commands(cmds, opts) { | ||
const { parallel = false } = opts || {}; | ||
const keys = Object.keys(cmds); | ||
if (parallel) | ||
return coroutine.parallel(keys, (cmd) => { | ||
return { cmd, result: this.command(cmd, ...Utils.arraify(cmds[cmd])) }; | ||
}); | ||
else | ||
return Object.keys(cmds).map((cmd) => { | ||
return { cmd, result: this.command(cmd, ...Utils.arraify(cmds[cmd])) }; | ||
}); | ||
} | ||
getConnection() { return db.openRedis(this.uri); } | ||
} | ||
exports.RedisDriver = RedisDriver; | ||
class MongoDriver extends Driver { | ||
constructor(conn) { | ||
super(conn); | ||
this.connection = null; | ||
} | ||
reopen() { | ||
try { | ||
this.close(); | ||
} | ||
catch (error) { } | ||
return this.open(); | ||
} | ||
open() { return super.open(); } | ||
close() { | ||
if (this.connection) | ||
this.connection.close(); | ||
} | ||
ping() { } | ||
command(cmd, arg) { | ||
return this.commands({ [cmd]: arg }); | ||
} | ||
commands(cmds, opts) { | ||
if (this.isPool) | ||
return this.pool(conn => conn.runCommand(cmds)); | ||
if (!this.connection) | ||
this.open(); | ||
return this.connection.runCommand(cmds); | ||
} | ||
getConnection() { return db.openMongoDB(this.uri); } | ||
} | ||
exports.MongoDriver = MongoDriver; |
@@ -20,6 +20,11 @@ /// <reference types="@fibjs/types" /> | ||
return 'mysql'; | ||
case 'postgresql:': | ||
case 'postgres:': | ||
case 'pg:': | ||
case 'psql:': | ||
return 'psql'; | ||
case 'redis:': | ||
return 'redis'; | ||
case 'mongodb:': | ||
return 'mongodb'; | ||
// case 'mongodb:': | ||
// return 'mongodb'; | ||
default: | ||
@@ -26,0 +31,0 @@ return 'unknown'; |
{ | ||
"name": "@fxjs/db-driver", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "", | ||
@@ -34,8 +34,8 @@ "keywords": [ | ||
"dependencies": { | ||
"@fxjs/orm-core": "^0.1.0", | ||
"@fxjs/orm-core": "^0.2.0", | ||
"parse-querystring-dotkey": "^1.0.3" | ||
}, | ||
"devDependencies": { | ||
"@fibjs/types": "^0.31.0", | ||
"@fxjs/knex": "^0.3.0", | ||
"@fibjs/types": "^0.34.1", | ||
"@fxjs/knex": "^0.3.1", | ||
"cross-env": "^5.2.0", | ||
@@ -51,11 +51,2 @@ "fib-pool": "^1.6.0", | ||
}, | ||
"ci": { | ||
"type": "travis, appveyor", | ||
"version": [ | ||
"0.28.0", | ||
"0.29.0", | ||
"0.30.0", | ||
"0.31.0" | ||
] | ||
}, | ||
"private": false, | ||
@@ -70,3 +61,3 @@ "homepage": "https://github.com/fxjs-modules/orm/tree/master/packages/db-driver", | ||
}, | ||
"gitHead": "1c80cd6d5609c80e4ec66f17f56adf27acc4cc60" | ||
"gitHead": "2f11deea2592a9a9919dfdde51ec2b3586fc93f5" | ||
} |
@@ -53,10 +53,2 @@ ## Fibjs Orm Drivers | ||
URI=mysql://username:password@localhost/database fibjs test/run-db | ||
``` | ||
## Credits | ||
This repo is checked out from [Diogo Resende]'s [node-sql-ddl-sync], which is one part of `orm`, and orm is the original source of `@fxjs/orm`. Thx a lot to him and his partner. | ||
[Diogo Resende]:dresende@thinkdigital.pt | ||
[node-sql-ddl-sync]:./README_orig.md | ||
``` |
@@ -5,12 +5,26 @@ /// <reference types="@fibjs/types" /> | ||
import { FxOrmCoreCallbackNS } from '@fxjs/orm-core'; | ||
declare function getDriver(name: 'sqlite'): typeof SQLiteDriver; | ||
declare function getDriver(name: 'redis'): typeof RedisDriver; | ||
declare function getDriver(name: 'mysql'): typeof MySQLDriver; | ||
export declare class Driver<CONN_TYPE = any> { | ||
declare function getDriver(name: 'mysql'): typeof import('./driver-mysql').default; | ||
declare function getDriver(name: 'psql' | 'postgresql' | 'pg'): typeof import('./driver-postgresql').default; | ||
declare function getDriver(name: 'sqlite'): typeof import('./driver-sqlite').default; | ||
declare function getDriver(name: 'redis'): typeof import('./driver-redis').default; | ||
export declare namespace Driver { | ||
export type IConnTypeEnum = Class_DbConnection | Class_MongoDB | Class_Redis; | ||
type IClass_PostgreSQL = Class_DbConnection; | ||
type IClass_MSSQL = Class_DbConnection; | ||
export type ISQLConn = IClass_PostgreSQL | IClass_MSSQL | Class_SQLite | Class_MySQL; | ||
export type ITypedDriver<T extends IConnTypeEnum = IConnTypeEnum> = T extends ISQLConn ? SQLDriver<T> : T extends Class_MongoDB ? import('./driver-mongodb').default : T extends Class_Redis ? import('./driver-redis').default : Driver<T>; | ||
export {}; | ||
} | ||
export declare class Driver<CONN_TYPE extends Driver.IConnTypeEnum = Driver.IConnTypeEnum> { | ||
static getDriver: typeof getDriver; | ||
static create(input: FxDbDriverNS.ConnectionInputArgs | string): SQLiteDriver; | ||
uid: string; | ||
static create(input: FxDbDriverNS.ConnectionInputArgs | string): import("./driver-mysql").default; | ||
/** | ||
* @descritpin there's a bug in fibjs <= 0.35.x, only string type `field` would be | ||
* used by `url.format` | ||
*/ | ||
static formatUrl(input: FxDbDriverNS.ConnectionInputArgs): string; | ||
readonly uid: string; | ||
get uri(): string; | ||
config: FxDbDriverNS.DBConnectionConfig; | ||
extend_config: Fibjs.AnyObject & FxDbDriverNS.DriverBuiltInExtConfig; | ||
readonly config: FxDbDriverNS.DBConnectionConfig; | ||
readonly extend_config: Fibjs.AnyObject & FxDbDriverNS.DriverBuiltInExtConfig; | ||
type: FxDbDriverNS.DriverType; | ||
@@ -25,2 +39,7 @@ connection: CONN_TYPE; | ||
/** | ||
* @description switch to another database, pointless for some databases such as sqlite | ||
* @param targetDb | ||
*/ | ||
switchDb(targetDb: string): void; | ||
/** | ||
* @description re open db connection | ||
@@ -49,5 +68,4 @@ */ | ||
} | ||
export declare class SQLDriver<CONN_TYPE> extends Driver<CONN_TYPE> implements FxDbDriverNS.SQLDriver { | ||
export declare class SQLDriver<CONN_TYPE extends Driver.IConnTypeEnum> extends Driver<CONN_TYPE> implements FxDbDriverNS.SQLDriver { | ||
currentDb: FxDbDriverNS.SQLDriver['currentDb']; | ||
switchDb(targetDb: string): void; | ||
/** | ||
@@ -71,50 +89,7 @@ * @override | ||
} | ||
export declare class MySQLDriver extends SQLDriver<Class_MySQL> implements FxDbDriverNS.SQLDriver { | ||
constructor(conn: FxDbDriverNS.ConnectionInputArgs | string); | ||
switchDb(targetDb: string): void; | ||
open(): Class_MySQL; | ||
close(): void; | ||
ping(): void; | ||
begin(): void; | ||
commit(): void; | ||
trans<T = any>(cb: FxOrmCoreCallbackNS.ExecutionCallback<T>): boolean; | ||
rollback(): void; | ||
getConnection(): Class_MySQL; | ||
execute<T = any>(sql: string): T; | ||
} | ||
export declare class SQLiteDriver extends SQLDriver<Class_SQLite> implements FxDbDriverNS.SQLDriver { | ||
constructor(conn: FxDbDriverNS.ConnectionInputArgs | string); | ||
open(): Class_SQLite; | ||
close(): void; | ||
ping(): void; | ||
begin(): void; | ||
commit(): void; | ||
trans<T = any>(cb: FxOrmCoreCallbackNS.ExecutionCallback<T>): boolean; | ||
rollback(): void; | ||
getConnection(): Class_SQLite; | ||
execute<T = any>(sql: string): T; | ||
} | ||
export declare class RedisDriver extends Driver<Class_Redis> implements FxDbDriverNS.CommandDriver { | ||
constructor(conn: FxDbDriverNS.ConnectionInputArgs | string); | ||
open(): Class_Redis; | ||
close(): void; | ||
ping(): void; | ||
command<T = any>(cmd: string, ...args: any[]): T; | ||
commands<T = any>(cmds: Fibjs.AnyObject, opts?: FxDbDriverNS.CommandDriverCommandOptions): T; | ||
getConnection(): Class_Redis; | ||
} | ||
export declare class MongoDriver extends Driver<Class_MongoDB> implements FxDbDriverNS.CommandDriver { | ||
constructor(conn: FxDbDriverNS.ConnectionInputArgs); | ||
reopen(): Class_MongoDB; | ||
open(): Class_MongoDB; | ||
close(): void; | ||
ping(): void; | ||
command<T = any>(cmd: string, arg: any): T; | ||
commands<T = any>(cmds: Fibjs.AnyObject, opts?: FxDbDriverNS.CommandDriverCommandOptions): T; | ||
getConnection(): Class_MongoDB; | ||
} | ||
export declare type IClsSQLDriver = typeof SQLDriver; | ||
export declare type IClsMySQLDriver = typeof MySQLDriver; | ||
export declare type IClsSQLiteDriver = typeof SQLiteDriver; | ||
export declare type IClsRedisDriver = typeof RedisDriver; | ||
export declare type IClsMySQLDriver = typeof import('./driver-mysql').default; | ||
export declare type IClsPostgreSQLDriver = typeof import('./driver-postgresql').default; | ||
export declare type IClsSQLiteDriver = typeof import('./driver-sqlite').default; | ||
export declare type IClsRedisDriver = typeof import('./driver-redis').default; | ||
export {}; |
export { Driver } from './built-ins/base.class'; | ||
export type { Driver as IDbDriver, IClsSQLDriver, IClsMySQLDriver, IClsSQLiteDriver, IClsRedisDriver, } from './built-ins/base.class'; | ||
export type { Driver as IDbDriver, IClsSQLDriver, IClsMySQLDriver, IClsPostgreSQLDriver, IClsSQLiteDriver, IClsRedisDriver, } from './built-ins/base.class'; | ||
export type { FxDbDriverNS } from './Typo'; |
@@ -5,3 +5,3 @@ /// <reference types="@fibjs/types" /> | ||
export declare namespace FxDbDriverNS { | ||
type DriverType = 'mysql' | 'sqlite' | 'redis' | 'mongodb' | 'unknown'; | ||
type DriverType = 'mysql' | 'sqlite' | 'psql' | 'redis' | 'unknown'; | ||
interface ConnectionInputArgs { | ||
@@ -94,3 +94,2 @@ protocol?: string; | ||
currentDb: string; | ||
switchDb(targetDb: string): void; | ||
execute: { | ||
@@ -97,0 +96,0 @@ <T = any>(sql: string): T; |
32199
21
843
53
+ Added@fxjs/orm-core@0.2.1(transitive)
- Removed@fxjs/orm-core@0.1.0(transitive)
Updated@fxjs/orm-core@^0.2.0