survos / key-value-bundle
Symfony Bundle to easily allow a SQLite Key Value lookup
Fund package maintenance!
kbond
Installs: 61
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 3
Forks: 0
Open Issues: 2
Type:symfony-bundle
Requires
- php: ^8.2
- ext-pdo: *
- endroid/installer: ^1.4
- symfony/config: ^6.4 || ^7.0
- symfony/dependency-injection: ^6.4 || ^7.0
- symfony/http-kernel: ^6.4 || ^7.0
- twig/twig: ^3.4
Requires (Dev)
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^11.2
- symfony/browser-kit: ^6.4 || ^7.0
- symfony/framework-bundle: ^6.4 || ^7.0
- symfony/phpunit-bridge: ^6.4 || ^7.0
- symfony/twig-bundle: ^6.4 || ^7.0
- symfony/var-dumper: ^6.4 || ^7.0
- dev-main
- 1.5.241
- 1.5.240
- 1.5.239
- 1.5.238
- 1.5.237
- 1.5.236
- 1.5.235
- 1.5.234
- 1.5.233
- 1.5.232
- 1.5.231
- 1.5.230
- 1.5.229
- 1.5.228
- 1.5.227
- 1.5.226
- 1.5.225
- 1.5.224
- 1.5.223
- 1.5.222
- 1.5.221
- 1.5.220
- 1.5.219
- 1.5.218
- 1.5.217
- 1.5.216
- 1.5.215
- 1.5.214
- 1.5.213
- 1.5.212
- 1.5.211
- 1.5.210
- 1.5.209
- 1.5.208
- 1.5.207
- 1.5.206
- 1.5.205
- 1.5.204
- 1.5.203
- 1.5.202
- 1.5.201
- 1.5.200
- 1.5.199
- 1.5.198
- 1.5.197
- 1.5.196
- 1.5.195
- 1.5.194
- 1.5.193
- 1.5.192
- 1.5.191
- 1.5.190
- 1.5.189
- 1.5.188
- 1.5.187
- 1.5.186
- 1.5.185
- 1.5.184
- 1.5.183
- 1.5.182
- 1.5.181
- 1.5.180
- 1.5.179
- 1.5.178
- 1.5.177
- 1.5.176
- 1.5.175
- 1.5.174
- 1.5.173
- 1.5.172
- 1.5.171
- 1.5.170
- 1.5.169
- 1.5.168
- 1.5.167
- 1.5.166
- 1.5.165
- 1.5.164
- 1.5.163
- 1.5.162
- 1.5.161
- 1.5.160
- 1.5.159
- 1.5.158
- 1.5.157
- 1.5.156
- 1.5.155
- 1.5.154
- 1.5.153
- 1.5.152
- 1.5.151
- 1.5.150
- 1.5.149
- 1.5.148
- 1.5.147
- 1.5.146
- 1.5.145
- 1.5.144
- 1.5.143
- 1.5.142
- 1.5.141
- 1.5.140
- 1.5.139
- 1.5.138
- 1.5.137
- 1.5.136
- 1.5.135
- 1.5.134
- 1.5.133
- 1.5.132
- 1.5.131
- 1.5.130
- 1.5.129
- 1.5.128
- 1.5.127
- 1.5.126
- 1.5.125
- 1.5.124
- 1.5.123
This package is auto-updated.
Last update: 2024-06-30 10:57:56 UTC
README
Key Value Bundle
A Symfony bundle that leverages Sqlite to create an indexed no-sql datastore.
Inspired by https://gist.github.com/sbrl/c3bfbbbb3d1419332e9ece1bac8bb71c and https://dexie.org/ and the Symfony PDO Cache component.
At its core, the idea is to store strings or unstructured data accessible by a key or a filter.
Initially, it was just a string lookup, which could be a JSON string, for example looking up a wikidata object by its QID, or a movie from a csv file by its imdb_id.
You can customize the import process by adding a .conf file with directives that facilitate renaming fields and munging data. You can also listen for events during the import and export process.
Pixy can (will, eventually) use json schema files to defining the fields. https://json-schema.org/ using LiFormBundle
The indexes can be defined via the command line, a listener, or a .conf file. There's a succinct format that is a comma-separated string, and a detailed format with is a hash with the index details as keys.
There is (will be) an API endpoint if api-platform is installed.
Integration with survos/translation-bundle
Setup
All pixy db files have an associated configuration file that describes the mapping and underlying data structure. Generally it shares the same base filename.
curl bin/console pixy:init movies --dir=./data/imdb # ./pixy/movies.yaml created with 4 tables, configure it and run bin/console pixy:import movies --limit 10 cat > pixy.movies << 'END' (full config) END bin/console pixy:import movies --limit 10 symfony open:local --path="/pixy/movies"
"Special" Tables
Since pixy databases offer a convenient way to work with Excel data, there is a special table for handling drawings (embedded images). It is created with bin/console grid:excel-to-csv (in the grid-group bundle? In museado?)
Excel stores embedded images as "Drawings".
Translations are also stored as pixy tables, and have their own section.
Examples
- Movies (imdb)
- Schools
CSV Datasets
Reading an Existing Pixy
$pixy = new Pixy::Reader('school.pixy'); $pixy->select('mo') foreach ($pixy->)
wget https://dummyjson.com/products products.json
// inject the service $id = 'tt123'; $kv = $keyValueService->getStorageBox('dummy.pixy', [ 'products' => 'sku,brand,category' // first key is text primary key by default ]); $kv->select('products'); // so that we don't have to pass it each time. $kv->set($data); // because they key is in the data. assert($kv->get($id)); assert($kv->has($id)); assert(json_decode($kv->get($id)) == $data); //
The conf file simplifies some of the php calls, but isn't 100% necessary
Suppose we want to filter by category. First, we need to add an index, dexie-style, to the table.
$kv = $keyValueService->getStorageBox('app.db', [ 'movies' => 'imdb_id, year|integer, category|string' // first key is text primary key by default ]); $rows = $kv->where("year < 2000 and category='drama'")->iterate(); // without the index $rows = $kv->where("json_extract(value, '$.year') < 2000")->iterate();
Often CSV files and other data sources have key names that aren't really compatible with sqlite column names. To map the old names to the new ones, you can create a sequence of regex rules for the column names.
From MOMA Artists.json
{ "ConstituentID": 1, "DisplayName": "Robert Arneson", "ArtistBio": "American, 1930–1992", "Nationality": "American", "Gender": "male", "BeginDate": 1930, "EndDate": 1992, "Wiki QID": null }
$kv->map([ '/ConstituentID/' => 'id', '/BeginDate/' => 'birthYear', '/EndDate/' => 'deathYear' ], [ 'artists' ]);
All fields will be converted to camel_case, even without regex rules.
Note that the JSON is NOT compressed, so the keys are duplicated. So the sqlite file is larger than the CSV by (rowCount * headerRowSize).
Usage
composer require survos/key-value-bundle
Initialize StorageBox with the sqlite database name plus any new tables to be created.
Existing tables can be used, but automatic table creation is disabled.
$kvDb = new StorageBox('translation.db', ['es', 'en'], 'en'); $key = md5('dog'); $kvDb->set($key, 'perro', 'es'); $kvDb->set($key, 'dog'); // defaults to en $trans = $kvDb->get($key); // dog $trans = $kvDb->get($key, 'es'); // perro // trans is perro // cache-like with callcack $trans = $kvDb->get($key, callback: fn($item) => $this->translator->trans('dog')); // keys are automatically slugified $trans = $kvDb->get('My name is', fn($item) => $this->translator->trans($item->key)); // keys are automatically slugified $trans = $kvDb->get('My name is', ) => $this->translator->trans($item->key));
Multi-lingual StorageBox
Specifically for kv lookups of text fields that may have translations
$kvDb = new MLSB('property.db', ['label','description']); $key = 'Q31'; $kvDb->set($key, 'label', 'Belgium', 'en-gb'); $kvDb->set($key, 'description', 'constitutional monarchy in Western Europe', 'en-gb'); $transArray = $kvDb->getFieldsByLocale($key, 'en-gb'); // ['label' => 'Belgium'...] $label = $kvDb->getField($key, 'label', 'en-gb'); // 'Belgium' $transArray = $kvDb->getFields($key); // ['en-gb' => ['label' => 'Belgium', 'description' => ...]] // ditto for set, needs transactions