@@ -38,6 +38,8 @@ import {
3838 WriteAuthorizationModelRequest,
3939 WriteAuthorizationModelResponse,
4040 WriteRequest,
41+ WriteRequestWritesOnDuplicateEnum,
42+ WriteRequestDeletesOnMissingEnum,
4143} from "./apiModel";
4244import { BaseAPI } from "./base";
4345import { CallResult, PromiseResult } from "./common";
@@ -176,12 +178,55 @@ export interface ClientBatchCheckResponse {
176178 result: ClientBatchCheckSingleResponse[];
177179}
178180
181+ export const OnDuplicateWrites = WriteRequestWritesOnDuplicateEnum;
182+
183+ export const OnMissingDeletes = WriteRequestDeletesOnMissingEnum;
184+
185+ export interface ClientWriteConflictOptions {
186+ /**
187+ * Controls behavior when writing a tuple that already exists
188+ * - `OnDuplicateWrites.Error`: Return error on duplicates (default )
189+ * - `OnDuplicateWrites.Ignore`: Silently skip duplicate writes
190+ */
191+ onDuplicateWrites?: typeof OnDuplicateWrites[keyof typeof OnDuplicateWrites];
192+
193+ /**
194+ * Controls behavior when deleting a tuple that doesn' t exist
195+ * - `OnMissingDeletes.Error`: Return error on missing deletes (default)
196+ * - `OnMissingDeletes.Ignore`: Silently skip missing deletes
197+ */
198+ onMissingDeletes?: typeof OnMissingDeletes[keyof typeof OnMissingDeletes];
199+ }
200+
179201export interface ClientWriteRequestOpts {
180202 transaction?: {
181203 disable?: boolean;
182204 maxPerChunk?: number;
183205 maxParallelRequests?: number;
184206 }
207+ conflict?: ClientWriteConflictOptions;
208+ }
209+
210+ export interface ClientWriteTuplesRequestOpts {
211+ transaction?: {
212+ disable?: boolean;
213+ maxPerChunk?: number;
214+ maxParallelRequests?: number;
215+ };
216+ conflict?: {
217+ onDuplicateWrites?: typeof OnDuplicateWrites[keyof typeof OnDuplicateWrites];
218+ };
219+ }
220+
221+ export interface ClientDeleteTuplesRequestOpts {
222+ transaction?: {
223+ disable?: boolean;
224+ maxPerChunk?: number;
225+ maxParallelRequests?: number;
226+ };
227+ conflict?: {
228+ onMissingDeletes?: typeof OnMissingDeletes[keyof typeof OnMissingDeletes];
229+ };
185230}
186231
187232export interface ClientWriteRequest {
@@ -463,6 +508,9 @@ export class {{appShortName}}Client extends BaseAPI {
463508 * @param {ClientWriteRequest} body
464509 * @param {ClientRequestOptsWithAuthZModelId & ClientWriteRequestOpts} [options]
465510 * @param {string} [options.authorizationModelId] - Overrides the authorization model id in the configuration
511+ * @param {object} [options.conflict] - Conflict handling options
512+ * @param {OnDuplicateWrites} [options.conflict.onDuplicateWrites] - Controls behavior when writing duplicate tuples. Defaults to `OnDuplicateWrites.Error`
513+ * @param {OnMissingDeletes} [options.conflict.onMissingDeletes] - Controls behavior when deleting non-existent tuples. Defaults to `OnMissingDeletes.Error`
466514 * @param {object} [options.transaction]
467515 * @param {boolean} [options.transaction.disable] - Disables running the write in a transaction mode. Defaults to `false`
468516 * @param {number} [options.transaction.maxPerChunk] - Max number of items to send in a single transaction chunk. Defaults to `1`
@@ -473,7 +521,7 @@ export class {{appShortName}}Client extends BaseAPI {
473521 * @param {number} [options.retryParams.minWaitInMs] - Override the minimum wait before a retry is initiated
474522 */
475523 async write(body: ClientWriteRequest, options: ClientRequestOptsWithAuthZModelId & ClientWriteRequestOpts = {}): Promise<ClientWriteResponse> {
476- const { transaction = {} , headers = { } } = options;
524+ const { transaction = {}, headers = {}, conflict } = options;
477525 const {
478526 maxPerChunk = 1, // 1 has to be the default otherwise the chunks will be sent in transactions
479527 maxParallelRequests = DEFAULT_MAX_METHOD_PARALLEL_REQS,
@@ -486,10 +534,16 @@ export class {{appShortName}}Client extends BaseAPI {
486534 authorization_model_id: authorizationModelId,
487535 };
488536 if (writes?.length) {
489- apiBody.writes = { tuple_keys: writes } ;
537+ apiBody.writes = {
538+ tuple_keys: writes,
539+ on_duplicate: conflict?.onDuplicateWrites
540+ };
490541 }
491542 if (deletes?.length) {
492- apiBody.deletes = { tuple_keys: deletes } ;
543+ apiBody.deletes = {
544+ tuple_keys: deletes,
545+ on_missing: conflict?.onMissingDeletes
546+ };
493547 }
494548 await this.api.write(this.getStoreId(options)!, apiBody, options);
495549 return {
@@ -553,8 +607,10 @@ export class {{appShortName}}Client extends BaseAPI {
553607 /**
554608 * WriteTuples - Utility method to write tuples, wraps Write
555609 * @param {TupleKey[]} tuples
556- * @param { ClientRequestOptsWithAuthZModelId & ClientWriteRequestOpts } [options]
610+ * @param {ClientRequestOptsWithAuthZModelId & ClientWriteTuplesRequestOpts } [options]
557611 * @param {string} [options.authorizationModelId] - Overrides the authorization model id in the configuration
612+ * @param {object} [options.conflict] - Conflict handling options
613+ * @param {OnDuplicateWrites} [options.conflict.onDuplicateWrites] - Controls behavior when writing duplicate tuples. Defaults to `OnDuplicateWrites.Error`
558614 * @param {object} [options.transaction]
559615 * @param {boolean} [options.transaction.disable] - Disables running the write in a transaction mode. Defaults to `false`
560616 * @param {number} [options.transaction.maxPerChunk] - Max number of items to send in a single transaction chunk. Defaults to `1`
@@ -564,7 +620,7 @@ export class {{appShortName}}Client extends BaseAPI {
564620 * @param {number} [options.retryParams.maxRetry] - Override the max number of retries on each API request
565621 * @param {number} [options.retryParams.minWaitInMs] - Override the minimum wait before a retry is initiated
566622 */
567- async writeTuples(tuples: TupleKey[], options: ClientRequestOptsWithAuthZModelId & ClientWriteRequestOpts = { } ): Promise<ClientWriteResponse > {
623+ async writeTuples(tuples: TupleKey[], options: ClientRequestOptsWithAuthZModelId & ClientWriteTuplesRequestOpts = {}): Promise<ClientWriteResponse> {
568624 const { headers = {} } = options;
569625 setHeaderIfNotSet(headers, CLIENT_METHOD_HEADER, "WriteTuples");
570626 return this.write({ writes: tuples }, { ...options, headers });
@@ -573,8 +629,10 @@ export class {{appShortName}}Client extends BaseAPI {
573629 /**
574630 * DeleteTuples - Utility method to delete tuples, wraps Write
575631 * @param {TupleKeyWithoutCondition[]} tuples
576- * @param { ClientRequestOptsWithAuthZModelId & ClientWriteRequestOpts } [options]
632+ * @param {ClientRequestOptsWithAuthZModelId & ClientDeleteTuplesRequestOpts } [options]
577633 * @param {string} [options.authorizationModelId] - Overrides the authorization model id in the configuration
634+ * @param {object} [options.conflict] - Conflict handling options
635+ * @param {OnMissingDeletes} [options.conflict.onMissingDeletes] - Controls behavior when deleting non-existent tuples. Defaults to `OnMissingDeletes.Error`
578636 * @param {object} [options.transaction]
579637 * @param {boolean} [options.transaction.disable] - Disables running the write in a transaction mode. Defaults to `false`
580638 * @param {number} [options.transaction.maxPerChunk] - Max number of items to send in a single transaction chunk. Defaults to `1`
@@ -584,7 +642,7 @@ export class {{appShortName}}Client extends BaseAPI {
584642 * @param {number} [options.retryParams.maxRetry] - Override the max number of retries on each API request
585643 * @param {number} [options.retryParams.minWaitInMs] - Override the minimum wait before a retry is initiated
586644 */
587- async deleteTuples(tuples: TupleKeyWithoutCondition[], options: ClientRequestOptsWithAuthZModelId & ClientWriteRequestOpts = { } ): Promise<ClientWriteResponse > {
645+ async deleteTuples(tuples: TupleKeyWithoutCondition[], options: ClientRequestOptsWithAuthZModelId & ClientDeleteTuplesRequestOpts = {}): Promise<ClientWriteResponse> {
588646 const { headers = {} } = options;
589647 setHeaderIfNotSet(headers, CLIENT_METHOD_HEADER, "DeleteTuples");
590648 return this.write({ deletes: tuples }, { ...options, headers });
0 commit comments