intra

intradb

Intra-DB is the database library used across the Intra project to interface with the various flat-file databases

flat-files

Every Intra database is stored as text files, each with their own specific format. The database for hydration logs for example looks like this:

2010011900;4;500;tea
2010011915;4;473;water

And my sleep logs are like this:

200101 0000-0600 3
200102 2300-0530 3

database.js

The database library is currently implemented in Deno

init

The flat file must first be parsed to use the database. Again, since each Intra log has their own unique format, custom parsers are needed for each. The parser must return an array of entry objects with timestamp properties.

Here’s an example of my mood log parser:

splitToLines(moodLog).map((entry) => {
const [ time, mood, note = "" ] = entry.split(" ")
return { note, timestamp: parseDate(time), mood: +mood }
})

props

const db = new Database([])

db.entries // array of entries
db.count // number of entries

methods

db.averages(key, sorted)

Returns daily averages of a given key. Entries are grouped by date and averages are calculated for each group

db.byDate(date)

Returns entries under a specified date

db.byPeriod(start, end)

Returns entries within a specified date range

db.groupBy(key)

Group entries by a specified key

db.list(key)

Returns an array of specified key values

const db = new Database([
{ id: 1, colour: "cyan" },
{ id: 2, colour: "salmon" },
{ id: 3, colour: "grey" },
])

db.list("id") // [ 1, 2, 3 ]
db.list("colour") // [ "cyan", "salmon", "grey" ]
db.listUniqueValues(key)

Returns an array of unique values of a specified key

const db = new Database([
{ id: 1, colour: "cyan" },
{ id: 2, colour: "salmon" },
{ id: 3, colour: "salmon" },
])

db.listUniqueValues("colour") // [ "cyan", "salmon" ]
db.peakDay(key)
db.peakDays(key)
db.peakMonth(key)

Returns the month in which the highest cumulative value of specified key is observed. Useful for cases for when you want to find the happiest month in a year based on mood index for example

db.peakMonths(key)
db.random()

Returns a random entry from entries

db.recent(days)

Returns entries within the last specified number of days

db.sort()

Sorts entries into groups by date

db.sortByDay()

Sorts entries by day

db.sortByMonth()

Sorts entries by month

db.sums()

Returns daily sums of a given key. Entries are grouped by date and sums are calculated for each group

db.spawn(entries)

Creates a new database instance given the entries