-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch.ts
executable file
·37 lines (31 loc) · 1.24 KB
/
fetch.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import * as dotenv from 'dotenv';
import * as path from 'path';
import { LinksRecord, XataClient } from './xata'; // Ensure correct import paths
dotenv.config({ path: path.resolve(__dirname, '.env') });
const xata = new XataClient({ apiKey: process.env.XATA_API_KEY, branch: 'main' });
const main = async () => {
const searchPhrase: string | undefined = process.argv[2] || undefined;
try {
let records;
if (!searchPhrase) {
records = await xata.db.links.sort('xata.createdAt', 'desc').getAll();
} else {
const results = await xata.db.links.search(searchPhrase, {
fuzziness: 1
});
records = results.records;
}
const formattedResults = records
.map((record: LinksRecord) => {
const trimmedUrl = record.url.substring(0, 90); // URL
const trimmedTitle = record.title ? record.title.substring(0, 45) : 'Untitled'; // Title, default to "Untitled" if empty
const trimmedTags = record.tags ? record.tags.substring(0, 50) : ''; // Tags
return `${record.id}\t${trimmedTitle}\t${trimmedUrl}\t${trimmedTags}`; // ID, Title, URL, Tags
})
.join('\n');
console.log(formattedResults);
} catch (error) {
console.error('An error occurred:', error);
}
};
main();