From 0b761fce488f0497ff457486c030c02067f6a4eb Mon Sep 17 00:00:00 2001 From: Gerzon Z Date: Wed, 9 Feb 2022 22:49:49 -0400 Subject: [PATCH 1/2] migrate database services and utils to ts --- app/lib/database/services/{Message.js => Message.ts} | 5 +++-- .../database/services/{Subscription.js => Subscription.ts} | 5 +++-- app/lib/database/services/{Thread.js => Thread.ts} | 5 +++-- .../services/{ThreadMessage.js => ThreadMessage.ts} | 5 +++-- app/lib/database/utils.js | 7 ------- app/lib/database/utils.ts | 7 +++++++ 6 files changed, 19 insertions(+), 15 deletions(-) rename app/lib/database/services/{Message.js => Message.ts} (61%) rename app/lib/database/services/{Subscription.js => Subscription.ts} (59%) rename app/lib/database/services/{Thread.js => Thread.ts} (61%) rename app/lib/database/services/{ThreadMessage.js => ThreadMessage.ts} (61%) delete mode 100644 app/lib/database/utils.js create mode 100644 app/lib/database/utils.ts diff --git a/app/lib/database/services/Message.js b/app/lib/database/services/Message.ts similarity index 61% rename from app/lib/database/services/Message.js rename to app/lib/database/services/Message.ts index 562bf589d9b..60295b37112 100644 --- a/app/lib/database/services/Message.js +++ b/app/lib/database/services/Message.ts @@ -1,9 +1,10 @@ import database from '..'; +import { TAppDatabase } from '../interfaces'; import { MESSAGES_TABLE } from '../model/Message'; -const getCollection = db => db.get(MESSAGES_TABLE); +const getCollection = (db: TAppDatabase) => db.get(MESSAGES_TABLE); -export const getMessageById = async messageId => { +export const getMessageById = async (messageId: string) => { const db = database.active; const messageCollection = getCollection(db); try { diff --git a/app/lib/database/services/Subscription.js b/app/lib/database/services/Subscription.ts similarity index 59% rename from app/lib/database/services/Subscription.js rename to app/lib/database/services/Subscription.ts index 19be6d58fef..4f68f4320f2 100644 --- a/app/lib/database/services/Subscription.js +++ b/app/lib/database/services/Subscription.ts @@ -1,9 +1,10 @@ import database from '..'; +import { TAppDatabase } from '../interfaces'; import { SUBSCRIPTIONS_TABLE } from '../model/Subscription'; -const getCollection = db => db.get(SUBSCRIPTIONS_TABLE); +const getCollection = (db: TAppDatabase) => db.get(SUBSCRIPTIONS_TABLE); -export const getSubscriptionByRoomId = async rid => { +export const getSubscriptionByRoomId = async (rid: string) => { const db = database.active; const subCollection = getCollection(db); try { diff --git a/app/lib/database/services/Thread.js b/app/lib/database/services/Thread.ts similarity index 61% rename from app/lib/database/services/Thread.js rename to app/lib/database/services/Thread.ts index 9d90dc00d8e..8dc61b8a4d3 100644 --- a/app/lib/database/services/Thread.js +++ b/app/lib/database/services/Thread.ts @@ -1,9 +1,10 @@ import database from '..'; +import { TAppDatabase } from '../interfaces'; import { THREADS_TABLE } from '../model/Thread'; -const getCollection = db => db.get(THREADS_TABLE); +const getCollection = (db: TAppDatabase) => db.get(THREADS_TABLE); -export const getThreadById = async tmid => { +export const getThreadById = async (tmid: string) => { const db = database.active; const threadCollection = getCollection(db); try { diff --git a/app/lib/database/services/ThreadMessage.js b/app/lib/database/services/ThreadMessage.ts similarity index 61% rename from app/lib/database/services/ThreadMessage.js rename to app/lib/database/services/ThreadMessage.ts index 7ac937b2a0f..899693f8416 100644 --- a/app/lib/database/services/ThreadMessage.js +++ b/app/lib/database/services/ThreadMessage.ts @@ -1,9 +1,10 @@ import database from '..'; +import { TAppDatabase } from '../interfaces'; import { THREAD_MESSAGES_TABLE } from '../model/ThreadMessage'; -const getCollection = db => db.get(THREAD_MESSAGES_TABLE); +const getCollection = (db: TAppDatabase) => db.get(THREAD_MESSAGES_TABLE); -export const getThreadMessageById = async messageId => { +export const getThreadMessageById = async (messageId: string) => { const db = database.active; const threadMessageCollection = getCollection(db); try { diff --git a/app/lib/database/utils.js b/app/lib/database/utils.js deleted file mode 100644 index 472af733b68..00000000000 --- a/app/lib/database/utils.js +++ /dev/null @@ -1,7 +0,0 @@ -import XRegExp from 'xregexp'; - -// Matches letters from any alphabet and numbers -const likeStringRegex = new XRegExp('[^\\p{L}\\p{Nd}]', 'g'); -export const sanitizeLikeString = str => str?.replace(likeStringRegex, '_'); - -export const sanitizer = r => r; diff --git a/app/lib/database/utils.ts b/app/lib/database/utils.ts new file mode 100644 index 00000000000..6c211c9606c --- /dev/null +++ b/app/lib/database/utils.ts @@ -0,0 +1,7 @@ +import XRegExp from 'xregexp'; + +// Matches letters from any alphabet and numbers +const likeStringRegex = XRegExp('[^\\p{L}\\p{Nd}]', 'g'); +export const sanitizeLikeString = (str: string) => str?.replace(likeStringRegex, '_'); + +export const sanitizer = (r: object) => r; From f0537c8b7628304b770fec596ec1061dd0bc5b3a Mon Sep 17 00:00:00 2001 From: Diego Mello Date: Thu, 10 Feb 2022 15:08:44 -0300 Subject: [PATCH 2/2] Migrate tests --- app/lib/database/{utils.test.js => utils.test.ts} | 4 +--- app/lib/database/utils.ts | 4 ++-- 2 files changed, 3 insertions(+), 5 deletions(-) rename app/lib/database/{utils.test.js => utils.test.ts} (89%) diff --git a/app/lib/database/utils.test.js b/app/lib/database/utils.test.ts similarity index 89% rename from app/lib/database/utils.test.js rename to app/lib/database/utils.test.ts index 8c1d054e17f..54ecbab6a83 100644 --- a/app/lib/database/utils.test.js +++ b/app/lib/database/utils.test.ts @@ -1,14 +1,12 @@ -/* eslint-disable no-undef */ import * as utils from './utils'; describe('sanitizeLikeStringTester', () => { // example chars that shouldn't return const disallowedChars = ',./;[]!@#$%^&*()_-=+~'; - const sanitizeLikeStringTester = str => + const sanitizeLikeStringTester = (str: string) => expect(utils.sanitizeLikeString(`${str}${disallowedChars}`)).toBe(`${str}${'_'.repeat(disallowedChars.length)}`); test('render empty', () => { - expect(utils.sanitizeLikeString(null)).toBe(undefined); expect(utils.sanitizeLikeString('')).toBe(''); expect(utils.sanitizeLikeString(undefined)).toBe(undefined); }); diff --git a/app/lib/database/utils.ts b/app/lib/database/utils.ts index 6c211c9606c..a5b9d8e8d77 100644 --- a/app/lib/database/utils.ts +++ b/app/lib/database/utils.ts @@ -2,6 +2,6 @@ import XRegExp from 'xregexp'; // Matches letters from any alphabet and numbers const likeStringRegex = XRegExp('[^\\p{L}\\p{Nd}]', 'g'); -export const sanitizeLikeString = (str: string) => str?.replace(likeStringRegex, '_'); +export const sanitizeLikeString = (str?: string): string | undefined => str?.replace(likeStringRegex, '_'); -export const sanitizer = (r: object) => r; +export const sanitizer = (r: object): object => r;