Skip to content

breeze-client / KeyGenerator

Class: KeyGenerator ​

Defined in: src/entity/key-generator.ts:25

Generates temporary key values for new entities - one added with an unset key when its type's autoGeneratedKeyType is not None, or one passed to EntityManager.generateTempKeyValue - and keeps a record of the temporary values the manager's entities hold, so that an import cannot give a second entity one of them. Each EntityManager has its own, as EntityManager.keyGenerator, and replaces it when cleared. To supply your own, pass a subclass's constructor as keyGeneratorCtor in the EntityManagerConfig, and have its generateTempKeyValue pass the value it makes to super.generateTempKeyValue as valueIfAvail, so that the value is recorded; Breeze calls only KeyGenerator.generateTempKeyValue.

Constructors ​

Constructor ​

new KeyGenerator(): KeyGenerator

Defined in: src/entity/key-generator.ts:32

Creates a generator that has handed out no values. An EntityManager creates its own from EntityManagerConfig.keyGeneratorCtor.

Returns ​

KeyGenerator

Methods ​

generateTempKeyValue() ​

generateTempKeyValue(entityType, valueIfAvail?): any

Defined in: src/entity/key-generator.ts:68

Returns a unique 'temporary' id for the specified EntityType. Uniqueness is defined for this purpose as being unique within each instance of a KeyGenerator. This is sufficient because each EntityManager will have its own instance of a KeyGenerator and any entities imported into the EntityManager with temporary keys will have them regenerated and remapped on import.

The return value of this method must be of the correct type as determined by the keyProperties of the specified EntityType

Parameters ​

entityType ​

EntityType

The type to generate a key for. Its key must be a single property.

valueIfAvail? ​

any

A key value to use if no entity in the manager holds it. When entities with temporary keys are imported, the manager passes each one's key here, so that the key survives the import where it can; a subclass passes the value it made. If the value is taken, or omitted, a new one is generated. Either way, the value returned is recorded as held.

Returns ​

any

The key value, of the key property's data type.

Example ​

ts
// Assume em1 is a preexisting EntityManager, and the server generates Order keys
    const order1 = em1.createEntity(Order);
    // next line calls em1.keyGenerator.generateTempKeyValue(order1.entityType),
    // sets order1's 'orderID' property to the result, and returns it
    const oid1 = em1.generateTempKeyValue(order1);
    const saveResult = await em1.saveChanges();
    const sameOrder1 = saveResult.entities[0];
    // order1 === sameOrder1;
    // but order1.orderID != oid1
    // because the server will have generated a new id
    // and the client will have been updated with this
    // new id.

getTempKeys() ​

getTempKeys(): EntityKey[]

Defined in: src/entity/key-generator.ts:106

Returns an EntityKey for each temporary key value the manager's entities hold: the values this generator handed out or recorded, less those released when an entity was saved and given the server's key, or detached.

Returns ​

EntityKey[]

Deprecated ​

Ask the entities instead: em.getEntities().filter(e => e.entityAspect.hasTempKey).map(e => e.entityAspect.getKey()). Breeze does not call it. It still works; it is not scheduled for removal.


isTempKey() ​

isTempKey(entityKey): boolean

Defined in: src/entity/key-generator.ts:126

Whether entityKey's value is a temporary key an entity in the manager holds - one of the values KeyGenerator.getTempKeys returns. Always false for a multipart key.

Parameters ​

entityKey ​

EntityKey

Returns ​

boolean

Deprecated ​

Ask the entity instead: em.getEntityByKey(key)?.entityAspect.hasTempKey, or EntityAspect.hasTempKey when you have the entity. Breeze does not call it. It still works; it is not scheduled for removal.

Released under the MIT License.