DBIO-PostgreSQL-Age
view release on metacpan or search on metacpan
.claude/skills/dbio-postgresql-age/SKILL.md view on Meta::CPAN
on_connect_call => 'load_age',
});
my $storage = $schema->storage;
```
## Graph Lifecycle
```perl
$storage->create_graph('social');
$storage->drop_graph('social', 1); # 1 = cascade
```
Graph names must be plain PG identifiers (validated by `cypher()`):
```
valid: social, app_graph_1
invalid: app-graph, public.social, "graph name"
```
## Running Cypher
```perl
my $rows = $storage->cypher($graph, $query, \@result_columns, \%params);
```
Every result column is declared `agtype`. Column names must be plain identifiers (`[A-Za-z_][A-Za-z0-9_]*`), otherwise `cypher()` throws `Invalid AGE column name`. Returns arrayref of hashrefs.
```perl
my $rows = $storage->cypher(
'social',
q{ MATCH (p:Person) RETURN p.name, p.age },
[qw(name age)],
);
for my $row (@$rows) {
say "$row->{name} is $row->{age}";
}
```
## Vertices
AGE requires every `cypher()` to RETURN at least one column, so CREATE returns a stub.
```perl
$storage->cypher(
'social',
q{
CREATE (:Person {name: $name, age: $age})
RETURN 1
},
['ok'],
{ name => 'Alice', age => 30 },
);
```
Always parameterise user input â never interpolate.
## Edges
Match endpoints first, then create. Can combine create + create in one query.
```perl
$storage->cypher(
'social',
q{
MATCH (a:Person {name: $from}), (b:Person {name: $to})
CREATE (a)-[:KNOWS {since: $since}]->(b)
RETURN 1
},
['ok'],
{ from => 'Alice', to => 'Bob', since => 2020 },
);
```
## Querying
```perl
my $rows = $storage->cypher(
'social',
q{
MATCH (a:Person)-[r:KNOWS]->(b:Person)
RETURN a.name, b.name, r.since
},
[qw(person friend since)],
);
```
## agtype Results
`cypher()` returns `agtype` as strings via DBI. Let it decode them: pass `{ auto_decode => 1 }` as the 5th argument.
```perl
my $rows = $storage->cypher(
'social',
q{ MATCH (p:Person {name: $name}) RETURN p, p.age },
[qw(person age)],
{ name => 'Alice' },
{ auto_decode => 1 },
);
# $rows->[0]{person} = { id => ..., label => 'Person', properties => { ... } }
# $rows->[0]{age} = 30
```
Or decode a single cell: `$storage->decode_agtype($cell)`. Vertices come back as `{ id, label, properties }`, edges additionally with `start_id` and `end_id`.
Fallback without auto_decode: scalar strings come back quoted.
```perl
sub ag_string {
my ($value) = @_;
return undef unless defined $value;
$value =~ s/\A"//;
$value =~ s/"\z//;
return $value;
}
```
JSON-like agtype values â use `JSON::MaybeXS`:
```perl
( run in 0.766 second using v1.01-cache-2.11-cpan-eb134014566 )