46af5a41b17510b5972f395377b689767b5ae495
[rss-reader.git] / sql / seed.ts
1 import Database from 'better-sqlite3';
2 import {RSSParser} from 'src/parsers/rss';
3 import { v4 as uuidv4 } from 'uuid';
4
5 const DB = 'feeds.db';
6 export const writer = new Database(DB, {
7   verbose: console.log
8 });
9
10
11 writer.prepare('drop table if exists feed_items').run();
12 writer.prepare('drop table feedlist').run();
13
14 writer.prepare(`
15                create table feedlist (
16                  id text unique,
17                  title text,
18                  link text unique
19                )
20                `).run();
21
22
23
24 writer.prepare(`
25                create table feed_items (
26                  id text unique,
27                  feed_id text,
28                  guid text unique,
29                  title text,
30                  link text,
31                  pub_date number,
32                  content text,
33                  read_at number default 0
34                )
35                `).run();
36
37
38 const feeds = [
39   {
40     id: uuidv4(),
41     title: 'Scripting',
42     link: 'http://scripting.com/rss.xml',
43   },
44   {
45     id: uuidv4(),
46     title: 'Hacker News Front Page',
47     link: 'https://hnrss.org/frontpage'
48   },
49   {
50     id: uuidv4(),
51     title: 'Xangelo.ca',
52     link: 'https://xangelo.ca/posts/index.xml'
53   }
54 ];
55
56 writer.prepare('insert into feedlist (id, title, link) values (?,?,?), (?, ?, ?), (?,?,?)').run(
57   feeds[0].id, feeds[0].title, feeds[0].link,
58   feeds[1].id, feeds[1].title, feeds[1].link,
59   feeds[2].id, feeds[2].title, feeds[2].link
60 );
61
62 const insert = writer.prepare(`insert into feed_items (id, feed_id, guid, title, link, pub_date, content) values (?, ?, ?, ?, ?, ?, ?)`);
63 const rss = new RSSParser();
64 feeds.forEach(async feed => {
65   const data = await rss.parse(feed.link);
66   data.items.forEach(item => {
67     const id = uuidv4();
68     insert.run(id, feed.id, item.guid, item.title, item.link, item.pubDate, item.content);
69   });
70 });