@lpdjs/firestore-repo-service 2.6.57 → 3.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +45 -27
- package/dist/{create-servers-BxkVVkg9.d.cts → create-servers-Cbvzs_ou.d.cts} +5 -5
- package/dist/{create-servers-B84cbslf.d.ts → create-servers-aWdDTcSu.d.ts} +5 -5
- package/dist/{firebase-auth-DZ8lb2Ec.d.cts → firebase-auth-Ds5P0kXT.d.cts} +1 -1
- package/dist/{firebase-auth-DZ8lb2Ec.d.ts → firebase-auth-Ds5P0kXT.d.ts} +1 -1
- package/dist/index.cjs +97 -85
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +9 -9
- package/dist/index.d.ts +9 -9
- package/dist/index.js +97 -85
- package/dist/index.js.map +1 -1
- package/dist/llms-full.txt +237 -263
- package/dist/llms.txt +5 -4
- package/dist/{openapi-BpbfjSHK.d.ts → openapi-BTu_1vSc.d.ts} +1 -1
- package/dist/{openapi-DG7aQUlX.d.cts → openapi-CoKsSOIU.d.cts} +1 -1
- package/dist/{queue-B4uwef6c.d.ts → queue-BQxyuEXb.d.ts} +8 -23
- package/dist/{queue-CQyWCQBk.d.cts → queue-CMiKvIW1.d.cts} +8 -23
- package/dist/rules-B3b-BZLf.d.cts +30 -0
- package/dist/rules-BJMT0N0i.d.ts +30 -0
- package/dist/servers/admin/index.d.cts +402 -6
- package/dist/servers/admin/index.d.ts +402 -6
- package/dist/servers/auth/index.d.cts +1 -1
- package/dist/servers/auth/index.d.ts +1 -1
- package/dist/servers/crud/index.cjs +2 -2
- package/dist/servers/crud/index.cjs.map +1 -1
- package/dist/servers/crud/index.d.cts +7 -6
- package/dist/servers/crud/index.d.ts +7 -6
- package/dist/servers/crud/index.js +2 -2
- package/dist/servers/crud/index.js.map +1 -1
- package/dist/servers/hono/index.d.cts +3 -3
- package/dist/servers/hono/index.d.ts +3 -3
- package/dist/servers/index.cjs +106 -94
- package/dist/servers/index.cjs.map +1 -1
- package/dist/servers/index.d.cts +10 -9
- package/dist/servers/index.d.ts +10 -9
- package/dist/servers/index.js +106 -94
- package/dist/servers/index.js.map +1 -1
- package/dist/sync/bigquery.cjs +5 -5
- package/dist/sync/bigquery.cjs.map +1 -1
- package/dist/sync/bigquery.d.cts +28 -3
- package/dist/sync/bigquery.d.ts +28 -3
- package/dist/sync/bigquery.js +5 -5
- package/dist/sync/bigquery.js.map +1 -1
- package/dist/sync/index.cjs +74 -54
- package/dist/sync/index.cjs.map +1 -1
- package/dist/sync/index.d.cts +63 -17
- package/dist/sync/index.d.ts +63 -17
- package/dist/sync/index.js +74 -54
- package/dist/sync/index.js.map +1 -1
- package/dist/sync/meilisearch.cjs +4 -0
- package/dist/sync/meilisearch.cjs.map +1 -0
- package/dist/sync/meilisearch.d.cts +114 -0
- package/dist/sync/meilisearch.d.ts +114 -0
- package/dist/sync/meilisearch.js +4 -0
- package/dist/sync/meilisearch.js.map +1 -0
- package/dist/{types-CE-EfugW.d.cts → types-B2e-T8EM.d.cts} +160 -8
- package/dist/{types-B4dMmfet.d.cts → types-BIZz3sxN.d.cts} +1 -1
- package/dist/{types-DT7Nypd_.d.ts → types-BbFZ57eK.d.ts} +101 -41
- package/dist/{types-DyKJJ9UQ.d.ts → types-DSMiiOrU.d.ts} +1 -1
- package/dist/{types-cY03CMya.d.ts → types-XRjVKN9r.d.ts} +160 -8
- package/dist/{types-BwH1C-wi.d.cts → types-zFrp8RnM.d.cts} +101 -41
- package/package.json +19 -1
- package/dist/index-D47y5giY.d.ts +0 -469
- package/dist/index-dW_7jrlK.d.cts +0 -469
package/dist/llms.txt
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# @lpdjs/firestore-repo-service (
|
|
1
|
+
# @lpdjs/firestore-repo-service (v3.0.1)
|
|
2
2
|
|
|
3
3
|
> ⚡ Type-safe Firestore ORM with auto-generated repositories, advanced queries, relations with typed select, pagination with include, batch/bulk operations, aggregations and transactions.
|
|
4
4
|
|
|
@@ -13,13 +13,14 @@ Type-safe Firestore ORM + servers (admin UI, CRUD REST, Hono file-based API, Fir
|
|
|
13
13
|
- [Admin Server](https://frs.lpdjs.fr/guide/admin): The admin UI is built via createServers(repos).admin(...) — a unified factory that auto-binds the repository registry so each entry's repo field is inferred from its key (and so are model field paths in fieldsConfig).
|
|
14
14
|
- [CRUD Server](https://frs.lpdjs.fr/guide/crud): The CRUD REST API server is built via createServers(repos).crud(...) — a unified factory that exposes standard RESTful endpoints and advanced query APIs for all registered repositories.
|
|
15
15
|
- [Change History](https://frs.lpdjs.fr/guide/history): Opt-in, per-repo change-history capture using Firestore triggers, with a typed read API on the configured repository.
|
|
16
|
-
- [Firestore →
|
|
16
|
+
- [Firestore → Storage & Search Sync](https://frs.lpdjs.fr/guide/sync): Automatically replicate Firestore collections to SQL databases (BigQuery, etc.) and search engines (Meilisearch, etc.) via Cloud Pub/Sub.
|
|
17
17
|
- [Hono API Server](https://frs.lpdjs.fr/guide/hono): A typed, file-based HTTP server built on Hono, designed
|
|
18
18
|
|
|
19
19
|
## Package entry points
|
|
20
20
|
- `@lpdjs/firestore-repo-service` — Core ORM: createRepositoryConfig, createRepositoryMapping (lazy Firestore), typed get/query/aggregate/crud/batch/bulk/transaction.
|
|
21
|
-
- `@lpdjs/firestore-repo-service/sync` — Firestore →
|
|
22
|
-
- `@lpdjs/firestore-repo-service/sync/bigquery` — BigQuery adapter for the sync pipeline.
|
|
21
|
+
- `@lpdjs/firestore-repo-service/sync` — Firestore → Storage & Search sync pipeline (triggers + worker + admin).
|
|
22
|
+
- `@lpdjs/firestore-repo-service/sync/bigquery` — BigQuery CDC adapter for the sync pipeline.
|
|
23
|
+
- `@lpdjs/firestore-repo-service/sync/meilisearch` — Meilisearch adapter for full-text search sync pipeline.
|
|
23
24
|
- `@lpdjs/firestore-repo-service/history` — Change-history triggers and read API (createHistoryTriggers).
|
|
24
25
|
- `@lpdjs/firestore-repo-service/servers` — createServers(repos) → admin / crud / sync builders (lazy, definable in separate files).
|
|
25
26
|
- `@lpdjs/firestore-repo-service/servers/admin` — Auto-generated admin UI (Zod forms, filtering, relations).
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { a as
|
|
1
|
+
import { a as SyncAdapter, S as SyncEvent } from './types-BbFZ57eK.js';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Per-repo in-memory batch buffer.
|
|
@@ -9,11 +9,11 @@ import { a as SqlAdapter, S as SyncEvent } from './types-DT7Nypd_.js';
|
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
11
|
interface SyncQueueOptions {
|
|
12
|
-
/**
|
|
13
|
-
adapter:
|
|
14
|
-
/** SQL table name */
|
|
12
|
+
/** Target adapter to flush data to */
|
|
13
|
+
adapter: SyncAdapter;
|
|
14
|
+
/** SQL table name or Search index UID */
|
|
15
15
|
tableName: string;
|
|
16
|
-
/** Primary key column name */
|
|
16
|
+
/** Primary key column/field name */
|
|
17
17
|
primaryKey: string;
|
|
18
18
|
/** Max rows per flush (default: 100) */
|
|
19
19
|
batchSize?: number;
|
|
@@ -24,7 +24,7 @@ interface SyncQueueOptions {
|
|
|
24
24
|
}
|
|
25
25
|
/**
|
|
26
26
|
* In-memory buffer that batches sync events per-repo and flushes them
|
|
27
|
-
* to a
|
|
27
|
+
* to a target adapter.
|
|
28
28
|
*/
|
|
29
29
|
declare class SyncQueue {
|
|
30
30
|
private buffer;
|
|
@@ -42,31 +42,16 @@ declare class SyncQueue {
|
|
|
42
42
|
/** Push one or more events into the buffer. Triggers auto-flush if batchSize reached. */
|
|
43
43
|
enqueue(...events: SyncEvent[]): void;
|
|
44
44
|
/**
|
|
45
|
-
* Flush all buffered events to the
|
|
45
|
+
* Flush all buffered events to the target adapter.
|
|
46
46
|
* Upserts and deletes are batched separately.
|
|
47
|
-
*
|
|
48
|
-
* Concurrent callers (e.g. several PubSub messages handled in parallel
|
|
49
|
-
* inside the same Cloud Function instance with `concurrency > 1`) all
|
|
50
|
-
* await the same in-flight drain cycle. A drain cycle empties the buffer
|
|
51
|
-
* completely (see {@link _drain}), so by the time `await flush()` resolves
|
|
52
|
-
* every event that was buffered when the call started has been persisted
|
|
53
|
-
* (or handed to `onFlushError`) — which is required for safe PubSub
|
|
54
|
-
* at-least-once ack semantics.
|
|
55
47
|
*/
|
|
56
48
|
flush(): Promise<void>;
|
|
57
49
|
/**
|
|
58
|
-
* Drain the buffer to empty.
|
|
59
|
-
* `batchSize` events, so loop until nothing remains. Without this loop,
|
|
60
|
-
* bursts larger than `batchSize` would leave events buffered after
|
|
61
|
-
* `flush()` resolved — and a container shutdown before the next interval
|
|
62
|
-
* would lose them permanently (breaking at-least-once).
|
|
50
|
+
* Drain the buffer to empty.
|
|
63
51
|
*/
|
|
64
52
|
private _drain;
|
|
65
53
|
/**
|
|
66
54
|
* Flush a single batch (up to `batchSize` events) to the adapter.
|
|
67
|
-
* @returns `true` when the batch was processed (or routed to the error
|
|
68
|
-
* handler) and the loop may continue; `false` when the batch was
|
|
69
|
-
* re-buffered after a failure with no `onFlushError` handler.
|
|
70
55
|
*/
|
|
71
56
|
private _doFlush;
|
|
72
57
|
/** Stop the auto-flush timer and flush remaining events. */
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { a as
|
|
1
|
+
import { a as SyncAdapter, S as SyncEvent } from './types-zFrp8RnM.cjs';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Per-repo in-memory batch buffer.
|
|
@@ -9,11 +9,11 @@ import { a as SqlAdapter, S as SyncEvent } from './types-BwH1C-wi.cjs';
|
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
11
|
interface SyncQueueOptions {
|
|
12
|
-
/**
|
|
13
|
-
adapter:
|
|
14
|
-
/** SQL table name */
|
|
12
|
+
/** Target adapter to flush data to */
|
|
13
|
+
adapter: SyncAdapter;
|
|
14
|
+
/** SQL table name or Search index UID */
|
|
15
15
|
tableName: string;
|
|
16
|
-
/** Primary key column name */
|
|
16
|
+
/** Primary key column/field name */
|
|
17
17
|
primaryKey: string;
|
|
18
18
|
/** Max rows per flush (default: 100) */
|
|
19
19
|
batchSize?: number;
|
|
@@ -24,7 +24,7 @@ interface SyncQueueOptions {
|
|
|
24
24
|
}
|
|
25
25
|
/**
|
|
26
26
|
* In-memory buffer that batches sync events per-repo and flushes them
|
|
27
|
-
* to a
|
|
27
|
+
* to a target adapter.
|
|
28
28
|
*/
|
|
29
29
|
declare class SyncQueue {
|
|
30
30
|
private buffer;
|
|
@@ -42,31 +42,16 @@ declare class SyncQueue {
|
|
|
42
42
|
/** Push one or more events into the buffer. Triggers auto-flush if batchSize reached. */
|
|
43
43
|
enqueue(...events: SyncEvent[]): void;
|
|
44
44
|
/**
|
|
45
|
-
* Flush all buffered events to the
|
|
45
|
+
* Flush all buffered events to the target adapter.
|
|
46
46
|
* Upserts and deletes are batched separately.
|
|
47
|
-
*
|
|
48
|
-
* Concurrent callers (e.g. several PubSub messages handled in parallel
|
|
49
|
-
* inside the same Cloud Function instance with `concurrency > 1`) all
|
|
50
|
-
* await the same in-flight drain cycle. A drain cycle empties the buffer
|
|
51
|
-
* completely (see {@link _drain}), so by the time `await flush()` resolves
|
|
52
|
-
* every event that was buffered when the call started has been persisted
|
|
53
|
-
* (or handed to `onFlushError`) — which is required for safe PubSub
|
|
54
|
-
* at-least-once ack semantics.
|
|
55
47
|
*/
|
|
56
48
|
flush(): Promise<void>;
|
|
57
49
|
/**
|
|
58
|
-
* Drain the buffer to empty.
|
|
59
|
-
* `batchSize` events, so loop until nothing remains. Without this loop,
|
|
60
|
-
* bursts larger than `batchSize` would leave events buffered after
|
|
61
|
-
* `flush()` resolved — and a container shutdown before the next interval
|
|
62
|
-
* would lose them permanently (breaking at-least-once).
|
|
50
|
+
* Drain the buffer to empty.
|
|
63
51
|
*/
|
|
64
52
|
private _drain;
|
|
65
53
|
/**
|
|
66
54
|
* Flush a single batch (up to `batchSize` events) to the adapter.
|
|
67
|
-
* @returns `true` when the batch was processed (or routed to the error
|
|
68
|
-
* handler) and the loop may continue; `false` when the batch was
|
|
69
|
-
* re-buffered after a failure with no `onFlushError` handler.
|
|
70
55
|
*/
|
|
71
56
|
private _doFlush;
|
|
72
57
|
/** Stop the auto-flush timer and flush remaining events. */
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { A as AuthUser } from './firebase-auth-Ds5P0kXT.cjs';
|
|
2
|
+
import { D as DeepDiffReturn, k as RulesMap, f as CrudUpdateRuleContext } from './types-B2e-T8EM.cjs';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Computes a typed diff between the existing document (`before`) and the incoming partial changes (`changesInput`).
|
|
6
|
+
* Only fields whose values have actually changed (via structural equality) are included in `changes`.
|
|
7
|
+
*/
|
|
8
|
+
declare function computeDeepDiff<T extends Record<string, unknown>>(before: T, changesInput: Partial<T>): DeepDiffReturn<T>;
|
|
9
|
+
/**
|
|
10
|
+
* Helper to define field-by-field before-rules with strong typing.
|
|
11
|
+
*/
|
|
12
|
+
declare function createRules<T>(rules: RulesMap<T>): RulesMap<T>;
|
|
13
|
+
/**
|
|
14
|
+
* Executes a map of field rules against a computed diff.
|
|
15
|
+
* Evaluates rules only for the keys that changed in `diff.changes`.
|
|
16
|
+
* Returns `{ allowed: true }` if all rules pass, or `{ allowed: false, reason: rule.description }` on the first failure.
|
|
17
|
+
*/
|
|
18
|
+
declare function applyRules<T>(diff: DeepDiffReturn<T>, rulesMap: RulesMap<T>, user?: AuthUser): Promise<{
|
|
19
|
+
allowed: boolean;
|
|
20
|
+
reason?: string;
|
|
21
|
+
}>;
|
|
22
|
+
/**
|
|
23
|
+
* Wraps a rules map into a standard `rules.update` handler for `createCrudServer`.
|
|
24
|
+
*/
|
|
25
|
+
declare function createBeforeRules<T>(rulesMap: RulesMap<T>): (ctx: CrudUpdateRuleContext<T>) => Promise<{
|
|
26
|
+
allowed: boolean;
|
|
27
|
+
reason?: string;
|
|
28
|
+
}>;
|
|
29
|
+
|
|
30
|
+
export { applyRules as a, createBeforeRules as b, computeDeepDiff as c, createRules as d };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { A as AuthUser } from './firebase-auth-Ds5P0kXT.js';
|
|
2
|
+
import { D as DeepDiffReturn, k as RulesMap, f as CrudUpdateRuleContext } from './types-XRjVKN9r.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Computes a typed diff between the existing document (`before`) and the incoming partial changes (`changesInput`).
|
|
6
|
+
* Only fields whose values have actually changed (via structural equality) are included in `changes`.
|
|
7
|
+
*/
|
|
8
|
+
declare function computeDeepDiff<T extends Record<string, unknown>>(before: T, changesInput: Partial<T>): DeepDiffReturn<T>;
|
|
9
|
+
/**
|
|
10
|
+
* Helper to define field-by-field before-rules with strong typing.
|
|
11
|
+
*/
|
|
12
|
+
declare function createRules<T>(rules: RulesMap<T>): RulesMap<T>;
|
|
13
|
+
/**
|
|
14
|
+
* Executes a map of field rules against a computed diff.
|
|
15
|
+
* Evaluates rules only for the keys that changed in `diff.changes`.
|
|
16
|
+
* Returns `{ allowed: true }` if all rules pass, or `{ allowed: false, reason: rule.description }` on the first failure.
|
|
17
|
+
*/
|
|
18
|
+
declare function applyRules<T>(diff: DeepDiffReturn<T>, rulesMap: RulesMap<T>, user?: AuthUser): Promise<{
|
|
19
|
+
allowed: boolean;
|
|
20
|
+
reason?: string;
|
|
21
|
+
}>;
|
|
22
|
+
/**
|
|
23
|
+
* Wraps a rules map into a standard `rules.update` handler for `createCrudServer`.
|
|
24
|
+
*/
|
|
25
|
+
declare function createBeforeRules<T>(rulesMap: RulesMap<T>): (ctx: CrudUpdateRuleContext<T>) => Promise<{
|
|
26
|
+
allowed: boolean;
|
|
27
|
+
reason?: string;
|
|
28
|
+
}>;
|
|
29
|
+
|
|
30
|
+
export { applyRules as a, createBeforeRules as b, computeDeepDiff as c, createRules as d };
|
|
@@ -1,10 +1,406 @@
|
|
|
1
|
-
import 'zod';
|
|
2
|
-
import 'firebase-functions/v2/https';
|
|
3
|
-
import '../../types-
|
|
4
|
-
|
|
5
|
-
export {
|
|
6
|
-
import '../../types-
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { HttpsOptions } from 'firebase-functions/v2/https';
|
|
3
|
+
import { C as ConfiguredRepository, l as RepositoryConfig, m as RelationalFieldMeta, n as FieldPath, F as FieldRole, S as SecurityHeadersOptions } from '../../types-B2e-T8EM.cjs';
|
|
4
|
+
import { b as AuthExtension, M as Middleware } from '../../firebase-auth-Ds5P0kXT.cjs';
|
|
5
|
+
export { h as MiniRouter, R as RouteHandler } from '../../firebase-auth-Ds5P0kXT.cjs';
|
|
6
|
+
import '../../types-BIZz3sxN.cjs';
|
|
7
7
|
import 'hono';
|
|
8
8
|
import '../../read-CTWZjxyh.cjs';
|
|
9
9
|
import 'firebase-admin/firestore';
|
|
10
10
|
import 'firebase-functions/v2/firestore';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* HTTP route handlers for the admin ORM server.
|
|
14
|
+
* Each handler corresponds to a URL pattern registered in the router.
|
|
15
|
+
*
|
|
16
|
+
* Routes:
|
|
17
|
+
* GET / → dashboard GET /:repoName → document list (paginated)
|
|
18
|
+
* GET /:repoName/create → create form
|
|
19
|
+
* POST /:repoName/create → submit create
|
|
20
|
+
* GET /:repoName/:id/edit → edit form (pre-filled)
|
|
21
|
+
* POST /:repoName/:id/edit → submit update
|
|
22
|
+
* POST /:repoName/:id/delete → delete document
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
interface AdminRepoEntry {
|
|
26
|
+
name: string;
|
|
27
|
+
path: string;
|
|
28
|
+
repo: ConfiguredRepository<RepositoryConfig<any, any, any, any, any, any, any, any, any, any, any>>;
|
|
29
|
+
schema: z.ZodObject<any>;
|
|
30
|
+
/** document key field name (default: "docId") */
|
|
31
|
+
documentKey?: string;
|
|
32
|
+
/** Field name that stores the full Firestore document path (e.g. "documentPath") */
|
|
33
|
+
pathKey?: string;
|
|
34
|
+
/** Whether this repo is a collection group (subcollection) */
|
|
35
|
+
isGroup?: boolean;
|
|
36
|
+
/** Parent key field names needed to build a subcollection document ref (auto-detected from refCb) */
|
|
37
|
+
parentKeys?: string[];
|
|
38
|
+
/** Field name for the creation timestamp (auto-set on create) */
|
|
39
|
+
createdKey?: string;
|
|
40
|
+
/** List of columns to display in the table (defaults to schema keys) */
|
|
41
|
+
listColumns?: string[];
|
|
42
|
+
/** Page size for list view (default: 25) */
|
|
43
|
+
pageSize?: number;
|
|
44
|
+
/** Fields exposed in the filter bar (defaults to all schema keys) */
|
|
45
|
+
filterableFields?: string[];
|
|
46
|
+
/** Fields shown in the edit form (defaults to all schema fields if unset) */
|
|
47
|
+
mutableFields?: string[];
|
|
48
|
+
/** Fields shown in the create form (defaults to all schema fields if unset) */
|
|
49
|
+
createFields?: string[];
|
|
50
|
+
/** Whether delete is allowed (default: false) */
|
|
51
|
+
allowDelete?: boolean;
|
|
52
|
+
/**
|
|
53
|
+
* Fields that link to another repository.
|
|
54
|
+
* Populated automatically from the repo's relationalKeys.
|
|
55
|
+
*/
|
|
56
|
+
relationalMeta?: RelationalFieldMeta[];
|
|
57
|
+
/**
|
|
58
|
+
* Auto-detected from `repo.history`. When true, the admin renders an
|
|
59
|
+
* extra "History" button on each row and exposes a dedicated route at
|
|
60
|
+
* `GET /:repoName/:id/history` that lists the change-log entries.
|
|
61
|
+
*/
|
|
62
|
+
historyEnabled?: boolean;
|
|
63
|
+
/** Subcollection name used to store history docs (display only). */
|
|
64
|
+
historySubcollection?: string;
|
|
65
|
+
}
|
|
66
|
+
type RepoRegistry = Record<string, AdminRepoEntry>;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* @module servers/admin
|
|
70
|
+
*
|
|
71
|
+
* Creates a static ORM admin interface served as a Firebase HTTPS function.
|
|
72
|
+
*
|
|
73
|
+
* Features:
|
|
74
|
+
* - Dashboard listing all registered repositories
|
|
75
|
+
* - Document list with cursor-based pagination
|
|
76
|
+
* - Create / Edit / Delete forms generated from Zod schemas
|
|
77
|
+
* - Forms map **exactly** to the repository model type
|
|
78
|
+
* - Zero JavaScript framework — plain HTML + inline CSS + vanilla JS
|
|
79
|
+
* - Body parsing for `application/x-www-form-urlencoded` (default HTML forms)
|
|
80
|
+
* and `application/json` (API clients)
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```ts
|
|
84
|
+
* import * as functions from "firebase-functions";
|
|
85
|
+
* import { z } from "zod";
|
|
86
|
+
* import { createAdminServer } from "@lpdjs/firestore-repo-service/servers/admin";
|
|
87
|
+
*
|
|
88
|
+
* const postSchema = z.object({
|
|
89
|
+
* title: z.string().min(1),
|
|
90
|
+
* content: z.string(),
|
|
91
|
+
* status: z.enum(["draft", "published"]),
|
|
92
|
+
* authorId: z.string(),
|
|
93
|
+
* });
|
|
94
|
+
*
|
|
95
|
+
* export const adminApp = functions.https.onRequest(
|
|
96
|
+
* createAdminServer({
|
|
97
|
+
* basePath: "/admin",
|
|
98
|
+
* repos: {
|
|
99
|
+
* posts: { repo: repos.posts, schema: postSchema, path: "posts" },
|
|
100
|
+
* },
|
|
101
|
+
* })
|
|
102
|
+
* );
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Extracts the model type `T` from a `ConfiguredRepository`.
|
|
108
|
+
* Two-step inference so it survives intersection types
|
|
109
|
+
* (e.g. `RepositoryConfig<...> & { schema: ZodObject }` produced by
|
|
110
|
+
* `createRepositoryConfig(schema)`).
|
|
111
|
+
* @internal
|
|
112
|
+
*/
|
|
113
|
+
type RepoModelType<TRepo> = TRepo extends ConfiguredRepository<infer C> ? C extends RepositoryConfig<infer T, any, any, any, any, any, any, any, any, any, any> ? T : never : never;
|
|
114
|
+
/**
|
|
115
|
+
* Configuration for a single repository in the admin server.
|
|
116
|
+
*
|
|
117
|
+
* @template TRepo - The `ConfiguredRepository` type; used to derive typed field names.
|
|
118
|
+
*
|
|
119
|
+
* If the repository was created with `createRepositoryConfig(schema)(config)`,
|
|
120
|
+
* the `schema` field is optional — it is auto-detected from the repo.
|
|
121
|
+
* Otherwise, pass `schema` explicitly.
|
|
122
|
+
*
|
|
123
|
+
* @example
|
|
124
|
+
* ```ts
|
|
125
|
+
* posts: {
|
|
126
|
+
* repo: repos.posts,
|
|
127
|
+
* fieldsConfig: {
|
|
128
|
+
* title: ["create", "mutable", "filterable"],
|
|
129
|
+
* content: ["create", "mutable"],
|
|
130
|
+
* status: ["create", "filterable"],
|
|
131
|
+
* },
|
|
132
|
+
* allowDelete: true,
|
|
133
|
+
* }
|
|
134
|
+
* ```
|
|
135
|
+
*/
|
|
136
|
+
interface AdminRepoConfig<TRepo extends ConfiguredRepository<any> = ConfiguredRepository<any>> {
|
|
137
|
+
/** The configured repository instance. Drives type inference for all other fields. */
|
|
138
|
+
repo: TRepo;
|
|
139
|
+
/**
|
|
140
|
+
* Zod schema — optional when the repo was created with `createRepositoryConfig(schema)`.
|
|
141
|
+
* Pass explicitly for repos created with the legacy `createRepositoryConfig<T>()` form.
|
|
142
|
+
*/
|
|
143
|
+
schema?: z.ZodObject<any>;
|
|
144
|
+
/** Firestore collection path (for display only) */
|
|
145
|
+
path: string;
|
|
146
|
+
/** Key used to identify documents (default: "docId") */
|
|
147
|
+
documentKey?: string;
|
|
148
|
+
/** Columns to display in the list view (default: all schema keys) */
|
|
149
|
+
listColumns?: string[];
|
|
150
|
+
/** Number of documents per page in the list view (default: 25) */
|
|
151
|
+
pageSize?: number;
|
|
152
|
+
/**
|
|
153
|
+
* Per-field role configuration.
|
|
154
|
+
* Each key is a model field (with autocomplete); the value is an array of roles.
|
|
155
|
+
*
|
|
156
|
+
* Roles:
|
|
157
|
+
* - `"create"` — field is shown in the create form
|
|
158
|
+
* - `"mutable"` — field is shown in the edit form
|
|
159
|
+
* - `"filterable"` — field is shown in the filter bar
|
|
160
|
+
*
|
|
161
|
+
* If omitted, all schema fields are shown in all contexts.
|
|
162
|
+
*
|
|
163
|
+
* @example
|
|
164
|
+
* ```ts
|
|
165
|
+
* fieldsConfig: {
|
|
166
|
+
* title: ["create", "mutable", "filterable"],
|
|
167
|
+
* content: ["create", "mutable"],
|
|
168
|
+
* status: ["create", "filterable"],
|
|
169
|
+
* }
|
|
170
|
+
* ```
|
|
171
|
+
*/
|
|
172
|
+
fieldsConfig?: Partial<Record<FieldPath<RepoModelType<TRepo>>, readonly FieldRole[]>>;
|
|
173
|
+
/**
|
|
174
|
+
* Whether to show the delete button in the list view.
|
|
175
|
+
* Default: false — delete is disabled unless explicitly set to true.
|
|
176
|
+
*/
|
|
177
|
+
allowDelete?: boolean;
|
|
178
|
+
/**
|
|
179
|
+
* Relational action columns appended to the list table.
|
|
180
|
+
* Each entry adds a dedicated button that navigates to the linked repository.
|
|
181
|
+
*
|
|
182
|
+
* - **type "one"** (e.g. `userId` on a post) → button links to the target
|
|
183
|
+
* document edit page: `/{targetRepo}/{value}/edit`
|
|
184
|
+
* - **type "many"** (e.g. `docId` on a user) → button links to the target
|
|
185
|
+
* repo list filtered by value: `/{targetRepo}?fv_{targetKey}={value}`
|
|
186
|
+
*
|
|
187
|
+
* @example
|
|
188
|
+
* ```ts
|
|
189
|
+
* users: {
|
|
190
|
+
* repo: repos.users,
|
|
191
|
+
* relationalFields: [
|
|
192
|
+
* { key: "docId", column: "Posts" }, // many → list of posts by this user
|
|
193
|
+
* ]
|
|
194
|
+
* }
|
|
195
|
+
* posts: {
|
|
196
|
+
* repo: repos.posts,
|
|
197
|
+
* relationalFields: [
|
|
198
|
+
* { key: "userId", column: "Author" }, // one → edit page of the user
|
|
199
|
+
* ]
|
|
200
|
+
* }
|
|
201
|
+
* ```
|
|
202
|
+
*/
|
|
203
|
+
relationalFields?: {
|
|
204
|
+
/** The relation name defined in buildRepositoryRelations (or the model property name) */
|
|
205
|
+
key: string;
|
|
206
|
+
column: string;
|
|
207
|
+
}[];
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* HTTP Basic Auth configuration.
|
|
211
|
+
* The browser will show a native login dialog.
|
|
212
|
+
*/
|
|
213
|
+
interface BasicAuthConfig {
|
|
214
|
+
type: "basic";
|
|
215
|
+
/** Realm displayed in the browser login dialog */
|
|
216
|
+
realm?: string;
|
|
217
|
+
username: string;
|
|
218
|
+
password: string;
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Options for `createAdminServer`.
|
|
222
|
+
*
|
|
223
|
+
* Made generic so TypeScript can infer the model type of each repo entry
|
|
224
|
+
* and provide autocomplete + type-checking on `fieldsConfig`.
|
|
225
|
+
*
|
|
226
|
+
* @template TRepos - Shape of the repos map (inferred automatically at the call site)
|
|
227
|
+
*/
|
|
228
|
+
interface AdminServerOptions<TRepos extends Record<string, ConfiguredRepository<any>> = Record<string, ConfiguredRepository<any>>> {
|
|
229
|
+
/**
|
|
230
|
+
* Base URL path of the function (e.g. "/admin").
|
|
231
|
+
* Must match the path where the Firebase Function is mounted.
|
|
232
|
+
* Default: "/"
|
|
233
|
+
*/
|
|
234
|
+
basePath?: string;
|
|
235
|
+
/**
|
|
236
|
+
* Repository entries keyed by a display name.
|
|
237
|
+
* TypeScript infers the model type from each `repo` field,
|
|
238
|
+
* so `fieldsConfig` keys are typed to that model's field paths.
|
|
239
|
+
*
|
|
240
|
+
* @example
|
|
241
|
+
* ```ts
|
|
242
|
+
* repos: {
|
|
243
|
+
* posts: {
|
|
244
|
+
* repo: repos.posts,
|
|
245
|
+
* fieldsConfig: {
|
|
246
|
+
* title: ["create", "mutable", "filterable"],
|
|
247
|
+
* content: ["create", "mutable"],
|
|
248
|
+
* status: ["create", "filterable"],
|
|
249
|
+
* },
|
|
250
|
+
* },
|
|
251
|
+
* }
|
|
252
|
+
* ```
|
|
253
|
+
*/
|
|
254
|
+
repos: {
|
|
255
|
+
[K in keyof TRepos]: AdminRepoConfig<TRepos[K]>;
|
|
256
|
+
};
|
|
257
|
+
/** Whether to parse URL-encoded bodies. Default: true. */
|
|
258
|
+
parseBody?: boolean;
|
|
259
|
+
/**
|
|
260
|
+
* Baseline security response headers (CSP, X-Frame-Options, nosniff,
|
|
261
|
+
* Referrer-Policy, Cache-Control). Enabled by default with HTML-safe
|
|
262
|
+
* defaults (see {@link DEFAULT_HTML_CSP}). Pass an options object to
|
|
263
|
+
* customise, or `false` to disable entirely (issues #12, #13).
|
|
264
|
+
*/
|
|
265
|
+
securityHeaders?: SecurityHeadersOptions | false;
|
|
266
|
+
/**
|
|
267
|
+
* Authentication guard executed before every request.
|
|
268
|
+
* - Pass an {@link AuthExtension} (e.g. result of `firebaseAuth({...})`) to
|
|
269
|
+
* wire Firebase Auth with a bundled `/__login` page and session cookie.
|
|
270
|
+
* - Pass a `BasicAuthConfig` to enable HTTP Basic Auth.
|
|
271
|
+
* - Pass a `Middleware` function for fully custom auth logic.
|
|
272
|
+
*/
|
|
273
|
+
auth?: AuthExtension | BasicAuthConfig | Middleware;
|
|
274
|
+
/**
|
|
275
|
+
* Additional middleware functions executed after auth, before route handlers.
|
|
276
|
+
*/
|
|
277
|
+
middleware?: Middleware[];
|
|
278
|
+
/**
|
|
279
|
+
* Options forwarded to `onRequest()` from `firebase-functions/v2/https`.
|
|
280
|
+
* Stored on the returned handler as `.httpsOptions` for easy access.
|
|
281
|
+
*
|
|
282
|
+
* @example
|
|
283
|
+
* ```ts
|
|
284
|
+
* const handler = createAdminServer({ httpsOptions: { invoker: "public" }, ... });
|
|
285
|
+
* export const admin = onRequest(handler.httpsOptions!, handler);
|
|
286
|
+
* ```
|
|
287
|
+
*/
|
|
288
|
+
httpsOptions?: HttpsOptions;
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* Creates an Express-compatible request handler for the admin ORM UI.
|
|
292
|
+
* Generates a complete admin interface with dashboard, lists, and CRUD forms.
|
|
293
|
+
*
|
|
294
|
+
* @template TRepos - Shape of the repos map (inferred automatically)
|
|
295
|
+
* @param options - Admin server configuration
|
|
296
|
+
* @returns Express-compatible request handler for Firebase Functions
|
|
297
|
+
*
|
|
298
|
+
* @example
|
|
299
|
+
* ```typescript
|
|
300
|
+
* // Basic admin server
|
|
301
|
+
* import { onRequest } from "firebase-functions/https";
|
|
302
|
+
* import { createAdminServer } from "@lpdjs/firestore-repo-service/servers/admin";
|
|
303
|
+
*
|
|
304
|
+
* export const admin = onRequest(
|
|
305
|
+
* createAdminServer({
|
|
306
|
+
* basePath: "/admin",
|
|
307
|
+
* repos: {
|
|
308
|
+
* users: {
|
|
309
|
+
* repo: repos.users,
|
|
310
|
+
* path: "users",
|
|
311
|
+
* },
|
|
312
|
+
* posts: {
|
|
313
|
+
* repo: repos.posts,
|
|
314
|
+
* path: "posts",
|
|
315
|
+
* },
|
|
316
|
+
* },
|
|
317
|
+
* })
|
|
318
|
+
* );
|
|
319
|
+
*
|
|
320
|
+
* // With HTTP Basic Auth
|
|
321
|
+
* export const admin = onRequest(
|
|
322
|
+
* createAdminServer({
|
|
323
|
+
* basePath: "/admin",
|
|
324
|
+
* auth: {
|
|
325
|
+
* type: "basic",
|
|
326
|
+
* realm: "Admin Area",
|
|
327
|
+
* username: "admin",
|
|
328
|
+
* password: process.env.ADMIN_PASSWORD!,
|
|
329
|
+
* },
|
|
330
|
+
* repos: { ... },
|
|
331
|
+
* })
|
|
332
|
+
* );
|
|
333
|
+
*
|
|
334
|
+
* // With custom auth middleware
|
|
335
|
+
* export const admin = onRequest(
|
|
336
|
+
* createAdminServer({
|
|
337
|
+
* auth: async (req, res, next) => {
|
|
338
|
+
* const token = req.headers?.authorization?.replace("Bearer ", "");
|
|
339
|
+
* if (!token || !(await verifyToken(token))) {
|
|
340
|
+
* res.status(401).send("Unauthorized");
|
|
341
|
+
* return;
|
|
342
|
+
* }
|
|
343
|
+
* next();
|
|
344
|
+
* },
|
|
345
|
+
* repos: { ... },
|
|
346
|
+
* })
|
|
347
|
+
* );
|
|
348
|
+
*
|
|
349
|
+
* // Full configuration with all options
|
|
350
|
+
* export const admin = onRequest(
|
|
351
|
+
* createAdminServer({
|
|
352
|
+
* basePath: "/admin",
|
|
353
|
+
* parseBody: true, // Parse URL-encoded bodies
|
|
354
|
+
* auth: { type: "basic", username: "admin", password: "secret" },
|
|
355
|
+
* middleware: [loggingMiddleware], // Additional middleware
|
|
356
|
+
* repos: {
|
|
357
|
+
* posts: {
|
|
358
|
+
* repo: repos.posts,
|
|
359
|
+
* path: "posts",
|
|
360
|
+
* documentKey: "docId", // Field used as document ID
|
|
361
|
+
* listColumns: ["title", "status", "createdAt"], // Columns in list
|
|
362
|
+
* pageSize: 25, // Items per page in list
|
|
363
|
+
* fieldsConfig: {
|
|
364
|
+
* title: ["create", "mutable", "filterable"],
|
|
365
|
+
* content: ["create", "mutable"],
|
|
366
|
+
* status: ["create", "mutable", "filterable"],
|
|
367
|
+
* userId: ["filterable"],
|
|
368
|
+
* },
|
|
369
|
+
* allowDelete: true, // Enable delete button
|
|
370
|
+
* relationalFields: [ // Relation navigation buttons
|
|
371
|
+
* { key: "userId", column: "Author" }, // Link to user
|
|
372
|
+
* { key: "docId", column: "Comments" }, // Link to comments list
|
|
373
|
+
* ],
|
|
374
|
+
* },
|
|
375
|
+
* users: {
|
|
376
|
+
* repo: repos.users,
|
|
377
|
+
* path: "users",
|
|
378
|
+
* fieldsConfig: {
|
|
379
|
+
* name: ["create", "mutable"],
|
|
380
|
+
* email: ["create", "mutable", "filterable"],
|
|
381
|
+
* isActive: ["mutable", "filterable"],
|
|
382
|
+
* },
|
|
383
|
+
* allowDelete: false,
|
|
384
|
+
* relationalFields: [
|
|
385
|
+
* { key: "docId", column: "Posts" },
|
|
386
|
+
* ],
|
|
387
|
+
* },
|
|
388
|
+
* },
|
|
389
|
+
* })
|
|
390
|
+
* );
|
|
391
|
+
*
|
|
392
|
+
* // Routes generated automatically:
|
|
393
|
+
* // GET /admin/ → Dashboard (list of repos)
|
|
394
|
+
* // GET /admin/:repo → Document list with pagination
|
|
395
|
+
* // GET /admin/:repo/create → Create form
|
|
396
|
+
* // POST /admin/:repo/create → Submit create
|
|
397
|
+
* // GET /admin/:repo/:id/edit → Edit form
|
|
398
|
+
* // POST /admin/:repo/:id/edit → Submit edit
|
|
399
|
+
* // POST /admin/:repo/:id/delete → Delete document
|
|
400
|
+
* ```
|
|
401
|
+
*/
|
|
402
|
+
declare function createAdminServer<TRepos extends Record<string, ConfiguredRepository<any>>>(options: AdminServerOptions<TRepos>): ((req: any, res: any) => Promise<void>) & {
|
|
403
|
+
httpsOptions?: HttpsOptions;
|
|
404
|
+
};
|
|
405
|
+
|
|
406
|
+
export { type AdminRepoConfig, type AdminRepoEntry, type AdminServerOptions, type BasicAuthConfig, Middleware, type RepoRegistry, createAdminServer };
|