@peerbit/document 13.0.34 → 13.0.35

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/program.ts CHANGED
@@ -6,7 +6,10 @@ import {
6
6
  variant,
7
7
  } from "@dao-xyz/borsh";
8
8
  import { AccessError, SignatureWithKey } from "@peerbit/crypto";
9
- import { NotFoundError, ResultIndexedValue } from "@peerbit/document-interface";
9
+ import {
10
+ NotFoundError,
11
+ type ResultIndexedValue,
12
+ } from "@peerbit/document-interface";
10
13
  import type { QueryCacheOptions } from "@peerbit/indexer-cache";
11
14
  import * as indexerTypes from "@peerbit/indexer-interface";
12
15
  import {
@@ -38,13 +41,14 @@ import {
38
41
  isDeleteOperation,
39
42
  isPutOperation,
40
43
  } from "./operation.js";
44
+ import { isResultIndexedValue } from "./result-shape.js";
41
45
  import {
42
46
  type CanRead,
43
47
  type CanSearch,
44
48
  DocumentIndex,
45
49
  type GetOptions,
46
- type IndexedContextOnly,
47
50
  INDEX_CONTEXT_SHAPE,
51
+ type IndexedContextOnly,
48
52
  type PrefetchOptions,
49
53
  type ReachScope,
50
54
  type TransformOptions,
@@ -196,12 +200,13 @@ export class Documents<
196
200
  | indexerTypes.IndexedResult<IndexedContextOnly<I>>
197
201
  | null
198
202
  | undefined,
199
- ): indexerTypes.ReturnTypeFromShape<
200
- WithContext<I>,
201
- typeof INDEX_CONTEXT_SHAPE
202
- >["__context"]
203
+ ):
204
+ | indexerTypes.ReturnTypeFromShape<
205
+ WithContext<I>,
206
+ typeof INDEX_CONTEXT_SHAPE
207
+ >["__context"]
203
208
  | undefined {
204
- return existing instanceof ResultIndexedValue
209
+ return isResultIndexedValue(existing)
205
210
  ? existing.context
206
211
  : existing?.value.__context;
207
212
  }
@@ -268,7 +273,7 @@ export class Documents<
268
273
  results.results
269
274
  .flat()
270
275
  .map((x) =>
271
- x instanceof ResultIndexedValue && x.entries.length > 0
276
+ isResultIndexedValue(x) && x.entries.length > 0
272
277
  ? x.entries[0]
273
278
  : x.context.head,
274
279
  ),
@@ -499,10 +504,7 @@ export class Documents<
499
504
  if (this.immutable) {
500
505
  // key already exist but pick the oldest entry
501
506
  // this is because we can not overwrite same id if immutable
502
- if (
503
- existingContext.created <
504
- entry.meta.clock.timestamp.wallTime
505
- ) {
507
+ if (existingContext.created < entry.meta.clock.timestamp.wallTime) {
506
508
  return false;
507
509
  }
508
510
 
@@ -0,0 +1,47 @@
1
+ import type { AbstractType } from "@dao-xyz/borsh";
2
+ import * as types from "@peerbit/document-interface";
3
+
4
+ const isObject = (value: unknown): value is Record<string, unknown> =>
5
+ value != null && typeof value === "object";
6
+
7
+ const isResultLike = (
8
+ value: unknown,
9
+ ): value is {
10
+ context: unknown;
11
+ entries?: unknown;
12
+ init: (type: unknown) => void;
13
+ } =>
14
+ isObject(value) &&
15
+ "_source" in value &&
16
+ "context" in value &&
17
+ typeof value.init === "function";
18
+
19
+ export const isResultIndexedValue = <I>(
20
+ value: unknown,
21
+ ): value is types.ResultIndexedValue<I> =>
22
+ value instanceof types.ResultIndexedValue ||
23
+ (isResultLike(value) && Array.isArray(value.entries));
24
+
25
+ export const isResultValue = <T, I = Record<string, any>>(
26
+ value: unknown,
27
+ ): value is types.ResultValue<T, I> =>
28
+ value instanceof types.ResultValue ||
29
+ (isResultLike(value) && !Array.isArray(value.entries));
30
+
31
+ export const isResults = <R extends types.Result>(
32
+ value: unknown,
33
+ ): value is types.Results<R> =>
34
+ value instanceof types.Results ||
35
+ (isObject(value) && Array.isArray(value.results) && "kept" in value);
36
+
37
+ export const initializeResultType = <T, I>(
38
+ result: unknown,
39
+ documentType: AbstractType<T>,
40
+ indexedType: AbstractType<I>,
41
+ ) => {
42
+ if (isResultValue(result)) {
43
+ result.init(documentType);
44
+ return;
45
+ }
46
+ (result as types.ResultIndexedValue<I>).init(indexedType);
47
+ };