Database
in package
A local copy of MTGJSON's AllPrintings SQLite build, kept current from the API. MTGJSON has no query endpoint — it publishes whole files — so card and set searches run here instead of over HTTP.
On first use the gzipped build (AllPrintings.sqlite.gz, ~250 MB) is
streamed and inflated straight to disk (~700 MB), verified, and swapped in.
Afterwards Meta.json is checked every refresh interval and a new build is
installed only when MTGJSON's version has changed. Queries run on a
read-only connection; the swap closes it first so it also works on Windows.
Rows come back decoded: the SQL build's ", "-joined list columns become
arrays, its JSON columns become arrays, BOOLEAN columns become bool, and
empty values are dropped — the same shape as MTGJSON's JSON files.
Tags
Table of Contents
Constants
- DEFAULT_REFRESH_INTERVAL : int = 86400
- Seconds between `Meta.json` checks by default: MTGJSON rebuilds daily.
- FILE : string = 'AllPrintings.sqlite'
- The file name of the installed build.
- INDEXES : array<string|int, string> = ['CREATE INDEX IF NOT EXISTS "idx_cardIdentifie...
- Indexes added to each build after download, for lookups MTGJSON does not index itself.
- JSON_COLUMNS : array<string, array<string|int, string>> = ['cards' => ['leadershipSkills', 'relatedCards'...
- Columns the SQL build stores as JSON text, by table.
- LIST_COLUMNS : array<string, array<string|int, string>> = ['cards' => ['artistIds', 'attractionLights', '...
- Columns the SQL build stores as `", "`-joined lists, by table.
Properties
- $browser : Browser
- $columns : array<string, array<string, string>>
- Declared column types, by table, read lazily with `PRAGMA table_info`.
- $date : string|null
- The date (`meta.date`) of the open build.
- $http : Http
- $logger : LoggerInterface
- $loop : LoopInterface
- $opening : PromiseInterface|null
- The pending first install, shared by every caller of {@see ready()}.
- $path : string
- $pdo : PDO|null
- The open read-only connection, or null while closed.
- $refreshing : PromiseInterface|null
- The pending refresh, shared by every caller of {@see refresh()}.
- $refreshInterval : int
- $timer : TimerInterface|null
- The periodic refresh timer.
- $version : string|null
- The version (`meta.version`) of the open build.
Methods
- __construct() : mixed
- close() : void
- Closes the connection and stops the refresh timer. The next query reopens it.
- decode() : array<string|int, mixed>
- Decodes a row of `$table` into MTGJSON's JSON shape: list and JSON columns become arrays, `BOOLEAN` columns become `bool`, and null or empty scalars are dropped.
- getColumns() : array<string, string>
- The columns of a table in the open build, with their declared types.
- getDate() : string|null
- The date (`meta.date`) of the open build, e.g. `2026-10-03`.
- getPath() : string
- Where the build is kept.
- getVersion() : string|null
- The version (`meta.version`) of the open build, e.g. `5.3.0+20261003`.
- isOpen() : bool
- Whether a build is open.
- query() : PromiseInterface<string|int, array<string|int, array<string|int, mixed>>>
- Runs a read query against the build, opening (or downloading) it first.
- ready() : PromiseInterface<string|int, PDO>
- Opens the build, downloading it first if there is none on disk, and starts the refresh timer.
- refresh() : PromiseInterface<string|int, bool>
- Checks `Meta.json` and installs MTGJSON's current build if it is newer than the open one (or always, when forced).
- select() : array<string|int, array<string|int, mixed>>
- Runs a read query against the open build, synchronously. Callers must have waited for {@see ready()}.
- disconnect() : void
- Drops the connection so the file can be replaced.
- download() : PromiseInterface<string|int, void>
- Streams a gzipped file, inflating it to `$target` chunk by chunk so it is never held in memory.
- inflateTo() : PromiseInterface<string|int, void>
- Inflates a gzip body stream into an open file.
- install() : PromiseInterface<string|int, string>
- Downloads, verifies and swaps in MTGJSON's current build.
- label() : string
- What this build is called in logs and errors.
- open() : void
- Opens the build at {@see $path} read-only and reads its version.
- prepare() : string
- Checks a downloaded build and adds the extra indexes.
- refreshQuietly() : void
- Refreshes, logging rather than throwing on failure; the open build stays in use.
- remoteVersion() : PromiseInterface<string|int, ?string>
- The version of the build currently published, from `Meta.json`.
- schedule() : void
- Starts the refresh timer, and checks right away if the build on disk is already older than one interval.
- source() : string
- Where the gzipped build is downloaded from.
Constants
DEFAULT_REFRESH_INTERVAL
Seconds between `Meta.json` checks by default: MTGJSON rebuilds daily.
public
int
DEFAULT_REFRESH_INTERVAL
= 86400
FILE
The file name of the installed build.
public
string
FILE
= 'AllPrintings.sqlite'
INDEXES
Indexes added to each build after download, for lookups MTGJSON does not index itself.
public
array<string|int, string>
INDEXES
= ['CREATE INDEX IF NOT EXISTS "idx_cardIdentifiers_multiverseId" ON "cardIdentifiers" ("multiverseId")', 'CREATE INDEX IF NOT EXISTS "idx_cardIdentifiers_scryfallId" ON "cardIdentifiers" ("scryfallId")', 'CREATE INDEX IF NOT EXISTS "idx_setBoosterContents_setCode_boosterName" ON "setBoosterContents" ("setCode", "boosterName")', 'CREATE INDEX IF NOT EXISTS "idx_setBoosterSheetCards_setCode_boosterName" ON "setBoosterSheetCards" ("setCode", "boosterName")']
JSON_COLUMNS
Columns the SQL build stores as JSON text, by table.
public
array<string, array<string|int, string>>
JSON_COLUMNS
= ['cards' => ['leadershipSkills', 'relatedCards', 'skuIds', 'sourceProducts'], 'tokens' => ['relatedCards', 'skuIds', 'sourceProducts'], 'cardForeignData' => ['identifiers', 'skuIds'], 'sealedProducts' => ['contents', 'identifiers', 'purchaseUrls'], 'setDecks' => ['commander', 'displayCommander', 'mainBoard', 'planes', 'schemes', 'sealedProductUuids', 'sideBoard', 'sourceSetCodes', 'tokens']]
LIST_COLUMNS
Columns the SQL build stores as `", "`-joined lists, by table.
public
array<string, array<string|int, string>>
LIST_COLUMNS
= ['cards' => ['artistIds', 'attractionLights', 'availability', 'boosterTypes', 'colorIdentity', 'colorIndicator', 'colors', 'finishes', 'frameEffects', 'keywords', 'originalPrintings', 'otherFaceIds', 'printings', 'producedMana', 'promoTypes', 'rebalancedPrintings', 'subtypes', 'supertypes', 'types', 'variations'], 'tokens' => ['artistIds', 'attractionLights', 'availability', 'boosterTypes', 'colorIdentity', 'colorIndicator', 'colors', 'finishes', 'frameEffects', 'keywords', 'otherFaceIds', 'producedMana', 'promoTypes', 'subtypes', 'supertypes', 'types'], 'sets' => ['languages']]
cardParts and subsets are left as strings, because their values
can contain commas.
Properties
$browser
protected
Browser
$browser
$columns
Declared column types, by table, read lazily with `PRAGMA table_info`.
protected
array<string, array<string, string>>
$columns
= []
$date
The date (`meta.date`) of the open build.
protected
string|null
$date
= null
$http
protected
Http
$http
$logger
protected
LoggerInterface
$logger
$loop
protected
LoopInterface
$loop
$opening
The pending first install, shared by every caller of {@see ready()}.
protected
PromiseInterface|null
$opening
= null
$path
protected
string
$path
$pdo
The open read-only connection, or null while closed.
protected
PDO|null
$pdo
= null
$refreshing
The pending refresh, shared by every caller of {@see refresh()}.
protected
PromiseInterface|null
$refreshing
= null
$refreshInterval
protected
int
$refreshInterval
= self::DEFAULT_REFRESH_INTERVAL
$timer
The periodic refresh timer.
protected
TimerInterface|null
$timer
= null
$version
The version (`meta.version`) of the open build.
protected
string|null
$version
= null
Methods
__construct()
public
__construct(LoopInterface $loop, LoggerInterface $logger, Http $http, Browser $browser, string $path[, int $refreshInterval = self::DEFAULT_REFRESH_INTERVAL ]) : mixed
Parameters
- $loop : LoopInterface
-
The event loop the download and refresh timer run on.
- $logger : LoggerInterface
-
Logger for download progress and refresh failures.
- $http : Http
-
The MTGJSON HTTP client, used for
Meta.json. - $browser : Browser
-
A browser for the streamed download (no response buffer applies to streams).
- $path : string
-
Where the build is kept, e.g.
var/mtgjson/AllPrintings.sqlite. - $refreshInterval : int = self::DEFAULT_REFRESH_INTERVAL
-
Seconds between
Meta.jsonchecks;0never refreshes.
close()
Closes the connection and stops the refresh timer. The next query reopens it.
public
close() : void
decode()
Decodes a row of `$table` into MTGJSON's JSON shape: list and JSON columns become arrays, `BOOLEAN` columns become `bool`, and null or empty scalars are dropped.
public
decode(string $table, array<string|int, mixed> $row) : array<string|int, mixed>
Parameters
- $table : string
-
The table the row came from.
- $row : array<string|int, mixed>
-
The raw row.
Return values
array<string|int, mixed> —The decoded row.
getColumns()
The columns of a table in the open build, with their declared types.
public
getColumns(string $table) : array<string, string>
Parameters
- $table : string
-
The table name.
Return values
array<string, string> —Column name => declared type (upper case).
getDate()
The date (`meta.date`) of the open build, e.g. `2026-10-03`.
public
getDate() : string|null
Return values
string|null —Null while no build is open.
getPath()
Where the build is kept.
public
getPath() : string
Return values
stringgetVersion()
The version (`meta.version`) of the open build, e.g. `5.3.0+20261003`.
public
getVersion() : string|null
Return values
string|null —Null while no build is open.
isOpen()
Whether a build is open.
public
isOpen() : bool
Return values
boolquery()
Runs a read query against the build, opening (or downloading) it first.
public
query(string $sql[, array<string|int, mixed> $params = [] ]) : PromiseInterface<string|int, array<string|int, array<string|int, mixed>>>
Parameters
- $sql : string
-
The statement, with
?or named placeholders. - $params : array<string|int, mixed> = []
-
Values bound to the placeholders.
Return values
PromiseInterface<string|int, array<string|int, array<string|int, mixed>>> —The raw rows.
ready()
Opens the build, downloading it first if there is none on disk, and starts the refresh timer.
public
ready() : PromiseInterface<string|int, PDO>
Return values
PromiseInterface<string|int, PDO> —The open connection.
refresh()
Checks `Meta.json` and installs MTGJSON's current build if it is newer than the open one (or always, when forced).
public
refresh([bool $force = false ]) : PromiseInterface<string|int, bool>
Parameters
- $force : bool = false
-
Download even when the versions match.
Return values
PromiseInterface<string|int, bool> —Whether a new build was installed.
select()
Runs a read query against the open build, synchronously. Callers must have waited for {@see ready()}.
public
select(string $sql[, array<string|int, mixed> $params = [] ]) : array<string|int, array<string|int, mixed>>
Parameters
- $sql : string
-
The statement, with
?or named placeholders. - $params : array<string|int, mixed> = []
-
Values bound to the placeholders.
Tags
Return values
array<string|int, array<string|int, mixed>> —The raw rows.
disconnect()
Drops the connection so the file can be replaced.
protected
disconnect() : void
download()
Streams a gzipped file, inflating it to `$target` chunk by chunk so it is never held in memory.
protected
download(string $url, string $target) : PromiseInterface<string|int, void>
Parameters
- $url : string
-
The
.gzURL. - $target : string
-
The file to write the inflated bytes to.
Return values
PromiseInterface<string|int, void>inflateTo()
Inflates a gzip body stream into an open file.
protected
inflateTo(ReadableStreamInterface $body, resource $file, int $total) : PromiseInterface<string|int, void>
Parameters
- $body : ReadableStreamInterface
-
The response body.
- $file : resource
-
The file to write to.
- $total : int
-
The compressed size, for progress logs (0 when unknown).
Return values
PromiseInterface<string|int, void> —Resolves once the gzip stream has ended cleanly.
install()
Downloads, verifies and swaps in MTGJSON's current build.
protected
install() : PromiseInterface<string|int, string>
Return values
PromiseInterface<string|int, string> —The installed version.
label()
What this build is called in logs and errors.
protected
label() : string
Return values
stringopen()
Opens the build at {@see $path} read-only and reads its version.
protected
open() : void
Tags
prepare()
Checks a downloaded build and adds the extra indexes.
protected
prepare(string $file) : string
Parameters
- $file : string
-
The downloaded file.
Tags
Return values
string —The build's version.
refreshQuietly()
Refreshes, logging rather than throwing on failure; the open build stays in use.
protected
refreshQuietly() : void
remoteVersion()
The version of the build currently published, from `Meta.json`.
protected
remoteVersion() : PromiseInterface<string|int, ?string>
Return values
PromiseInterface<string|int, ?string>schedule()
Starts the refresh timer, and checks right away if the build on disk is already older than one interval.
protected
schedule() : void
source()
Where the gzipped build is downloaded from.
protected
source() : string