Skip to content

Commit f7fa73d

Browse files
Merge branch 'main' into feat/suggestion-status-message
2 parents 911759d + dc39ba0 commit f7fa73d

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

packages/spacecat-shared-data-access/CLAUDE.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ Lambda/ECS service
2121
| File | Purpose |
2222
|------|---------|
2323
| `src/index.js` | Default export: `dataAccessWrapper(fn)` for Helix/Lambda handlers |
24-
| `src/service/index.js` | `createDataAccess(config, log?, client?)` factory |
24+
| `src/service/index.js` | `createDataAccess(config, log?, client?)` factory — returns entity collections + `services.postgrestClient` |
25+
| `src/service/index.d.ts` | `DataAccess` and `DataAccessServices` type definitions |
2526
| `src/models/base/schema.builder.js` | DSL for defining entity schemas (attributes, references, indexes) |
2627
| `src/models/base/base.model.js` | Base entity class (auto-generated getters/setters, save, remove) |
2728
| `src/models/base/base.collection.js` | Base collection class (findById, all, query, count) |
@@ -152,6 +153,22 @@ npm run test:it
152153
- PostgREST calls are stubbed via sinon
153154
- Each entity model and collection has its own test file
154155

156+
## Direct PostgREST Queries
157+
158+
`dataAccess.services.postgrestClient` exposes the raw `@supabase/postgrest-js` `PostgrestClient` for querying tables that don't have entity models (e.g. analytics views, reporting tables):
159+
160+
```js
161+
const { postgrestClient } = context.dataAccess.services;
162+
163+
const { data, error } = await postgrestClient
164+
.from('brand_presence_executions')
165+
.select('*')
166+
.eq('site_id', siteId)
167+
.limit(100);
168+
```
169+
170+
This is the same client instance used internally by entity collections — same URL, auth, and schema.
171+
155172
## Common Patterns
156173

157174
### Collection query with WHERE clause

packages/spacecat-shared-data-access/README.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ npm install @adobe/spacecat-shared-data-access
1616
## What You Get
1717

1818
The package provides:
19-
- `createDataAccess(config, log?, client?)`
19+
- `createDataAccess(config, log?, client?)` — returns entity collections + `services.postgrestClient`
2020
- `dataAccessWrapper(fn)` (default export) for Helix/Lambda style handlers
2121
- Entity collections/models with stable external API shape for services
22+
- `services.postgrestClient` for direct PostgREST queries against non-entity tables
2223

2324
## Quick Start
2425

@@ -89,6 +90,29 @@ The wrapper reads from `context.env`:
8990
- `S3_CONFIG_BUCKET`
9091
- `AWS_REGION`
9192

93+
## Direct PostgREST Queries
94+
95+
For querying PostgREST tables that are not modeled as entities (e.g. analytics views, reporting tables), the `postgrestClient` is available under `dataAccess.services`:
96+
97+
```js
98+
const { Site } = dataAccess; // entity collections
99+
const { postgrestClient } = dataAccess.services; // raw PostgREST client
100+
101+
// Use entity collections as usual
102+
const site = await Site.findById(siteId);
103+
104+
// Direct queries against non-entity tables
105+
const { data, error } = await postgrestClient
106+
.from('brand_presence_executions')
107+
.select('execution_date, visibility_score, sentiment')
108+
.eq('site_id', siteId)
109+
.gte('execution_date', '2025-01-01')
110+
.order('execution_date', { ascending: false })
111+
.limit(100);
112+
```
113+
114+
This is the same `@supabase/postgrest-js` `PostgrestClient` instance used internally by the entity collections. Full IDE autocomplete is available for the query builder chain.
115+
92116
## Field Mapping Behavior
93117

94118
Public model API remains camelCase while Postgres/PostgREST tables are snake_case.
@@ -383,7 +407,10 @@ export MYSTICAT_DATA_SERVICE_REPOSITORY=682033462621.dkr.ecr.us-east-1.amazonaws
383407

384408
Type definitions are shipped from:
385409
- `src/index.d.ts`
386-
- `src/models/**/index.d.ts`
410+
- `src/service/index.d.ts` — `DataAccess` and `DataAccessServices` interfaces
411+
- `src/models/**/index.d.ts` — per-entity collection and model interfaces
412+
413+
The `DataAccess` interface provides full typing for all entity collections and `services.postgrestClient` (typed as `PostgrestClient` from `@supabase/postgrest-js`).
387414

388415
Use the package directly in TS projects; no extra setup required.
389416

0 commit comments

Comments
 (0)