Use NodeJS global for typeorm

This commit is contained in:
Nabarun 2025-02-06 08:33:12 +05:30 committed by Adw8
parent 0e3ca90cef
commit b5c9cb7956
7 changed files with 37 additions and 37 deletions

View File

@ -14,6 +14,6 @@ PINATA_JWT=
PINATA_GATEWAY=
# Change to your website URL
# For development: set to http://localhost:3000
# For development: SITE_URL=http://localhost:3000
SITE_URL=https://memes.markto.market
NEXT_PUBLIC_ACCOUNT_HANDLE=

View File

@ -1,11 +1,15 @@
import { createServer } from 'http';
import { parse } from 'url';
import next from 'next';
import { DataSource, EntityTarget } from 'typeorm';
// Reference: https://github.com/motdotla/dotenv?tab=readme-ov-file#how-do-i-use-dotenv-with-import
import 'dotenv/config'
import { QuotesService } from './quotes-service';
import { initializeDataSource } from './src/data-source';
import { Payment } from './src/entity/Payment';
import { Tweet } from './src/entity/Tweet';
const port = parseInt(process.env.PORT || '3000', 10);
const app = next({ dev: process.env.NODE_ENV !== 'production' });
@ -16,16 +20,25 @@ const quotesService = new QuotesService();
declare global {
namespace NodeJS {
interface Global {
quotesService: typeof quotesService
quotesService: QuotesService
appDataSource: DataSource
entities: { [key: string]: EntityTarget<any>}
}
}
}
// TODO: Look for a better way to use quotesService
// Initialize global quotes service
(global as any).quotesService = quotesService
global.quotesService = quotesService
global.entities = {
Payment,
Tweet
};
app.prepare().then(async() => {
global.appDataSource = await initializeDataSource();
const server = createServer(async (req, res) => {
const parsedUrl = parse(req.url!, true);

View File

@ -3,7 +3,6 @@ import BN from 'bn.js';
import { fal } from "@fal-ai/client"
import { FLUX_MODELS } from '../../../services/fluxService'
import { initializeDataSource } from '../../../data-source'
import { verifyPayment, markSignatureAsUsed } from '../../../utils/verifyPayment';
import { uploadToPinata } from '../../../utils/uploadToPinata';
@ -22,8 +21,6 @@ const IMAGE_HEIGHT: number = 1024
export async function POST(req: NextRequest): Promise<NextResponse> {
try {
await initializeDataSource();
const { prompt, modelId, transactionSignature } = await req.json();
const host = req.headers.get("host"); // Get the hostname from request headers
const protocol = req.headers.get("x-forwarded-proto") || "http"; // Handle reverse proxies

View File

@ -1,13 +1,9 @@
import { NextRequest, NextResponse } from 'next/server';
import { saveTweet, verifySignatureInTweet } from '../../../utils/verifyTweet';
import { initializeDataSource } from '../../../data-source';
export async function POST(req: NextRequest): Promise<NextResponse> {
try {
// TODO: Move initialization to server file
await initializeDataSource();
const { tweetUrl } = await req.json();
const url = `https://publish.twitter.com/oembed?url=${tweetUrl}&maxwidth=600`;

View File

@ -1,24 +1,21 @@
import { DataSource } from 'typeorm';
import { Payment } from './entity/Payment';
export async function initializeDataSource() {
try {
console.log('Initializing Data Source');
export const AppDataSource = new DataSource({
const appDataSource = new DataSource({
type: 'sqlite',
database: './database.sqlite',
synchronize: true,
logging: false,
// entities: [__dirname + "./entity/*{.js,.ts}"],
entities: [Payment],
entities: [global.entities.Payment, global.entities.Tweet],
migrations: [],
subscribers: [],
});
});
export async function initializeDataSource() {
try {
if (!AppDataSource.isInitialized){
await AppDataSource.initialize();
console.log('Data Source has been initialized!');
};
await appDataSource.initialize();
return appDataSource;
} catch (err) {
console.error('Error during Data Source initialization:', err);
throw err;

View File

@ -4,7 +4,6 @@ import BN from 'bn.js';
import { Connection } from '@solana/web3.js';
import { TOKEN_PROGRAM_ID } from '@solana/spl-token';
import { AppDataSource } from '../data-source';
import { Payment } from '../entity/Payment';
assert(process.env.NEXT_PUBLIC_SOLANA_RPC_URL, 'SOLANA_RPC_URL is required');
@ -22,7 +21,7 @@ const connection = new Connection(
);
export async function isSignatureUsed(transactionSignature: string): Promise<boolean> {
const paymentRepository = AppDataSource.getRepository(Payment);
const paymentRepository = global.appDataSource.getRepository(global.entities.Payment);
const payment = await paymentRepository.findOneBy({ transactionSignature });
if (payment) {
return true;
@ -31,8 +30,8 @@ export async function isSignatureUsed(transactionSignature: string): Promise<boo
}
export async function markSignatureAsUsed(transactionSignature: string): Promise<void> {
await AppDataSource.transaction(async (transactionalEntityManager) => {
const paymentRepository = transactionalEntityManager.getRepository(Payment);
await global.appDataSource.transaction(async (transactionalEntityManager) => {
const paymentRepository = transactionalEntityManager.getRepository(global.entities.Payment);
// Check if the payment with the given signature already exists
const exists = await paymentRepository.exists({ where: { transactionSignature } });

View File

@ -1,16 +1,14 @@
import { AppDataSource } from '../data-source';
import { Payment } from '../entity/Payment';
import { Tweet } from '../entity/Tweet';
export async function verifySignatureInTweet(transactionSignature: string): Promise<boolean> {
const paymentRepository = AppDataSource.getRepository(Payment);
const paymentRepository = global.appDataSource.getRepository(global.entities.Payment);
const payment = await paymentRepository.findOneBy({ transactionSignature });
if (!payment) {
return false;
}
const tweetRepository = AppDataSource.getRepository(Tweet);
const tweetRepository = global.appDataSource.getRepository(global.entities.Tweet);
const tweet = await tweetRepository.findOneBy({ transactionSignature });
if (tweet) {
@ -21,8 +19,8 @@ export async function verifySignatureInTweet(transactionSignature: string): Prom
}
export async function saveTweet(data: Partial<Tweet>): Promise<{ isFourthUser: boolean }> {
return await AppDataSource.transaction(async (transactionalEntityManager) => {
const tweetRepository = transactionalEntityManager.getRepository(Tweet);
return await global.appDataSource.transaction(async (transactionalEntityManager) => {
const tweetRepository = transactionalEntityManager.getRepository(global.entities.Tweet);
const tweet = await tweetRepository.save(data);