MCPcopy Create free account
hub / github.com/Rafacv23/F1-api / insertDrivers

Function insertDrivers

lib/insert/drivers.js:3–47  ·  view source on GitHub ↗
(drivers)

Source from the content-addressed store, hash-verified

1import { clientWriter } from "../turso.js"
2
3export const insertDrivers = async (drivers) => {
4 try {
5 console.log("Inserting Drivers:", drivers)
6
7 for (const driver of drivers) {
8 // Check if driver already exists
9 const result = await clientWriter.execute({
10 sql: `SELECT COUNT(*) as count FROM Drivers WHERE Driver_ID = :Driver_ID`,
11 args: {
12 Driver_ID: driver.driverId,
13 },
14 })
15
16 // Access count from result rows
17 const count = result.rows[0]?.count || 0
18
19 if (count > 0) {
20 console.log(
21 `Driver with ID ${driver.driverId} already exists. Skipping insertion.`
22 )
23 continue
24 }
25
26 // Insert new driver if it doesn't exist
27 await clientWriter.execute({
28 sql: `
29 INSERT INTO Drivers (Driver_ID, Name, Surname, Birthday, Nationality, Number, Short_Name, URL)
30 VALUES (:Driver_ID, :Name, :Surname, :Birthday, :Nationality, :Number, :Short_Name, :URL)`,
31 args: {
32 Driver_ID: driver.driverId,
33 Name: driver.givenName,
34 Surname: driver.familyName,
35 Birthday: driver.dateOfBirth,
36 Nationality: driver.nationality,
37 Number: driver.permanentNumber || null, // Use null if the value is not present
38 Short_Name: driver.code || null, // Use null if the value is not present
39 URL: driver.url || null, // Use null if the value is not present
40 },
41 })
42 }
43 } catch (error) {
44 console.error("Error inserting or updating drivers:", error)
45 throw error
46 }
47}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected