@roit/roit-data-firestore 1.2.0 → 1.2.3

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 (91) hide show
  1. package/.github/workflows/deploy.yml +1 -0
  2. package/README.md +9 -0
  3. package/dist/cache/CacheResolver.d.ts +15 -0
  4. package/dist/cache/CacheResolver.js +88 -0
  5. package/dist/cache/providers/CacheProvider.d.ts +6 -0
  6. package/dist/cache/providers/CacheProvider.js +2 -0
  7. package/dist/cache/providers/InMemoryCacheProvider.d.ts +8 -0
  8. package/dist/cache/providers/InMemoryCacheProvider.js +46 -0
  9. package/dist/cache/providers/RedisCacheProvider.d.ts +19 -0
  10. package/dist/cache/providers/RedisCacheProvider.js +137 -0
  11. package/dist/cache/providers/index.d.ts +2 -0
  12. package/dist/cache/providers/index.js +5 -0
  13. package/dist/config/BaseRepository.d.ts +38 -0
  14. package/dist/config/BaseRepository.js +70 -0
  15. package/dist/config/ClassCollectionMap.d.ts +7 -0
  16. package/dist/config/ClassCollectionMap.js +16 -0
  17. package/dist/config/ClassMethodQueryMap.d.ts +11 -0
  18. package/dist/config/ClassMethodQueryMap.js +31 -0
  19. package/dist/config/FirestoreInstance.d.ts +7 -0
  20. package/dist/config/FirestoreInstance.js +31 -0
  21. package/dist/config/ReadonlyRepository.d.ts +9 -0
  22. package/dist/config/ReadonlyRepository.js +46 -0
  23. package/dist/decorators/Cacheable.d.ts +2 -0
  24. package/dist/decorators/Cacheable.js +11 -0
  25. package/dist/decorators/Query.d.ts +6 -0
  26. package/dist/decorators/Query.js +19 -0
  27. package/dist/decorators/Repository.d.ts +2 -0
  28. package/dist/decorators/Repository.js +39 -0
  29. package/dist/emulator/FirestoreEmuator.d.ts +1 -0
  30. package/dist/emulator/FirestoreEmuator.js +26 -0
  31. package/dist/exception/RepositoryBusinessException.d.ts +4 -0
  32. package/dist/exception/RepositoryBusinessException.js +10 -0
  33. package/dist/exception/RepositoryException.d.ts +14 -0
  34. package/dist/exception/RepositoryException.js +23 -0
  35. package/dist/exception/RepositorySystemException.d.ts +4 -0
  36. package/dist/exception/RepositorySystemException.js +10 -0
  37. package/dist/exception/RepositoryValidationException.d.ts +5 -0
  38. package/dist/exception/RepositoryValidationException.js +10 -0
  39. package/dist/exception/handle/ValidatorDataHandle.d.ts +6 -0
  40. package/dist/exception/handle/ValidatorDataHandle.js +56 -0
  41. package/dist/firestore-read-audit/FirestoreReadAuditResolver.d.ts +10 -0
  42. package/dist/firestore-read-audit/FirestoreReadAuditResolver.js +50 -0
  43. package/dist/firestore-read-audit/providers/BigQueryFirestoreReadAuditProvider.d.ts +13 -0
  44. package/dist/firestore-read-audit/providers/BigQueryFirestoreReadAuditProvider.js +111 -0
  45. package/dist/firestore-read-audit/providers/FirestoreReadAuditProvider.d.ts +4 -0
  46. package/dist/firestore-read-audit/providers/FirestoreReadAuditProvider.js +2 -0
  47. package/dist/firestore-read-audit/providers/PubSubFirestoreReadAuditProvider.d.ts +9 -0
  48. package/dist/firestore-read-audit/providers/PubSubFirestoreReadAuditProvider.js +59 -0
  49. package/dist/index.d.ts +15 -0
  50. package/dist/index.js +35 -0
  51. package/dist/model/CacheProviders.d.ts +4 -0
  52. package/dist/model/CacheProviders.js +8 -0
  53. package/dist/model/CacheableOptions.d.ts +13 -0
  54. package/dist/model/CacheableOptions.js +20 -0
  55. package/dist/model/MQuery.d.ts +23 -0
  56. package/dist/model/MQuery.js +27 -0
  57. package/dist/model/Paging.d.ts +7 -0
  58. package/dist/model/Paging.js +12 -0
  59. package/dist/model/PersistFirestoreReadProps.d.ts +19 -0
  60. package/dist/model/PersistFirestoreReadProps.js +2 -0
  61. package/dist/model/QueryPredicate.d.ts +6 -0
  62. package/dist/model/QueryPredicate.js +6 -0
  63. package/dist/model/QueryResult.d.ts +4 -0
  64. package/dist/model/QueryResult.js +6 -0
  65. package/dist/model/RepositoryOptions.d.ts +13 -0
  66. package/dist/model/RepositoryOptions.js +12 -0
  67. package/dist/model/index.d.ts +7 -0
  68. package/dist/model/index.js +19 -0
  69. package/dist/platform/PlatformTools.d.ts +3 -0
  70. package/dist/platform/PlatformTools.js +46 -0
  71. package/dist/query/ManualQueryHelper.d.ts +9 -0
  72. package/dist/query/ManualQueryHelper.js +126 -0
  73. package/dist/query/QueryCreatorConfig.d.ts +9 -0
  74. package/dist/query/QueryCreatorConfig.js +40 -0
  75. package/dist/query/QueryPredicateFunctionTransform.d.ts +10 -0
  76. package/dist/query/QueryPredicateFunctionTransform.js +87 -0
  77. package/dist/query/TransformMethodFromQuery.d.ts +6 -0
  78. package/dist/query/TransformMethodFromQuery.js +38 -0
  79. package/dist/query/operator/CreateFunction.d.ts +13 -0
  80. package/dist/query/operator/CreateFunction.js +307 -0
  81. package/dist/query/operator/OperatorMap.d.ts +1 -0
  82. package/dist/query/operator/OperatorMap.js +62 -0
  83. package/dist/tsconfig.build.tsbuildinfo +1 -0
  84. package/dist/util/EnvironmentUtil.d.ts +3 -0
  85. package/dist/util/EnvironmentUtil.js +10 -0
  86. package/dist/util/TemplateLoading.d.ts +4 -0
  87. package/dist/util/TemplateLoading.js +30 -0
  88. package/dist/util/TtlBuilderUtil.d.ts +3 -0
  89. package/dist/util/TtlBuilderUtil.js +21 -0
  90. package/package.json +3 -1
  91. package/src/cache/providers/RedisCacheProvider.ts +32 -6
@@ -73,6 +73,7 @@ jobs:
73
73
  node-version: '18.x'
74
74
  registry-url: 'https://registry.npmjs.org'
75
75
  - run: npm ci
76
+ - run: npm run build
76
77
  - run: npm publish
77
78
  env:
78
79
  NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
package/README.md CHANGED
@@ -84,6 +84,15 @@ The anotation Cacheable is responsible from handler storage data in cache, local
84
84
  })
85
85
  ```
86
86
 
87
+ ##### Cache environment variables
88
+
89
+ | Environment variable | Description | Default value |
90
+ | -------------------|------------------------------------| ---------------------------------------- |
91
+ | firestore.cache.redisUrl | Ex: redis://localhost:63279 |
92
+ | firestore.cache.timeout | Timeout to Redis response (ms) | 2000 |
93
+ | firestore.cache.reconnectInSecondsAfterTimeout | Time to try to reconnect after Redis timeout (s) | 30 |
94
+ | firestore.debug | Toggle debugging logs | false |
95
+
87
96
  ## BaseRepository and ReadonlyRepository
88
97
  To standardize the BaseRepository already provides the common methods for implementation
89
98
 
@@ -0,0 +1,15 @@
1
+ import { CacheableOptions } from "../model/CacheableOptions";
2
+ export declare class CacheResolver {
3
+ private static instance;
4
+ private repositorys;
5
+ private providersImplMap;
6
+ private cacheProvider;
7
+ private constructor();
8
+ static getInstance(): CacheResolver;
9
+ addRepository(repository: string, option?: CacheableOptions): void;
10
+ private buildKey;
11
+ buildRepositoryKey(repositoryClassName: string): string;
12
+ getCacheResult(repositoryClassName: string, methodSignature: string, ...paramValue: any[]): Promise<any | null | any[]>;
13
+ revokeCacheFromRepository(repositoryClassName: string): Promise<void>;
14
+ cacheResult(repositoryClassName: string, methodSignature: string, valueToCache: any, ...paramValue: any[]): Promise<boolean>;
15
+ }
@@ -0,0 +1,88 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.CacheResolver = void 0;
13
+ const CacheableOptions_1 = require("../model/CacheableOptions");
14
+ const providers_1 = require("./providers");
15
+ const roit_environment_1 = require("roit-environment");
16
+ const RedisCacheProvider_1 = require("./providers/RedisCacheProvider");
17
+ const CacheProviders_1 = require("../model/CacheProviders");
18
+ class CacheResolver {
19
+ constructor() {
20
+ this.repositorys = new Map;
21
+ this.providersImplMap = new Map;
22
+ this.providersImplMap.set(CacheProviders_1.CacheProviders.LOCAL, providers_1.InMemoryCacheProvider);
23
+ this.providersImplMap.set(CacheProviders_1.CacheProviders.REDIS, RedisCacheProvider_1.RedisCacheProvider);
24
+ }
25
+ static getInstance() {
26
+ return this.instance;
27
+ }
28
+ addRepository(repository, option) {
29
+ const options = option || new CacheableOptions_1.CacheableOptions;
30
+ const implementation = options.cacheProvider || CacheProviders_1.CacheProviders.LOCAL;
31
+ const providerImpl = this.providersImplMap.get(implementation);
32
+ this.cacheProvider = new providerImpl();
33
+ this.repositorys.set(repository, options);
34
+ }
35
+ buildKey(repositoryClassName, methodSignature, ...paramValue) {
36
+ return `${roit_environment_1.Environment.currentEnv()}:${repositoryClassName}:${methodSignature}:${paramValue.join(',')}`;
37
+ }
38
+ buildRepositoryKey(repositoryClassName) {
39
+ return `${roit_environment_1.Environment.currentEnv()}:${repositoryClassName}`;
40
+ }
41
+ getCacheResult(repositoryClassName, methodSignature, ...paramValue) {
42
+ return __awaiter(this, void 0, void 0, function* () {
43
+ if (!this.repositorys.get(repositoryClassName)) {
44
+ return null;
45
+ }
46
+ const key = this.buildKey(repositoryClassName, methodSignature, paramValue);
47
+ return this.cacheProvider.getCacheResult(key);
48
+ });
49
+ }
50
+ revokeCacheFromRepository(repositoryClassName) {
51
+ return __awaiter(this, void 0, void 0, function* () {
52
+ const key = this.buildRepositoryKey(repositoryClassName);
53
+ if (!this.repositorys.get(repositoryClassName)) {
54
+ return;
55
+ }
56
+ const keys = yield this.cacheProvider.getKeys(`${key}`);
57
+ if (keys && Array.isArray(keys)) {
58
+ for (const key of keys) {
59
+ if (Boolean(roit_environment_1.Environment.getProperty('firestore.debug'))) {
60
+ console.debug('[DEBUG] Caching >', `Removing key: ${key}`);
61
+ }
62
+ yield this.cacheProvider.delete(key);
63
+ }
64
+ }
65
+ yield this.cacheProvider.delete(key);
66
+ });
67
+ }
68
+ cacheResult(repositoryClassName, methodSignature, valueToCache, ...paramValue) {
69
+ var _a, _b, _c;
70
+ return __awaiter(this, void 0, void 0, function* () {
71
+ const option = this.repositorys.get(repositoryClassName);
72
+ if (option) {
73
+ const key = this.buildKey(repositoryClassName, methodSignature, paramValue);
74
+ const excludesMethod = Array.isArray(option === null || option === void 0 ? void 0 : option.excludesMethods) && ((_a = option === null || option === void 0 ? void 0 : option.excludesMethods) === null || _a === void 0 ? void 0 : _a.find(me => me == methodSignature));
75
+ const notIncludeOnlyMethod = Array.isArray(option === null || option === void 0 ? void 0 : option.includeOnlyMethods) && ((_b = option === null || option === void 0 ? void 0 : option.includeOnlyMethods) === null || _b === void 0 ? void 0 : _b.length) > 0 && ((_c = option === null || option === void 0 ? void 0 : option.includeOnlyMethods) === null || _c === void 0 ? void 0 : _c.find(me => me == methodSignature)) == undefined;
76
+ const notContainResult = (option === null || option === void 0 ? void 0 : option.cacheOnlyContainResults) ? ((Array.isArray(valueToCache) && valueToCache.length == 0) || !valueToCache) : false;
77
+ if (excludesMethod || notIncludeOnlyMethod || notContainResult) {
78
+ return false;
79
+ }
80
+ yield this.cacheProvider.saveCacheResult(key, valueToCache, option.cacheExpiresInSeconds);
81
+ return true;
82
+ }
83
+ return false;
84
+ });
85
+ }
86
+ }
87
+ exports.CacheResolver = CacheResolver;
88
+ CacheResolver.instance = new CacheResolver;
@@ -0,0 +1,6 @@
1
+ export interface CacheProvider {
2
+ getCacheResult(key: string): Promise<any | null | any[]>;
3
+ getKeys(query: string): Promise<string[]>;
4
+ saveCacheResult(key: string, valueToCache: any, ttl: number | undefined): Promise<void>;
5
+ delete(key: string): Promise<void>;
6
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,8 @@
1
+ import { CacheProvider } from "./CacheProvider";
2
+ export declare class InMemoryCacheProvider implements CacheProvider {
3
+ private cache;
4
+ getCacheResult(key: string): Promise<any | null>;
5
+ getKeys(query: string): Promise<string[]>;
6
+ saveCacheResult(key: string, valueToCache: any, ttl: number): Promise<void>;
7
+ delete(key: string): Promise<void>;
8
+ }
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.InMemoryCacheProvider = void 0;
7
+ const roit_environment_1 = require("roit-environment");
8
+ const node_cache_1 = __importDefault(require("node-cache"));
9
+ class InMemoryCacheProvider {
10
+ constructor() {
11
+ this.cache = new node_cache_1.default();
12
+ }
13
+ getCacheResult(key) {
14
+ const result = this.cache.get(key);
15
+ if (Boolean(roit_environment_1.Environment.getProperty('firestore.debug'))) {
16
+ if (result) {
17
+ console.debug('[DEBUG] Memory Caching >', `Return value in cache from key: ${key}`);
18
+ }
19
+ else {
20
+ console.log("[DEBUG] Memory Caching > ", `Key [${key}] is not found in the cache`);
21
+ }
22
+ }
23
+ return Promise.resolve(result);
24
+ }
25
+ getKeys(query) {
26
+ try {
27
+ return Promise.resolve(this.cache.keys().filter(key => key.includes(query)));
28
+ }
29
+ catch (error) {
30
+ console.log(`[DEBUG] Memory Caching > Error when getting KEYS with query: ${query}, error: ${error}`);
31
+ }
32
+ return Promise.resolve([]);
33
+ }
34
+ saveCacheResult(key, valueToCache, ttl) {
35
+ this.cache.set(key, valueToCache, ttl || 0);
36
+ if (Boolean(roit_environment_1.Environment.getProperty('firestore.debug'))) {
37
+ console.debug('[DEBUG] Memory Caching >', `Storage cache from key: ${key}`);
38
+ }
39
+ return Promise.resolve();
40
+ }
41
+ delete(key) {
42
+ this.cache.del(key);
43
+ return Promise.resolve();
44
+ }
45
+ }
46
+ exports.InMemoryCacheProvider = InMemoryCacheProvider;
@@ -0,0 +1,19 @@
1
+ import { CacheProvider } from "./CacheProvider";
2
+ export declare class RedisCacheProvider implements CacheProvider {
3
+ /**
4
+ * Redis module instance loaded dynamically.
5
+ */
6
+ private redis;
7
+ /**
8
+ * Connected redis client.
9
+ */
10
+ private client;
11
+ private isRedisReady;
12
+ constructor();
13
+ private handleTimeoutError;
14
+ getKeys(query: string): Promise<string[]>;
15
+ getCacheResult(key: string): Promise<any | null>;
16
+ saveCacheResult(key: string, valueToCache: any, ttl: number | undefined): Promise<void>;
17
+ delete(key: string): Promise<void>;
18
+ private loadRedis;
19
+ }
@@ -0,0 +1,137 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.RedisCacheProvider = void 0;
13
+ const roit_environment_1 = require("roit-environment");
14
+ const PlatformTools_1 = require("../../platform/PlatformTools");
15
+ const promise_timeout_1 = require("promise-timeout");
16
+ const REDIS_TIMEOUT = roit_environment_1.Environment.getProperty('firestore.cache.timeout') || 2000;
17
+ const REDIS_RECONNECT = roit_environment_1.Environment.getProperty('firestore.cache.reconnectInSecondsAfterTimeout') || 30;
18
+ class RedisCacheProvider {
19
+ constructor() {
20
+ this.isRedisReady = false;
21
+ if (!this.redis) {
22
+ const url = roit_environment_1.Environment.getProperty('firestore.cache.redisUrl');
23
+ if (!url) {
24
+ console.error(`[ERROR] Redis Caching > environtment variable "firestore.cache.redisUrl" was not found!`);
25
+ return;
26
+ }
27
+ const isJest = typeof jest !== 'undefined';
28
+ if (isJest) {
29
+ return;
30
+ }
31
+ this.redis = this.loadRedis();
32
+ this.client = this.redis.createClient({ url });
33
+ this.client.on('error', (err) => {
34
+ this.isRedisReady = false;
35
+ if (Boolean(roit_environment_1.Environment.getProperty('firestore.debug'))) {
36
+ console.warn('[WARN] Redis error', err);
37
+ }
38
+ });
39
+ this.client.on('ready', () => {
40
+ this.isRedisReady = true;
41
+ if (Boolean(roit_environment_1.Environment.getProperty('firestore.debug'))) {
42
+ console.log('[DEBUG] Redis Caching > Redis is ready');
43
+ }
44
+ });
45
+ this.client.connect();
46
+ }
47
+ }
48
+ handleTimeoutError(error) {
49
+ if (error.message === 'Timeout' && this.isRedisReady) {
50
+ console.log('Setting isRedisReady as false');
51
+ this.isRedisReady = false;
52
+ // Stop everything for a while to unburden Redis
53
+ setTimeout(() => {
54
+ console.log('Setting isRedisReady as true');
55
+ this.isRedisReady = true;
56
+ }, REDIS_RECONNECT * 1000);
57
+ }
58
+ }
59
+ getKeys(query) {
60
+ try {
61
+ if (this.isRedisReady) {
62
+ return (0, promise_timeout_1.timeout)(this.client.KEYS(`*${query}*`), REDIS_TIMEOUT);
63
+ }
64
+ }
65
+ catch (error) {
66
+ this.handleTimeoutError(error);
67
+ console.log(`[DEBUG] Redis Caching > Error when getting KEYS with query: ${query}, error: ${error}`);
68
+ }
69
+ return Promise.resolve([]);
70
+ }
71
+ getCacheResult(key) {
72
+ return __awaiter(this, void 0, void 0, function* () {
73
+ try {
74
+ if (this.isRedisReady) {
75
+ const result = yield (0, promise_timeout_1.timeout)(this.client.get(key), REDIS_TIMEOUT);
76
+ if (Boolean(roit_environment_1.Environment.getProperty('firestore.debug'))) {
77
+ if (result) {
78
+ console.debug('[DEBUG] Redis Caching >', `Return value in cache from key: ${key}`);
79
+ }
80
+ else {
81
+ console.log("[DEBUG] Redis Caching > ", `Key [${key}] is not found in the cache`);
82
+ }
83
+ }
84
+ if (result) {
85
+ return JSON.parse(result);
86
+ }
87
+ return null;
88
+ }
89
+ }
90
+ catch (error) {
91
+ this.handleTimeoutError(error);
92
+ console.log(`[DEBUG] Redis Caching > Error when getting key from redis. ${key}`, { error });
93
+ return null;
94
+ }
95
+ });
96
+ }
97
+ saveCacheResult(key, valueToCache, ttl) {
98
+ return __awaiter(this, void 0, void 0, function* () {
99
+ if (this.isRedisReady) {
100
+ try {
101
+ yield (0, promise_timeout_1.timeout)(this.client.set(key, JSON.stringify(valueToCache), {
102
+ EX: ttl || 0
103
+ }), REDIS_TIMEOUT);
104
+ if (Boolean(roit_environment_1.Environment.getProperty('firestore.debug'))) {
105
+ console.debug('[DEBUG] Redis Caching >', `Storage cache from key: ${key}`);
106
+ }
107
+ }
108
+ catch (error) {
109
+ this.handleTimeoutError(error);
110
+ console.log(`[DEBUG] Redis Caching > Error when saving cache. Key: ${key}, value: ${valueToCache}, error: ${error}`);
111
+ }
112
+ }
113
+ });
114
+ }
115
+ delete(key) {
116
+ return __awaiter(this, void 0, void 0, function* () {
117
+ try {
118
+ if (this.isRedisReady) {
119
+ yield (0, promise_timeout_1.timeout)(this.client.del(key), REDIS_TIMEOUT);
120
+ }
121
+ }
122
+ catch (error) {
123
+ this.handleTimeoutError(error);
124
+ console.log(`[DEBUG] Redis Caching > Error when deleting key from redis. ${key}`);
125
+ }
126
+ });
127
+ }
128
+ loadRedis() {
129
+ try {
130
+ return PlatformTools_1.PlatformTools.load("redis");
131
+ }
132
+ catch (e) {
133
+ throw new Error(`Cannot use cache because redis is not installed. Please run "npm i redis@4.0.6 --save-exact".`);
134
+ }
135
+ }
136
+ }
137
+ exports.RedisCacheProvider = RedisCacheProvider;
@@ -0,0 +1,2 @@
1
+ export { CacheProvider } from './CacheProvider';
2
+ export { InMemoryCacheProvider } from './InMemoryCacheProvider';
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.InMemoryCacheProvider = void 0;
4
+ var InMemoryCacheProvider_1 = require("./InMemoryCacheProvider");
5
+ Object.defineProperty(exports, "InMemoryCacheProvider", { enumerable: true, get: function () { return InMemoryCacheProvider_1.InMemoryCacheProvider; } });
@@ -0,0 +1,38 @@
1
+ import { QueryResult } from "../model";
2
+ import { Config } from '../model/MQuery';
3
+ import { Paging } from "../model/Paging";
4
+ export declare abstract class BaseRepository<T> {
5
+ /**
6
+ * FindAll
7
+ * Get all available records in the collection
8
+ * @param paging
9
+ */
10
+ findAll: (paging?: Paging) => Promise<T[]>;
11
+ /**
12
+ * FindById
13
+ * Get registe
14
+ */
15
+ findById: (id: Required<string>) => Promise<T | undefined>;
16
+ create: (item: T | Array<T>) => Promise<Array<T>>;
17
+ update: (items: T | Array<T>) => Promise<Array<T>>;
18
+ createOrUpdate: (items: T | Array<T>) => Promise<Array<T>>;
19
+ /**
20
+ * Update partial data
21
+ */
22
+ updatePartial: (id: Required<string>, itemPartial: Partial<T>) => Promise<void>;
23
+ /**
24
+ * Delete Row or Rows in bath
25
+ * @param id
26
+ */
27
+ delete: (id: Required<string> | Array<string>) => Promise<Array<string>>;
28
+ /**
29
+ * Distributed counters
30
+ * https://firebase.google.com/docs/firestore/solutions/counters#web
31
+ *
32
+ * @param field field from increment
33
+ * @param increment value increment, default value 1
34
+ */
35
+ incrementField: (id: Required<string>, field: Required<string>, increment?: number) => Promise<void>;
36
+ query(config: Config): Promise<T[]>;
37
+ queryPaginated(config: Config): Promise<QueryResult<T>>;
38
+ }
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
12
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
13
+ return new (P || (P = Promise))(function (resolve, reject) {
14
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
15
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
16
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
17
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
18
+ });
19
+ };
20
+ Object.defineProperty(exports, "__esModule", { value: true });
21
+ exports.BaseRepository = void 0;
22
+ const Query_1 = require("../decorators/Query");
23
+ const ManualQueryHelper_1 = require("../query/ManualQueryHelper");
24
+ class BaseRepository {
25
+ query(config) {
26
+ return __awaiter(this, void 0, void 0, function* () {
27
+ const className = this.constructor.prototype.constructor.name;
28
+ return ManualQueryHelper_1.ManualQueryHelper.executeQueryManual(className, config);
29
+ });
30
+ }
31
+ queryPaginated(config) {
32
+ return __awaiter(this, void 0, void 0, function* () {
33
+ const className = this.constructor.prototype.constructor.name;
34
+ return ManualQueryHelper_1.ManualQueryHelper.executeQueryManualPaginated(className, config);
35
+ });
36
+ }
37
+ }
38
+ __decorate([
39
+ (0, Query_1.Query)(),
40
+ __metadata("design:type", Function)
41
+ ], BaseRepository.prototype, "findAll", void 0);
42
+ __decorate([
43
+ (0, Query_1.Query)(),
44
+ __metadata("design:type", Function)
45
+ ], BaseRepository.prototype, "findById", void 0);
46
+ __decorate([
47
+ (0, Query_1.Query)(),
48
+ __metadata("design:type", Function)
49
+ ], BaseRepository.prototype, "create", void 0);
50
+ __decorate([
51
+ (0, Query_1.Query)(),
52
+ __metadata("design:type", Function)
53
+ ], BaseRepository.prototype, "update", void 0);
54
+ __decorate([
55
+ (0, Query_1.Query)(),
56
+ __metadata("design:type", Function)
57
+ ], BaseRepository.prototype, "createOrUpdate", void 0);
58
+ __decorate([
59
+ (0, Query_1.Query)(),
60
+ __metadata("design:type", Function)
61
+ ], BaseRepository.prototype, "updatePartial", void 0);
62
+ __decorate([
63
+ (0, Query_1.Query)(),
64
+ __metadata("design:type", Function)
65
+ ], BaseRepository.prototype, "delete", void 0);
66
+ __decorate([
67
+ (0, Query_1.Query)(),
68
+ __metadata("design:type", Function)
69
+ ], BaseRepository.prototype, "incrementField", void 0);
70
+ exports.BaseRepository = BaseRepository;
@@ -0,0 +1,7 @@
1
+ export declare class ClassCollectionMap {
2
+ private static instance;
3
+ private classMap;
4
+ private constructor();
5
+ register(className: string, collection: string): void;
6
+ static getInstance(): ClassCollectionMap;
7
+ }
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ClassCollectionMap = void 0;
4
+ class ClassCollectionMap {
5
+ constructor() {
6
+ this.classMap = new Map;
7
+ }
8
+ register(className, collection) {
9
+ this.classMap.set(className, collection);
10
+ }
11
+ static getInstance() {
12
+ return this.instance;
13
+ }
14
+ }
15
+ exports.ClassCollectionMap = ClassCollectionMap;
16
+ ClassCollectionMap.instance = new ClassCollectionMap;
@@ -0,0 +1,11 @@
1
+ import { QueryOptions } from '../decorators/Query';
2
+ export declare class ClassMethodQueryMap {
3
+ private static instance;
4
+ private collectionMap;
5
+ private queryConfig;
6
+ constructor();
7
+ register(className: string, methodSignature: string, options?: QueryOptions): void;
8
+ get(className: string): Array<string> | undefined;
9
+ getMethodConfig(className: string, methodSignature: string): QueryOptions | undefined;
10
+ static getInstance(): ClassMethodQueryMap;
11
+ }
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ClassMethodQueryMap = void 0;
4
+ class ClassMethodQueryMap {
5
+ constructor() {
6
+ this.collectionMap = new Map;
7
+ this.queryConfig = new Map;
8
+ }
9
+ register(className, methodSignature, options) {
10
+ let list = this.collectionMap.get(className);
11
+ if (!list) {
12
+ list = new Array;
13
+ }
14
+ list.push(methodSignature);
15
+ this.collectionMap.set(className, list);
16
+ if (options) {
17
+ this.queryConfig.set(`${className}:${methodSignature}`, options);
18
+ }
19
+ }
20
+ get(className) {
21
+ return this.collectionMap.get(className);
22
+ }
23
+ getMethodConfig(className, methodSignature) {
24
+ return this.queryConfig.get(`${className}:${methodSignature}`);
25
+ }
26
+ static getInstance() {
27
+ return this.instance;
28
+ }
29
+ }
30
+ exports.ClassMethodQueryMap = ClassMethodQueryMap;
31
+ ClassMethodQueryMap.instance = new ClassMethodQueryMap;
@@ -0,0 +1,7 @@
1
+ import { Firestore } from "@google-cloud/firestore";
2
+ export declare class FirestoreInstance {
3
+ private static instance;
4
+ private firestore;
5
+ constructor();
6
+ static getInstance(): Firestore;
7
+ }
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FirestoreInstance = void 0;
4
+ const firestore_1 = require("@google-cloud/firestore");
5
+ const roit_environment_1 = require("roit-environment");
6
+ const RepositorySystemException_1 = require("../exception/RepositorySystemException");
7
+ class FirestoreInstance {
8
+ constructor() {
9
+ if (roit_environment_1.Environment.acceptedEnv(roit_environment_1.Env.TEST)) {
10
+ return;
11
+ }
12
+ const projectId = roit_environment_1.Environment.getProperty("firestore.projectId") || roit_environment_1.Environment.systemProperty("PROJECT_ID");
13
+ if (!projectId) {
14
+ throw new RepositorySystemException_1.RepositorySystemException(`ProjectId is required in env.yaml {firestore.projectId}`);
15
+ }
16
+ try {
17
+ this.firestore = new firestore_1.Firestore({
18
+ projectId
19
+ });
20
+ }
21
+ catch (err) {
22
+ console.error(err);
23
+ throw new RepositorySystemException_1.RepositorySystemException(`Error in initializeApp: ${err}`);
24
+ }
25
+ }
26
+ static getInstance() {
27
+ return this.instance.firestore;
28
+ }
29
+ }
30
+ exports.FirestoreInstance = FirestoreInstance;
31
+ FirestoreInstance.instance = new FirestoreInstance();
@@ -0,0 +1,9 @@
1
+ import { Paging } from "../model/Paging";
2
+ import { Config } from '../model/MQuery';
3
+ import { QueryResult } from "../model";
4
+ export declare abstract class ReadonlyRepository<T> {
5
+ findAll: (paging?: Paging) => Promise<T[]>;
6
+ findById: (id: string) => Promise<T> | undefined;
7
+ query(config: Config): Promise<T[]>;
8
+ queryPaginated(config: Config): Promise<QueryResult<T>>;
9
+ }
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
12
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
13
+ return new (P || (P = Promise))(function (resolve, reject) {
14
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
15
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
16
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
17
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
18
+ });
19
+ };
20
+ Object.defineProperty(exports, "__esModule", { value: true });
21
+ exports.ReadonlyRepository = void 0;
22
+ const Query_1 = require("../decorators/Query");
23
+ const ManualQueryHelper_1 = require("../query/ManualQueryHelper");
24
+ class ReadonlyRepository {
25
+ query(config) {
26
+ return __awaiter(this, void 0, void 0, function* () {
27
+ const className = this.constructor.prototype.constructor.name;
28
+ return ManualQueryHelper_1.ManualQueryHelper.executeQueryManual(className, config);
29
+ });
30
+ }
31
+ queryPaginated(config) {
32
+ return __awaiter(this, void 0, void 0, function* () {
33
+ const className = this.constructor.prototype.constructor.name;
34
+ return ManualQueryHelper_1.ManualQueryHelper.executeQueryManualPaginated(className, config);
35
+ });
36
+ }
37
+ }
38
+ __decorate([
39
+ (0, Query_1.Query)(),
40
+ __metadata("design:type", Function)
41
+ ], ReadonlyRepository.prototype, "findAll", void 0);
42
+ __decorate([
43
+ (0, Query_1.Query)(),
44
+ __metadata("design:type", Function)
45
+ ], ReadonlyRepository.prototype, "findById", void 0);
46
+ exports.ReadonlyRepository = ReadonlyRepository;
@@ -0,0 +1,2 @@
1
+ import { CacheableOptions } from "../model/CacheableOptions";
2
+ export declare function Cacheable(options?: CacheableOptions): (constructor: Function) => void;