survos / key-value-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
键值包
一个利用 Sqlite 创建索引化非结构化数据存储库的 Symfony 包。
灵感来源于 https://gist.github.com/sbrl/c3bfbbbb3d1419332e9ece1bac8bb71c 和 https://dexie.org/ 以及 Symfony PDO 缓存组件。
其核心思想是存储可以通过键或过滤器访问的字符串或非结构化数据。
最初,它只是一个字符串查找,例如,通过 QID 查找 wikidata 对象,或通过 imdb_id 查找 csv 文件中的电影。
您可以通过添加一个包含便于重命名字段和数据转换指令的 .conf 文件来自定义导入过程。您还可以在导入和导出过程中监听事件。
Pixy 可以(将,最终)使用 json schema 文件来定义字段。使用 LiFormBundle 的 https://json-schema.fullstack.org.cn/
可以通过命令行、监听器或 .conf 文件定义索引。有一种简短的格式,即逗号分隔的字符串格式,以及一种详细的格式,即键为索引详细信息的哈希格式。
如果安装了 api-platform,将存在一个 API 端点。
与 survos/translation-bundle 集成
设置
所有 Pixy 数据库文件都有一个关联的配置文件,该文件描述了映射和底层数据结构。通常它与相同的基准文件名共享。
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"
“特殊”表
由于 Pixy 数据库提供了一种方便地处理 Excel 数据的方法,因此存在一个用于处理绘图(嵌入图像)的特殊表。它是通过 bin/console grid:excel-to-csv(在 grid-group 包中?在 museado 中?)创建的。
Excel 将嵌入的图像存储为“绘图”。
翻译也存储为 Pixy 表,并有自己的部分。
示例
- 电影(imdb)
- 学校
CSV 数据集
读取现有的 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); //
conf 文件简化了一些 PHP 调用,但并非绝对必要
假设我们想按类别筛选。首先,我们需要在表中添加一个类似于 dexie 的索引。
$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();
通常 CSV 文件和其他数据源中的键名与 sqlite 列名不兼容。为了将旧名称映射到新名称,您可以创建一个用于列名称的正则表达式规则序列。
从 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' ]);
所有字段都将转换为 camel_case,即使没有正则表达式规则。
请注意,JSON 未压缩,因此键是重复的。因此,sqlite 文件的大小比 CSV 大(rowCount * headerRowSize)。
用法
composer require survos/key-value-bundle
使用 sqlite 数据库名称以及任何要创建的新表初始化 StorageBox。
可以重复使用现有表,但自动表创建已禁用。
$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));
多语言 StorageBox
特别是用于可能具有翻译的文本字段的 kv 查找
$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