@mxpicture/gcp-functions 0.1.11 → 0.1.12

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.
@@ -14,11 +14,11 @@ class BaseApi {
14
14
  async get(p) {
15
15
  return this.store.get(p.id);
16
16
  }
17
- async query() {
18
- return this.store.query();
17
+ async query(payload) {
18
+ return this.store.query(payload?.filters);
19
19
  }
20
- async count() {
21
- return { count: await this.store.count() };
20
+ async count(payload) {
21
+ return { count: await this.store.count(payload?.filters) };
22
22
  }
23
23
  async exists(p) {
24
24
  return { exists: await this.store.exists(p.id) };
@@ -32,9 +32,9 @@ class BaseHttp {
32
32
  throw new https_1.HttpsError("not-found", `Id ${req.keys?.id} not found`);
33
33
  }
34
34
  }
35
- async query() {
35
+ async query(req) {
36
36
  try {
37
- return this.api.query();
37
+ return this.api.query(req.content);
38
38
  }
39
39
  catch (error) {
40
40
  if (error instanceof https_1.HttpsError)
@@ -42,9 +42,9 @@ class BaseHttp {
42
42
  throw new https_1.HttpsError("unknown", "Unknown error");
43
43
  }
44
44
  }
45
- async count() {
45
+ async count(req) {
46
46
  try {
47
- return this.api.count();
47
+ return this.api.count(req.content);
48
48
  }
49
49
  catch (error) {
50
50
  if (error instanceof https_1.HttpsError)
@@ -63,6 +63,22 @@ class Store {
63
63
  updateTime: doc.updateTime.toDate(),
64
64
  }));
65
65
  }
66
+ async querySnapshot(p) {
67
+ if (!p)
68
+ return this.db().get();
69
+ if (p.parentId)
70
+ return this.refByParent(p.parentId).get();
71
+ let query;
72
+ for (const [field, filter] of Object.entries(p)) {
73
+ if (typeof filter === "string")
74
+ continue;
75
+ if (!query)
76
+ query = this.db().where(field, filter.op, filter.value);
77
+ else
78
+ query = query.where(field, filter.op, filter.value);
79
+ }
80
+ return (query ?? this.db()).get();
81
+ }
66
82
  async count(p) {
67
83
  const snapshot = p && p.parentId
68
84
  ? await this.refByParent(p.parentId).count().get()
@@ -1,2 +1,16 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ApiFilterOp = void 0;
4
+ var ApiFilterOp;
5
+ (function (ApiFilterOp) {
6
+ ApiFilterOp["lessThan"] = "<";
7
+ ApiFilterOp["lessEquals"] = "<=";
8
+ ApiFilterOp["equals"] = "==";
9
+ ApiFilterOp["notEquals"] = "!=";
10
+ ApiFilterOp["greaterEquals"] = ">=";
11
+ ApiFilterOp["greaterThan"] = ">";
12
+ ApiFilterOp["arrayContains"] = "array-contains";
13
+ ApiFilterOp["in"] = "in";
14
+ ApiFilterOp["notIn"] = "not-in";
15
+ ApiFilterOp["arrayContainsAny"] = "array-contains-any";
16
+ })(ApiFilterOp || (exports.ApiFilterOp = ApiFilterOp = {}));
@@ -14,11 +14,11 @@ class BaseApi {
14
14
  async get(p) {
15
15
  return this.store.get(p.id);
16
16
  }
17
- async query() {
18
- return this.store.query();
17
+ async query(payload) {
18
+ return this.store.query(payload?.filters);
19
19
  }
20
- async count() {
21
- return { count: await this.store.count() };
20
+ async count(payload) {
21
+ return { count: await this.store.count(payload?.filters) };
22
22
  }
23
23
  async exists(p) {
24
24
  return { exists: await this.store.exists(p.id) };
@@ -32,9 +32,9 @@ class BaseHttp {
32
32
  throw new https_1.HttpsError("not-found", `Id ${req.keys?.id} not found`);
33
33
  }
34
34
  }
35
- async query() {
35
+ async query(req) {
36
36
  try {
37
- return this.api.query();
37
+ return this.api.query(req.content);
38
38
  }
39
39
  catch (error) {
40
40
  if (error instanceof https_1.HttpsError)
@@ -42,9 +42,9 @@ class BaseHttp {
42
42
  throw new https_1.HttpsError("unknown", "Unknown error");
43
43
  }
44
44
  }
45
- async count() {
45
+ async count(req) {
46
46
  try {
47
- return this.api.count();
47
+ return this.api.count(req.content);
48
48
  }
49
49
  catch (error) {
50
50
  if (error instanceof https_1.HttpsError)
@@ -63,6 +63,22 @@ class Store {
63
63
  updateTime: doc.updateTime.toDate(),
64
64
  }));
65
65
  }
66
+ async querySnapshot(p) {
67
+ if (!p)
68
+ return this.db().get();
69
+ if (p.parentId)
70
+ return this.refByParent(p.parentId).get();
71
+ let query;
72
+ for (const [field, filter] of Object.entries(p)) {
73
+ if (typeof filter === "string")
74
+ continue;
75
+ if (!query)
76
+ query = this.db().where(field, filter.op, filter.value);
77
+ else
78
+ query = query.where(field, filter.op, filter.value);
79
+ }
80
+ return (query ?? this.db()).get();
81
+ }
66
82
  async count(p) {
67
83
  const snapshot = p && p.parentId
68
84
  ? await this.refByParent(p.parentId).count().get()
@@ -1,2 +1,16 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ApiFilterOp = void 0;
4
+ var ApiFilterOp;
5
+ (function (ApiFilterOp) {
6
+ ApiFilterOp["lessThan"] = "<";
7
+ ApiFilterOp["lessEquals"] = "<=";
8
+ ApiFilterOp["equals"] = "==";
9
+ ApiFilterOp["notEquals"] = "!=";
10
+ ApiFilterOp["greaterEquals"] = ">=";
11
+ ApiFilterOp["greaterThan"] = ">";
12
+ ApiFilterOp["arrayContains"] = "array-contains";
13
+ ApiFilterOp["in"] = "in";
14
+ ApiFilterOp["notIn"] = "not-in";
15
+ ApiFilterOp["arrayContainsAny"] = "array-contains-any";
16
+ })(ApiFilterOp || (exports.ApiFilterOp = ApiFilterOp = {}));
@@ -1,5 +1,5 @@
1
1
  import { MetaItem } from "@mxpicture/zod-meta";
2
- import { ApiRoutes, ApiFromRoutes, ApiKey } from "../types/types.apiRoutes";
2
+ import { ApiRoutes, ApiFromRoutes, ApiKey, ApiFilter } from "../types/types.apiRoutes";
3
3
  import { Store } from "../store/Store";
4
4
  import { DocumentData, WithKey } from "../types/types.document";
5
5
  import { Validation } from "../validation/Validation";
@@ -11,8 +11,8 @@ export declare class BaseApi<DTO extends DocumentData, STORE extends Store<DTO>
11
11
  constructor(store: STORE, validation: VAL, shape: ZodRawShape);
12
12
  delete(p: ApiKey): Promise<void>;
13
13
  get(p: ApiKey): Promise<WithKey<DTO>>;
14
- query(): Promise<WithKey<DTO>[]>;
15
- count(): Promise<{
14
+ query(payload?: ApiFilter): Promise<WithKey<DTO>[]>;
15
+ count(payload?: ApiFilter): Promise<{
16
16
  count: number;
17
17
  }>;
18
18
  exists(p: ApiKey): Promise<{
@@ -1,4 +1,4 @@
1
- import { ApiKey, ApiRoutes } from "../types/types.apiRoutes";
1
+ import { ApiFilter, ApiKey, ApiRoutes } from "../types/types.apiRoutes";
2
2
  import { FunctionPayload } from "../types/types.function";
3
3
  import { HttpHandlers } from "../types/types.httpHandler";
4
4
  import { BaseApi } from "./BaseApi";
@@ -8,8 +8,8 @@ export declare class BaseHttp<DTO extends DocumentData, API extends BaseApi<DTO>
8
8
  constructor(api: API);
9
9
  delete(req: FunctionPayload<never, ApiKey>): Promise<void>;
10
10
  get(req: FunctionPayload<never, ApiKey>): Promise<import("../types/types.document").WithKey<DTO>>;
11
- query(): Promise<import("../types/types.document").WithKey<DTO>[]>;
12
- count(): Promise<{
11
+ query(req: FunctionPayload<ApiFilter>): Promise<import("../types/types.document").WithKey<DTO>[]>;
12
+ count(req: FunctionPayload<ApiFilter>): Promise<{
13
13
  count: number;
14
14
  }>;
15
15
  exists(req: FunctionPayload<never, ApiKey>): Promise<{
@@ -1,5 +1,6 @@
1
- import { CollectionReference, DocumentReference, FirestoreDataConverter } from "firebase-admin/firestore";
1
+ import { CollectionReference, DocumentReference, FirestoreDataConverter, Query, QuerySnapshot } from "firebase-admin/firestore";
2
2
  import { DocumentData, WithKey } from "../types/types.document.js";
3
+ import { ApiFilterItems } from "../types/types.apiRoutes.js";
3
4
  export declare class Store<DTO extends DocumentData> {
4
5
  readonly collectionName: string;
5
6
  protected readonly converter?: FirestoreDataConverter<DTO> | undefined;
@@ -7,9 +8,8 @@ export declare class Store<DTO extends DocumentData> {
7
8
  protected _db?: CollectionReference<DTO>;
8
9
  constructor(collectionName: string, converter?: FirestoreDataConverter<DTO> | undefined, namespace?: string);
9
10
  db(): CollectionReference<DTO>;
10
- query(p?: {
11
- parentId?: string;
12
- }): Promise<WithKey<DTO>[]>;
11
+ query(p?: ApiFilterItems): Promise<WithKey<DTO>[]>;
12
+ protected querySnapshot(p?: ApiFilterItems): Promise<QuerySnapshot<DTO>>;
13
13
  count(p?: {
14
14
  parentId?: string;
15
15
  }): Promise<number>;
@@ -20,5 +20,5 @@ export declare class Store<DTO extends DocumentData> {
20
20
  update(id: string, doc: Partial<DTO>): Promise<WithKey<DTO>>;
21
21
  delete(...ids: string[]): Promise<void>;
22
22
  deleteByParent(parentId: string): Promise<void>;
23
- refByParent(parentId: string): FirebaseFirestore.Query<DTO, FirebaseFirestore.DocumentData>;
23
+ refByParent(parentId: string): Query<DTO, FirebaseFirestore.DocumentData>;
24
24
  }
@@ -10,13 +10,37 @@ export type WithApiRoutes<R extends ApiRoutesMap> = R;
10
10
  export interface ApiKey {
11
11
  id: string;
12
12
  }
13
+ export declare enum ApiFilterOp {
14
+ lessThan = "<",
15
+ lessEquals = "<=",
16
+ equals = "==",
17
+ notEquals = "!=",
18
+ greaterEquals = ">=",
19
+ greaterThan = ">",
20
+ arrayContains = "array-contains",
21
+ in = "in",
22
+ notIn = "not-in",
23
+ arrayContainsAny = "array-contains-any"
24
+ }
25
+ export interface ApiFilterItem {
26
+ op: ApiFilterOp;
27
+ value: string | number | Date;
28
+ }
29
+ export type ApiFilterItems = {
30
+ [key: string]: ApiFilterItem;
31
+ } & {
32
+ parentId?: string;
33
+ };
34
+ export interface ApiFilter {
35
+ filters?: ApiFilterItems;
36
+ }
13
37
  export type ApiRoutes<DTO extends DocumentData> = WithApiRoutes<{
14
38
  create: ApiRouteDef<never, DTO, WithKey<DTO>>;
15
39
  update: ApiRouteDef<ApiKey, Partial<DTO>, WithKey<DTO>>;
16
40
  delete: ApiRouteDef<ApiKey, never, void>;
17
41
  get: ApiRouteDef<ApiKey, never, WithKey<DTO>>;
18
- query: ApiRouteDef<never, never, WithKey<DTO>[]>;
19
- count: ApiRouteDef<never, never, {
42
+ query: ApiRouteDef<never, ApiFilter, WithKey<DTO>[]>;
43
+ count: ApiRouteDef<never, ApiFilter, {
20
44
  count: number;
21
45
  }>;
22
46
  exists: ApiRouteDef<ApiKey, never, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mxpicture/gcp-functions",
3
- "version": "0.1.11",
3
+ "version": "0.1.12",
4
4
  "description": "Utils for google cloud functions, publishing both CommonJS and ESM builds",
5
5
  "author": "MXPicture",
6
6
  "license": "MIT",