Skip to content

Commit

Permalink
fix(packages/db): make use of @npwd/database (#1088)
Browse files Browse the repository at this point in the history
- inc password cases where user:password@host and the password has a trailing @
  • Loading branch information
lukealford authored Oct 23, 2023
1 parent 6baee63 commit c2551f0
Show file tree
Hide file tree
Showing 24 changed files with 33 additions and 244 deletions.
4 changes: 1 addition & 3 deletions apps/game/server/boot/boot.db.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { CONNECTION_STRING } from '../db';
import DbInterface from '../db/db_wrapper';
import { parseUri } from '../db/parseUri';
import { CONNECTION_STRING, DbInterface, parseUri } from '@npwd/database';
import { config } from '../config';

const mysqlConnectionString = GetConvar(CONNECTION_STRING, 'none');
Expand Down
4 changes: 1 addition & 3 deletions apps/game/server/commands/registerCommands.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { mainLogger } from '../sv_logger';
import { config } from '../config';
import DbInterface from '../db/db_wrapper';
import { CONNECTION_STRING } from '../db';
import { parseUri } from '../db/parseUri';
import { CONNECTION_STRING, DbInterface, parseUri } from '@npwd/database';

const mysqlConnectionString = GetConvar(CONNECTION_STRING, 'none');

Expand Down
19 changes: 0 additions & 19 deletions apps/game/server/db/db_utils.test.ts

This file was deleted.

29 changes: 0 additions & 29 deletions apps/game/server/db/db_utils.ts

This file was deleted.

77 changes: 0 additions & 77 deletions apps/game/server/db/db_wrapper.ts

This file was deleted.

1 change: 0 additions & 1 deletion apps/game/server/db/index.ts

This file was deleted.

37 changes: 0 additions & 37 deletions apps/game/server/db/parseUri.ts

This file was deleted.

56 changes: 0 additions & 56 deletions apps/game/server/db/pool.ts

This file was deleted.

2 changes: 1 addition & 1 deletion apps/game/server/misc/functions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { config } from '../server';
import DbInterface from '../db/db_wrapper';
import { DbInterface } from '@npwd/database';
import { generateUniquePhoneNumber } from './generateUniquePhoneNumber';
import { playerLogger } from '../players/player.utils';
import { ResultSetHeader } from 'mysql2';
Expand Down
2 changes: 1 addition & 1 deletion apps/game/server/misc/generateUniquePhoneNumber.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { config } from '../server';
import DbInterface from '../db/db_wrapper';
import { DbInterface } from '@npwd/database';
import { playerLogger } from '../players/player.utils';

const exp = global.exports;
Expand Down
2 changes: 1 addition & 1 deletion packages/database/src/calls/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CallHistoryItem } from '@typings/call';
import { FetchDefaultLimits } from '@game/server/utils/ServerConstants';
import DbInterface from '../db/db_wrapper';
import { DbInterface } from '../db';

export class _CallsRepo {
async saveCall(call: CallHistoryItem): Promise<void> {
Expand Down
2 changes: 1 addition & 1 deletion packages/database/src/contacts/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Contact, PreDBContact } from '@typings/contact';
import { ResultSetHeader } from 'mysql2';
import DbInterface from '../db/db_wrapper';
import { DbInterface } from '../db';

export class _ContactsDB {
async fetchAllContacts(identifier: string): Promise<Contact[]> {
Expand Down
2 changes: 1 addition & 1 deletion packages/database/src/darkchat/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import DbInterface from '../db/db_wrapper';
import { DbInterface } from '../db';
import {
ChannelItemProps,
ChannelMember,
Expand Down
4 changes: 1 addition & 3 deletions packages/database/src/db/db_wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,4 @@ class _DbInterface {
return <T>castRes;
}
}
const DbInterface = new _DbInterface();

export default DbInterface;
export const DbInterface = new _DbInterface();
4 changes: 4 additions & 0 deletions packages/database/src/db/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './db_utils';
export * from './db_wrapper';
export * from './parseUri';
export * from './pool'
13 changes: 13 additions & 0 deletions packages/database/src/db/parseUri.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ export const parseUri = (connectionUri: string) => {
(options as Record<typeof key, any>)[key] = value;
}
});

if (!options.password || !options.user || !options.database) {
const regex = new RegExp('^(?:([^:/?#.]+):)?(?://(?:([^/?]*):([^/?]*)@)?([[A-Za-z0-9_.]+]*)(?::([0-9]+))?)?(?:\\\\?([^#]*))?$', '')
const specialCharactersRegex = regex.exec(connectionUri);
if (specialCharactersRegex) {
options.user = specialCharactersRegex[2] || void 0;
options.password = specialCharactersRegex[3] || void 0;
options.host = specialCharactersRegex[4];
options.port = parseInt(specialCharactersRegex[5]);
options.database = specialCharactersRegex[6].replace(/^\/+/, "");
}
}

return options;
}

Expand Down
3 changes: 1 addition & 2 deletions packages/database/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ export * from './match/index';
export * from './marketplace/index';
export * from './darkchat/index';
export * from './calls/index';

export { pool } from './db/pool';
export * from './db/index'
2 changes: 1 addition & 1 deletion packages/database/src/marketplace/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
ReportListingDTO,
} from '@typings/marketplace';
import { ResultSetHeader } from 'mysql2';
import DbInterface from '../db/db_wrapper';
import { DbInterface } from '../db';

export class _MarketplaceDB {
async addListing(
Expand Down
3 changes: 1 addition & 2 deletions packages/database/src/match/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { Like, Match, NewProfile, Profile } from '@typings/match';
import { pool } from '../db/pool';
import { DbInterface, pool } from '../db';
import { ResultSetHeader } from 'mysql2';
import { config } from '@npwd/config/server';
import { generateProfileName } from '@game/server/utils/generateProfileName';
import { matchLogger } from '@game/server/match/match.utils';
import DbInterface from '../db/db_wrapper';

const DEFAULT_IMAGE = 'https://upload.wikimedia.org/wikipedia/commons/a/ac/No_image_available.svg';
const MATCHES_PER_PAGE = 20;
Expand Down
2 changes: 1 addition & 1 deletion packages/database/src/messages/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import DbInterface from '../db/db_wrapper';
import { DbInterface } from '../db';
import { CreateMessageDTO, Message, MessageConversation, MessagesRequest } from '@typings/messages';
import { ResultSetHeader } from 'mysql2';
import { messagesLogger } from '../../../../apps/game/server/messages/messages.utils';
Expand Down
2 changes: 1 addition & 1 deletion packages/database/src/notes/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BeforeDBNote, NoteItem } from '@typings/notes';
import { ResultSetHeader } from 'mysql2';
import DbInterface from '../db/db_wrapper';
import { DbInterface } from '../db';

export class _NotesDB {
async addNote(identifier: string, note: BeforeDBNote): Promise<number> {
Expand Down
2 changes: 1 addition & 1 deletion packages/database/src/photo/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GalleryPhoto } from '@typings/photo';
import DbInterface from '../db/db_wrapper';
import { DbInterface } from '../db';

export class _PhotoDB {
async uploadPhoto(identifier: string, image: string): Promise<GalleryPhoto> {
Expand Down
2 changes: 1 addition & 1 deletion packages/database/src/players/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { config } from '@npwd/config/server';
import DbInterface from '../db/db_wrapper';
import { DbInterface } from '../db';

export class _PlayerRepo {
async fetchIdentifierFromPhoneNumber(phoneNumber: string): Promise<string | null> {
Expand Down
3 changes: 1 addition & 2 deletions packages/database/src/twitter/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { NewTweet, TwitterProfile as Profile, Tweet } from '@typings/twitter';
import { pool } from '../db/pool';
import { DbInterface, pool } from '../db';
import { ResultSetHeader } from 'mysql2';
import { config } from '@npwd/config/server';
import { generateProfileName } from '../../../../apps/game/server/utils/generateProfileName';
import { twitterLogger } from '../../../../apps/game/server/twitter/twitter.utils';
import DbInterface from '../db/db_wrapper';

const SELECT_FIELDS = `
npwd_twitter_tweets.id,
Expand Down

0 comments on commit c2551f0

Please sign in to comment.