Skip to content

Commit

Permalink
books to csv
Browse files Browse the repository at this point in the history
  • Loading branch information
bramses committed Oct 6, 2023
1 parent 91f0924 commit 282563e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
node_modules
data*.json
amazon_links.json
records*.json
records*.json
books-notion-table.csv
46 changes: 46 additions & 0 deletions books-to-csv.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// read supabase data and convert to csv from books table
// convert to csv from books table w format: { title, book_id, author, cover_image_url }
// NOTE TO SELF: this is a one-time script, so it's okay to use fs.appendFileSync here
// open in Sheets and then share export as unicode csv and delete all rows in Notion with ctrl+a, delete after scrolling to the bottom


import { createClient } from "@supabase/supabase-js";
import dotenv from "dotenv";
import fs from "fs";

dotenv.config();

const supabaseUrl = process.env.SUPABASE_URL;
const supabaseKey = process.env.SUPABASE_KEY;
const supabase = createClient(supabaseUrl, supabaseKey, {
auth: {
persistSession: false,
},
});

const convertSupabaseBooksToCSV = async () => {
const { data: books, error } = await supabase.from("books").select("*").limit(1000);

if (error) throw error;

const csv = books.map((book) => {
return {
title: book.title.replace(/,/g, ""),
book_id: book.book_id,
author: book.author.replace(/,/g, ""),
cover_image_url: book.cover_image_url,
wander_command: `/wander book_ids:${book.book_id}`,
};
});

// add csv header and write to file
const header = "title,book_id,author,cover_image_url,wander_command\n";
fs.writeFileSync("books-notion-table.csv", header);
csv.forEach((row) => {
fs.appendFileSync("books-notion-table.csv", `${row.title},${row.book_id},${row.author},${row.cover_image_url},${row.wander_command}\n`);
});


};

convertSupabaseBooksToCSV();

0 comments on commit 282563e

Please sign in to comment.