initial database integration for twitter

This commit is contained in:
erik-sn committed 2020-10-11 19:26:21 -04:00
1 parent 660e171789
commit c19d0ee3aa
7 files changed
+182 -8

No files matched your search

+22
View File
@@ -0,0 +1,22 @@
import mysql from 'mysql2/promise';
import { ConnectionStringParser } from "connection-string-parser";
const CONNECTION_STRING = 'mysql_connection_string';
// we require set mysql_connection_string "mysql://root:password@localhost/es_extended?charset=utf8mb4" to be set in the config
const mysqlConnectionString = GetConvar(CONNECTION_STRING, null);
if (mysqlConnectionString === null) {
throw new Error(`No connection string provided. make sure ${CONNECTION_STRING} is set in your config.`)
}
const connectionStringParser = new ConnectionStringParser({ scheme: 'mysql', hosts: []});
const connectionOjbect = connectionStringParser .parse(mysqlConnectionString);
export const pool = mysql.createPool({
host: connectionOjbect.hosts[0].host,
user: connectionOjbect.username,
password: connectionOjbect.password,
database: connectionOjbect.endpoint,
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0
});
+7 -2
View File
@@ -1,3 +1,8 @@
let ESX = null;
import { ESXServer } from "fivem-esx-js/server/esx_server";
emit('esx:getSharedObject', (obj) => ESX = obj);
import './db';
import './sv_twitter';
let ESX: ESXServer = null;
emit('esx:getSharedObject', (obj: ESXServer) => ESX = obj);
+46
View File
@@ -0,0 +1,46 @@
import { pool } from './db';
async function fetchAllTweets(profileId: number) {
try {
const query = `
SELECT
npwd_twitter_profiles.profile_name,
npwd_twitter_profiles.avatar_url,
npwd_twitter_tweets.*,
npwd_twitter_likes.id IS NOT NULL AS isLiked,
TIME_TO_SEC(TIMEDIFF( NOW(), npwd_twitter_tweets.createdAt)) AS seconds_since_tweet
FROM npwd_twitter_tweets
LEFT OUTER JOIN npwd_twitter_profiles ON npwd_twitter_tweets.identifier = npwd_twitter_profiles.identifier
LEFT OUTER JOIN npwd_twitter_likes ON npwd_twitter_tweets.id = npwd_twitter_likes.tweet_id AND npwd_twitter_likes.profile_id = ?
WHERE visible = 1
ORDER BY npwd_twitter_tweets.createdAt DESC
LIMIT 100
`;
const [results ] = await pool.query(query, [profileId]);
} catch (e) {
console.warn(e)
}
}
async function fetchTweetsFiltered(profileId: number, searchValue: string) {
try {
const parameterizedSearchValue = `%${searchValue}%`;
const query = `
SELECT
npwd_twitter_profiles.profile_name,
npwd_twitter_profiles.avatar_url,
npwd_twitter_tweets.*,
npwd_twitter_likes.id IS NOT NULL AS isLiked,
TIME_TO_SEC(TIMEDIFF( NOW(), npwd_twitter_tweets.createdAt)) AS seconds_since_tweet
FROM npwd_twitter_tweets
LEFT OUTER JOIN npwd_twitter_profiles ON npwd_twitter_tweets.identifier = npwd_twitter_profiles.identifier
LEFT OUTER JOIN npwd_twitter_likes ON npwd_twitter_tweets.id = npwd_twitter_likes.tweet_id AND npwd_twitter_likes.profile_id = ?
WHERE visible = 1 AND (npwd_twitter_profiles.profile_name LIKE @profile_name OR npwd_twitter_tweets.message LIKE ?)
ORDER BY npwd_twitter_tweets.createdAt DESC
LIMIT 100
`;
const [results ] = await pool.query(query, [profileId, parameterizedSearchValue]);
} catch (e) {
console.warn(e)
}
}