@lpdjs/firestore-repo-service 2.6.32 → 2.6.34

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.
Files changed (40) hide show
  1. package/README.md +14 -373
  2. package/dist/{create-servers-nXXPnlJt.d.ts → create-servers-DDHHOqOW.d.ts} +3 -3
  3. package/dist/{create-servers-BoJrRNI2.d.cts → create-servers-DmQNkgM_.d.cts} +3 -3
  4. package/dist/{index-DZnXSClM.d.cts → index-bkOZzCvS.d.cts} +1 -1
  5. package/dist/{index-BE-Iuye5.d.ts → index-qSs2G4CW.d.ts} +1 -1
  6. package/dist/index.cjs +94 -94
  7. package/dist/index.cjs.map +1 -1
  8. package/dist/index.d.cts +7 -5
  9. package/dist/index.d.ts +7 -5
  10. package/dist/index.js +94 -94
  11. package/dist/index.js.map +1 -1
  12. package/dist/llms-full.txt +43 -21
  13. package/dist/llms.txt +1 -1
  14. package/dist/{openapi-BHW1HViI.d.cts → openapi-B_2O8iJW.d.cts} +1 -1
  15. package/dist/{openapi-BupSdudk.d.ts → openapi-DBi6hg2V.d.ts} +1 -1
  16. package/dist/servers/admin/index.d.cts +4 -2
  17. package/dist/servers/admin/index.d.ts +4 -2
  18. package/dist/servers/crud/index.cjs +7 -7
  19. package/dist/servers/crud/index.cjs.map +1 -1
  20. package/dist/servers/crud/index.d.cts +7 -171
  21. package/dist/servers/crud/index.d.ts +7 -171
  22. package/dist/servers/crud/index.js +7 -7
  23. package/dist/servers/crud/index.js.map +1 -1
  24. package/dist/servers/hono/index.d.cts +5 -858
  25. package/dist/servers/hono/index.d.ts +5 -858
  26. package/dist/servers/index.cjs +98 -98
  27. package/dist/servers/index.cjs.map +1 -1
  28. package/dist/servers/index.d.cts +7 -5
  29. package/dist/servers/index.d.ts +7 -5
  30. package/dist/servers/index.js +98 -98
  31. package/dist/servers/index.js.map +1 -1
  32. package/dist/sync/index.cjs +7 -7
  33. package/dist/sync/index.cjs.map +1 -1
  34. package/dist/sync/index.js +7 -7
  35. package/dist/sync/index.js.map +1 -1
  36. package/dist/types-B4dMmfet.d.cts +860 -0
  37. package/dist/{types-CpkQQn4b.d.cts → types-BRS75oO7.d.cts} +10 -38
  38. package/dist/{types-BcT92G9B.d.ts → types-DnycnK_G.d.ts} +10 -38
  39. package/dist/types-DyKJJ9UQ.d.ts +860 -0
  40. package/package.json +1 -1
package/README.md CHANGED
@@ -185,6 +185,8 @@ batch.update(repos.users.documentRef("u2"), { age: 25 });
185
185
  batch.delete(repos.users.documentRef("u3"));
186
186
  await batch.commit();
187
187
 
188
+ // Note: A `POST /:repoName/batch` endpoint is also exposed automatically on the CRUD server!
189
+
188
190
  // Bulk (auto-split into batches of 500)
189
191
  await repos.users.bulk.set([
190
192
  { docRef: repos.users.documentRef("u1"), data: { ... }, merge: true },
@@ -453,14 +455,24 @@ export const admin = servers.admin({
453
455
  },
454
456
  });
455
457
 
456
- // CRUD REST API
458
+ // CRUD REST API (Hono-based)
457
459
  export const api = servers.crud({
458
460
  basePath: "/api",
461
+ middlewares: [
462
+ async (c, next) => {
463
+ // Add your Hono middleware logic here (e.g. Auth)
464
+ await next();
465
+ }
466
+ ],
459
467
  repos: {
460
468
  posts: { path: "posts", allowDelete: true },
461
469
  users: { path: "users" },
462
470
  },
463
- openapi: { title: "My API", version: "1.0.0" },
471
+ openapi: {
472
+ title: "My API",
473
+ version: "1.0.0",
474
+ // Optional: Protect /docs and /openapi.json using firebaseDocsAuth or custom middleware
475
+ },
464
476
  });
465
477
 
466
478
  // Firestore → BigQuery sync (triggers + worker + admin Cloud Functions)
@@ -537,374 +549,3 @@ Firestore emulator runs on `localhost:8080`, UI on `http://localhost:4000`.
537
549
  ## License
538
550
 
539
551
  MIT
540
-
541
- ## Installation
542
-
543
- ```bash
544
- npm install @lpdjs/firestore-repo-service firebase-admin
545
- ```
546
-
547
- ## Quick start
548
-
549
- ### Define your models
550
-
551
- ```typescript
552
- interface UserModel {
553
- docId: string;
554
- email: string;
555
- name: string;
556
- age: number;
557
- isActive: boolean;
558
- }
559
-
560
- interface PostModel {
561
- docId: string;
562
- userId: string;
563
- title: string;
564
- status: "draft" | "published";
565
- }
566
- ```
567
-
568
- ### Create the repository mapping
569
-
570
- ```typescript
571
- import {
572
- createRepositoryConfig,
573
- buildRepositoryRelations,
574
- createRepositoryMapping,
575
- } from "@lpdjs/firestore-repo-service";
576
- import { doc } from "firebase/firestore";
577
- import type { Firestore } from "firebase/firestore";
578
-
579
- const repositoryMapping = {
580
- users: createRepositoryConfig<UserModel>()({
581
- path: "users",
582
- isGroup: false,
583
- foreignKeys: ["docId", "email"] as const,
584
- queryKeys: ["name", "isActive"] as const,
585
- refCb: (db: Firestore, docId: string) => doc(db, "users", docId),
586
- }),
587
- posts: createRepositoryConfig<PostModel>()({
588
- path: "posts",
589
- isGroup: false,
590
- foreignKeys: ["docId", "userId"] as const,
591
- queryKeys: ["status"] as const,
592
- refCb: (db: Firestore, docId: string) => doc(db, "posts", docId),
593
- }),
594
- };
595
-
596
- // Optional: add relations
597
- const mappingWithRelations = buildRepositoryRelations(repositoryMapping, {
598
- posts: {
599
- userId: { repo: "users", key: "docId", type: "one" as const },
600
- },
601
- });
602
-
603
- export const repos = createRepositoryMapping(() => db, mappingWithRelations);
604
- ```
605
-
606
- ### Use the repositories
607
-
608
- ```typescript
609
- // Fetch a single document
610
- const user = await repos.users.get.byDocId("user123");
611
- const userByEmail = await repos.users.get.byEmail("john@example.com");
612
-
613
- // Query multiple documents
614
- const activeUsers = await repos.users.query.byIsActive(true);
615
-
616
- // With query options
617
- const results = await repos.users.query.byIsActive(true, {
618
- where: [["age", ">=", 18]],
619
- orderBy: [{ field: "name", direction: "asc" }],
620
- limit: 50,
621
- });
622
-
623
- // Populate a relation
624
- const post = await repos.posts.get.byDocId("post123");
625
- if (post) {
626
- const postWithUser = await repos.posts.populate(post, "userId");
627
- console.log(postWithUser.populated.users?.name); // type-safe
628
- }
629
-
630
- // Update
631
- const updated = await repos.users.update("user123", {
632
- name: "New name",
633
- age: 31,
634
- });
635
- ```
636
-
637
- ## API reference
638
-
639
- ### `createRepositoryConfig()`
640
-
641
- | Option | Description |
642
- | ------------- | --------------------------------------------------- |
643
- | `path` | Collection path in Firestore |
644
- | `isGroup` | `true` for collection group, `false` for simple |
645
- | `foreignKeys` | Keys for `get.by*` methods (single document lookup) |
646
- | `queryKeys` | Keys for `query.by*` methods (multi-document query) |
647
- | `refCb` | Function that returns the document reference |
648
-
649
- **Sub-collection example:**
650
-
651
- ```typescript
652
- comments: createRepositoryConfig<CommentModel>()({
653
- path: "comments",
654
- isGroup: true,
655
- foreignKeys: ["docId"] as const,
656
- queryKeys: ["postId", "userId"] as const,
657
- refCb: (db, postId, commentId) =>
658
- doc(db, "posts", postId, "comments", commentId),
659
- });
660
- ```
661
-
662
- ### Query options
663
-
664
- ```typescript
665
- interface QueryOptions<T> {
666
- where?: [keyof T, WhereFilterOp, any][]; // AND conditions
667
- orWhere?: [keyof T, WhereFilterOp, any][][]; // OR conditions
668
- orderBy?: { field: keyof T; direction?: "asc" | "desc" }[];
669
- limit?: number;
670
- offset?: number;
671
- select?: (keyof T)[];
672
- startAt?: DocumentSnapshot | any[];
673
- startAfter?: DocumentSnapshot | any[];
674
- endAt?: DocumentSnapshot | any[];
675
- endBefore?: DocumentSnapshot | any[];
676
- }
677
- ```
678
-
679
- ### CRUD
680
-
681
- ```typescript
682
- // Create (auto-generated ID)
683
- const newUser = await repos.users.create({ email: "...", name: "...", age: 25, isActive: true });
684
-
685
- // Set (create or replace)
686
- await repos.users.set("user123", { ... });
687
-
688
- // Set with merge
689
- await repos.users.set("user123", { age: 31 }, { merge: true });
690
-
691
- // Update (partial)
692
- await repos.users.update("user123", { age: 32 });
693
-
694
- // Delete
695
- await repos.users.delete("user123");
696
-
697
- // Document ref
698
- const ref = repos.users.documentRef("user123");
699
-
700
- // Raw collection ref
701
- const colRef = repos.users.ref;
702
- ```
703
-
704
- ### Batch & Bulk
705
-
706
- ```typescript
707
- // Atomic batch (max 500 operations)
708
- const batch = repos.users.batch.create();
709
- batch.set(repos.users.documentRef("u1"), { ... });
710
- batch.update(repos.users.documentRef("u2"), { age: 25 });
711
- batch.delete(repos.users.documentRef("u3"));
712
- await batch.commit();
713
-
714
- // Bulk (auto-split into batches of 500)
715
- await repos.users.bulk.set([
716
- { docRef: repos.users.documentRef("u1"), data: { ... }, merge: true },
717
- ]);
718
- await repos.users.bulk.update([...]);
719
- await repos.users.bulk.delete([...]);
720
- ```
721
-
722
- ### Real-time listeners
723
-
724
- ```typescript
725
- const unsubscribe = repos.users.query.onSnapshot(
726
- { where: [["isActive", "==", true]] },
727
- (users) => console.log(users),
728
- );
729
- unsubscribe();
730
- ```
731
-
732
- ### Pagination
733
-
734
- ```typescript
735
- const firstPage = await repos.users.query.by({
736
- orderBy: [{ field: "createdAt", direction: "desc" }],
737
- limit: 10,
738
- });
739
-
740
- const nextPage = await repos.users.query.by({
741
- orderBy: [{ field: "createdAt", direction: "desc" }],
742
- startAfter: firstPage[firstPage.length - 1],
743
- limit: 10,
744
- });
745
-
746
- // Paginate with relations
747
- const page = await repos.posts.query.paginate({
748
- pageSize: 10,
749
- include: [{ relation: "userId", select: ["docId", "name", "email"] }],
750
- });
751
- ```
752
-
753
- ### Aggregations
754
-
755
- ```typescript
756
- import { count, sum, average } from "@lpdjs/firestore-repo-service";
757
-
758
- const activeCount = await repos.users.aggregate.count({
759
- where: [["isActive", "==", true]],
760
- });
761
- const totalViews = await repos.posts.aggregate.sum("views");
762
- const avgAge = await repos.users.aggregate.average("age");
763
- ```
764
-
765
- ### Transactions
766
-
767
- ```typescript
768
- const result = await repos.users.transaction.run(async (txn) => {
769
- const user = await txn.get(repos.users.documentRef("user123"));
770
- if (user.exists()) {
771
- txn.update(repos.users.documentRef("user123"), {
772
- age: user.data().age + 1,
773
- });
774
- }
775
- return { success: true };
776
- });
777
- ```
778
-
779
- ### OR queries
780
-
781
- ```typescript
782
- // (status = 'active' AND age >= 18) OR (status = 'pending' AND verified = true)
783
- const users = await repos.users.query.by({
784
- orWhere: [
785
- [
786
- ["status", "==", "active"],
787
- ["age", ">=", 18],
788
- ],
789
- [
790
- ["status", "==", "pending"],
791
- ["verified", "==", true],
792
- ],
793
- ],
794
- });
795
- ```
796
-
797
- ## Servers (admin UI · CRUD REST · Firestore → SQL sync)
798
-
799
- A single unified factory binds all servers to your repository registry. Per-repo `repo: …` is no longer needed — the registry key drives both the runtime binding and the inferred model type for `fieldsConfig` autocomplete.
800
-
801
- ```typescript
802
- import { createServers } from "@lpdjs/firestore-repo-service";
803
- import { onRequest } from "firebase-functions/v2/https";
804
- import { BigQueryAdapter } from "@lpdjs/firestore-repo-service/sync/bigquery";
805
- import { BigQuery } from "@google-cloud/bigquery";
806
- import { PubSub } from "@google-cloud/pubsub";
807
- import * as firestoreTriggers from "firebase-functions/v2/firestore";
808
- import * as pubsubHandler from "firebase-functions/v2/pubsub";
809
-
810
- const servers = createServers(repos, {
811
- onRequest,
812
- httpsOptions: { invoker: "public" },
813
- });
814
-
815
- // Admin UI — repo auto-injected from the key, fieldsConfig typed against the model
816
- export const admin = servers.admin({
817
- basePath: "/admin",
818
- auth: { type: "basic", username: "admin", password: "secret" },
819
- repos: {
820
- posts: {
821
- path: "posts",
822
- fieldsConfig: { title: ["create", "mutable"], status: ["filterable"] },
823
- allowDelete: true,
824
- },
825
- users: { path: "users" },
826
- },
827
- });
828
-
829
- // CRUD REST API
830
- export const api = servers.crud({
831
- basePath: "/api",
832
- repos: {
833
- posts: { path: "posts", allowDelete: true },
834
- users: { path: "users" },
835
- },
836
- openapi: { title: "My API", version: "1.0.0" },
837
- });
838
-
839
- // Firestore → BigQuery sync (triggers + worker + admin Cloud Functions)
840
- export const { functions } = servers.sync({
841
- deps: { firestoreTriggers, pubsubHandler, pubsub: new PubSub() },
842
- adapter: new BigQueryAdapter({
843
- bigquery: new BigQuery({ projectId: "my-project" }),
844
- projectId: "my-project",
845
- datasetId: "firestore_sync",
846
- maxStaleness: "INTERVAL 15 MINUTE",
847
- }),
848
- topicPrefix: "firestore-sync",
849
- autoMigrate: true,
850
- admin: {
851
- auth: { type: "basic", username: "admin", password: "secret" },
852
- featuresFlag: { healthCheck: true, manualSync: true, configCheck: true },
853
- },
854
- repos: {
855
- users: { tableName: "users", columnMap: { docId: "user_id" } },
856
- posts: { columnMap: { docId: "post_id" } },
857
- comments: { triggerPath: "posts/{postId}/comments/{docId}" },
858
- },
859
- });
860
-
861
- // Spread Cloud Functions into your exports
862
- export const {
863
- users_onCreate,
864
- users_onUpdate,
865
- users_onDelete,
866
- sync_users,
867
- posts_onCreate,
868
- posts_onUpdate,
869
- posts_onDelete,
870
- sync_posts,
871
- comments_onCreate,
872
- comments_onUpdate,
873
- comments_onDelete,
874
- sync_comments,
875
- adminsync,
876
- } = functions;
877
- ```
878
-
879
- When `onRequest` is passed to `createServers`, `servers.admin()` and `servers.crud()` return ready-to-export Cloud Functions. Without it, they return raw HTTP handlers you can wrap yourself.
880
-
881
- The sync admin endpoint (`/`) exposes a UI for health checks, force-sync, queue inspection, and GCP config verification.
882
-
883
- For a custom SQL backend, implement the `SqlAdapter` interface:
884
-
885
- ```typescript
886
- import type { SqlAdapter } from "@lpdjs/firestore-repo-service/sync";
887
-
888
- class MyAdapter implements SqlAdapter {
889
- // tableExists, getTableColumns, createTable, upsertRows, deleteRows, executeRaw
890
- }
891
- ```
892
-
893
- Full sync documentation: [frs.lpdjs.fr/guide/sync](https://frs.lpdjs.fr/guide/sync)
894
-
895
- ## Testing
896
-
897
- ```bash
898
- # Run emulator + tests (watch mode)
899
- bun run test:watch
900
-
901
- # Two-terminal alternative
902
- bun run emulator # terminal 1
903
- bun run test # terminal 2
904
- ```
905
-
906
- Firestore emulator runs on `localhost:8080`, UI on `http://localhost:4000`.
907
-
908
- ## License
909
-
910
- MIT
@@ -1,11 +1,11 @@
1
1
  import { S as SyncQueue } from './queue-B4uwef6c.js';
2
2
  import { F as FirestoreSyncConfig, S as SyncEvent } from './types-DT7Nypd_.js';
3
- import { O as OpenAPIDocument } from './openapi-BupSdudk.js';
3
+ import { O as OpenAPIDocument } from './openapi-DBi6hg2V.js';
4
4
  import * as firebase_functions_v2_https from 'firebase-functions/v2/https';
5
5
  import { onRequest, HttpsOptions } from 'firebase-functions/v2/https';
6
6
  import { H as HistoryTriggersConfig } from './read-CTWZjxyh.js';
7
- import { C as ConfiguredRepository, a as CrudServerOptions, b as CrudRepoConfig } from './types-BcT92G9B.js';
8
- import { A as AdminServerOptions, a as AdminRepoConfig } from './index-BE-Iuye5.js';
7
+ import { C as ConfiguredRepository, a as CrudServerOptions, b as CrudRepoConfig } from './types-DnycnK_G.js';
8
+ import { A as AdminServerOptions, a as AdminRepoConfig } from './index-qSs2G4CW.js';
9
9
 
10
10
  /** Per-repo admin config with `repo` omitted (auto-bound from the registry key). */
11
11
  type BoundAdminRepoConfig<TRepo extends ConfiguredRepository<any>> = Omit<AdminRepoConfig<TRepo>, "repo">;
@@ -1,11 +1,11 @@
1
1
  import { S as SyncQueue } from './queue-CQyWCQBk.cjs';
2
2
  import { F as FirestoreSyncConfig, S as SyncEvent } from './types-BwH1C-wi.cjs';
3
- import { O as OpenAPIDocument } from './openapi-BHW1HViI.cjs';
3
+ import { O as OpenAPIDocument } from './openapi-B_2O8iJW.cjs';
4
4
  import * as firebase_functions_v2_https from 'firebase-functions/v2/https';
5
5
  import { onRequest, HttpsOptions } from 'firebase-functions/v2/https';
6
6
  import { H as HistoryTriggersConfig } from './read-CTWZjxyh.cjs';
7
- import { C as ConfiguredRepository, a as CrudServerOptions, b as CrudRepoConfig } from './types-CpkQQn4b.cjs';
8
- import { A as AdminServerOptions, a as AdminRepoConfig } from './index-DZnXSClM.cjs';
7
+ import { C as ConfiguredRepository, a as CrudServerOptions, b as CrudRepoConfig } from './types-BRS75oO7.cjs';
8
+ import { A as AdminServerOptions, a as AdminRepoConfig } from './index-bkOZzCvS.cjs';
9
9
 
10
10
  /** Per-repo admin config with `repo` omitted (auto-bound from the registry key). */
11
11
  type BoundAdminRepoConfig<TRepo extends ConfiguredRepository<any>> = Omit<AdminRepoConfig<TRepo>, "repo">;
@@ -1,6 +1,6 @@
1
1
  import { z } from 'zod';
2
2
  import { HttpsOptions } from 'firebase-functions/v2/https';
3
- import { C as ConfiguredRepository, f as RepositoryConfig, g as FieldPath, F as FieldRole, S as SecurityHeadersOptions } from './types-CpkQQn4b.cjs';
3
+ import { C as ConfiguredRepository, f as RepositoryConfig, g as FieldPath, F as FieldRole, S as SecurityHeadersOptions } from './types-BRS75oO7.cjs';
4
4
  import { A as AuthExtension, h as Middleware } from './firebase-auth-DZ8lb2Ec.cjs';
5
5
 
6
6
  interface PageOptions {
@@ -1,6 +1,6 @@
1
1
  import { z } from 'zod';
2
2
  import { HttpsOptions } from 'firebase-functions/v2/https';
3
- import { C as ConfiguredRepository, f as RepositoryConfig, g as FieldPath, F as FieldRole, S as SecurityHeadersOptions } from './types-BcT92G9B.js';
3
+ import { C as ConfiguredRepository, f as RepositoryConfig, g as FieldPath, F as FieldRole, S as SecurityHeadersOptions } from './types-DnycnK_G.js';
4
4
  import { A as AuthExtension, h as Middleware } from './firebase-auth-DZ8lb2Ec.js';
5
5
 
6
6
  interface PageOptions {