Menu

Exchange Rates API Data in a Database: Schema, Indexes and As-Of Queries
📰

Exchange Rates API Data in a Database: Schema, Indexes and As-Of Queries

DEV Community: postgres·Exchange Rate API·about 14 hours ago
#yiT1dLkt
#devtopostgresql#devto#dev#rate#rates
Reading 0:00
15s threshold

Sooner or later, every app that uses an exchange rates API needs its own copy of the rates. Invoices must be reproducible next year. Reports must use the rate of the transaction date, not today's. And you do not want a page view to depend on a network call. This guide gives a schema that holds up: one table, the right keys, an as-of lookup, idempotent loading and a retention plan. The SQL is for PostgreSQL, with notes for MySQL and SQLite at the end.

The table

CREATE TABLE fx_rate ( base CHAR ( 3 ) NOT NULL , -- ISO 4217, e.g. 'USD' quote CHAR ( 3 ) NOT NULL , -- 1 base = rate quote rate NUMERIC ( 20 , 10 ) NOT NULL CHECK ( rate > 0 ) , rate_type TEXT NOT NULL CHECK ( rate_type IN ( 'mid' , 'official' ) ) , source TEXT NOT NULL , -- 'allratestoday', 'ecb', 'rbi', ...…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.