Skip to content

Commit

Permalink
fixed not updating node to database
Browse files Browse the repository at this point in the history
  • Loading branch information
appujet committed Oct 5, 2024
1 parent a7cc6c0 commit 8300f52
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 73 deletions.
4 changes: 0 additions & 4 deletions .github/workflows/main .yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,10 @@ jobs:
- name: Install Dependencies
run: yarn install

- name: Generate Prisma Client
run: npx prisma generate

- name: Run TypeScript Script
env:
TURSO_DATABASE_URL: ${{ secrets.TURSO_DATABASE_URL }}
TURSO_AUTH_TOKEN: ${{ secrets.TURSO_AUTH_TOKEN }}
DATABASE_URL: ${{ secrets.DATABASE_URL }}
run: yarn start

- name: Commit Changes
Expand Down
8 changes: 8 additions & 0 deletions .hintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": [
"development"
],
"hints": {
"typescript-config/consistent-casing": "off"
}
}
13 changes: 5 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,20 @@
"name": "lavalink-list",
"version": "1.0.0",
"description": "A list of free and available public Lavalink nodes with their live status. Feel free to make a pull request!",
"main": "update_nodes.js",
"main": "update_nodes.ts",
"scripts": {
"start": "npx ts-node update_nodes.ts"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@libsql/client": "^0.8.0",
"@prisma/adapter-libsql": "^5.18.0",
"@prisma/client": "^5.18.0",
"dotenv": "^16.4.5"
"@libsql/client": "^0.8.1",
"dotenv": "^16.4.5",
"ts-node": "^10.9.2"
},
"devDependencies": {
"@types/node": "^22.5.0",
"prisma": "^5.18.0",
"ts-node": "^10.9.2",
"typescript": "^5.5.4"
}
}
}
44 changes: 0 additions & 44 deletions prisma/schema.prisma

This file was deleted.

42 changes: 25 additions & 17 deletions update_nodes.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
const fs = require('fs');
const { PrismaClient } = require('@prisma/client');
const { PrismaLibSQL } = require('@prisma/adapter-libsql');
const { createClient } = require('@libsql/client');
require('dotenv').config();

// Initialize the libsql client
const libsql = createClient({
url: process.env.TURSO_DATABASE_URL || '',
authToken: process.env.TURSO_AUTH_TOKEN,
});

const adapter = new PrismaLibSQL(libsql);
// Define types for node objects
interface Node {
host: string;
Expand Down Expand Up @@ -37,12 +35,12 @@ function removeDuplicates(data: Node[]): Node[] {

// Function to add restVersion if it is missing
function addRestVersion(data: Node[]): Node[] {
for (const node of data) {
return data.map(node => {
if (!node.restVersion) {
node.restVersion = 'v4';
}
}
return data;
return node;
});
}

// Load data from nodes.json
Expand All @@ -67,18 +65,28 @@ if (uniqueData.length < data.length) {
// Update database with unique nodes
async function updateNodes(nodes: Node[]): Promise<void> {
console.log("Updating nodes in database...");
const prisma = new PrismaClient({ adapter });
await prisma.$connect();
// delete all nodes
await prisma.nodes.deleteMany();
// create new nodes

await prisma.nodes.createMany({
data: {
nodes: JSON.stringify(nodes)
}
});
await prisma.$disconnect();
try {
// Delete all nodes
await libsql.execute("DELETE FROM Node");

// Create an array of placeholders and values for batch insert
const placeholders = nodes.map(() => '(?, ?, ?, ?, ?, ?, ?)').join(',');
const values = nodes.flatMap(node => [
node.host,
node.identifier,
node.password,
node.port,
node.restVersion,
node.secure,
node.authorId,
]);

// Insert new nodes in one query
await libsql.execute(`INSERT INTO Node (host, identifier, password, port, restVersion, secure, authorId) VALUES ${placeholders}`, values);
} catch (error) {
console.error("Error updating nodes:", error);
}
}

// Call updateNodes with uniqueData
Expand Down

0 comments on commit 8300f52

Please sign in to comment.