Skip to content

Commit

Permalink
Feature/typescript - Adds typescript for better insights. (#364)
Browse files Browse the repository at this point in the history
* Migrated source file.

* Added tsconfig file.

* Added tests as typescript.

* Updated tests.

* Fixed Commonjs import of AbstractDatabase.

* Create ts files first.

* Fixed most of the errors.

* Fixed build.

* Removed debugger from normal test.

* Fixed package.json

* Fixed elasticsearch.

* Fixed mongodb tests.

* Fixed tests.

* Fixed sqlite tests.

* Added working npm publish.
  • Loading branch information
SamTV12345 authored Jul 1, 2023
1 parent 44755b9 commit 44f67cd
Show file tree
Hide file tree
Showing 51 changed files with 11,290 additions and 5,007 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist
**/*.ts
49 changes: 44 additions & 5 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,61 @@
require('eslint-config-etherpad/patch/modern-module-resolution');

module.exports = {
parserOptions: {
project: ['./tsconfig.json'],
},

root: true,
extends: 'etherpad/node',

rules: {
'mocha/no-exports': 'off',
'@typescript-eslint/no-unsafe-call': 'off',
'max-len': 'off',
'@typescript-eslint/restrict-template-expressions': 'off',
'@typescript-eslint/no-unsafe-member-access': 'off',
'n/no-missing-import': 'off',
'strict': 'off',
'@typescript-eslint/no-unsafe-return': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-unsafe-argument': 'off',
'@typescript-eslint/no-unsafe-assignment': 'off',
'prefer-arrow/prefer-arrow-functions': 'off',
'@typescript-eslint/await-thenable': 'off',
'@typescript-eslint/brace-style': 'off',
'@typescript-eslint/comma-spacing': 'off',
'@typescript-eslint/consistent-type-assertions': 'off',
'@typescript-eslint/consistent-type-definitions': 'off',
'@typescript-eslint/default-param-last': 'off',
'@typescript-eslint/dot-notation': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-member-accessibility': 'off',
'func-call-spacing': 'off',
'@typescript-eslint/no-floating-promises': 'off',
'camelcase': 'off',
'n/no-unpublished-import': 'off',
'n/no-unpublished-require': 'off',
'no-unused-vars': 'off',
'@typescript-eslint/no-unused-vars': 'off',
'@typescript-eslint/ban-ts-comment': 'off',
'@typescript-eslint/restrict-plus-operands': 'off',
},
overrides: [
{
files: [
'test/**/*',
'lib/**/*',
'databases/**/*',
'tests/**/*',
],
extends: 'etherpad/tests/backend',
overrides: [
{
files: [
'test/lib/**/*',
'lib/**/*',
'databases/**/*',
'tests/**/*',
],
rules: {
'mocha/no-exports': 'off',
},

},
],
},
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ node_modules
leveldb-store
npm-debug.log
.idea
dist
5 changes: 5 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
./databases
./test
tsconfig.json
tslint.json
eslintrc.js
75 changes: 45 additions & 30 deletions databases/cassandra_db.js → databases/cassandra_db.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
'use strict';
/**
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -13,10 +12,24 @@
* limitations under the License.
*/

const AbstractDatabase = require('../lib/AbstractDatabase');
const cassandra = require('cassandra-driver');
import AbstractDatabase, {Settings} from '../lib/AbstractDatabase';
import cassandra, {ArrayOrObject, Client, types, ValueCallback} from 'cassandra-driver';
import ResultSet = types.ResultSet;

exports.Database = class extends AbstractDatabase {

type Result = {
rows: any[];
};

export type BulkObject = {
type: string
key:string
value?: string
};

export const Database = class Cassandra_db extends AbstractDatabase {
private client: Client | undefined;
private pool: any;
/**
* @param {Object} settings The required settings object to initiate the Cassandra database
* @param {String[]} settings.clientOptions See
Expand All @@ -28,16 +41,15 @@ exports.Database = class extends AbstractDatabase {
* the Cassandra driver. See https://github.com/datastax/nodejs-driver#logging for more
* information
*/
constructor(settings) {
constructor(settings:Settings) {
super();
if (!settings.clientOptions) {
throw new Error('The Cassandra client options should be defined');
}
if (!settings.columnFamily) {
throw new Error('The Cassandra column family should be defined');
}

this.settings = {};
this.settings = {database: settings.database};
this.settings.clientOptions = settings.clientOptions;
this.settings.columnFamily = settings.columnFamily;
this.settings.logger = settings.logger;
Expand All @@ -50,7 +62,7 @@ exports.Database = class extends AbstractDatabase {
* @param {Function} callback Standard callback method.
* @param {Error} callback.err An error object (if any.)
*/
init(callback) {
init(callback: (arg: any)=>{}) {
// Create a client
this.client = new cassandra.Client(this.settings.clientOptions);

Expand Down Expand Up @@ -83,7 +95,7 @@ exports.Database = class extends AbstractDatabase {
const cql =
`CREATE COLUMNFAMILY "${this.settings.columnFamily}" ` +
'(key text PRIMARY KEY, data text)';
this.client.execute(cql, callback);
this.client && this.client.execute(cql, callback);
}
});
}
Expand All @@ -96,9 +108,9 @@ exports.Database = class extends AbstractDatabase {
* @param {Error} callback.err An error object, if any
* @param {String} callback.value The value for the given key (if any)
*/
get(key, callback) {
get(key:string, callback: (err:Error | null, data?:any)=>{}) {
const cql = `SELECT data FROM "${this.settings.columnFamily}" WHERE key = ?`;
this.client.execute(cql, [key], (err, result) => {
this.client && this.client.execute(cql, [key], (err, result) => {
if (err) {
return callback(err);
}
Expand All @@ -122,20 +134,20 @@ exports.Database = class extends AbstractDatabase {
* @param {Error} callback.err An error object, if any
* @param {String[]} callback.keys An array of keys that match the specified filters
*/
findKeys(key, notKey, callback) {
findKeys(key:string, notKey:string, callback: Function) {
let cql = null;
if (!notKey) {
// Get all the keys
cql = `SELECT key FROM "${this.settings.columnFamily}"`;
this.client.execute(cql, (err, result) => {
this.client && this.client.execute(cql, (err: Error, result:Result) => {
if (err) {
return callback(err);
}

// Construct a regular expression based on the given key
const regex = new RegExp(`^${key.replace(/\*/g, '.*')}$`);

const keys = [];
const keys:string[] = [];
result.rows.forEach((row) => {
if (regex.test(row.key)) {
keys.push(row.key);
Expand All @@ -151,18 +163,20 @@ exports.Database = class extends AbstractDatabase {
// Get the 'text' bit out of the key and get all those keys from a special column.
// We can retrieve them from this column as we're duplicating them on .set/.remove
cql = `SELECT * from "${this.settings.columnFamily}" WHERE key = ?`;
this.client.execute(cql, [`ueberdb:keys:${matches[1]}`], (err, result) => {
if (err) {
return callback(err);
}
this.client &&
this.client
.execute(cql, [`ueberdb:keys:${matches[1]}`], (err, result) => {
if (err) {
return callback(err);
}

if (!result.rows || result.rows.length === 0) {
return callback(null, []);
}
if (!result.rows || result.rows.length === 0) {
return callback(null, []);
}

const keys = result.rows.map((row) => row.data);
return callback(null, keys);
});
const keys = result.rows.map((row) => row.data);
return callback(null, keys);
});
} else {
const msg =
'Cassandra db only supports key patterns like pad:* when notKey is set to *:*:*';
Expand All @@ -181,7 +195,7 @@ exports.Database = class extends AbstractDatabase {
* @param {Function} callback Standard callback method
* @param {Error} callback.err An error object, if any
*/
set(key, value, callback) {
set(key: string, value:string, callback:()=>{}) {
this.doBulk([{type: 'set', key, value}], callback);
}

Expand All @@ -192,19 +206,20 @@ exports.Database = class extends AbstractDatabase {
* @param {Function} callback Standard callback method
* @param {Error} callback.err An error object, if any
*/
remove(key, callback) {
remove(key:string, callback: ValueCallback<ResultSet>) {
this.doBulk([{type: 'remove', key}], callback);
}


/**
* Performs multiple operations in one action
*
* @param {Object[]} bulk The set of operations that should be performed
* @param {Function} callback Standard callback method
* @param {Error} callback.err An error object, if any
*/
doBulk(bulk, callback) {
const queries = [];
doBulk(bulk:BulkObject[], callback:ValueCallback<ResultSet>) {
const queries:Array<string | {query: string, params?: ArrayOrObject}> = [];
bulk.forEach((operation) => {
// We support finding keys of the form `test:*`. If anything matches, we will try and save
// this
Expand Down Expand Up @@ -235,7 +250,7 @@ exports.Database = class extends AbstractDatabase {
}
}
});
this.client.batch(queries, {prepare: true}, callback);
this.client && this.client.batch(queries, {prepare: true}, callback);
}

/**
Expand All @@ -244,7 +259,7 @@ exports.Database = class extends AbstractDatabase {
* @param {Function} callback Standard callback method
* @param {Error} callback.err Error object in case something goes wrong
*/
close(callback) {
close(callback: ()=>{}) {
this.pool.shutdown(callback);
}
};
Loading

0 comments on commit 44f67cd

Please sign in to comment.