From 88172f0aa9ed378dc78a4528af1f017647f6beb6 Mon Sep 17 00:00:00 2001 From: Gerzon Z Date: Wed, 16 Feb 2022 18:19:04 -0400 Subject: [PATCH] Chore: Migrate database/services and database/utils to TS (#3708) * migrate database services and utils to ts * Migrate tests Co-authored-by: Diego Mello --- app/lib/database/services/{Message.js => Message.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.test.js => utils.test.ts} | 4 +--- app/lib/database/utils.ts | 7 +++++++ 6 files changed, 17 insertions(+), 16 deletions(-) rename app/lib/database/services/{Message.js => Message.ts} (61%) 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 rename app/lib/database/{utils.test.js => utils.test.ts} (89%) 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 562bf589d..60295b371 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/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 9d90dc00d..8dc61b8a4 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 7ac937b2a..899693f84 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 472af733b..000000000 --- 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.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 8c1d054e1..54ecbab6a 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 new file mode 100644 index 000000000..a5b9d8e8d --- /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): string | undefined => str?.replace(likeStringRegex, '_'); + +export const sanitizer = (r: object): object => r;