RecordLite: a library (and executable) that declaratively maintains SQLite tables and views of semi-structured data

RecordLite

RecordLite is a library (and executable) that declaratively maintains SQLite tables and views of semi-structured data (henceforth known as records). RecordLite is based on a hidden gem in Backtrace's sqlite_protobuf library.

TLDR: RecordLite stores semi-structured records into SQLite table where one column is the raw payload (JSON or Protobuf) and define views with virtual columns from the raw column via extraction functions, e.g. json_extract and protobuf_extract. Tell RecordLite what you want the view and its indexes to look like, and RecordLite spits out idempotent DDL statements to make that happen, for any initial state and with minimal churn.

Installation

To install the recordlite executable via go get.

# go install github.com/fsaintjacques/recordlite/cmd/recordlite@latest

To install recordlite as a go module dependency

# go get github.com/fsaintjacques/recordlite/cmd/recordlite@latest

About

Why

Schema management is a hard practical problem. By using semi-structured data and storing it as a single BLOB column, one does not need to modify the "true" table's schema, only the dynamic views.

How

For a given table of records, RecordLite generates a companion view that exposes virtual columns of fields of interested defined by the user. The columns can be optionally indexed if they're often projected and/or used for filtering. The column are defined with a function extracting the data from the raw column (either JSON or Protobuf), note that this would also work for any type of expression.

Since the view does not own the data, it is safe to delete/add/update columns without affecting the underlying table. In other words, it is relatively cheap to modify the view since it will not trigger a massive scan + write loop. OTOH, updating any index will require recomputing the index.

Examples

$ # Define a schema
$ cat schema.json
{
  "name": "records",
  "columns": [
    {"name": "status", "expr": "json_extract(raw, '$.status')", "with_index": true},
    {"name": "color", "expr": "json_extract(raw, '$.attrs.color')", "with_index": true}
  ]
}

$ # Create the table and views definitions
$ recordlite schema.json | sqlite3 records.db

$ # Simulate a process appending to the records
$ cat << EOF | awk '{print "INSERT INTO records(raw) VALUES('"'"'" $0 "'"'"');"}' | sqlite3 records.db
> {"status":"ok", "attrs": {"color": "red", "size": "big"}}
> {"status":"failed", "attrs": {"color":"blue", "size": "small"}}
> EOF

$ sqlite3 --box records.db
sqlite> SELECT id, status, color from records;
┌────┬────────┬───────┐
│ id │ status │ color │
├────┼────────┼───────┤
│ 1  │ ok     │ red   │
│ 2  │ failed │ blue  │
└────┴────────┴───────┘

$ # Let's modify the schema to index attrs.size
$ cat schema.json
{
  "name": "records",
  "columns": [
    {"name": "status", "expr": "json_extract(raw, '$.status')", "with_index": true},
    {"name": "color", "expr": "json_extract(raw, '$.attrs.color')", "with_index": true},
    {"name": "size", "expr": "json_extract(raw, '$.attrs.size')", "with_index": true}
  ]
}

$ # Update the views definition
$ recordlite schema.json | sqlite3 records.db

$ sqlite3 --box records.db
sqlite> SELECT id, status, color, size FROM records;
┌────┬────────┬───────┬───────┐
│ id │ status │ color │ size  │
├────┼────────┼───────┼───────┤
│ 1  │ ok     │ red   │ big   │
│ 2  │ failed │ blue  │ small │
└────┴────────┴───────┴───────┘

Owner
François Saint-Jacques
Jack of all trades, master of none. Unhealthy addiction to write prototypes only within the shell.
François Saint-Jacques
Similar Resources

SQLite extension for accessing other SQL databases

dblite SQLite extension for accessing other SQL databases, in SQLite. Similar to how Postgres Foreign Data Wrappers enable access to other databases i

Dec 23, 2022

Golang database driver for SQLite

go-sqlite Golang database driver for SQLite. Does not use cgo. This driver is based on pure-Go SQLite implementation (https://gitlab.com/cznic/sqlite)

Dec 30, 2022

Tracking down a Memory Leak in Go/SQLite

Tracking down a Memory Leak in Go/SQLite run make test - WARNING: long running - several minutes on my workstation OSs supported: Windows_NT = memory

Feb 28, 2022

Dbench - An unscientific benchmark of SQLite vs the file system (btrfs)

DBENCH Basic benchmarks for SQLite vs file system (btrfs on a 2020 Dell XPS SSD)

May 20, 2022

Sqlair - SQLite Query Layer With Golang

sqlair SQLite Query Layer Creates an abstract over the go sql package to provide

Feb 18, 2022

Simple key-value store on top of SQLite or MySQL

KV Work in progress, not ready for prime time. A simple key/value store on top of SQLite or MySQL (Go port of GitHub's KV). Aims to be 100% compatible

Dec 3, 2022

Library for scanning data from a database into Go structs and more

scany Overview Go favors simplicity, and it's pretty common to work with a database via driver directly without any ORM. It provides great control and

Jan 9, 2023

Go library that stores data in Redis with SQL-like schema

Go library that stores data in Redis with SQL-like schema. The goal of this library is we can store data in Redis with table form.

Mar 14, 2022

sqlx is a library which provides a set of extensions on go's standard database/sql library

sqlx is a library which provides a set of extensions on go's standard database/sql library. The sqlx versions of sql.DB, sql.TX, sql.Stmt, et al. all leave the underlying interfaces untouched, so that their interfaces are a superset on the standard ones. This makes it relatively painless to integrate existing codebases using database/sql with sqlx.

Jan 7, 2023
Comments
  • README: highlight the declarative approach

    README: highlight the declarative approach

    It's not immediately obvious that the main value of this approach is declarative schema management: you tell RecordLite what you want the view to look like, and you get DDL to achieve that goal state.

Mar 7, 2022
CloudQuery extracts, transforms, and loads your cloud assets into normalized PostgreSQL tables.
CloudQuery extracts, transforms, and loads your cloud assets into normalized PostgreSQL tables.

The open-source cloud asset inventory backed by SQL. CloudQuery extracts, transforms, and loads your cloud assets into normalized PostgreSQL tables. C

Dec 31, 2022
Convert data exports from various services to a single SQLite database
Convert data exports from various services to a single SQLite database

Bionic Bionic is a tool to convert data exports from web apps to a single SQLite database. Bionic currently supports data exports from Google, Apple H

Dec 9, 2022
BQB is a lightweight and easy to use query builder that works with sqlite, mysql, mariadb, postgres, and others.

Basic Query Builder Why Simple, lightweight, and fast Supports any and all syntax by the nature of how it works Doesn't require learning special synta

Dec 7, 2022
Single binary CLI for generating structured JSON, CSV, Excel, etc.

fakegen: Single binary CLI for generating a random schema of M columns to populate N rows of JSON, CSV, Excel, etc. This program generates a random sc

Dec 26, 2022
Pure Go SQLite file reader

Package SQLittle provides pure Go, read-only, access to SQLite (version 3) database files. What SQLittle reads SQLite3 tables and indexes. It iterates

Oct 12, 2022
Low-level Go interface to SQLite 3

zombiezen.com/go/sqlite This package provides a low-level Go interface to SQLite 3. It is a fork of crawshaw.io/sqlite that uses modernc.org/sqlite, a

Dec 21, 2022
Streaming replication for SQLite.

Litestream Litestream is a standalone streaming replication tool for SQLite. It runs as a background process and safely replicates changes incremental

Jan 9, 2023
Go sqlite3 http vfs: query sqlite databases over http with range headers

sqlite3vfshttp: a Go sqlite VFS for querying databases over http(s) sqlite3vfshttp is a sqlite3 VFS for querying remote databases over http(s). This a

Dec 27, 2022
Experimental implementation of a SQLite backend for go-mysql-server

go-mysql-sqlite-server This is an experimental implementation of a SQLite backend for go-mysql-server from DoltHub. The go-mysql-server is a "frontend

Dec 23, 2022