Last active
December 8, 2024 04:58
-
-
Save maietta/0af3058892409ffdb5cef9287f720d9b to your computer and use it in GitHub Desktop.
Impersonate Superuser via API token to import CSV
This file contains hidden or 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
| import fs from 'fs'; | |
| import csv from 'csv-parser'; | |
| import PocketBase from 'pocketbase'; | |
| const token = "API_KEY_OF_IMPERSONATED_SUPERUSER"; | |
| // Initialize PocketBase client | |
| const pb = new PocketBase('https://<YOUR-POCKETBASE-URL>'); | |
| pb.authStore.save(token, null); | |
| interface Listing { | |
| class: string; | |
| created: string; | |
| data: string; // JSON string | |
| id: string; | |
| mlsid: string; | |
| modified: string; | |
| photos: string; // JSON string | |
| status: string; | |
| updated: string; | |
| } | |
| // Read CSV and insert into PocketBase | |
| async function importCSV(filePath: string) { | |
| const records: Listing[] = []; | |
| // Read and parse the CSV file | |
| fs.createReadStream(filePath) | |
| .pipe(csv()) | |
| .on('data', (row) => { | |
| records.push({ | |
| class: row.class, | |
| created: row.created, | |
| data: row.data, | |
| id: row.id, | |
| mlsid: row.mlsid, | |
| modified: row.modified, | |
| photos: row.photos, // Ensure photos field is properly formatted | |
| status: row.status, | |
| updated: row.updated, | |
| }); | |
| }) | |
| .on('end', async () => { | |
| console.log('CSV file successfully processed.'); | |
| // Insert records into PocketBase | |
| for (const record of records) { | |
| try { | |
| await pb.collection('listings').create(record); | |
| console.log(`Record added: ${record.id}`); | |
| } catch (err) { | |
| console.error(`Failed to add record ${record.id}:`, err); | |
| } | |
| } | |
| }) | |
| .on('error', (err) => { | |
| console.error('Error reading CSV file:', err); | |
| }); | |
| } | |
| (async () => { | |
| await importCSV('./listings.csv'); | |
| })(); |
This file contains hidden or 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
| { | |
| "dependencies": { | |
| "csv-parser": "^3.0.0", | |
| "pocketbase": "^0.22.1" | |
| }, | |
| "devDependencies": { | |
| "@types/bun": "^1.1.14" | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment