Skip to content

Commit

Permalink
Added ts-definitions. (#583)
Browse files Browse the repository at this point in the history
* Added ts-definitions.

* Fixed.
  • Loading branch information
SamTV12345 authored Feb 9, 2024
1 parent 59464d2 commit e750673
Show file tree
Hide file tree
Showing 24 changed files with 118 additions and 118 deletions.
8 changes: 4 additions & 4 deletions databases/cassandra_db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ export type BulkObject = {
value?: string
};

export const Database = class Cassandra_db extends AbstractDatabase {
private client: Client | undefined;
private pool: any;
export default class Cassandra_db extends AbstractDatabase {
public client: Client | undefined;
public pool: any;
/**
* @param {Object} settings The required settings object to initiate the Cassandra database
* @param {String[]} settings.clientOptions See
Expand All @@ -42,7 +42,7 @@ export const Database = class Cassandra_db extends AbstractDatabase {
* information
*/
constructor(settings:Settings) {
super();
super(settings);
if (!settings.clientOptions) {
throw new Error('The Cassandra client options should be defined');
}
Expand Down
8 changes: 4 additions & 4 deletions databases/couch_db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ type CouchDBSettings = {
agent: Agent
}
};
export const Database = class Couch_db extends AbstractDatabase {
private agent: Agent | null;
private db: nano.DocumentScope<string> | null;
export default class Couch_db extends AbstractDatabase {
public agent: Agent | null;
public db: nano.DocumentScope<string> | null;
constructor(settings: Settings) {
super();
super(settings);
this.agent = null;
this.db = null;
this.settings = settings;
Expand Down
6 changes: 3 additions & 3 deletions databases/dirty_db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ import Dirty from 'dirty-ts';
type DirtyDBCallback = (p?:any, keys?: string[])=>{};


export const Database = class extends AbstractDatabase {
private db: any;
export default class extends AbstractDatabase {
public db: any;
constructor(settings:Settings) {
super();
super(settings);
this.db = null;

if (!settings || !settings.filename) {
Expand Down
6 changes: 3 additions & 3 deletions databases/dirty_git_db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import AbstractDatabase, {Settings} from '../lib/AbstractDatabase';

import Dirty from 'dirty-ts';

export const Database = class extends AbstractDatabase {
private db: any;
export default class extends AbstractDatabase {
public db: any;
constructor(settings: Settings) {
super();
super(settings);
// @ts-ignore
this.db = null;

Expand Down
12 changes: 6 additions & 6 deletions databases/elasticsearch_db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,13 @@ const migrateToSchema2 = async (client: Client, v1BaseIndex: string | undefined,
}
};

export const Database = class extends AbstractDatabase {
private _client: any;
private readonly _index: any;
private _indexClean: boolean;
private readonly _q: {index: any};
export default class extends AbstractDatabase {
public _client: any;
public readonly _index: any;
public _indexClean: boolean;
public readonly _q: {index: any};
constructor(settings:Settings) {
super();
super(settings);
this._client = null;
this.settings = {
host: '127.0.0.1',
Expand Down
6 changes: 3 additions & 3 deletions databases/memory_db.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import AbstractDatabase, {Settings} from '../lib/AbstractDatabase';


export const Database = class MemoryDB extends AbstractDatabase {
private _data: any;
export default class MemoryDB extends AbstractDatabase {
public _data: any;
constructor(settings:Settings) {
super();
super(settings);
this.settings = settings;
settings.json = false;
settings.cache = 0;
Expand Down
4 changes: 2 additions & 2 deletions databases/mock_db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import {Settings} from '../lib/AbstractDatabase';

import events from 'events';

export const Database = class extends events.EventEmitter {
private settings: Settings;
export default class extends events.EventEmitter {
public settings: Settings;
public mock: any;
constructor(settings:Settings) {
super();
Expand Down
12 changes: 6 additions & 6 deletions databases/mongodb_db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ import AbstractDatabase, {Settings} from '../lib/AbstractDatabase';
import {BulkObject} from './cassandra_db';
import {Collection, Db, MongoClient} from 'mongodb';

export const Database = class extends AbstractDatabase {
private interval: NodeJS.Timer | undefined;
private database: Db|undefined;
private client: MongoClient|undefined;
private collection: Collection|undefined;
export default class extends AbstractDatabase {
public interval: NodeJS.Timer | undefined;
public database: Db|undefined;
public client: MongoClient|undefined;
public collection: Collection|undefined;
constructor(settings:Settings) {
super();
super(settings);
this.settings = settings;

if (!this.settings.url) throw new Error('You must specify a mongodb url');
Expand Down
6 changes: 3 additions & 3 deletions databases/mssql_db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ type RowResult = {
};


export const Database = class MSSQL extends AbstractDatabase {
private db: ConnectionPool | undefined;
export default class MSSQL extends AbstractDatabase {
public db: ConnectionPool | undefined;
constructor(settings:Settings) {
super();
super(settings);
settings = settings || {};

if (settings.json != null) {
Expand Down
8 changes: 4 additions & 4 deletions databases/mysql_db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ import util from 'util';
import {BulkObject} from './cassandra_db';
import {ConnectionConfig, createPool, Pool, QueryError} from "mysql2";

export const Database = class extends AbstractDatabase {
private readonly _mysqlSettings: Settings;
private _pool: Pool|null;
export default class extends AbstractDatabase {
public readonly _mysqlSettings: Settings;
public _pool: Pool|null;
constructor(settings:Settings) {
super();
super(settings);
this.logger = console;
this._mysqlSettings = {
charset: 'utf8mb4', // temp hack needs a proper fix..
Expand Down
8 changes: 4 additions & 4 deletions databases/postgres_db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ import async from 'async';
import * as pg from 'pg';
import {BulkObject} from './cassandra_db';

export const Database = class extends AbstractDatabase {
private db: pg.Pool;
private upsertStatement: string | null | undefined;
export default class extends AbstractDatabase {
public db: pg.Pool;
public upsertStatement: string | null | undefined;
constructor(settings:Settings | string) {
super();
super(settings as Settings);
if (typeof settings === 'string') settings = {connectionString: settings};
this.settings = settings;

Expand Down
4 changes: 2 additions & 2 deletions databases/postgrespool_db.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {Settings} from '../lib/AbstractDatabase';

import {Database as PGDatabase} from './postgres_db'
import Postgres_db from './postgres_db'

export const Database = class PostgresDB extends PGDatabase {
export default class PostgresDB extends Postgres_db {
constructor(settings:Settings) {
console.warn('ueberdb: The postgrespool database driver is deprecated ' +
'and will be removed in a future version. Use postgres instead.');
Expand Down
6 changes: 3 additions & 3 deletions databases/redis_db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ import AbstractDatabase, {Settings} from '../lib/AbstractDatabase';
import {createClient, RedisClientOptions} from 'redis';
import {BulkObject} from './cassandra_db';

export const Database = class RedisDB extends AbstractDatabase {
private _client: any
export default class RedisDB extends AbstractDatabase {
public _client: any
constructor(settings:Settings) {
super();
super(settings);
this._client = null;
this.settings = settings || {};
}
Expand Down
14 changes: 7 additions & 7 deletions databases/rethink_db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ import r from 'rethinkdb';
import async from 'async';
import {BulkObject} from './cassandra_db';

export const Database = class extends AbstractDatabase {
private host: string;
private db: string;
private port: number | string;
private table: string;
private connection: r.Connection | null;
export default class Rethink_db extends AbstractDatabase {
public host: string;
public db: string;
public port: number | string;
public table: string;
public connection: r.Connection | null;
constructor(settings:Settings) {
super();
super(settings);
if (!settings) settings = {};
if (!settings.host) { settings.host = 'localhost'; }
if (!settings.port) { settings.port = 28015; }
Expand Down
6 changes: 3 additions & 3 deletions databases/sqlite_db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ type RequestVal = {
value: string;
}

export const Database = class SQLiteDB extends AbstractDatabase {
private db: any|null;
export default class SQLiteDB extends AbstractDatabase {
public db: any|null;
constructor(settings:Settings) {
super();
super(settings);
this.db = null;

if (!settings || !settings.filename) {
Expand Down
7 changes: 3 additions & 4 deletions databases/surrealdb_db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,11 @@ const unescapeKey = (key:string, originalKey:string)=>{
return key
}

export const Database = class SurrealDB extends AbstractDatabase {
private _client: Surreal | null;
export default class SurrealDB extends AbstractDatabase {
public _client: Surreal | null;
constructor(settings:Settings) {
super();
super(settings);
this._client = null;
this.settings = settings || {};
}

get isAsync() { return true; }
Expand Down
78 changes: 41 additions & 37 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,28 @@ import {Database as DatabaseCache} from './lib/CacheAndBufferLayer';
import {normalizeLogger} from './lib/logging';
import {callbackify} from 'util';
import {Settings} from './lib/AbstractDatabase';
import {Database as CassandraDatabase} from './databases/cassandra_db'
import {Database as CouchDatabase} from './databases/couch_db'
import {Database as DirtyDatabase} from './databases/dirty_db'
import {Database as DirtyGitDatabase} from './databases/dirty_git_db'
import {Database as ElasticSearchDatabase} from './databases/elasticsearch_db'
import {Database as MemoryDatabase} from './databases/memory_db'
import {Database as MockDatabase} from './databases/mock_db'
import {Database as MongoDBDatabase} from './databases/mongodb_db'
import {Database as MSSQLDatabase} from './databases/mssql_db'
import {Database as MYSQLDatabase} from './databases/mysql_db'
import {Database as PostgresDatabase} from './databases/postgres_db'
import {Database as PostgresPoolDatabase} from './databases/postgrespool_db'
import {Database as RedisDatabase} from './databases/redis_db'
import {Database as RethinkDatabase} from './databases/rethink_db'
import {Database as SQLiteDatabase} from './databases/sqlite_db'
import {Database as Surrealb} from './databases/surrealdb_db'
import Cassandra_db from './databases/cassandra_db'
import Couch_db from './databases/couch_db'
import Dirty_db from './databases/dirty_db'
import Dirty_git_db from './databases/dirty_git_db'
import Elasticsearch_db from './databases/elasticsearch_db'
import MemoryDB from './databases/memory_db'
import Mock_db from './databases/mock_db'
import Mongodb_db from './databases/mongodb_db'
import MSSQL from './databases/mssql_db'
import Mysql_db from './databases/mysql_db'
import Postgres_db from './databases/postgres_db'
import Postgrespool_db from './databases/postgrespool_db'
import RedisDB from './databases/redis_db'
import Rethink_db from './databases/rethink_db'
import SQLiteDB from './databases/sqlite_db'
import SurrealDB from './databases/surrealdb_db'

type CBDBType = {
[key: string]:Function
}

const cbDb = {
const cbDb: CBDBType= {
init: () => {},
flush: () => {},
get: () => {},
Expand All @@ -51,18 +54,19 @@ const cbDb = {
};
const fns = ['close', 'findKeys', 'flush', 'get', 'getSub', 'init', 'remove', 'set', 'setSub'];
for (const fn of fns) {
// @ts-ignore
cbDb[fn] = callbackify(DatabaseCache.prototype[fn]);
if (fn in cbDb){
// @ts-ignore
cbDb[fn] = callbackify(DatabaseCache.prototype[fn]);
}
}
const makeDoneCallback = (callback: (err?:any)=>{}, deprecated:(err:any)=>{}) => (err: null) => {
if (callback) callback(err);
if (deprecated) deprecated(err);
if (err != null && callback == null && deprecated == null) throw err;
};

export const Database = class {
export class Database {
public readonly type: any;
public dbModule: any;
public readonly dbSettings: any;
public readonly wrapperSettings: any | {};
public readonly logger: Function | null;
Expand Down Expand Up @@ -109,37 +113,37 @@ export const Database = class {
initDB(){
switch (this.type){
case 'mysql':
return new MYSQLDatabase(this.dbSettings);
return new Mysql_db(this.dbSettings);
case 'postgres':
return new PostgresDatabase(this.dbSettings);
return new Postgres_db(this.dbSettings);
case 'sqlite':
return new SQLiteDatabase(this.dbSettings);
return new SQLiteDB(this.dbSettings);
case 'mongodb':
return new MongoDBDatabase(this.dbSettings);
return new Mongodb_db(this.dbSettings);
case 'redis':
return new RedisDatabase(this.dbSettings);
return new RedisDB(this.dbSettings);
case 'cassandra':
return new CassandraDatabase(this.dbSettings);
return new Cassandra_db(this.dbSettings);
case 'dirty':
return new DirtyDatabase(this.dbSettings);
return new Dirty_db(this.dbSettings);
case 'dirtygit':
return new DirtyGitDatabase(this.dbSettings);
return new Dirty_git_db(this.dbSettings);
case 'elasticsearch':
return new ElasticSearchDatabase(this.dbSettings);
return new Elasticsearch_db(this.dbSettings);
case 'memory':
return new MemoryDatabase(this.dbSettings);
return new MemoryDB(this.dbSettings);
case 'mock':
return new MockDatabase(this.dbSettings);
return new Mock_db(this.dbSettings);
case 'mssql':
return new MSSQLDatabase(this.dbSettings);
return new MSSQL(this.dbSettings);
case 'postgrespool':
return new PostgresPoolDatabase(this.dbSettings);
return new Postgrespool_db(this.dbSettings);
case 'rethink':
return new RethinkDatabase(this.dbSettings);
return new Rethink_db(this.dbSettings);
case 'couch':
return new CouchDatabase(this.dbSettings);
return new Couch_db(this.dbSettings);
case 'surrealdb':
return new Surrealb(this.dbSettings);
return new SurrealDB(this.dbSettings);
default:
throw new Error('Invalid database type');
}
Expand Down
8 changes: 4 additions & 4 deletions lib/AbstractDatabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,9 @@ export type Settings = {


class AbstractDatabase {
logger: any;
// @ts-ignore
settings: Settings;
constructor() {
public logger: any;
public settings: Settings;
constructor(settings: Settings) {
if (new.target === module.exports) {
throw new TypeError('cannot instantiate Abstract Database directly');
}
Expand All @@ -58,6 +57,7 @@ class AbstractDatabase {
if (typeof this[fn] !== 'function') throw new TypeError(`method ${fn} not defined`);
}
this.logger = nullLogger;
this.settings = settings
}

/**
Expand Down
Loading

0 comments on commit e750673

Please sign in to comment.