@stackone/datasync
v1.9.3
Published
DataSync document model, s1query grammar, OpenSearch query builders and client construction
Keywords
Readme
@stackone/datasync
Description
The DataSync document model and s1query grammar, extracted from unified-cloud-api so its read and write
paths and the datasync-read-handler Lambda can share one source of truth:
documents— flatten/unflatten synced items (with overflow, viaflattenRecord/restoreRecord), infer field types, allocate field-map slots.query/aggregations— validate and translate the caller-supplied s1queryfilter,search,source,sortandaggsinto OpenSearch DSL over aliased columns.searchcovers every string field unless the caller scopes it to the fields its policy leaves visible. Atermsaggregation with no bucket aggregation above or below it gets ashard_sizeof at least 1000, so its counts are exact under concurrent segment search while each slice holds at most that many values.limits— page bounds shared by every surface.aggregation-buckets— keeps a synced aggregation under OpenSearch'ssearch.max_buckets, which a narrow histogram interval or nestedterms/rangecan exceed for every tenant on the cluster.needsBucketProbesays whether the request alone proves it fits; when it does not, runbuildBucketProbeBody(a size-0 search under the same scope and filter), read its response withreadBucketProbe(aggs, response.aggregations), which throws on a missing or malformed result rather than reading it as empty, thenassertAggBucketBudgetrejects anything overMAX_AGG_BUCKETS.isTooManyBucketsErrorrecognises, in the thrown error or the returned response, the 503 OpenSearch still answers if data grew after the probe; map it totooManyBucketsRejection()rather than a server error. Send both the probe and the aggregation with the client optionignore: [503]so it hands that response back instead of retrying it and marking the node dead. That option also stops the client throwing, so any other 503 must still be raised as an outage:isTooManyBucketsErrormatches on the cause, never the status.opensearch— tenant-scoped search/aggregation body builders and response predicates.describeOpenSearchRejectionreturns{ message, parameters, reason }only; the consumer adds its own transport fields (error,statusCode), unlikeSyncedQueryRejection.toResponse()which is complete.buildAggregationBodytakesTranslatedAggregationsfromtranslateAggs, not a raw object, so an untranslated aggregation cannot reach the request.buildSearchBodyalso takes optionalaggregations, returning a page of hits and the aggregations over the same query in one request. Guard it like an aggregation: bucket probe first,ignore: [503].- Both body builders accept only translator outputs —
TranslatedQuery,TranslatedSource,TranslatedSort,FullTextQueryandTranslatedAggregations— never raw caller objects.
read-contract— the wire contract betweenunified-cloud-apiand thedatasync-read-handlerLambda:DataSyncReadEvent/DataSyncReadResulttypes plus a zod schema for each direction.dataSyncReadEventSchemavalidates the event's shape and per-field bounds; theskip + pageSizewindow isassertPageWindow's job, called separately.dataSyncReadResultSchemavalidates what comes back, and is deliberately not.strict()— the Lambda deploys independently and may run ahead, so an unknown result key is stripped rather than failing every read during the skew window. Its two arms are mutually exclusive, so a response carrying bothdataanderroris rejected rather than silently resolved to one of them. This package pins[email protected]whileunified-cloud-apiuses zod 4. Each side calls these schema instances' ownparse/safeParse, which is safe; what a future consumer must not do is import a schema from here and compose or merge it with a zod-4 one.opensearch-client—buildClientOptions, the pure env-driven OpenSearch client construction shared by unified-cloud-api and thedatasync-read-handlerLambda: IAM SigV4, either signed directly with the caller's own task/execution role or — only whenOPENSEARCH_MASTER_ROLE_ARNis set — via an assumed master role, or basic auth otherwise.errors—SyncedQueryRejection, the framework-free 400 every consumer maps to its own transport.
This package uses the tech stack provided by the Connect Monorepo global setup. Please check the root README for more information.
Depth limit and overflow
flattenItem stops at depth 5 (an object costs 1, an array of objects 2) and drops anything deeper; arrays of arrays pass through it as-is. flattenRecord keeps both: each such subtree is stored whole in overflow, keyed by its
flattened path, and restoreRecord merges it back on read. Overflow is not indexed: filter, sort
and aggs reject paths past the limit, and source selects the whole subtree at the cut point.
maxOverflowBytes caps a record's overflow; over it, the largest paths are dropped first.
An array of arrays within the depth limit has no column, so it is returned with the record but cannot
be selected through source; past the limit it sits inside the overflow subtree a deep source path
selects.
