Skip to content

Commit

Permalink
feat: add defaultMaxAge to entity operator
Browse files Browse the repository at this point in the history
 * Introduce to `defaultMaxAge` to IEntityOptions
 + Add EntityAge with predefined common ages for use with `defaultMaxAge`
 * Update @entity decorator to support passing name in only or options or both
 > Move key decorator tokens into entity-tokens.ts
 > Extract entity decorator into its own file entity-decorator.ts
 > Extract key decorator into its own file key-decorator.ts
 * Update index/public api to reference all exports from new locations
 * Added EntityAge enum to public api
 * Removed all imports from '../..'

Issues #144 #141
  • Loading branch information
Jon Rista committed Aug 30, 2020
1 parent d106ce4 commit 8e4f61b
Show file tree
Hide file tree
Showing 40 changed files with 210 additions and 205 deletions.
12 changes: 0 additions & 12 deletions e2e/protractor.conf.js

This file was deleted.

14 changes: 0 additions & 14 deletions e2e/src/app.e2e-spec.ts

This file was deleted.

11 changes: 0 additions & 11 deletions e2e/src/app.po.ts

This file was deleted.

9 changes: 0 additions & 9 deletions e2e/tsconfig.e2e.json

This file was deleted.

14 changes: 7 additions & 7 deletions projects/ngrx-auto-entity/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export {
} from './lib/module';

/*
* Common models referenced throughout Auto-Entity
* Common models and types referenced throughout Auto-Entity
*/
export {
IPage,
Expand All @@ -29,8 +29,8 @@ export {
RangeValue,
IPageInfo
} from './lib/models';

export { IEntityDictionary, IEntityState, EntityIdentity } from './lib/util/entity-state';
export { EntityIdentity } from './lib/types/entity-identity';
export { IEntityDictionary, IEntityState } from './lib/util/entity-state';
export { IModelState, IModelClass } from './lib/util/model-state';
export { IEntityFacade } from './lib/util/facade';
export { ISelectorMap } from './lib/util/selector-map';
Expand Down Expand Up @@ -99,14 +99,14 @@ export { Clear } from './lib/actions/actions';
/*
* Decorators
*/
export { Entity } from './lib/decorators/entity';
export { Key } from './lib/decorators/key';
export { Entity } from './lib/decorators/entity-decorator';
export { Key } from './lib/decorators/key-decorator';

/*
* Entity Metadata and Management
*/
export { ENTITY_OPTS_PROP } from './lib/decorators/entity-tokens';
export { IEffectExcept, IEntityOptions, IEntityTransformer } from './lib/decorators/entity';
export { IEffectExcept, IEntityOptions, IEntityTransformer, EntityAge } from './lib/decorators/entity-options';
export { IEffectExclusions } from './lib/decorators/effect-exclusions';
export { curd, loads, extra, all, matching, except } from './lib/decorators/effect-exclusion-utils';

Expand All @@ -129,7 +129,7 @@ export {
getKeyNamesFromModel,
getKeyNamesFromEntity,
checkKeyName
} from './lib/decorators/key';
} from './lib/decorators/key-util';

/*
* Reducer
Expand Down
4 changes: 2 additions & 2 deletions projects/ngrx-auto-entity/src/lib/actions/actions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { provideMockActions } from '@ngrx/effects/testing';
import { hot } from 'jasmine-marbles';
import { Observable } from 'rxjs';

import { Entity } from '../decorators/entity';
import { Key } from '../decorators/key';
import { Entity } from '../decorators/entity-decorator';
import { Key } from '../decorators/key-decorator';
import { IEntityError } from '../service/wrapper-models';
import { fromEntityActions, ofEntityAction, ofEntityType } from './action-operators';
import { EntityActionTypes } from './action-types';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EntityIdentity } from '../util/entity-state';
import { EntityIdentity } from '../types/entity-identity';
import { EntityActionTypes } from './action-types';
import { EntityAction } from './entity-action';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EntityIdentity } from '../util/entity-state';
import { EntityIdentity } from '../types/entity-identity';
import { EntityActionTypes } from './action-types';
import { EntityAction } from './entity-action';

Expand Down
2 changes: 1 addition & 1 deletion projects/ngrx-auto-entity/src/lib/actions/edit-actions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EntityIdentity } from '../util/entity-state';
import { EntityIdentity } from '../types/entity-identity';
import { EntityActionTypes } from './action-types';
import { EntityAction } from './entity-action';

Expand Down
2 changes: 1 addition & 1 deletion projects/ngrx-auto-entity/src/lib/actions/entity-info.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IEntityNames, IEntityTransformer } from '../decorators/entity';
import { IEntityNames, IEntityTransformer } from '../decorators/entity-options';

/**
* Descriptor of an Entity model and related metadata.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EntityIdentity } from '../util/entity-state';
import { EntityIdentity } from '../types/entity-identity';
import { EntityActionTypes } from './action-types';
import { EntityAction } from './entity-action';

Expand Down
4 changes: 2 additions & 2 deletions projects/ngrx-auto-entity/src/lib/actions/util.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { pascalCase } from '../../util/case';
import { IEntityOptions } from '../decorators/entity';
import { IEntityOptions } from '../decorators/entity-options';
import { ENTITY_OPTS_PROP } from '../decorators/entity-tokens';
import { checkKeyName } from '../decorators/key';
import { checkKeyName } from '../decorators/key-util';
import { IEntityInfo } from './entity-info';

/**
Expand Down
39 changes: 39 additions & 0 deletions projects/ngrx-auto-entity/src/lib/decorators/entity-decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { IEntityOptions } from './entity-options';
import { ENTITY_OPTS_PROP } from './entity-tokens';

/**
* Entity decorator for configuring each entity model.
*
* @param options - The configuration options to apply
*/
export function Entity(options: IEntityOptions);

/**
* Entity decorator for configuring each entity model.
*
* @param modelName - The model name option to apply
* @param options - Additional configuration options to apply
*/
export function Entity(modelName?: string, options?: IEntityOptions);

/**
* Entity decorator for configuring each entity model.
*
* @param nameOrOptions - The model name or configuration options to apply
* @param maybeOptions - Additional options to apply if a model name is passed as the first param
*/
export function Entity(nameOrOptions: string | IEntityOptions, maybeOptions?: IEntityOptions) {
return function entityDecorator(constructor: any) {
const initialOptions = typeof nameOrOptions === 'object' ? nameOrOptions : { modelName: nameOrOptions };
const options = maybeOptions ? { ...maybeOptions, ...initialOptions } : initialOptions;

const descriptor = Object.create(null);
descriptor.configurable = false;
descriptor.enumerable = false;
descriptor.writable = false;
descriptor.value = options;
Object.defineProperty(constructor, ENTITY_OPTS_PROP, descriptor);

return constructor;
};
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { EntityActionTypes } from '../actions/action-types';
import { IEffectExclusions } from './effect-exclusions';
import { ENTITY_OPTS_PROP } from './entity-tokens';

/**
* Defines effect exceptions for the decorated model.
Expand All @@ -18,6 +17,7 @@ export interface IEntityTransformer {
}

export type EntityComparer = (a, b) => number;

export interface IEntityComparerMap {
[key: string]: EntityComparer | string;
}
Expand All @@ -28,27 +28,29 @@ export interface IEntityNames {
uriName?: string;
}

export enum EntityAge {
Minute = 60,
Hour = EntityAge.Minute * 60,
QuarterDay = EntityAge.Hour * 6,
HalfDay = EntityAge.Hour * 12,
Day = EntityAge.Hour * 24,
Week = EntityAge.Day * 7
}

/**
* The options that may be configured for a decorated entity model.
*
* @property comparer - (optional) A default comparer for sorting entities on selection
* @property comparers - (optional) A set of comparer functions or named comparer references for sorting entities
* @property transform - (optional) A set of entity transform objects that may be composed, in order, to transform the entity
* @property excludeEffects - (optional) The effect exclusion config
* @see IEffectExclusions
* @property defaultMaxAge - (optional) A default maximum age, in seconds, after which load*IfNecessary actions will always load
*/
export interface IEntityOptions extends IEntityNames {
comparer?: EntityComparer;
comparers?: IEntityComparerMap;
transform?: IEntityTransformer[];
excludeEffects?: IEffectExclusions | IEffectExcept;
}

/**
* Entity decorator for configuring each entity model.
*
* @param options The configuration options to apply.
*/
export function Entity(options: IEntityOptions) {
return function entityDecorator(constructor: any) {
const descriptor = Object.create(null);
descriptor.value = options;
Object.defineProperty(constructor, ENTITY_OPTS_PROP, descriptor);

return constructor;
};
defaultMaxAge?: number | EntityAge;
}
6 changes: 6 additions & 0 deletions projects/ngrx-auto-entity/src/lib/decorators/entity-tokens.ts
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
// NOTE: The following constants should be Symbol() to avoid any potential conflict with
// any user-defined properties on the entity models. However, use of Symbol() here causes
// problems with the Jest test runner at the current time

export const ENTITY_OPTS_PROP = '__nae_entity_opts';
export const NAE_KEYS = '__nae_keys';
export const NAE_KEY_NAMES = '__nae_key_names';
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Entity } from './entity';
import { Entity } from './entity-decorator';
import { entityComparer, entityTransforms, nameOfEntity, pluralNameOfEntity, uriNameOfEntity } from './entity-util';
import { Key } from './key';
import { Key } from './key-decorator';

@Entity({
modelName: 'TestEntity',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TNew } from '../actions/model-constructor';
import { EntityComparer, IEntityOptions, IEntityTransformer } from './entity';
import { EntityComparer, IEntityOptions, IEntityTransformer } from './entity-options';
import { ENTITY_OPTS_PROP } from './entity-tokens';

export const nameOfEntity = <TModel>(entityOrType: TNew<TModel> | TModel): string | null | undefined =>
Expand Down
4 changes: 2 additions & 2 deletions projects/ngrx-auto-entity/src/lib/decorators/entity.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { hot } from 'jasmine-marbles';
import { EntityActionTypes } from '../actions/action-types';
import { Clear } from '../actions/actions';
import { all, curd, except, extra, loads, matching } from './effect-exclusion-utils';
import { Entity } from './entity';
import { Entity } from './entity-decorator';
import { shouldApplyEffect } from './entity-operators';
import { ENTITY_OPTS_PROP } from './entity-tokens';
import { Key } from './key';
import { Key } from './key-decorator';

describe('Function: except', () => {
test('should return function if called partially', () => {
Expand Down
12 changes: 12 additions & 0 deletions projects/ngrx-auto-entity/src/lib/decorators/key-decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { NAE_KEY_NAMES, NAE_KEYS } from './entity-tokens';

/**
* Used to designate the key property for the entity
*
* @param target the entity's class
* @param keyName the key's name
*/
export function Key(target, keyName: string | symbol): void {
target[NAE_KEY_NAMES] = target[NAE_KEY_NAMES] ? [...target[NAE_KEY_NAMES], keyName] : [keyName];
Object.defineProperty(target, NAE_KEYS, { get: () => target[NAE_KEY_NAMES] });
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,6 @@
import { IEntityAction } from '../actions/entity-action';
import { EntityIdentity } from '../util/entity-state';

// NOTE: The following two constants should be Symbol() to avoid any potential conflict with
// any user-defined properties on the entity models. However, use of Symbol() here causes
// problems with the Jest test runner at the current time
export const NAE_KEYS = '__nae_keys';
export const NAE_KEY_NAMES = '__nae_key_names';

/**
* Used to designate the key property for the entity
*
* @param target the entity's class
* @param keyName the key's name
*/
export function Key(target, keyName: string | symbol): void {
target[NAE_KEY_NAMES] = target[NAE_KEY_NAMES] ? [...target[NAE_KEY_NAMES], keyName] : [keyName];
Object.defineProperty(target, NAE_KEYS, { get: () => target[NAE_KEY_NAMES] });
}
import { EntityIdentity } from '../types/entity-identity';
import { NAE_KEYS } from './entity-tokens';

export function checkKeyName(type: any, modelName: string): boolean {
const keys = type.prototype[NAE_KEYS];
Expand Down
11 changes: 5 additions & 6 deletions projects/ngrx-auto-entity/src/lib/decorators/key.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { getKey, getKeyFromModel, Load, LoadAll } from '../..';
import { getKey, getKeyFromModel } from '../decorators/key-util';
import { makeEntity } from '../util/make-entity';
import { NAE_KEY_NAMES, NAE_KEYS } from './entity-tokens';
import {
getKeyFromEntity,
getKeyNames,
getKeyNamesFromEntity,
getKeyNamesFromModel,
Key,
NAE_KEY_NAMES,
NAE_KEYS
} from './key';
getKeyNamesFromModel
} from './key-util';
import { Key } from './key-decorator';

class TestEntity {
@Key id: number;
Expand Down
6 changes: 4 additions & 2 deletions projects/ngrx-auto-entity/src/lib/effects/operators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import {
} from '../actions/delete-actions';
import {
DeleteByKey,
DeleteByKeyFailure, DeleteByKeySuccess,
DeleteByKeyFailure,
DeleteByKeySuccess,
DeleteManyByKeys,
DeleteManyByKeysFailure,
DeleteManyByKeysSuccess
Expand Down Expand Up @@ -53,7 +54,8 @@ import {
SelectByKey,
Selected,
SelectedMany,
SelectedMore, SelectMany,
SelectedMore,
SelectMany,
SelectManyByKeys,
SelectMore,
SelectMoreByKeys
Expand Down
Loading

0 comments on commit 8e4f61b

Please sign in to comment.