account based feeds
[rss-reader.git] / sql / seed.ts
1 import Database from 'better-sqlite3';
2 import { cli } from './migrate';
3
4 const DB = 'feeds.db';
5 export const writer = new Database(DB, {
6   verbose: console.log
7 });
8
9 export function down() {
10   writer.prepare('drop table feed_items').run();
11   writer.prepare('drop table feedlist').run();
12   writer.prepare('drop table accounts').run();
13 }
14
15 export function up() {
16   writer.prepare(`
17                  create table accounts (
18                    id text unique,
19                    email text unique,
20                    signup_date number,
21                    login_code text,
22                    login_code_expires number
23                  )
24                  `).run();
25
26
27 writer.prepare(`
28                create table feedlist (
29                  id text unique,
30                  account_id text,
31                  title text,
32                  link text
33                )
34                `).run();
35
36 writer.prepare(`create index idx_account_feed on feedlist (account_id, link)`).run();
37
38
39 writer.prepare(`
40                create table feed_items (
41                  id text unique,
42                  feed_id text,
43                  guid text unique,
44                  title text,
45                  link text,
46                  pub_date number,
47                  content text,
48                  read_at number default 0
49                )
50                `).run();
51
52 }
53
54 /*
55 const feeds = [
56   {
57     id: uuidv4(),
58     title: 'Scripting',
59     link: 'http://scripting.com/rss.xml',
60   },
61   {
62     id: uuidv4(),
63     title: 'Hacker News Front Page',
64     link: 'https://hnrss.org/frontpage'
65   },
66   {
67     id: uuidv4(),
68     title: 'Xangelo.ca',
69     link: 'https://xangelo.ca/posts/index.xml'
70   }
71 ];
72
73 writer.prepare('insert into feedlist (id, title, link) values (?,?,?), (?, ?, ?), (?,?,?)').run(
74   feeds[0].id, feeds[0].title, feeds[0].link,
75   feeds[1].id, feeds[1].title, feeds[1].link,
76   feeds[2].id, feeds[2].title, feeds[2].link
77 );
78
79 const insert = writer.prepare(`insert into feed_items (id, feed_id, guid, title, link, pub_date, content) values (?, ?, ?, ?, ?, ?, ?)`);
80 const rss = new RSSParser();
81 feeds.forEach(async feed => {
82   const data = await rss.parse(feed.link);
83   data.items.forEach(item => {
84     const id = uuidv4();
85     insert.run(id, feed.id, item.guid, item.title, item.link, item.pubDate, item.content);
86   });
87 });
88 */
89 const args = cli(process.argv);
90 if(args.dir === 'up') {
91   up();
92 }
93 else if(args.dir === 'down') {
94   down();
95 }
96 else {
97   console.error('Invalid dir');
98   process.exit(1);
99 }
100
101 process.exit(0);