dolittle / JavaScript.SDK

Dolittle JavaScript SDK
https://dolittle.io
MIT License
5 stars 2 forks source link

Embeddings, pinging & Event Horizon retry #47

Closed joelhoisko closed 3 years ago

joelhoisko commented 3 years ago

Summary

Adds a new feature, Embeddings! They are similar to Projections, but they are meant to be used to event source changes coming from an external system. Check the sample for an example.

Also changes the behavior of the pinging system to be more reliable and to be ready to receive pings immediately upon connecting to the Runtime. This is to deal with a bug that was causing connections between the SDK and the Runtime to be dropped. This is a breaking behavioral change and it's related to the release of version v6 of the Runtime. You have to update to version v6* of the Runtime, older versions wont work with this release of the SDK. We've added a compatibility table for checking the supported versions.

Added

Changed

Fixed

joelhoisko commented 3 years ago

@embedding('98f9db66-b6ca-4e5f-9fc3-638626c9ecfa') export class DishCounter { numberOfTimesPrepared: number = 0;

@compare()
compare(receivedState: DishCounter, embeddingContext: EmbeddingContext) {
    if (receivedState.numberOfTimesPrepared > this.numberOfTimesPrepared) {
        return new DishPrepared(embeddingContext.key.value, embeddingContext.key.value);
    }
}

@deleteMethod()
remove(embeddingContext: EmbeddingContext) {
    return new DishRemoved(embeddingContext.key.value);
}

@on(DishPrepared, _ => _.keyFromProperty('Dish'))
onDishPrepared(event: DishPrepared, context: EmbeddingProjectContext) {
    this.numberOfTimesPrepared ++;
}

@on(DishRemoved, _ => _.keyFromProperty('Dish'))
onDishRemoved(event: DishRemoved, context: EmbeddingProjectContext) {
    return ProjectionResult.delete;
}

}

- Syntax for getting embeddings:
```typescript
for (const [dish, { state: counter }] of await client.embeddings.forTenant(TenantId.development).getAll(DishCounter)) {
    console.log(`The kitchen has prepared ${dish} ${counter.numberOfTimesPrepared} times`);
}

const chef = await client.embeddings.forTenant(TenantId.development).get<Chef>(Chef, 'Mrs. Tex Mex');
console.log(`${chef.key} has prepared ${chef.state.dishes}`);

const dishes = await client.embeddings.forTenant(TenantId.development).getKeys(DishCounter);
console.log(`Got dem dish keys: ${dishes}`);