@expo/entity 0.23.0 → 0.24.0
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/build/EntityFieldDefinition.d.ts +8 -0
- package/build/EntityFieldDefinition.js +5 -0
- package/build/EntityFieldDefinition.js.map +1 -1
- package/build/EntityFields.d.ts +38 -0
- package/build/EntityFields.js +38 -0
- package/build/EntityFields.js.map +1 -1
- package/build/GenericSecondaryEntityCache.d.ts +19 -0
- package/build/GenericSecondaryEntityCache.js +74 -0
- package/build/GenericSecondaryEntityCache.js.map +1 -0
- package/build/IEntityGenericCacher.d.ts +11 -0
- package/build/IEntityGenericCacher.js +3 -0
- package/build/IEntityGenericCacher.js.map +1 -0
- package/build/index.d.ts +2 -0
- package/build/index.js +3 -1
- package/build/index.js.map +1 -1
- package/package.json +1 -1
- package/src/EntityFieldDefinition.ts +8 -0
- package/src/EntityFields.ts +45 -0
- package/src/GenericSecondaryEntityCache.ts +98 -0
- package/src/IEntityGenericCacher.ts +15 -0
- package/src/index.ts +2 -0
|
@@ -24,6 +24,9 @@ export declare enum EntityEdgeDeletionBehavior {
|
|
|
24
24
|
*/
|
|
25
25
|
SET_NULL = 2
|
|
26
26
|
}
|
|
27
|
+
/**
|
|
28
|
+
* Defines an association between entities. An association is primarily used to define cascading deletion behavior.
|
|
29
|
+
*/
|
|
27
30
|
export interface EntityAssociationDefinition<TViewerContext extends ViewerContext, TAssociatedFields, TAssociatedID extends NonNullable<TAssociatedFields[TAssociatedSelectedFields]>, TAssociatedEntity extends ReadonlyEntity<TAssociatedFields, TAssociatedID, TViewerContext, TAssociatedSelectedFields>, TAssociatedPrivacyPolicy extends EntityPrivacyPolicy<TAssociatedFields, TAssociatedID, TViewerContext, TAssociatedEntity, TAssociatedSelectedFields>, TAssociatedSelectedFields extends keyof TAssociatedFields = keyof TAssociatedFields> {
|
|
28
31
|
/**
|
|
29
32
|
* Class of entity on the other end of this edge.
|
|
@@ -51,6 +54,10 @@ export interface EntityAssociationDefinition<TViewerContext extends ViewerContex
|
|
|
51
54
|
*/
|
|
52
55
|
edgeDeletionBehavior?: EntityEdgeDeletionBehavior;
|
|
53
56
|
}
|
|
57
|
+
/**
|
|
58
|
+
* Definition for a field referencing a column in the underlying database. Specifies things like
|
|
59
|
+
* cache behavior and associations, and handles input validation.
|
|
60
|
+
*/
|
|
54
61
|
export declare abstract class EntityFieldDefinition<T> {
|
|
55
62
|
readonly columnName: string;
|
|
56
63
|
readonly cache: boolean;
|
|
@@ -61,6 +68,7 @@ export declare abstract class EntityFieldDefinition<T> {
|
|
|
61
68
|
* @param cache - Whether or not to cache loaded instances of the entity by this field. The column name is
|
|
62
69
|
* used to derive a cache key for the cache entry. If true, this column must be able uniquely
|
|
63
70
|
* identify the entity.
|
|
71
|
+
* @param association - Defines the association behavior for an entity that this column references.
|
|
64
72
|
*/
|
|
65
73
|
constructor({ columnName, cache, association, }: {
|
|
66
74
|
columnName: string;
|
|
@@ -24,6 +24,10 @@ var EntityEdgeDeletionBehavior;
|
|
|
24
24
|
*/
|
|
25
25
|
EntityEdgeDeletionBehavior[EntityEdgeDeletionBehavior["SET_NULL"] = 2] = "SET_NULL";
|
|
26
26
|
})(EntityEdgeDeletionBehavior = exports.EntityEdgeDeletionBehavior || (exports.EntityEdgeDeletionBehavior = {}));
|
|
27
|
+
/**
|
|
28
|
+
* Definition for a field referencing a column in the underlying database. Specifies things like
|
|
29
|
+
* cache behavior and associations, and handles input validation.
|
|
30
|
+
*/
|
|
27
31
|
class EntityFieldDefinition {
|
|
28
32
|
/**
|
|
29
33
|
*
|
|
@@ -31,6 +35,7 @@ class EntityFieldDefinition {
|
|
|
31
35
|
* @param cache - Whether or not to cache loaded instances of the entity by this field. The column name is
|
|
32
36
|
* used to derive a cache key for the cache entry. If true, this column must be able uniquely
|
|
33
37
|
* identify the entity.
|
|
38
|
+
* @param association - Defines the association behavior for an entity that this column references.
|
|
34
39
|
*/
|
|
35
40
|
constructor({ columnName, cache = false, association, }) {
|
|
36
41
|
this.columnName = columnName;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"EntityFieldDefinition.js","sourceRoot":"","sources":["../src/EntityFieldDefinition.ts"],"names":[],"mappings":";;;AAKA,IAAY,0BAuBX;AAvBD,WAAY,0BAA0B;IACpC;;;;;;OAMG;IACH,iIAA+B,CAAA;IAE/B;;;;OAIG;IACH,+FAAc,CAAA;IAEd;;;;OAIG;IACH,mFAAQ,CAAA;AACV,CAAC,EAvBW,0BAA0B,GAA1B,kCAA0B,KAA1B,kCAA0B,QAuBrC;
|
|
1
|
+
{"version":3,"file":"EntityFieldDefinition.js","sourceRoot":"","sources":["../src/EntityFieldDefinition.ts"],"names":[],"mappings":";;;AAKA,IAAY,0BAuBX;AAvBD,WAAY,0BAA0B;IACpC;;;;;;OAMG;IACH,iIAA+B,CAAA;IAE/B;;;;OAIG;IACH,+FAAc,CAAA;IAEd;;;;OAIG;IACH,mFAAQ,CAAA;AACV,CAAC,EAvBW,0BAA0B,GAA1B,kCAA0B,KAA1B,kCAA0B,QAuBrC;AA4DD;;;GAGG;AACH,MAAsB,qBAAqB;IAIzC;;;;;;;OAOG;IACH,YAAY,EACV,UAAU,EACV,KAAK,GAAG,KAAK,EACb,WAAW,GAKZ;QACC,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;QAC7B,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,WAAW,GAAG,WAAW,CAAC;IACjC,CAAC;IAED;;;;OAIG;IACI,kBAAkB,CAAC,KAA2B;QACnD,IAAI,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,SAAS,EAAE;YACzC,OAAO,IAAI,CAAC;SACb;QAED,OAAO,IAAI,CAAC,0BAA0B,CAAC,KAAK,CAAC,CAAC;IAChD,CAAC;CAEF;AAvCD,sDAuCC"}
|
package/build/EntityFields.d.ts
CHANGED
|
@@ -1,34 +1,72 @@
|
|
|
1
1
|
import { EntityFieldDefinition } from './EntityFieldDefinition';
|
|
2
|
+
/**
|
|
3
|
+
* {@link EntityFieldDefinition} for a column with a JS string type.
|
|
4
|
+
*/
|
|
2
5
|
export declare class StringField extends EntityFieldDefinition<string> {
|
|
3
6
|
protected validateInputValueInternal(value: string): boolean;
|
|
4
7
|
}
|
|
8
|
+
/**
|
|
9
|
+
* {@link EntityFieldDefinition} for a column with a JS string type.
|
|
10
|
+
* Enforces that the string is a valid UUID.
|
|
11
|
+
*/
|
|
5
12
|
export declare class UUIDField extends StringField {
|
|
6
13
|
protected validateInputValueInternal(value: string): boolean;
|
|
7
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* {@link EntityFieldDefinition} for a column with a JS Date type.
|
|
17
|
+
*/
|
|
8
18
|
export declare class DateField extends EntityFieldDefinition<Date> {
|
|
9
19
|
protected validateInputValueInternal(value: Date): boolean;
|
|
10
20
|
}
|
|
21
|
+
/**
|
|
22
|
+
* {@link EntityFieldDefinition} for a column with a JS boolean type.
|
|
23
|
+
*/
|
|
11
24
|
export declare class BooleanField extends EntityFieldDefinition<boolean> {
|
|
12
25
|
protected validateInputValueInternal(value: boolean): boolean;
|
|
13
26
|
}
|
|
27
|
+
/**
|
|
28
|
+
* {@link EntityFieldDefinition} for a column with a JS number type.
|
|
29
|
+
* Enforces that the number is an integer.
|
|
30
|
+
*/
|
|
14
31
|
export declare class IntField extends EntityFieldDefinition<number> {
|
|
15
32
|
protected validateInputValueInternal(value: number): boolean;
|
|
16
33
|
}
|
|
34
|
+
/**
|
|
35
|
+
* {@link EntityFieldDefinition} for a column with a JS number type.
|
|
36
|
+
* Enforces that the number is a float (which includes integers in JS).
|
|
37
|
+
*/
|
|
17
38
|
export declare class FloatField extends EntityFieldDefinition<number> {
|
|
18
39
|
protected validateInputValueInternal(value: number): boolean;
|
|
19
40
|
}
|
|
41
|
+
/**
|
|
42
|
+
* {@link EntityFieldDefinition} for a column with a JS string array type.
|
|
43
|
+
* Enforces that every member of the string array is a string.
|
|
44
|
+
*/
|
|
20
45
|
export declare class StringArrayField extends EntityFieldDefinition<string[]> {
|
|
21
46
|
protected validateInputValueInternal(value: string[]): boolean;
|
|
22
47
|
}
|
|
48
|
+
/**
|
|
49
|
+
* {@link EntityFieldDefinition} for a column with a JS JSON object type.
|
|
50
|
+
*/
|
|
23
51
|
export declare class JSONObjectField extends EntityFieldDefinition<object> {
|
|
24
52
|
protected validateInputValueInternal(value: object): boolean;
|
|
25
53
|
}
|
|
54
|
+
/**
|
|
55
|
+
* {@link EntityFieldDefinition} for a enum column with a JS string or number type.
|
|
56
|
+
*/
|
|
26
57
|
export declare class EnumField extends EntityFieldDefinition<string | number> {
|
|
27
58
|
protected validateInputValueInternal(value: string | number): boolean;
|
|
28
59
|
}
|
|
60
|
+
/**
|
|
61
|
+
* {@link EntityFieldDefinition} for a column with a JS JSON array type.
|
|
62
|
+
*/
|
|
29
63
|
export declare class JSONArrayField extends EntityFieldDefinition<any[]> {
|
|
30
64
|
protected validateInputValueInternal(value: any[]): boolean;
|
|
31
65
|
}
|
|
66
|
+
/**
|
|
67
|
+
* {@link EntityFieldDefinition} for a column that may be a JS JSON array type.
|
|
68
|
+
* Does not do any validation.
|
|
69
|
+
*/
|
|
32
70
|
export declare class MaybeJSONArrayField extends EntityFieldDefinition<any | any[]> {
|
|
33
71
|
protected validateInputValueInternal(_value: any): boolean;
|
|
34
72
|
}
|
package/build/EntityFields.js
CHANGED
|
@@ -3,66 +3,104 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.MaybeJSONArrayField = exports.JSONArrayField = exports.EnumField = exports.JSONObjectField = exports.StringArrayField = exports.FloatField = exports.IntField = exports.BooleanField = exports.DateField = exports.UUIDField = exports.StringField = void 0;
|
|
4
4
|
const uuid_1 = require("uuid");
|
|
5
5
|
const EntityFieldDefinition_1 = require("./EntityFieldDefinition");
|
|
6
|
+
/**
|
|
7
|
+
* {@link EntityFieldDefinition} for a column with a JS string type.
|
|
8
|
+
*/
|
|
6
9
|
class StringField extends EntityFieldDefinition_1.EntityFieldDefinition {
|
|
7
10
|
validateInputValueInternal(value) {
|
|
8
11
|
return typeof value === 'string';
|
|
9
12
|
}
|
|
10
13
|
}
|
|
11
14
|
exports.StringField = StringField;
|
|
15
|
+
/**
|
|
16
|
+
* {@link EntityFieldDefinition} for a column with a JS string type.
|
|
17
|
+
* Enforces that the string is a valid UUID.
|
|
18
|
+
*/
|
|
12
19
|
class UUIDField extends StringField {
|
|
13
20
|
validateInputValueInternal(value) {
|
|
14
21
|
return (0, uuid_1.validate)(value);
|
|
15
22
|
}
|
|
16
23
|
}
|
|
17
24
|
exports.UUIDField = UUIDField;
|
|
25
|
+
/**
|
|
26
|
+
* {@link EntityFieldDefinition} for a column with a JS Date type.
|
|
27
|
+
*/
|
|
18
28
|
class DateField extends EntityFieldDefinition_1.EntityFieldDefinition {
|
|
19
29
|
validateInputValueInternal(value) {
|
|
20
30
|
return value instanceof Date;
|
|
21
31
|
}
|
|
22
32
|
}
|
|
23
33
|
exports.DateField = DateField;
|
|
34
|
+
/**
|
|
35
|
+
* {@link EntityFieldDefinition} for a column with a JS boolean type.
|
|
36
|
+
*/
|
|
24
37
|
class BooleanField extends EntityFieldDefinition_1.EntityFieldDefinition {
|
|
25
38
|
validateInputValueInternal(value) {
|
|
26
39
|
return typeof value === 'boolean';
|
|
27
40
|
}
|
|
28
41
|
}
|
|
29
42
|
exports.BooleanField = BooleanField;
|
|
43
|
+
/**
|
|
44
|
+
* {@link EntityFieldDefinition} for a column with a JS number type.
|
|
45
|
+
* Enforces that the number is an integer.
|
|
46
|
+
*/
|
|
30
47
|
class IntField extends EntityFieldDefinition_1.EntityFieldDefinition {
|
|
31
48
|
validateInputValueInternal(value) {
|
|
32
49
|
return typeof value === 'number' && Number.isInteger(value);
|
|
33
50
|
}
|
|
34
51
|
}
|
|
35
52
|
exports.IntField = IntField;
|
|
53
|
+
/**
|
|
54
|
+
* {@link EntityFieldDefinition} for a column with a JS number type.
|
|
55
|
+
* Enforces that the number is a float (which includes integers in JS).
|
|
56
|
+
*/
|
|
36
57
|
class FloatField extends EntityFieldDefinition_1.EntityFieldDefinition {
|
|
37
58
|
validateInputValueInternal(value) {
|
|
38
59
|
return typeof value === 'number';
|
|
39
60
|
}
|
|
40
61
|
}
|
|
41
62
|
exports.FloatField = FloatField;
|
|
63
|
+
/**
|
|
64
|
+
* {@link EntityFieldDefinition} for a column with a JS string array type.
|
|
65
|
+
* Enforces that every member of the string array is a string.
|
|
66
|
+
*/
|
|
42
67
|
class StringArrayField extends EntityFieldDefinition_1.EntityFieldDefinition {
|
|
43
68
|
validateInputValueInternal(value) {
|
|
44
69
|
return Array.isArray(value) && value.every((subValue) => typeof subValue === 'string');
|
|
45
70
|
}
|
|
46
71
|
}
|
|
47
72
|
exports.StringArrayField = StringArrayField;
|
|
73
|
+
/**
|
|
74
|
+
* {@link EntityFieldDefinition} for a column with a JS JSON object type.
|
|
75
|
+
*/
|
|
48
76
|
class JSONObjectField extends EntityFieldDefinition_1.EntityFieldDefinition {
|
|
49
77
|
validateInputValueInternal(value) {
|
|
50
78
|
return typeof value === 'object' && !Array.isArray(value);
|
|
51
79
|
}
|
|
52
80
|
}
|
|
53
81
|
exports.JSONObjectField = JSONObjectField;
|
|
82
|
+
/**
|
|
83
|
+
* {@link EntityFieldDefinition} for a enum column with a JS string or number type.
|
|
84
|
+
*/
|
|
54
85
|
class EnumField extends EntityFieldDefinition_1.EntityFieldDefinition {
|
|
55
86
|
validateInputValueInternal(value) {
|
|
56
87
|
return typeof value === 'number' || typeof value === 'string';
|
|
57
88
|
}
|
|
58
89
|
}
|
|
59
90
|
exports.EnumField = EnumField;
|
|
91
|
+
/**
|
|
92
|
+
* {@link EntityFieldDefinition} for a column with a JS JSON array type.
|
|
93
|
+
*/
|
|
60
94
|
class JSONArrayField extends EntityFieldDefinition_1.EntityFieldDefinition {
|
|
61
95
|
validateInputValueInternal(value) {
|
|
62
96
|
return Array.isArray(value);
|
|
63
97
|
}
|
|
64
98
|
}
|
|
65
99
|
exports.JSONArrayField = JSONArrayField;
|
|
100
|
+
/**
|
|
101
|
+
* {@link EntityFieldDefinition} for a column that may be a JS JSON array type.
|
|
102
|
+
* Does not do any validation.
|
|
103
|
+
*/
|
|
66
104
|
class MaybeJSONArrayField extends EntityFieldDefinition_1.EntityFieldDefinition {
|
|
67
105
|
validateInputValueInternal(_value) {
|
|
68
106
|
return true;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"EntityFields.js","sourceRoot":"","sources":["../src/EntityFields.ts"],"names":[],"mappings":";;;AAAA,+BAAgD;AAEhD,mEAAgE;AAEhE,MAAa,WAAY,SAAQ,6CAA6B;IAClD,0BAA0B,CAAC,KAAa;QAChD,OAAO,OAAO,KAAK,KAAK,QAAQ,CAAC;IACnC,CAAC;CACF;AAJD,kCAIC;
|
|
1
|
+
{"version":3,"file":"EntityFields.js","sourceRoot":"","sources":["../src/EntityFields.ts"],"names":[],"mappings":";;;AAAA,+BAAgD;AAEhD,mEAAgE;AAEhE;;GAEG;AACH,MAAa,WAAY,SAAQ,6CAA6B;IAClD,0BAA0B,CAAC,KAAa;QAChD,OAAO,OAAO,KAAK,KAAK,QAAQ,CAAC;IACnC,CAAC;CACF;AAJD,kCAIC;AAED;;;GAGG;AACH,MAAa,SAAU,SAAQ,WAAW;IACrB,0BAA0B,CAAC,KAAa;QACzD,OAAO,IAAA,eAAY,EAAC,KAAK,CAAC,CAAC;IAC7B,CAAC;CACF;AAJD,8BAIC;AAED;;GAEG;AACH,MAAa,SAAU,SAAQ,6CAA2B;IAC9C,0BAA0B,CAAC,KAAW;QAC9C,OAAO,KAAK,YAAY,IAAI,CAAC;IAC/B,CAAC;CACF;AAJD,8BAIC;AAED;;GAEG;AACH,MAAa,YAAa,SAAQ,6CAA8B;IACpD,0BAA0B,CAAC,KAAc;QACjD,OAAO,OAAO,KAAK,KAAK,SAAS,CAAC;IACpC,CAAC;CACF;AAJD,oCAIC;AAED;;;GAGG;AACH,MAA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
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { ISecondaryEntityCache } from './EntitySecondaryCacheLoader';
|
|
2
|
+
import IEntityGenericCacher from './IEntityGenericCacher';
|
|
3
|
+
/**
|
|
4
|
+
* A custom secondary read-through entity cache is a way to add a custom second layer of caching for a particular
|
|
5
|
+
* single entity load. One common way this may be used is to add a second layer of caching in a hot path that makes
|
|
6
|
+
* a call to {@link EntityLoader.loadManyByFieldEqualityConjunctionAsync} is guaranteed to return at most one entity.
|
|
7
|
+
*/
|
|
8
|
+
export default abstract class GenericSecondaryEntityCache<TFields, TLoadParams> implements ISecondaryEntityCache<TFields, TLoadParams> {
|
|
9
|
+
protected readonly cacher: IEntityGenericCacher<TFields>;
|
|
10
|
+
protected readonly constructCacheKey: (params: Readonly<TLoadParams>) => string;
|
|
11
|
+
constructor(cacher: IEntityGenericCacher<TFields>, constructCacheKey: (params: Readonly<TLoadParams>) => string);
|
|
12
|
+
loadManyThroughAsync(loadParamsArray: readonly Readonly<TLoadParams>[], fetcher: (fetcherLoadParamsArray: readonly Readonly<TLoadParams>[]) => Promise<ReadonlyMap<Readonly<TLoadParams>, Readonly<TFields> | null>>): Promise<ReadonlyMap<Readonly<TLoadParams>, Readonly<TFields> | null>>;
|
|
13
|
+
/**
|
|
14
|
+
* Invalidate the cache for objects cached by constructCacheKey(loadParams).
|
|
15
|
+
*
|
|
16
|
+
* @param loadParamsArray - load params to invalidate
|
|
17
|
+
*/
|
|
18
|
+
invalidateManyAsync(loadParamsArray: readonly Readonly<TLoadParams>[]): Promise<void>;
|
|
19
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
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
|
+
const invariant_1 = __importDefault(require("invariant"));
|
|
7
|
+
const ReadThroughEntityCache_1 = require("./internal/ReadThroughEntityCache");
|
|
8
|
+
const maps_1 = require("./utils/collections/maps");
|
|
9
|
+
/**
|
|
10
|
+
* A custom secondary read-through entity cache is a way to add a custom second layer of caching for a particular
|
|
11
|
+
* single entity load. One common way this may be used is to add a second layer of caching in a hot path that makes
|
|
12
|
+
* a call to {@link EntityLoader.loadManyByFieldEqualityConjunctionAsync} is guaranteed to return at most one entity.
|
|
13
|
+
*/
|
|
14
|
+
class GenericSecondaryEntityCache {
|
|
15
|
+
constructor(cacher, constructCacheKey) {
|
|
16
|
+
this.cacher = cacher;
|
|
17
|
+
this.constructCacheKey = constructCacheKey;
|
|
18
|
+
}
|
|
19
|
+
async loadManyThroughAsync(loadParamsArray, fetcher) {
|
|
20
|
+
const cacheKeys = loadParamsArray.map(this.constructCacheKey);
|
|
21
|
+
const cacheKeyToLoadParamsMap = (0, maps_1.zipToMap)(cacheKeys, loadParamsArray);
|
|
22
|
+
const cacheLoadResults = await this.cacher.loadManyAsync(cacheKeys);
|
|
23
|
+
(0, invariant_1.default)(cacheLoadResults.size === loadParamsArray.length, `${this.constructor.name} loadMany should return a result for each key`);
|
|
24
|
+
const cacheKeysToFetch = Array.from((0, maps_1.filterMap)(cacheLoadResults, (cacheLoadResult) => cacheLoadResult.status === ReadThroughEntityCache_1.CacheStatus.MISS).keys());
|
|
25
|
+
// put cache hits in result map
|
|
26
|
+
const results = new Map();
|
|
27
|
+
cacheLoadResults.forEach((cacheLoadResult, cacheKey) => {
|
|
28
|
+
if (cacheLoadResult.status === ReadThroughEntityCache_1.CacheStatus.HIT) {
|
|
29
|
+
const loadParams = cacheKeyToLoadParamsMap.get(cacheKey);
|
|
30
|
+
(0, invariant_1.default)(loadParams !== undefined, 'load params should be in cache key map');
|
|
31
|
+
results.set(loadParams, cacheLoadResult.item);
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
// fetch any misses from DB, add DB objects to results, cache DB results, inform cache of any missing DB results
|
|
35
|
+
if (cacheKeysToFetch.length > 0) {
|
|
36
|
+
const loadParamsToFetch = cacheKeysToFetch.map((cacheKey) => {
|
|
37
|
+
const loadParams = cacheKeyToLoadParamsMap.get(cacheKey);
|
|
38
|
+
(0, invariant_1.default)(loadParams !== undefined, 'load params should be in cache key map');
|
|
39
|
+
return loadParams;
|
|
40
|
+
});
|
|
41
|
+
const fetchResults = await fetcher(loadParamsToFetch);
|
|
42
|
+
const fetchMisses = loadParamsToFetch.filter((loadParams) => {
|
|
43
|
+
// all values of fetchResults should be field objects or undefined
|
|
44
|
+
return !fetchResults.get(loadParams);
|
|
45
|
+
});
|
|
46
|
+
for (const fetchMiss of fetchMisses) {
|
|
47
|
+
results.set(fetchMiss, null);
|
|
48
|
+
}
|
|
49
|
+
const objectsToCache = new Map();
|
|
50
|
+
for (const [loadParams, object] of fetchResults.entries()) {
|
|
51
|
+
if (object) {
|
|
52
|
+
objectsToCache.set(this.constructCacheKey(loadParams), object);
|
|
53
|
+
results.set(loadParams, object);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
await Promise.all([
|
|
57
|
+
this.cacher.cacheManyAsync(objectsToCache),
|
|
58
|
+
this.cacher.cacheDBMissesAsync(fetchMisses.map(this.constructCacheKey)),
|
|
59
|
+
]);
|
|
60
|
+
}
|
|
61
|
+
return results;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Invalidate the cache for objects cached by constructCacheKey(loadParams).
|
|
65
|
+
*
|
|
66
|
+
* @param loadParamsArray - load params to invalidate
|
|
67
|
+
*/
|
|
68
|
+
invalidateManyAsync(loadParamsArray) {
|
|
69
|
+
const cacheKeys = loadParamsArray.map(this.constructCacheKey);
|
|
70
|
+
return this.cacher.invalidateManyAsync(cacheKeys);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
exports.default = GenericSecondaryEntityCache;
|
|
74
|
+
//# sourceMappingURL=GenericSecondaryEntityCache.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"GenericSec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AAM,CAAC,kBAAkB,CAAC,WAAW,CAAC,GAAG,CAAC,IAAI,CAAC,iBAAiB,CAAC,CAAC;aACxE,CAAC,CAAC;SACJ;QAED,OAAO,OAAO,CAAC;IACjB,CAAC;IAED;;;;OAIG;IACI,mBAAmB,CAAC,eAAiD;QAC1E,MAAM,SAAS,GAAG,eAAe,CAAC,GAAG,CAAC,IAAI,CAAC,iBAAiB,CAAC,CAAC;QAC9D,OAAO,IAAI,CAAC,MAAM,CAAC,mBAAmB,CAAC,SAAS,CAAC,CAAC;IACpD,CAAC;CACF;AArFD,8CAqFC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { CacheLoadResult } from './internal/ReadThroughEntityCache';
|
|
2
|
+
/**
|
|
3
|
+
* A cacher stores and loads key-value pairs. It also supports negative caching - it stores the absence
|
|
4
|
+
* of keys that don't exist in the backing datastore.
|
|
5
|
+
*/
|
|
6
|
+
export default interface IEntityGenericCacher<TFields> {
|
|
7
|
+
loadManyAsync(keys: readonly string[]): Promise<ReadonlyMap<string, CacheLoadResult<TFields>>>;
|
|
8
|
+
cacheManyAsync(objectMap: ReadonlyMap<string, Readonly<TFields>>): Promise<void>;
|
|
9
|
+
cacheDBMissesAsync(keys: string[]): Promise<void>;
|
|
10
|
+
invalidateManyAsync(keys: string[]): Promise<void>;
|
|
11
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"IEntityGenericCacher.js","sourceRoot":"","sources":["../src/IEntityGenericCacher.ts"],"names":[],"mappings":""}
|
package/build/index.d.ts
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
* @packageDocumentation
|
|
3
3
|
* @module @expo/entity
|
|
4
4
|
*/
|
|
5
|
+
export { default as GenericSecondaryEntityCache } from './GenericSecondaryEntityCache';
|
|
5
6
|
export { default as EnforcingEntityLoader } from './EnforcingEntityLoader';
|
|
6
7
|
export { default as Entity } from './Entity';
|
|
7
8
|
export * from './Entity';
|
|
@@ -41,6 +42,7 @@ export * from './EntityQueryContext';
|
|
|
41
42
|
export { default as IEntityCacheAdapterProvider } from './IEntityCacheAdapterProvider';
|
|
42
43
|
export { default as IEntityDatabaseAdapterProvider } from './IEntityDatabaseAdapterProvider';
|
|
43
44
|
export { default as EntityQueryContextProvider } from './EntityQueryContextProvider';
|
|
45
|
+
export { default as IEntityGenericCacher } from './IEntityGenericCacher';
|
|
44
46
|
export { default as ReadonlyEntity } from './ReadonlyEntity';
|
|
45
47
|
export { default as ViewerContext } from './ViewerContext';
|
|
46
48
|
export { default as ViewerScopedEntityCompanion } from './ViewerScopedEntityCompanion';
|
package/build/index.js
CHANGED
|
@@ -18,7 +18,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
18
18
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
19
19
|
};
|
|
20
20
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
21
|
-
exports.StubQueryContextProvider = exports.StubDatabaseAdapterProvider = exports.StubDatabaseAdapter = exports.describeFieldTestCase = exports.PrivacyPolicyRule = exports.AlwaysSkipPrivacyPolicyRule = exports.AlwaysDenyPrivacyPolicyRule = exports.AlwaysAllowPrivacyPolicyRule = exports.NoOpEntityMetricsAdapter = exports.ReadThroughEntityCache = exports.EntityDataManager = exports.ViewerScopedEntityMutatorFactory = exports.ViewerScopedEntityLoaderFactory = exports.ViewerScopedEntityCompanionProvider = exports.ViewerScopedEntityCompanion = exports.ViewerContext = exports.ReadonlyEntity = exports.EntityQueryContextProvider = exports.EntityPrivacyPolicy = exports.EntityMutatorFactory = exports.EntityMutationValidator = exports.EntitySecondaryCacheLoader = exports.EntityLoaderFactory = exports.EntityLoader = exports.EntityNotFoundError = exports.EntityNotAuthorizedError = exports.EntityError = exports.EntityCacheAdapterError = exports.EntityDatabaseAdapterError = exports.EntityDatabaseAdapter = exports.EntityConfiguration = exports.EntityCompanionProvider = exports.EntityCompanion = exports.EntityCacheAdapter = exports.EntityAssociationLoader = exports.Entity = exports.EnforcingEntityLoader = void 0;
|
|
21
|
+
exports.StubQueryContextProvider = exports.StubDatabaseAdapterProvider = exports.StubDatabaseAdapter = exports.describeFieldTestCase = exports.PrivacyPolicyRule = exports.AlwaysSkipPrivacyPolicyRule = exports.AlwaysDenyPrivacyPolicyRule = exports.AlwaysAllowPrivacyPolicyRule = exports.NoOpEntityMetricsAdapter = exports.ReadThroughEntityCache = exports.EntityDataManager = exports.ViewerScopedEntityMutatorFactory = exports.ViewerScopedEntityLoaderFactory = exports.ViewerScopedEntityCompanionProvider = exports.ViewerScopedEntityCompanion = exports.ViewerContext = exports.ReadonlyEntity = exports.EntityQueryContextProvider = exports.EntityPrivacyPolicy = exports.EntityMutatorFactory = exports.EntityMutationValidator = exports.EntitySecondaryCacheLoader = exports.EntityLoaderFactory = exports.EntityLoader = exports.EntityNotFoundError = exports.EntityNotAuthorizedError = exports.EntityError = exports.EntityCacheAdapterError = exports.EntityDatabaseAdapterError = exports.EntityDatabaseAdapter = exports.EntityConfiguration = exports.EntityCompanionProvider = exports.EntityCompanion = exports.EntityCacheAdapter = exports.EntityAssociationLoader = exports.Entity = exports.EnforcingEntityLoader = exports.GenericSecondaryEntityCache = void 0;
|
|
22
|
+
var GenericSecondaryEntityCache_1 = require("./GenericSecondaryEntityCache");
|
|
23
|
+
Object.defineProperty(exports, "GenericSecondaryEntityCache", { enumerable: true, get: function () { return __importDefault(GenericSecondaryEntityCache_1).default; } });
|
|
22
24
|
var EnforcingEntityLoader_1 = require("./EnforcingEntityLoader");
|
|
23
25
|
Object.defineProperty(exports, "EnforcingEntityLoader", { enumerable: true, get: function () { return __importDefault(EnforcingEntityLoader_1).default; } });
|
|
24
26
|
var Entity_1 = require("./Entity");
|
package/build/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,iCAAiC;AACjC;;;GAGG;;;;;;;;;;;;;;;;AAEH,iEAA2E;AAA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A,OAAO,OAAuC;AACvD,qFAA+F;AAAtF,mKAAA,OAAO,OAAmC;AACnD,uFAAiG;AAAxF,qKAAA,OAAO,OAAoC;AACpD,gDAA8B;AAC9B,kEAA4E;AAAnE,uIAAA,OAAO,OAAqB;AACrC,4EAA0D;AAC1D,4EAAsF;AAA7E,iJAAA,OAAO,OAA0B;AAC1C,oEAAkD;AAClD,+DAA6C;AAE7C,kEAAgD;AAChD,+EAAyF;AAAhF,qJAAA,OAAO,OAA4B;AAC5C,qFAA+F;AAAtF,6JAAA,OAAO,OAAgC;AAChD,mFAA6F;AAApF,2JAAA,OAAO,OAA+B;AAC/C,mFAA6F;AAApF,2JAAA,OAAO,OAA+B;AAC/C,+DAAyE;AAAhE,uIAAA,OAAO,OAAqB;AACrC,4DAA0C;AAC1C,6EAA2D;AAC3D,mEAAiD;AACjD,+EAAyF;AAAhF,+IAAA,OAAO,OAAyB;AACzC,2EAAqF;AAA5E,2IAAA,OAAO,OAAuB;AACvC,2FAAqG;AAA5F,2JAAA,OAAO,OAA+B;AAC/C,qFAA+F;AAAtF,qJAAA,OAAO,OAA4B;AAC5C,wFAAsE;AACtE,2DAAyC"}
|
package/package.json
CHANGED
|
@@ -28,6 +28,9 @@ export enum EntityEdgeDeletionBehavior {
|
|
|
28
28
|
SET_NULL,
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
+
/**
|
|
32
|
+
* Defines an association between entities. An association is primarily used to define cascading deletion behavior.
|
|
33
|
+
*/
|
|
31
34
|
export interface EntityAssociationDefinition<
|
|
32
35
|
TViewerContext extends ViewerContext,
|
|
33
36
|
TAssociatedFields,
|
|
@@ -83,6 +86,10 @@ export interface EntityAssociationDefinition<
|
|
|
83
86
|
edgeDeletionBehavior?: EntityEdgeDeletionBehavior;
|
|
84
87
|
}
|
|
85
88
|
|
|
89
|
+
/**
|
|
90
|
+
* Definition for a field referencing a column in the underlying database. Specifies things like
|
|
91
|
+
* cache behavior and associations, and handles input validation.
|
|
92
|
+
*/
|
|
86
93
|
export abstract class EntityFieldDefinition<T> {
|
|
87
94
|
readonly columnName: string;
|
|
88
95
|
readonly cache: boolean;
|
|
@@ -93,6 +100,7 @@ export abstract class EntityFieldDefinition<T> {
|
|
|
93
100
|
* @param cache - Whether or not to cache loaded instances of the entity by this field. The column name is
|
|
94
101
|
* used to derive a cache key for the cache entry. If true, this column must be able uniquely
|
|
95
102
|
* identify the entity.
|
|
103
|
+
* @param association - Defines the association behavior for an entity that this column references.
|
|
96
104
|
*/
|
|
97
105
|
constructor({
|
|
98
106
|
columnName,
|
package/src/EntityFields.ts
CHANGED
|
@@ -2,59 +2,104 @@ import { validate as validateUUID } from 'uuid';
|
|
|
2
2
|
|
|
3
3
|
import { EntityFieldDefinition } from './EntityFieldDefinition';
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* {@link EntityFieldDefinition} for a column with a JS string type.
|
|
7
|
+
*/
|
|
5
8
|
export class StringField extends EntityFieldDefinition<string> {
|
|
6
9
|
protected validateInputValueInternal(value: string): boolean {
|
|
7
10
|
return typeof value === 'string';
|
|
8
11
|
}
|
|
9
12
|
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* {@link EntityFieldDefinition} for a column with a JS string type.
|
|
16
|
+
* Enforces that the string is a valid UUID.
|
|
17
|
+
*/
|
|
10
18
|
export class UUIDField extends StringField {
|
|
11
19
|
protected override validateInputValueInternal(value: string): boolean {
|
|
12
20
|
return validateUUID(value);
|
|
13
21
|
}
|
|
14
22
|
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* {@link EntityFieldDefinition} for a column with a JS Date type.
|
|
26
|
+
*/
|
|
15
27
|
export class DateField extends EntityFieldDefinition<Date> {
|
|
16
28
|
protected validateInputValueInternal(value: Date): boolean {
|
|
17
29
|
return value instanceof Date;
|
|
18
30
|
}
|
|
19
31
|
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* {@link EntityFieldDefinition} for a column with a JS boolean type.
|
|
35
|
+
*/
|
|
20
36
|
export class BooleanField extends EntityFieldDefinition<boolean> {
|
|
21
37
|
protected validateInputValueInternal(value: boolean): boolean {
|
|
22
38
|
return typeof value === 'boolean';
|
|
23
39
|
}
|
|
24
40
|
}
|
|
25
41
|
|
|
42
|
+
/**
|
|
43
|
+
* {@link EntityFieldDefinition} for a column with a JS number type.
|
|
44
|
+
* Enforces that the number is an integer.
|
|
45
|
+
*/
|
|
26
46
|
export class IntField extends EntityFieldDefinition<number> {
|
|
27
47
|
protected validateInputValueInternal(value: number): boolean {
|
|
28
48
|
return typeof value === 'number' && Number.isInteger(value);
|
|
29
49
|
}
|
|
30
50
|
}
|
|
31
51
|
|
|
52
|
+
/**
|
|
53
|
+
* {@link EntityFieldDefinition} for a column with a JS number type.
|
|
54
|
+
* Enforces that the number is a float (which includes integers in JS).
|
|
55
|
+
*/
|
|
32
56
|
export class FloatField extends EntityFieldDefinition<number> {
|
|
33
57
|
protected validateInputValueInternal(value: number): boolean {
|
|
34
58
|
return typeof value === 'number';
|
|
35
59
|
}
|
|
36
60
|
}
|
|
37
61
|
|
|
62
|
+
/**
|
|
63
|
+
* {@link EntityFieldDefinition} for a column with a JS string array type.
|
|
64
|
+
* Enforces that every member of the string array is a string.
|
|
65
|
+
*/
|
|
38
66
|
export class StringArrayField extends EntityFieldDefinition<string[]> {
|
|
39
67
|
protected validateInputValueInternal(value: string[]): boolean {
|
|
40
68
|
return Array.isArray(value) && value.every((subValue) => typeof subValue === 'string');
|
|
41
69
|
}
|
|
42
70
|
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* {@link EntityFieldDefinition} for a column with a JS JSON object type.
|
|
74
|
+
*/
|
|
43
75
|
export class JSONObjectField extends EntityFieldDefinition<object> {
|
|
44
76
|
protected validateInputValueInternal(value: object): boolean {
|
|
45
77
|
return typeof value === 'object' && !Array.isArray(value);
|
|
46
78
|
}
|
|
47
79
|
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* {@link EntityFieldDefinition} for a enum column with a JS string or number type.
|
|
83
|
+
*/
|
|
48
84
|
export class EnumField extends EntityFieldDefinition<string | number> {
|
|
49
85
|
protected validateInputValueInternal(value: string | number): boolean {
|
|
50
86
|
return typeof value === 'number' || typeof value === 'string';
|
|
51
87
|
}
|
|
52
88
|
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* {@link EntityFieldDefinition} for a column with a JS JSON array type.
|
|
92
|
+
*/
|
|
53
93
|
export class JSONArrayField extends EntityFieldDefinition<any[]> {
|
|
54
94
|
protected validateInputValueInternal(value: any[]): boolean {
|
|
55
95
|
return Array.isArray(value);
|
|
56
96
|
}
|
|
57
97
|
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* {@link EntityFieldDefinition} for a column that may be a JS JSON array type.
|
|
101
|
+
* Does not do any validation.
|
|
102
|
+
*/
|
|
58
103
|
export class MaybeJSONArrayField extends EntityFieldDefinition<any | any[]> {
|
|
59
104
|
protected validateInputValueInternal(_value: any): boolean {
|
|
60
105
|
return true;
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import invariant from 'invariant';
|
|
2
|
+
|
|
3
|
+
import { ISecondaryEntityCache } from './EntitySecondaryCacheLoader';
|
|
4
|
+
import IEntityGenericCacher from './IEntityGenericCacher';
|
|
5
|
+
import { CacheStatus } from './internal/ReadThroughEntityCache';
|
|
6
|
+
import { filterMap, zipToMap } from './utils/collections/maps';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* A custom secondary read-through entity cache is a way to add a custom second layer of caching for a particular
|
|
10
|
+
* single entity load. One common way this may be used is to add a second layer of caching in a hot path that makes
|
|
11
|
+
* a call to {@link EntityLoader.loadManyByFieldEqualityConjunctionAsync} is guaranteed to return at most one entity.
|
|
12
|
+
*/
|
|
13
|
+
export default abstract class GenericSecondaryEntityCache<TFields, TLoadParams>
|
|
14
|
+
implements ISecondaryEntityCache<TFields, TLoadParams>
|
|
15
|
+
{
|
|
16
|
+
constructor(
|
|
17
|
+
protected readonly cacher: IEntityGenericCacher<TFields>,
|
|
18
|
+
protected readonly constructCacheKey: (params: Readonly<TLoadParams>) => string
|
|
19
|
+
) {}
|
|
20
|
+
|
|
21
|
+
public async loadManyThroughAsync(
|
|
22
|
+
loadParamsArray: readonly Readonly<TLoadParams>[],
|
|
23
|
+
fetcher: (
|
|
24
|
+
fetcherLoadParamsArray: readonly Readonly<TLoadParams>[]
|
|
25
|
+
) => Promise<ReadonlyMap<Readonly<TLoadParams>, Readonly<TFields> | null>>
|
|
26
|
+
): Promise<ReadonlyMap<Readonly<TLoadParams>, Readonly<TFields> | null>> {
|
|
27
|
+
const cacheKeys = loadParamsArray.map(this.constructCacheKey);
|
|
28
|
+
const cacheKeyToLoadParamsMap = zipToMap(cacheKeys, loadParamsArray);
|
|
29
|
+
|
|
30
|
+
const cacheLoadResults = await this.cacher.loadManyAsync(cacheKeys);
|
|
31
|
+
|
|
32
|
+
invariant(
|
|
33
|
+
cacheLoadResults.size === loadParamsArray.length,
|
|
34
|
+
`${this.constructor.name} loadMany should return a result for each key`
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
const cacheKeysToFetch = Array.from(
|
|
38
|
+
filterMap(
|
|
39
|
+
cacheLoadResults,
|
|
40
|
+
(cacheLoadResult) => cacheLoadResult.status === CacheStatus.MISS
|
|
41
|
+
).keys()
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
// put cache hits in result map
|
|
45
|
+
const results: Map<Readonly<TLoadParams>, Readonly<TFields> | null> = new Map();
|
|
46
|
+
cacheLoadResults.forEach((cacheLoadResult, cacheKey) => {
|
|
47
|
+
if (cacheLoadResult.status === CacheStatus.HIT) {
|
|
48
|
+
const loadParams = cacheKeyToLoadParamsMap.get(cacheKey);
|
|
49
|
+
invariant(loadParams !== undefined, 'load params should be in cache key map');
|
|
50
|
+
results.set(loadParams, cacheLoadResult.item);
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
// fetch any misses from DB, add DB objects to results, cache DB results, inform cache of any missing DB results
|
|
55
|
+
if (cacheKeysToFetch.length > 0) {
|
|
56
|
+
const loadParamsToFetch = cacheKeysToFetch.map((cacheKey) => {
|
|
57
|
+
const loadParams = cacheKeyToLoadParamsMap.get(cacheKey);
|
|
58
|
+
invariant(loadParams !== undefined, 'load params should be in cache key map');
|
|
59
|
+
return loadParams;
|
|
60
|
+
});
|
|
61
|
+
const fetchResults = await fetcher(loadParamsToFetch);
|
|
62
|
+
|
|
63
|
+
const fetchMisses = loadParamsToFetch.filter((loadParams) => {
|
|
64
|
+
// all values of fetchResults should be field objects or undefined
|
|
65
|
+
return !fetchResults.get(loadParams);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
for (const fetchMiss of fetchMisses) {
|
|
69
|
+
results.set(fetchMiss, null);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const objectsToCache: Map<string, Readonly<TFields>> = new Map();
|
|
73
|
+
for (const [loadParams, object] of fetchResults.entries()) {
|
|
74
|
+
if (object) {
|
|
75
|
+
objectsToCache.set(this.constructCacheKey(loadParams), object);
|
|
76
|
+
results.set(loadParams, object);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
await Promise.all([
|
|
81
|
+
this.cacher.cacheManyAsync(objectsToCache),
|
|
82
|
+
this.cacher.cacheDBMissesAsync(fetchMisses.map(this.constructCacheKey)),
|
|
83
|
+
]);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return results;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Invalidate the cache for objects cached by constructCacheKey(loadParams).
|
|
91
|
+
*
|
|
92
|
+
* @param loadParamsArray - load params to invalidate
|
|
93
|
+
*/
|
|
94
|
+
public invalidateManyAsync(loadParamsArray: readonly Readonly<TLoadParams>[]): Promise<void> {
|
|
95
|
+
const cacheKeys = loadParamsArray.map(this.constructCacheKey);
|
|
96
|
+
return this.cacher.invalidateManyAsync(cacheKeys);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { CacheLoadResult } from './internal/ReadThroughEntityCache';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A cacher stores and loads key-value pairs. It also supports negative caching - it stores the absence
|
|
5
|
+
* of keys that don't exist in the backing datastore.
|
|
6
|
+
*/
|
|
7
|
+
export default interface IEntityGenericCacher<TFields> {
|
|
8
|
+
loadManyAsync(keys: readonly string[]): Promise<ReadonlyMap<string, CacheLoadResult<TFields>>>;
|
|
9
|
+
|
|
10
|
+
cacheManyAsync(objectMap: ReadonlyMap<string, Readonly<TFields>>): Promise<void>;
|
|
11
|
+
|
|
12
|
+
cacheDBMissesAsync(keys: string[]): Promise<void>;
|
|
13
|
+
|
|
14
|
+
invalidateManyAsync(keys: string[]): Promise<void>;
|
|
15
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
* @module @expo/entity
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
+
export { default as GenericSecondaryEntityCache } from './GenericSecondaryEntityCache';
|
|
7
8
|
export { default as EnforcingEntityLoader } from './EnforcingEntityLoader';
|
|
8
9
|
export { default as Entity } from './Entity';
|
|
9
10
|
export * from './Entity';
|
|
@@ -43,6 +44,7 @@ export * from './EntityQueryContext';
|
|
|
43
44
|
export { default as IEntityCacheAdapterProvider } from './IEntityCacheAdapterProvider';
|
|
44
45
|
export { default as IEntityDatabaseAdapterProvider } from './IEntityDatabaseAdapterProvider';
|
|
45
46
|
export { default as EntityQueryContextProvider } from './EntityQueryContextProvider';
|
|
47
|
+
export { default as IEntityGenericCacher } from './IEntityGenericCacher';
|
|
46
48
|
export { default as ReadonlyEntity } from './ReadonlyEntity';
|
|
47
49
|
export { default as ViewerContext } from './ViewerContext';
|
|
48
50
|
export { default as ViewerScopedEntityCompanion } from './ViewerScopedEntityCompanion';
|