-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspatial-relations.ts
More file actions
121 lines (115 loc) · 3.92 KB
/
Copy pathspatial-relations.ts
File metadata and controls
121 lines (115 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
* spatial-relations.ts — Cross-table PostGIS spatial relations
*
* Declares 5 @spatialRelation virtual relations across the agentic-db schema
* using the blueprint SDK (same declarative path as every other schema here).
*
* Each entry lands as a metaschema_public.spatial_relation row; the
* sync_spatial_relation_tags trigger then projects a @spatialRelation smart
* tag onto the owner column so graphile-postgis' PostgisSpatialRelationsPlugin
* exposes it as a cross-table filter in the GraphQL `where:` input — no FK
* required, no GeoJSON on the wire.
*
* Generated ORM shape:
* orm.memory.findMany({
* where: { nearbyPlaces: { some: { name: { equalTo: 'Bay Area' } } } },
* });
*
* All 5 entries use `st_dwithin` because every agentic-db geom column is a
* Point. Radii (in metres since columns are geography) match the query intent:
*
* 1. memories → places @ 5 km "memories within 5 km of a known place"
* 2. memories → contacts @ 2 km "memories logged near where a contact lives"
* 3. trips → venues @ 1 km "trips whose destination is near a venue"
* 4. events → venues @ 500 m "events near a specific venue (no FK)"
* 5. memories → memories @ 1 km "what else happened near this memory" (self)
*
* Must run AFTER all domain schemas — source and target tables/columns must
* already exist on the database.
*/
import {
type BlueprintDefinition,
type BlueprintRelation,
provisionBlueprint,
} from '../blueprint';
const SCHEMA_NAME = 'app_public';
/**
* 5 RelationSpatial entries typed directly against the generated
* BlueprintRelation union (node-type-registry ≥ 0.16.0 exposes
* source_field/target_field on the RelationSpatial arm). Column names are
* resolved server-side by resolve_blueprint_field.
*/
const entries: BlueprintRelation[] = [
{
$type: 'RelationSpatial',
source_table: 'memories',
source_schema_name: SCHEMA_NAME,
target_table: 'places',
target_schema_name: SCHEMA_NAME,
source_field: 'location_geo',
target_field: 'location_geo',
name: 'nearbyPlaces',
operator: 'st_dwithin',
param_name: 'distance',
},
{
$type: 'RelationSpatial',
source_table: 'memories',
source_schema_name: SCHEMA_NAME,
target_table: 'contacts',
target_schema_name: SCHEMA_NAME,
source_field: 'location_geo',
target_field: 'location_geo',
name: 'nearbyContacts',
operator: 'st_dwithin',
param_name: 'distance',
},
{
$type: 'RelationSpatial',
source_table: 'trips',
source_schema_name: SCHEMA_NAME,
target_table: 'venues',
target_schema_name: SCHEMA_NAME,
source_field: 'destination_geo',
target_field: 'location',
name: 'nearbyVenues',
operator: 'st_dwithin',
param_name: 'distance',
},
{
$type: 'RelationSpatial',
source_table: 'events',
source_schema_name: SCHEMA_NAME,
target_table: 'venues',
target_schema_name: SCHEMA_NAME,
source_field: 'location_geo',
target_field: 'location',
name: 'nearbyVenues',
operator: 'st_dwithin',
param_name: 'distance',
},
{
$type: 'RelationSpatial',
source_table: 'memories',
source_schema_name: SCHEMA_NAME,
target_table: 'memories',
target_schema_name: SCHEMA_NAME,
source_field: 'location_geo',
target_field: 'location_geo',
name: 'nearbyMemories',
operator: 'st_dwithin',
param_name: 'distance',
},
];
const definition: BlueprintDefinition = {
// No new tables — we reference already-provisioned app_public tables by name.
tables: [],
// Cast-to-BlueprintRelation: the generated union type carries *_id props
// (server-resolved UUIDs), but the blueprint JSON shape accepts field names
// which construct_blueprint resolves via resolve_blueprint_field.
relations: entries as unknown as BlueprintRelation[],
};
async function main() {
await provisionBlueprint(definition, 'Spatial Relations');
}
export { main as default };