Download the PHP package prewk/snapper-php without Composer
On this page you can find all versions of the php package prewk/snapper-php. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download prewk/snapper-php
More information about prewk/snapper-php
Files in prewk/snapper-php
Package snapper-php
Short Description Anonymized (de)serialization
License MIT
Homepage http://github.com/prewk/snapper-php
Informations about the package snapper-php
Snapper 🐡
Converts rows from a relational database into serialization snapshots that can be deserialized into new rows at a later time with complex relationships preserved.
Example
From database into the serializer
Out from the serializer
Deserialize into database again
How to serialize?
How to deserialize?
Ingredient types
Value
Pass through the value of the field.
Raw
Force the value of the field.
Ref
The field is a foreign key that references another table. The optional
method takes arguments that, when equal to the encountered field value, is considered non-references and are just passed through like Value
.
Morph
Specify a polymorphic relation using two fields, one type (foo_type
) and one id (foo_id
), and map values in foo_type
against other tables.
Requires both fields to be present to function properly, supports optional values that when matched ignores the foreign relation and just passes though the value.
Match
Look at the given field (type
in the example above) and become different ingredients depending on its value.
on
: Exact matchpattern
: Regexp matchdefault
: Fallback
If no match is successful and no default
is provided, the field will not be included.
Json
Define references nested in JSON.
On string and integer refs inside the JSON
The library is built for the normal scenario of integer keys (AUTO_INCREMENT
), but when serializing references they will be converted to UUIDS (v4). They are strings that look like this: a0ff60f5-87fe-4d4e-855b-8993f1c3b065
.
This poses a problem in JSON when..
..gets serialized into..
..and back into a integer key:
"456"
is not strictly equal to 456
. Therefore, the deserialization logic is as follows:
- If the id returned from the inserter is numeric then all
"UUID"
will be replaced withINSERT_ID
- All
UUID
will be replaced withINSERT_ID
Circular
If two of your tables contain circular references to each other, wrap one of the references with a Circular
ingredient. Specify a valid fallback as optional value and specify that value as a fallback Raw
ingredient.
The resulting serialization will start with an INSERT
op containing the fallback value, and end with an UPDATE
op with the real reference.
Events
onDeps
Override recipes/updaters/inserters
Recipe JSON
Recipes can be converted to/from JSON which can be useful.
Validation
Validate a serialization
Check a serialization for unresolvable references
Validate a JSON recipe
Recipes can be validated with a JSON schema validator.
Batched inserts/updates
The insert/update closures will be called with batches of rows that each can be executed in one SQL operation if you want to optimize:
The following rules apply for the return value from inserters:
- Returning void is acceptable, but the serialization will fail if later rows depend on the skipped primary keys
- To return the primary keys, return an array of the same length as
$rows
, everything else is invalid
The batch grouping logic considers one of the following conditions as "start a new batch of operations":
- A new row type (table) is encountered
- The row is dependent on the primary key of another row earlier in the same batch
- The exact number of, or names of, fields has changed from one row to the next
Id manifest
The result of a compilation ($serializer->compile()
) is a Serialization
. It has two methods:
getOps()
- Get an array representing the sequence of operations (the actual "serialization" if you will)getIdManifest()
- Get a dictionary mapping the internal uuids in the serialization to your given db ids, grouped by type (table name)
Why?
The library is useful for providing snapshot functionality to multi-tenant services where a user owns one or more complex sets of data with complex internal relationships.
Howtos
I don't want to create table X, it already exists
The deserializer doesn't care about the inserter closure's internal logic, it only requires you to return the primary key:
I need row metadata to be serialized
Just use $recipe->value()
in the recipe and feed whatever fields you want to the Serializer and it'll end up in the snapshot.
When deserializing, pick and choose which fields to insert into the database in your inserter.
I have composite keys
Unsupported at the moment, a workaround is adding a unique id and pretending that's the primary key.
License
MIT
All versions of snapper-php with dependencies
illuminate/support Version 5.*
marcj/topsort Version ^1.1
ramsey/uuid Version ^3.7
prewk/option Version 1.*
prewk/snapper-schema Version 2.1.2
prewk/result Version 1.*