-
Notifications
You must be signed in to change notification settings - Fork 225
/
create.sql
254 lines (220 loc) · 6.03 KB
/
create.sql
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
-- This trigger updates the value in the updated_at column. It is used in the tables below to log
-- when a row was last updated.
CREATE OR REPLACE FUNCTION trigger_set_timestamp()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = NOW();
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
-- USERS
-- This table is used to store the users of our application. The view returns the same data as the
-- table, we're just creating it to follow the pattern used in other tables.
CREATE TABLE users_table
(
id SERIAL PRIMARY KEY,
username text UNIQUE NOT NULL,
created_at timestamptz default now(),
updated_at timestamptz default now()
);
CREATE TRIGGER users_updated_at_timestamp
BEFORE UPDATE ON users_table
FOR EACH ROW
EXECUTE PROCEDURE trigger_set_timestamp();
CREATE VIEW users
AS
SELECT
id,
username,
created_at,
updated_at
FROM
users_table;
-- ITEMS
-- This table is used to store the items associated with each user. The view returns the same data
-- as the table, we're just using both to maintain consistency with our other tables. For more info
-- on the Plaid Item schema, see the docs page: https://plaid.com/docs/#item-schema
CREATE TABLE items_table
(
id SERIAL PRIMARY KEY,
user_id integer REFERENCES users_table(id) ON DELETE CASCADE,
plaid_access_token text UNIQUE NOT NULL,
plaid_item_id text UNIQUE NOT NULL,
plaid_institution_id text NOT NULL,
status text NOT NULL,
created_at timestamptz default now(),
updated_at timestamptz default now(),
transactions_cursor text
);
CREATE TRIGGER items_updated_at_timestamp
BEFORE UPDATE ON items_table
FOR EACH ROW
EXECUTE PROCEDURE trigger_set_timestamp();
CREATE VIEW items
AS
SELECT
id,
plaid_item_id,
user_id,
plaid_access_token,
plaid_institution_id,
status,
created_at,
updated_at,
transactions_cursor
FROM
items_table;
-- -- ASSETS
-- -- This table is used to store the assets associated with each user. The view returns the same data
-- -- as the table, we're just using both to maintain consistency with our other tables.
CREATE TABLE assets_table
(
id SERIAL PRIMARY KEY,
user_id integer REFERENCES users_table(id) ON DELETE CASCADE,
value numeric(28,2),
description text,
created_at timestamptz default now(),
updated_at timestamptz default now()
);
CREATE TRIGGER assets_updated_at_timestamp
BEFORE UPDATE ON assets_table
FOR EACH ROW
EXECUTE PROCEDURE trigger_set_timestamp();
CREATE VIEW assets
AS
SELECT
id,
user_id,
value,
description,
created_at,
updated_at
FROM
assets_table;
-- ACCOUNTS
-- This table is used to store the accounts associated with each item. The view returns all the
-- data from the accounts table and some data from the items view. For more info on the Plaid
-- Accounts schema, see the docs page: https://plaid.com/docs/#account-schema
CREATE TABLE accounts_table
(
id SERIAL PRIMARY KEY,
item_id integer REFERENCES items_table(id) ON DELETE CASCADE,
plaid_account_id text UNIQUE NOT NULL,
name text NOT NULL,
mask text NOT NULL,
official_name text,
current_balance numeric(28,10),
available_balance numeric(28,10),
iso_currency_code text,
unofficial_currency_code text,
type text NOT NULL,
subtype text NOT NULL,
created_at timestamptz default now(),
updated_at timestamptz default now()
);
CREATE TRIGGER accounts_updated_at_timestamp
BEFORE UPDATE ON accounts_table
FOR EACH ROW
EXECUTE PROCEDURE trigger_set_timestamp();
CREATE VIEW accounts
AS
SELECT
a.id,
a.plaid_account_id,
a.item_id,
i.plaid_item_id,
i.user_id,
a.name,
a.mask,
a.official_name,
a.current_balance,
a.available_balance,
a.iso_currency_code,
a.unofficial_currency_code,
a.type,
a.subtype,
a.created_at,
a.updated_at
FROM
accounts_table a
LEFT JOIN items i ON i.id = a.item_id;
-- TRANSACTIONS
-- This table is used to store the transactions associated with each account. The view returns all
-- the data from the transactions table and some data from the accounts view. For more info on the
-- Plaid Transactions schema, see the docs page: https://plaid.com/docs/#transaction-schema
CREATE TABLE transactions_table
(
id SERIAL PRIMARY KEY,
account_id integer REFERENCES accounts_table(id) ON DELETE CASCADE,
plaid_transaction_id text UNIQUE NOT NULL,
plaid_category_id text,
category text,
subcategory text,
type text NOT NULL,
name text NOT NULL,
amount numeric(28,10) NOT NULL,
iso_currency_code text,
unofficial_currency_code text,
date date NOT NULL,
pending boolean NOT NULL,
account_owner text,
created_at timestamptz default now(),
updated_at timestamptz default now()
);
CREATE TRIGGER transactions_updated_at_timestamp
BEFORE UPDATE ON transactions_table
FOR EACH ROW
EXECUTE PROCEDURE trigger_set_timestamp();
CREATE VIEW transactions
AS
SELECT
t.id,
t.plaid_transaction_id,
t.account_id,
a.plaid_account_id,
a.item_id,
a.plaid_item_id,
a.user_id,
t.category,
t.subcategory,
t.type,
t.name,
t.amount,
t.iso_currency_code,
t.unofficial_currency_code,
t.date,
t.pending,
t.account_owner,
t.created_at,
t.updated_at
FROM
transactions_table t
LEFT JOIN accounts a ON t.account_id = a.id;
-- The link_events_table is used to log responses from the Plaid API for client requests to the
-- Plaid Link client. This information is useful for troubleshooting.
CREATE TABLE link_events_table
(
id SERIAL PRIMARY KEY,
type text NOT NULL,
user_id integer,
link_session_id text,
request_id text UNIQUE,
error_type text,
error_code text,
status text,
created_at timestamptz default now()
);
-- The plaid_api_events_table is used to log responses from the Plaid API for server requests to
-- the Plaid client. This information is useful for troubleshooting.
CREATE TABLE plaid_api_events_table
(
id SERIAL PRIMARY KEY,
item_id integer,
user_id integer,
plaid_method text NOT NULL,
arguments text,
request_id text UNIQUE,
error_type text,
error_code text,
created_at timestamptz default now()
);