add ability to delete feed that belongs to a user
[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
37
38 writer.prepare(`
39                create table feed_items (
40                  id text unique,
41                  feed_id text,
42                  guid text unique,
43                  title text,
44                  link text,
45                  pub_date number,
46                  content text,
47                  read_at number default 0
48                )
49                `).run();
50
51 writer.prepare(`create unique index idx_account_feed on feedlist (account_id, link)`).run();
52
53 }
54
55 /*
56 const feeds = [
57   {
58     id: uuidv4(),
59     title: 'Scripting',
60     link: 'http://scripting.com/rss.xml',
61   },
62   {
63     id: uuidv4(),
64     title: 'Hacker News Front Page',
65     link: 'https://hnrss.org/frontpage'
66   },
67   {
68     id: uuidv4(),
69     title: 'Xangelo.ca',
70     link: 'https://xangelo.ca/posts/index.xml'
71   }
72 ];
73
74 writer.prepare('insert into feedlist (id, title, link) values (?,?,?), (?, ?, ?), (?,?,?)').run(
75   feeds[0].id, feeds[0].title, feeds[0].link,
76   feeds[1].id, feeds[1].title, feeds[1].link,
77   feeds[2].id, feeds[2].title, feeds[2].link
78 );
79
80 const insert = writer.prepare(`insert into feed_items (id, feed_id, guid, title, link, pub_date, content) values (?, ?, ?, ?, ?, ?, ?)`);
81 const rss = new RSSParser();
82 feeds.forEach(async feed => {
83   const data = await rss.parse(feed.link);
84   data.items.forEach(item => {
85     const id = uuidv4();
86     insert.run(id, feed.id, item.guid, item.title, item.link, item.pubDate, item.content);
87   });
88 });
89 */
90 const args = cli(process.argv);
91 if(args.dir === 'up') {
92   up();
93 }
94 else if(args.dir === 'down') {
95   down();
96 }
97 else {
98   console.error('Invalid dir');
99   process.exit(1);
100 }
101
102 process.exit(0);