Skip to content

Commit

Permalink
chore: rename entity.set to entity.put (#407)
Browse files Browse the repository at this point in the history
  • Loading branch information
thantos authored Aug 3, 2023
1 parent 0374a1b commit 3f15e46
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 129 deletions.
36 changes: 18 additions & 18 deletions apps/tests/aws-runtime/test/test-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ export const counterNamespaceWatcher = counter.batchStream(
items.map(async (item) => {
console.log(item);
const value = await counter.get(item.key);
await counter.set({
await counter.put({
namespace: "default",
id: value!.id,
n: (value?.n ?? 0) + 1,
Expand All @@ -644,7 +644,7 @@ export const onEntityEvent = subscription(
{ events: [entityEvent] },
async ({ id }) => {
const value = await counter.get({ namespace: "default", id });
await counter.set({
await counter.put({
namespace: "default",
id,
n: (value?.n ?? 0) + 1,
Expand All @@ -658,7 +658,7 @@ export const entityTask = task(
"entityTask",
async (_, { execution: { id } }) => {
const value = await counter.get(["default", id]);
await counter.set({
await counter.put({
namespace: "default",
id,
n: (value?.n ?? 0) + 1,
Expand All @@ -670,13 +670,13 @@ export const entityTask = task(
export const entityIndexTask = task(
"entityIndexTask",
async (_, { execution: { id } }) => {
await counter.set({
await counter.put({
namespace: "another",
id,
n: 1000,
optional: "hello",
});
await counter.set({
await counter.put({
namespace: "another2",
id,
n: 0,
Expand Down Expand Up @@ -748,8 +748,8 @@ export const entityIndexTask = task(
export const entityWorkflow = workflow(
"entityWorkflow",
async (_, { execution: { id } }) => {
await counter.set({ namespace: "default", id, n: 1, optional: undefined });
await counter.set({
await counter.put({ namespace: "default", id, n: 1, optional: undefined });
await counter.put({
namespace: "different",
id,
n: 1,
Expand All @@ -760,7 +760,7 @@ export const entityWorkflow = workflow(
await Promise.all([entityEvent.emit({ id }), entitySignal.expectSignal()]);
try {
// will fail
await counter.set(
await counter.put(
{ namespace: "default", id, n: 0, optional: undefined },
{ expectedVersion: 1 }
);
Expand All @@ -769,15 +769,15 @@ export const entityWorkflow = workflow(
}
const { value: entityValue, version } =
(await counter.getWithMetadata(["default", id])) ?? {};
await counter.set(
await counter.put(
{ namespace: "default", id, n: entityValue!.n + 1, optional: undefined },
{ expectedVersion: version }
);
const value = await counter.get(["default", id]);
await Entity.transactWrite([
{
entity: counter,
operation: "set",
operation: "put",
value: { namespace: "default", id, n: (value?.n ?? 0) + 1 },
},
]);
Expand All @@ -798,13 +798,13 @@ export const entityWorkflow = workflow(
*/

await Promise.all([
counterCollection.set({ id, counterNumber: 1, n: 1 }),
counterCollection.set({ id, counterNumber: 2, n: 1 }),
counterCollection.set({ id, counterNumber: 3, n: 1 }),
counterCollection.put({ id, counterNumber: 1, n: 1 }),
counterCollection.put({ id, counterNumber: 2, n: 1 }),
counterCollection.put({ id, counterNumber: 3, n: 1 }),
]);

const counter1 = await counterCollection.get({ id, counterNumber: 1 });
await counterCollection.set({
await counterCollection.put({
id,
counterNumber: 2,
n: (counter1?.n ?? 0) + 1,
Expand Down Expand Up @@ -850,7 +850,7 @@ export const check = entity("check4", {

const gitErDone = transaction("gitErDone", async ({ id }: { id: string }) => {
const val = await check.get([id]);
await check.set({ id, n: val?.n ?? 0 + 1 });
await check.put({ id, n: val?.n ?? 0 + 1 });
return val?.n ?? 0 + 1;
});

Expand All @@ -861,7 +861,7 @@ const noise = task(
let transact: Promise<number> | undefined;
while (n-- > 0) {
try {
await check.set({ id, n });
await check.put({ id, n });
} catch (err) {
console.error(err);
if (
Expand Down Expand Up @@ -897,9 +897,9 @@ export const transactionWorkflow = workflow(
const one = await noise({ x: 40 });
const two = await noise({ x: 60 });
const [, three] = await Promise.allSettled([
check.set({ id, n: two ?? 0 + 1 }),
check.put({ id, n: two ?? 0 + 1 }),
gitErDone({ id }),
check.set({ id, n: two ?? 0 + 1 }),
check.put({ id, n: two ?? 0 + 1 }),
]);
await check.delete([id]);
return [one, two, three.status === "fulfilled" ? three.value : "AHHH"];
Expand Down
10 changes: 5 additions & 5 deletions packages/@eventual/aws-runtime/src/stores/entity-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
EntityQueryResult,
EntityReadOptions,
EntityScanOptions,
EntitySetOptions,
EntityPutOptions,
EntityWithMetadata,
KeyValue,
TransactionCancelled,
Expand Down Expand Up @@ -122,11 +122,11 @@ export class AWSEntityStore extends EntityStore {
};
}

public override async _set(
public override async _put(
entity: Entity,
value: Attributes,
key: NormalizedEntityCompositeKeyComplete,
options?: EntitySetOptions
options?: EntityPutOptions
): Promise<{ version: number }> {
try {
const result = await this.props.dynamo.send(
Expand Down Expand Up @@ -341,7 +341,7 @@ export class AWSEntityStore extends EntityStore {
await this.props.dynamo.send(
new TransactWriteItemsCommand({
TransactItems: items.map((item): TransactWriteItem => {
return item.operation === "set"
return item.operation === "put"
? {
Update: this.createSetRequest(
item.entity,
Expand Down Expand Up @@ -416,7 +416,7 @@ export class AWSEntityStore extends EntityStore {
entity: Entity,
value: Attr,
key: NormalizedEntityCompositeKey,
options?: EntitySetOptions
options?: EntityPutOptions
): Update {
const indexGeneratedAttributes = computeGeneratedIndexKeyAttributes(
entity,
Expand Down
8 changes: 4 additions & 4 deletions packages/@eventual/cli/src/display/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ function displayEntityCommand(operation: EntityOperation) {
const [key] = operation.params;
output.push(`Key: ${JSON.stringify(key)}`);
}
if (isEntityOperationOfType("set", operation)) {
if (isEntityOperationOfType("put", operation)) {
const [value] = operation.params;
output.push(`Entity: ${JSON.stringify(value)}`);
}
if (
isEntityOperationOfType("set", operation) ||
isEntityOperationOfType("put", operation) ||
isEntityOperationOfType("delete", operation)
) {
const [, options] = operation.params;
Expand All @@ -97,9 +97,9 @@ function displayEntityCommand(operation: EntityOperation) {
function displayEntityTransactItem(item: EntityTransactItem): string[] {
const entityName =
typeof item.entity === "string" ? item.entity : item.entity.name;
if (item.operation === "set") {
if (item.operation === "put") {
return displayEntityCommand({
operation: "set",
operation: "put",
entityName,
params: [item.value, item.options],
});
Expand Down
10 changes: 5 additions & 5 deletions packages/@eventual/core-runtime/src/local/stores/entity-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
EntityQueryOptions,
EntityQueryResult,
EntityScanOptions,
EntitySetOptions,
EntityPutOptions,
EntityStreamItem,
EntityWithMetadata,
KeyValue,
Expand Down Expand Up @@ -70,11 +70,11 @@ export class LocalEntityStore extends EntityStore {
return this.getPartitionMap(entity, key.partition).get(skOrDefault(key));
}

protected override async _set(
protected override async _put(
entity: Entity,
value: Attributes,
key: NormalizedEntityCompositeKeyComplete,
options?: EntitySetOptions
options?: EntityPutOptions
): Promise<{ version: number }> {
const { version = 0, value: oldValue } =
(await this._getWithMetadata(entity, key)) ?? {};
Expand Down Expand Up @@ -296,8 +296,8 @@ export class LocalEntityStore extends EntityStore {
*/
await Promise.all(
items.map(async (item) => {
if (item.operation === "set") {
return await this._set(
if (item.operation === "put") {
return await this._put(
item.entity,
item.value,
item.key,
Expand Down
22 changes: 11 additions & 11 deletions packages/@eventual/core-runtime/src/stores/entity-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
EntityQueryResult,
EntityReadOptions,
EntityScanOptions,
EntitySetOptions,
EntityPutOptions,
EntityTransactItem,
EntityWithMetadata,
KeyMap,
Expand Down Expand Up @@ -62,10 +62,10 @@ export abstract class EntityStore implements EntityHook {
options?: EntityReadOptions
): Promise<EntityWithMetadata | undefined>;

public set(
public put(
entityName: string,
value: Attributes,
options?: EntitySetOptions
options?: EntityPutOptions
): Promise<{ version: number }> {
const entity = this.getEntity(entityName);
const normalizedKey = normalizeCompositeKey(entity, value);
Expand All @@ -74,14 +74,14 @@ export abstract class EntityStore implements EntityHook {
throw new Error("Key cannot be partial for set.");
}

return this._set(entity, value, normalizedKey, options);
return this._put(entity, value, normalizedKey, options);
}

protected abstract _set(
protected abstract _put(
entity: Entity,
value: Attributes,
key: NormalizedEntityCompositeKeyComplete,
options?: EntitySetOptions
options?: EntityPutOptions
): Promise<{ version: number }>;

public delete(
Expand Down Expand Up @@ -196,17 +196,17 @@ export abstract class EntityStore implements EntityHook {
typeof item.entity === "string"
? this.getEntity(item.entity)
: item.entity;
const keyValue = item.operation === "set" ? item.value : item.key;
const keyValue = item.operation === "put" ? item.value : item.key;
const key = normalizeCompositeKey(entity, keyValue);
if (!isCompleteKey(key)) {
throw new Error(
"Entity key cannot be partial for set, delete, or condition operations."
);
}

return item.operation === "set"
return item.operation === "put"
? {
operation: "set",
operation: "put",
entity,
key,
value: item.value,
Expand Down Expand Up @@ -248,9 +248,9 @@ export type NormalizedEntityTransactItem = {
key: NormalizedEntityCompositeKeyComplete;
} & (
| {
operation: "set";
operation: "put";
value: Attributes;
options?: EntitySetOptions;
options?: EntityPutOptions;
}
| {
operation: "delete";
Expand Down
18 changes: 9 additions & 9 deletions packages/@eventual/core-runtime/src/transaction-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
type EntityTransactConditionalOperation,
type EntityTransactDeleteOperation,
type EntityTransactItem,
type EntityTransactSetOperation,
type EntityTransactPutOperation,
type EntityWithMetadata,
type KeyMap,
type TransactionContext,
Expand Down Expand Up @@ -143,7 +143,7 @@ export function createTransactionExecutor(
}
> {
// a map of the keys of all mutable entity calls that have been made to the request
const entityCalls = new Map<string, EntityOperation<"set" | "delete">>();
const entityCalls = new Map<string, EntityOperation<"put" | "delete">>();
// store all of the event and signal calls to execute after the transaction completes
const eventCalls: (EmitEventsCall | SendSignalCall)[] = [];
// a map of the keys of all get operations or mutation operations to check during the transaction.
Expand All @@ -154,11 +154,11 @@ export function createTransactionExecutor(
registerEventualCall: (eventual): any => {
if (isEntityCall(eventual)) {
if (
isEntityOperationOfType("set", eventual) ||
isEntityOperationOfType("put", eventual) ||
isEntityOperationOfType("delete", eventual)
) {
return createEventualPromise<
Awaited<ReturnType<Entity["delete"] | Entity["set"]>>
Awaited<ReturnType<Entity["delete"] | Entity["put"]>>
>(async () => {
const entity = getEntity(eventual.entityName);
// should either by the key or the value object, which can be used as the key
Expand Down Expand Up @@ -192,7 +192,7 @@ export function createTransactionExecutor(
}

entityCalls.set(serializedKey, eventual);
if (isEntityOperationOfType("set", eventual)) {
if (isEntityOperationOfType("put", eventual)) {
const newVersion = entityValue.originalVersion + 1;
retrievedEntities.set(serializedKey, {
entityName: eventual.entityName,
Expand Down Expand Up @@ -279,16 +279,16 @@ export function createTransactionExecutor(
if (call) {
const [, options] = call.params;

return call.operation === "set"
return call.operation === "put"
? ({
entity: entityName,
operation: "set",
operation: "put",
value: call.params[0],
options: {
...options,
expectedVersion: originalVersion,
},
} satisfies EntityTransactSetOperation)
} satisfies EntityTransactPutOperation)
: ({
entity: entityName,
operation: "delete",
Expand Down Expand Up @@ -340,7 +340,7 @@ export function createTransactionExecutor(
// normalize the key to extract only the key fields.
const key = normalizeCompositeKey(
entity,
x.operation === "set" ? x.value : x.key
x.operation === "put" ? x.value : x.key
);
return {
entityName: entity.name,
Expand Down
Loading

0 comments on commit 3f15e46

Please sign in to comment.