-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add script to export playback events to CSV
After we've recorded a number of playback events, we may have interest in getting them all into a CSV with the song title, playback timestamp, and who marked the playback
- Loading branch information
Showing
4 changed files
with
532 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import sqlite3Lib from "sqlite3"; | ||
import { createObjectCsvWriter } from "csv-writer"; | ||
|
||
const sqlite3 = sqlite3Lib.verbose(); | ||
|
||
// Open the database | ||
let db = new sqlite3.Database("sqlite.db", sqlite3.OPEN_READONLY, (err) => { | ||
if (err) { | ||
console.error(err.message); | ||
} | ||
console.log("Connected to the SQLite database."); | ||
}); | ||
|
||
const sql = `SELECT p.createdAt, s.title, u.firstName, u.lastName | ||
FROM PlaybackEvent p | ||
JOIN Song s ON p.songId = s.id | ||
JOIN User u ON p.userId = u.id`; | ||
|
||
// Specify path and header for CSV file | ||
const csvWriter = createObjectCsvWriter({ | ||
path: "out.csv", | ||
header: [ | ||
{ id: "createdAt", title: "PLAYBACK TIME" }, | ||
{ id: "title", title: "TITLE" }, | ||
{ id: "firstName", title: "FIRST NAME" }, | ||
{ id: "lastName", title: "LAST NAME" }, | ||
], | ||
}); | ||
|
||
// Fetch data and write to CSV | ||
db.all(sql, [], (err, rows) => { | ||
if (err) { | ||
throw err; | ||
} | ||
csvWriter | ||
.writeRecords(rows) // returns a promise | ||
.then(() => { | ||
console.log("CSV file written successfully"); | ||
}); | ||
}); | ||
|
||
// Close the database connection | ||
db.close((err) => { | ||
if (err) { | ||
console.error(err.message); | ||
} | ||
console.log("Closed the database connection."); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.