angular-odata 0.145.0 → 0.146.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.
@@ -12241,7 +12241,7 @@ class ODataModelOptions {
12241
12241
  let resource = null;
12242
12242
  let prevField = null;
12243
12243
  for (const [model, field] of ODataModelOptions.chain(child)) {
12244
- resource = resource || model._resource;
12244
+ resource = resource || model._resource?.clone();
12245
12245
  if (resource === null)
12246
12246
  break;
12247
12247
  if (ODataModelOptions.isModel(model) && (prevField === null || prevField.collection)) {
@@ -12443,10 +12443,8 @@ class ODataModelOptions {
12443
12443
  }
12444
12444
  }
12445
12445
  asEntity(self, ctx) {
12446
- // Clone query from him or parent
12447
- let query = self._resource?.cloneQuery();
12448
- if (query === undefined && self._parent && self._parent[0] instanceof ODataCollection)
12449
- query = self._parent[0]._resource?.cloneQuery();
12446
+ // Clone query from resource
12447
+ let query = self.resource()?.cloneQuery();
12450
12448
  // Build new resource
12451
12449
  const resource = this.modelResourceFactory(query);
12452
12450
  return this.withResource(self, resource, ctx);
@@ -14054,10 +14052,16 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.0.5", ngImpor
14054
14052
 
14055
14053
  class ODataBaseCache {
14056
14054
  maxAge;
14057
- entries;
14058
14055
  constructor({ maxAge = DEFAULT_MAXAGE }) {
14059
14056
  this.maxAge = maxAge;
14060
- this.entries = new Map();
14057
+ }
14058
+ /**
14059
+ * Check if the entry is expired
14060
+ * @param entry The cache entry
14061
+ * @returns Boolean indicating if the entry is expired
14062
+ */
14063
+ isExpired(entry) {
14064
+ return entry.date < (Date.now() - entry.maxAge);
14061
14065
  }
14062
14066
  /**
14063
14067
  * Using the resource on the request build an array of string to identify the scope of the request
@@ -14110,6 +14114,14 @@ class ODataBaseCache {
14110
14114
  buildKey(names) {
14111
14115
  return names.join(CACHE_KEY_SEPARATOR);
14112
14116
  }
14117
+ }
14118
+
14119
+ class ODataInMemoryCache extends ODataBaseCache {
14120
+ entries;
14121
+ constructor({ maxAge } = {}) {
14122
+ super({ maxAge });
14123
+ this.entries = new Map();
14124
+ }
14113
14125
  /**
14114
14126
  * Put some payload in the cache
14115
14127
  * @param name The name for the entry
@@ -14134,12 +14146,35 @@ class ODataBaseCache {
14134
14146
  const entry = this.entries.get(key);
14135
14147
  return entry !== undefined && !this.isExpired(entry) ? entry.payload : undefined;
14136
14148
  }
14149
+ /**
14150
+ * Store the response in the cache
14151
+ * @param req The request with the resource to store the response
14152
+ * @param res The response to store in the cache
14153
+ */
14154
+ putResponse(req, res) {
14155
+ let scope = this.scope(req);
14156
+ let tags = this.tags(res);
14157
+ this.put(req.cacheKey, res, {
14158
+ maxAge: req.maxAge ?? res.options.maxAge,
14159
+ scope,
14160
+ tags,
14161
+ });
14162
+ }
14163
+ /**
14164
+ * Restore the response from the cache
14165
+ * @param req The request with the resource to get the response
14166
+ * @returns The response from the cache
14167
+ */
14168
+ getResponse(req) {
14169
+ let scope = this.scope(req);
14170
+ return this.get(req.cacheKey, { scope });
14171
+ }
14137
14172
  /**
14138
14173
  * Remove all cache entries that are matching with the given options
14139
14174
  * @param options The options to forget
14140
14175
  */
14141
14176
  forget({ name, scope = [], tags = [], } = {}) {
14142
- if (name !== undefined)
14177
+ if (name)
14143
14178
  scope.push(name);
14144
14179
  const key = scope.length > 0 ? this.buildKey(scope) : undefined;
14145
14180
  this.entries.forEach((entry, k) => {
@@ -14157,19 +14192,53 @@ class ODataBaseCache {
14157
14192
  flush() {
14158
14193
  this.entries = new Map();
14159
14194
  }
14160
- /**
14161
- * Check if the entry is expired
14162
- * @param entry The cache entry
14163
- * @returns Boolean indicating if the entry is expired
14164
- */
14165
- isExpired(entry) {
14166
- return entry.date < Date.now() - entry.maxAge;
14195
+ size() {
14196
+ return this.entries.size;
14167
14197
  }
14168
14198
  }
14169
14199
 
14170
- class ODataInMemoryCache extends ODataBaseCache {
14171
- constructor({ maxAge } = {}) {
14200
+ class ODataInStorageCache extends ODataBaseCache {
14201
+ prefix;
14202
+ storage;
14203
+ constructor({ prefix, storage = sessionStorage, maxAge, }) {
14172
14204
  super({ maxAge });
14205
+ this.prefix = prefix;
14206
+ this.storage = storage;
14207
+ }
14208
+ buildKey(names) {
14209
+ return super.buildKey([this.prefix, ...names]);
14210
+ }
14211
+ put(name, payload, { maxAge, scope, tags } = {}) {
14212
+ const entry = this.buildEntry(payload, { maxAge, tags });
14213
+ const key = this.buildKey([...(scope ?? []), name]);
14214
+ this.storage.setItem(key, JSON.stringify(entry));
14215
+ }
14216
+ get(name, { scope } = {}) {
14217
+ const key = this.buildKey([...(scope || []), name]);
14218
+ const entry = JSON.parse(this.storage.getItem(key) ?? "{}");
14219
+ return entry !== undefined && !this.isExpired(entry) ? entry.payload : undefined;
14220
+ }
14221
+ forget({ name, scope = [], tags = [], }) {
14222
+ if (name)
14223
+ scope.push(name);
14224
+ const key = scope.length > 0 ? this.buildKey(scope) : undefined;
14225
+ Object.keys(this.storage).filter(k => k.startsWith(this.prefix)).forEach(k => {
14226
+ const entry = JSON.parse(this.storage.getItem(k) ?? "{}");
14227
+ if (this.isExpired(entry) || // Expired
14228
+ (key !== undefined && k.startsWith(key)) || // Key
14229
+ (tags.length > 0 && tags.some((t) => entry.tags.indexOf(t) !== -1)) // Tags
14230
+ ) {
14231
+ this.storage.removeItem(k);
14232
+ }
14233
+ });
14234
+ }
14235
+ /**
14236
+ * Flush the cache and clean the storage
14237
+ */
14238
+ flush() {
14239
+ Object.keys(this.storage).filter(k => k.startsWith(this.prefix)).forEach(k => {
14240
+ this.storage.removeItem(k);
14241
+ });
14173
14242
  }
14174
14243
  /**
14175
14244
  * Store the response in the cache
@@ -14177,9 +14246,9 @@ class ODataInMemoryCache extends ODataBaseCache {
14177
14246
  * @param res The response to store in the cache
14178
14247
  */
14179
14248
  putResponse(req, res) {
14180
- let scope = this.scope(req);
14181
- let tags = this.tags(res);
14182
- this.put(req.cacheKey, res, {
14249
+ const scope = this.scope(req);
14250
+ const tags = this.tags(res);
14251
+ this.put(req.cacheKey, res.toJson(), {
14183
14252
  maxAge: req.maxAge ?? res.options.maxAge,
14184
14253
  scope,
14185
14254
  tags,
@@ -14191,73 +14260,144 @@ class ODataInMemoryCache extends ODataBaseCache {
14191
14260
  * @returns The response from the cache
14192
14261
  */
14193
14262
  getResponse(req) {
14194
- let scope = this.scope(req);
14195
- return this.get(req.cacheKey, { scope });
14263
+ const scope = this.scope(req);
14264
+ const data = this.get(req.cacheKey, { scope });
14265
+ return data !== undefined ? ODataResponse.fromJson(req, data) : undefined;
14266
+ }
14267
+ size() {
14268
+ return Object.keys(this.storage).filter(k => k.startsWith(this.prefix)).length;
14196
14269
  }
14197
14270
  }
14198
14271
 
14199
- class ODataInStorageCache extends ODataBaseCache {
14272
+ class ODataIndexedDBCache extends ODataBaseCache {
14200
14273
  name;
14201
- storage;
14202
- constructor({ name, storage = sessionStorage, maxAge, }) {
14274
+ version;
14275
+ store;
14276
+ entries;
14277
+ _db;
14278
+ constructor({ name = 'ODataCache', store = 'cache', version = 1, maxAge, } = {}) {
14203
14279
  super({ maxAge });
14204
14280
  this.name = name;
14205
- this.storage = storage;
14206
- this.restore();
14207
- window.addEventListener('beforeunload', () => this.store());
14281
+ this.store = store;
14282
+ this.version = version;
14283
+ this.entries = new Map();
14284
+ this._db = this.initDb();
14285
+ }
14286
+ initDb() {
14287
+ return new Promise((resolve, reject) => {
14288
+ const request = indexedDB.open(this.name, this.version);
14289
+ request.onupgradeneeded = (event) => {
14290
+ const db = event.target.result;
14291
+ if (!db.objectStoreNames.contains(this.store)) {
14292
+ db.createObjectStore(this.store);
14293
+ }
14294
+ };
14295
+ request.onsuccess = async (event) => {
14296
+ const db = event.target.result;
14297
+ await this.loadFromDb(db);
14298
+ resolve(db);
14299
+ };
14300
+ request.onerror = (event) => {
14301
+ reject(event.target.error);
14302
+ };
14303
+ });
14208
14304
  }
14209
- /**
14210
- * Store the cache in the storage
14211
- */
14212
- store() {
14213
- this.storage.setItem(this.name, JSON.stringify(Array.from(this.entries.entries()).map(([key, entry]) => [
14214
- key,
14215
- {
14216
- ...entry,
14217
- payload: entry.payload instanceof ODataResponse ? entry.payload.toJson() : entry.payload,
14218
- },
14219
- ])));
14305
+ loadFromDb(db) {
14306
+ return new Promise((resolve, reject) => {
14307
+ const transaction = db.transaction(this.store, 'readonly');
14308
+ const store = transaction.objectStore(this.store);
14309
+ const request = store.openCursor();
14310
+ request.onsuccess = (event) => {
14311
+ const cursor = event.target.result;
14312
+ if (cursor) {
14313
+ this.entries.set(cursor.key, cursor.value);
14314
+ cursor.continue();
14315
+ }
14316
+ else {
14317
+ resolve();
14318
+ }
14319
+ };
14320
+ request.onerror = (event) => {
14321
+ reject(event.target.error);
14322
+ };
14323
+ });
14220
14324
  }
14221
- /**
14222
- * Restore the cache from the storage
14223
- */
14224
- restore() {
14225
- this.entries = new Map(JSON.parse(this.storage.getItem(this.name) || '[]'));
14325
+ async withDb(fn) {
14326
+ const db = await this._db;
14327
+ return fn(db);
14226
14328
  }
14227
- /**
14228
- * Flush the cache and clean the storage
14229
- */
14230
- flush() {
14231
- super.flush();
14232
- this.store();
14329
+ put(name, payload, { maxAge, scope, tags } = {}) {
14330
+ const entry = this.buildEntry(payload, { maxAge, tags });
14331
+ const key = this.buildKey([...(scope ?? []), name]);
14332
+ this.entries.set(key, entry);
14333
+ this.withDb((db) => {
14334
+ return new Promise((resolve, reject) => {
14335
+ const transaction = db.transaction(this.store, 'readwrite');
14336
+ const store = transaction.objectStore(this.store);
14337
+ store.put(entry, key);
14338
+ transaction.oncomplete = () => resolve();
14339
+ transaction.onerror = (event) => reject(event.target.error);
14340
+ });
14341
+ });
14342
+ }
14343
+ get(name, { scope } = {}) {
14344
+ const key = this.buildKey([...(scope || []), name]);
14345
+ const entry = this.entries.get(key);
14346
+ return entry !== undefined && !this.isExpired(entry) ? entry.payload : undefined;
14233
14347
  }
14234
- /**
14235
- * Store the response in the cache
14236
- * @param req The request with the resource to store the response
14237
- * @param res The response to store in the cache
14238
- */
14239
14348
  putResponse(req, res) {
14240
14349
  const scope = this.scope(req);
14241
14350
  const tags = this.tags(res);
14242
- this.put(req.cacheKey, res, {
14351
+ this.put(req.cacheKey, res.toJson(), {
14243
14352
  maxAge: req.maxAge ?? res.options.maxAge,
14244
14353
  scope,
14245
14354
  tags,
14246
14355
  });
14247
14356
  }
14248
- /**
14249
- * Restore the response from the cache
14250
- * @param req The request with the resource to get the response
14251
- * @returns The response from the cache
14252
- */
14253
14357
  getResponse(req) {
14254
14358
  const scope = this.scope(req);
14255
14359
  const data = this.get(req.cacheKey, { scope });
14256
- return data instanceof ODataResponse
14257
- ? data
14258
- : data !== undefined
14259
- ? ODataResponse.fromJson(req, data)
14260
- : undefined;
14360
+ return data !== undefined ? ODataResponse.fromJson(req, data) : undefined;
14361
+ }
14362
+ forget({ name, scope = [], tags = [], } = {}) {
14363
+ if (name)
14364
+ scope.push(name);
14365
+ const key = scope.length > 0 ? this.buildKey(scope) : undefined;
14366
+ const keysToDelete = [];
14367
+ this.entries.forEach((entry, k) => {
14368
+ if (this.isExpired(entry) ||
14369
+ (key !== undefined && k.startsWith(key)) ||
14370
+ (tags.length > 0 && tags.some((t) => entry.tags.indexOf(t) !== -1))) {
14371
+ keysToDelete.push(k);
14372
+ }
14373
+ });
14374
+ keysToDelete.forEach((k) => this.entries.delete(k));
14375
+ if (keysToDelete.length > 0) {
14376
+ this.withDb((db) => {
14377
+ return new Promise((resolve, reject) => {
14378
+ const transaction = db.transaction(this.store, 'readwrite');
14379
+ const store = transaction.objectStore(this.store);
14380
+ keysToDelete.forEach((k) => store.delete(k));
14381
+ transaction.oncomplete = () => resolve();
14382
+ transaction.onerror = (event) => reject(event.target.error);
14383
+ });
14384
+ });
14385
+ }
14386
+ }
14387
+ flush() {
14388
+ this.entries = new Map();
14389
+ this.withDb((db) => {
14390
+ return new Promise((resolve, reject) => {
14391
+ const transaction = db.transaction(this.store, 'readwrite');
14392
+ const store = transaction.objectStore(this.store);
14393
+ store.clear();
14394
+ transaction.oncomplete = () => resolve();
14395
+ transaction.onerror = (event) => reject(event.target.error);
14396
+ });
14397
+ });
14398
+ }
14399
+ size() {
14400
+ return this.entries.size;
14261
14401
  }
14262
14402
  }
14263
14403
 
@@ -14269,5 +14409,5 @@ class ODataInStorageCache extends ODataBaseCache {
14269
14409
  * Generated bundle index. Do not edit.
14270
14410
  */
14271
14411
 
14272
- export { Aggregate, ApplyExpression, ArithmeticFunctions, ArithmeticOperators, BUBBLES, Casting, CastingOperators, CollectionFunctions, ComputeExpression, ConditionalFunctions, DateAndTimeFunctions, Dates, Durations, EDM_PARSERS, EdmType, Enums, ExpandExpression, ExpandField, Expression, FieldFactory, FilterExpression, Function, GeoFunctions, GroupBy, GroupByTransformations, Grouping, GroupingOperators, Http, INCLUDE_DEEP, INCLUDE_SHALLOW, ITEM_ROOT, JsonSchemaType, Lambda, LambdaOperators, LogicalOperators, Model, ModelField, NONE_PARSER, ODATA_CONFIG, OData, ODataActionResource, ODataAnnotations, ODataApi, ODataBaseCache, ODataBaseService, ODataBatchRequest, ODataBatchResource, ODataCallable, ODataCallableParser, ODataClient, ODataCollection, ODataConfigAsyncLoader, ODataConfigLoader, ODataConfigSyncLoader, ODataCountResource, ODataEntitiesAnnotations, ODataEntityAnnotations, ODataEntityContainer, ODataEntityResource, ODataEntitySet, ODataEntitySetResource, ODataEntitySetService, ODataEntityTypeKey, ODataEnumType, ODataEnumTypeFieldParser, ODataEnumTypeParser, ODataFunctionResource, ODataFunctions, ODataInMemoryCache, ODataInStorageCache, ODataMediaResource, ODataMetadata, ODataMetadataLoader, ODataMetadataParser, ODataMetadataResource, ODataModel, ODataModelAttribute, ODataModelEvent, ODataModelEventEmitter, ODataModelEventType, ODataModelField, ODataModelOptions, ODataModelState, ODataModule, ODataNavigationPropertyResource, ODataOperators, ODataParameterParser, ODataPathSegments, ODataPathSegmentsHandler, ODataPropertyAnnotations, ODataPropertyResource, ODataQueryOptionHandler, ODataQueryOptions, ODataQueryOptionsHandler, ODataReferenceResource, ODataReferential, ODataRequest, ODataResource, ODataResponse, ODataSchema, ODataServiceFactory, ODataSettings, ODataSingleton, ODataSingletonResource, ODataSingletonService, ODataStructuredType, ODataStructuredTypeFieldParser, ODataStructuredTypeParser, ODataTransformations, ODataValueResource, Objects, Operator, OrderByExpression, OrderByField, PathSegment, QueryCustomTypes, QueryOption, RenderableFactory, SearchExpression, SearchTerm, SegmentHandler, SelectExpression, StandardAggregateMethods, StringAndCollectionFunctions, StringFunctions, Strings, Transformations, Type, TypeFunctions, Types, Urls, alias, binary, buildPathAndQuery, createSyncLoader, duration, encode, functions, isQueryCustomType, isRawType, normalizeValue, operators, pathAndParamsFromQueryOptions, pathAndParamsFromSegments, provideODataClient, raw, render, resolve, transformations };
14412
+ export { Aggregate, ApplyExpression, ArithmeticFunctions, ArithmeticOperators, BUBBLES, Casting, CastingOperators, CollectionFunctions, ComputeExpression, ConditionalFunctions, DateAndTimeFunctions, Dates, Durations, EDM_PARSERS, EdmType, Enums, ExpandExpression, ExpandField, Expression, FieldFactory, FilterExpression, Function, GeoFunctions, GroupBy, GroupByTransformations, Grouping, GroupingOperators, Http, INCLUDE_DEEP, INCLUDE_SHALLOW, ITEM_ROOT, JsonSchemaType, Lambda, LambdaOperators, LogicalOperators, Model, ModelField, NONE_PARSER, ODATA_CONFIG, OData, ODataActionResource, ODataAnnotations, ODataApi, ODataBaseCache, ODataBaseService, ODataBatchRequest, ODataBatchResource, ODataCallable, ODataCallableParser, ODataClient, ODataCollection, ODataConfigAsyncLoader, ODataConfigLoader, ODataConfigSyncLoader, ODataCountResource, ODataEntitiesAnnotations, ODataEntityAnnotations, ODataEntityContainer, ODataEntityResource, ODataEntitySet, ODataEntitySetResource, ODataEntitySetService, ODataEntityTypeKey, ODataEnumType, ODataEnumTypeFieldParser, ODataEnumTypeParser, ODataFunctionResource, ODataFunctions, ODataInMemoryCache, ODataInStorageCache, ODataIndexedDBCache, ODataMediaResource, ODataMetadata, ODataMetadataLoader, ODataMetadataParser, ODataMetadataResource, ODataModel, ODataModelAttribute, ODataModelEvent, ODataModelEventEmitter, ODataModelEventType, ODataModelField, ODataModelOptions, ODataModelState, ODataModule, ODataNavigationPropertyResource, ODataOperators, ODataParameterParser, ODataPathSegments, ODataPathSegmentsHandler, ODataPropertyAnnotations, ODataPropertyResource, ODataQueryOptionHandler, ODataQueryOptions, ODataQueryOptionsHandler, ODataReferenceResource, ODataReferential, ODataRequest, ODataResource, ODataResponse, ODataSchema, ODataServiceFactory, ODataSettings, ODataSingleton, ODataSingletonResource, ODataSingletonService, ODataStructuredType, ODataStructuredTypeFieldParser, ODataStructuredTypeParser, ODataTransformations, ODataValueResource, Objects, Operator, OrderByExpression, OrderByField, PathSegment, QueryCustomTypes, QueryOption, RenderableFactory, SearchExpression, SearchTerm, SegmentHandler, SelectExpression, StandardAggregateMethods, StringAndCollectionFunctions, StringFunctions, Strings, Transformations, Type, TypeFunctions, Types, Urls, alias, binary, buildPathAndQuery, createSyncLoader, duration, encode, functions, isQueryCustomType, isRawType, normalizeValue, operators, pathAndParamsFromQueryOptions, pathAndParamsFromSegments, provideODataClient, raw, render, resolve, transformations };
14273
14413
  //# sourceMappingURL=angular-odata.mjs.map