@peerbit/document
Advanced tools
Comparing version 9.0.26 to 9.1.0
export * from "@peerbit/indexer-interface"; | ||
export * from "@peerbit/document-interface"; | ||
export * from "./program.js"; | ||
export type { CanRead, CanSearch, DocumentIndex, IDocumentWithContext, OpenOptions, QueryOptions, RemoteQueryOptions, ResultsIterator, SearchOptions, TransformOptions, TransformerAsConstructor, TransformerAsFunction, } from "./search.js"; | ||
export type { CanRead, CanSearch, DocumentIndex, WithIndexedContext, OpenOptions, QueryOptions, RemoteQueryOptions, ResultsIterator, SearchOptions, TransformOptions, TransformerAsConstructor, TransformerAsFunction, } from "./search.js"; | ||
export * from "./operation.js"; | ||
@@ -6,0 +6,0 @@ export { MAX_BATCH_SIZE as MAX_DOCUMENT_SIZE } from "./constants.js"; |
@@ -40,3 +40,3 @@ import { type AbstractType } from "@dao-xyz/borsh"; | ||
export type CanReadIndexed<I> = (result: I, from: PublicSignKey) => Promise<boolean> | boolean; | ||
type ValueTypeFromRequest<Resolve extends boolean | undefined, T, I> = Resolve extends false ? I : T; | ||
type ValueTypeFromRequest<Resolve extends boolean | undefined, T, I> = Resolve extends false ? WithContext<I> : WithContext<T>; | ||
export declare class IndexableContext implements types.Context { | ||
@@ -57,5 +57,8 @@ created: bigint; | ||
} | ||
export type IDocumentWithContext<I> = { | ||
export type WithIndexedContext<I> = { | ||
__context: IndexableContext; | ||
} & I; | ||
export type WithContext<I> = { | ||
__context: types.Context; | ||
} & I; | ||
export type TransformerAsConstructor<T, I> = { | ||
@@ -75,3 +78,3 @@ type?: new (arg: T, context: types.Context) => I; | ||
canSearch?: CanSearch; | ||
replicate: (request: types.SearchRequest | types.SearchRequestIndexed, results: types.Results<types.ResultTypeFromRequest<types.SearchRequest | types.SearchRequestIndexed>>) => Promise<void>; | ||
replicate: (request: types.SearchRequest | types.SearchRequestIndexed, results: types.Results<types.ResultTypeFromRequest<types.SearchRequest | types.SearchRequestIndexed, T, I>>) => Promise<void>; | ||
indexBy?: string | string[]; | ||
@@ -83,3 +86,3 @@ transform?: TransformOptions<T, I>; | ||
}; | ||
type IndexableClass<I> = new (value: I, context: IndexableContext) => IDocumentWithContext<I>; | ||
type IndexableClass<I> = new (value: I, context: IndexableContext) => WithIndexedContext<I>; | ||
export declare class DocumentIndex<T, I extends Record<string, any>, D extends ReplicationDomain<any, Operation, any>> extends Program<OpenOptions<T, I, D>> { | ||
@@ -95,3 +98,3 @@ _query: RPC<types.AbstractSearchRequest, types.AbstractSearchResult>; | ||
private indexByResolver; | ||
index: indexerTypes.Index<IDocumentWithContext<I>>; | ||
index: indexerTypes.Index<WithIndexedContext<I>>; | ||
private _resumableIterators; | ||
@@ -116,8 +119,8 @@ compatibility: 6 | 7 | 8 | undefined; | ||
drop(from?: Program): Promise<boolean>; | ||
get<Options extends QueryOptions<T, D, true | undefined>>(key: indexerTypes.Ideable | indexerTypes.IdKey, options?: Options): Promise<T>; | ||
get<Options extends QueryOptions<T, D, false>>(key: indexerTypes.Ideable | indexerTypes.IdKey, options?: Options): Promise<I>; | ||
getFromGid(gid: string): Promise<indexerTypes.IndexedResult<IDocumentWithContext<I>>>; | ||
get<Options extends QueryOptions<T, D, true | undefined>>(key: indexerTypes.Ideable | indexerTypes.IdKey, options?: Options): Promise<WithContext<T>>; | ||
get<Options extends QueryOptions<T, D, false>>(key: indexerTypes.Ideable | indexerTypes.IdKey, options?: Options): Promise<WithContext<I>>; | ||
getFromGid(gid: string): Promise<indexerTypes.IndexedResult<WithIndexedContext<I>>>; | ||
put(value: T, entry: Entry<Operation>, id: indexerTypes.IdKey): Promise<void>; | ||
del(key: indexerTypes.IdKey): indexerTypes.IdKey[] | Promise<indexerTypes.IdKey[]>; | ||
getDetailed<Options extends QueryOptions<T, D, Resolve>, Resolve extends boolean | undefined = ExtractResolveFromOptions<Options>, RT extends types.Result = Resolve extends true ? types.ResultValue<T> : types.ResultIndexedValue<I>>(key: indexerTypes.IdKey | indexerTypes.IdPrimitive, options?: QueryOptions<T, D, Resolve>): Promise<types.Results<RT>[] | undefined>; | ||
getDetailed<Options extends QueryOptions<T, D, Resolve>, Resolve extends boolean | undefined = ExtractResolveFromOptions<Options>, RT extends types.Result = Resolve extends true ? types.ResultValue<WithContext<T>> : types.ResultIndexedValue<WithContext<I>>>(key: indexerTypes.IdKey | indexerTypes.IdPrimitive, options?: QueryOptions<T, D, Resolve>): Promise<types.Results<RT>[] | undefined>; | ||
getSize(): Promise<number> | number; | ||
@@ -127,3 +130,3 @@ private resolveDocument; | ||
canRead?: CanRead<I>; | ||
}): Promise<types.Results<types.ResultTypeFromRequest<R>>>; | ||
}): Promise<types.Results<types.ResultTypeFromRequest<R, T, I>>>; | ||
private clearResultsQueue; | ||
@@ -130,0 +133,0 @@ private clearAllResultQueues; |
@@ -154,2 +154,10 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { | ||
}; | ||
const coerceWithContext = (value, context) => { | ||
if (value.__context) { | ||
return value; | ||
} | ||
let valueWithContext = value; | ||
valueWithContext.__context = context; | ||
return valueWithContext; | ||
}; | ||
let DocumentIndex = class DocumentIndex extends Program { | ||
@@ -327,3 +335,4 @@ _query; | ||
async get(key, options) { | ||
return (await this.getDetailed(key instanceof indexerTypes.IdKey ? key : indexerTypes.toId(key), options))?.[0]?.results[0]?.value; | ||
const result = (await this.getDetailed(key instanceof indexerTypes.IdKey ? key : indexerTypes.toId(key), options))?.[0]?.results[0]; | ||
return result?.value; | ||
} | ||
@@ -421,23 +430,35 @@ async getFromGid(gid) { | ||
} | ||
if (resolve && | ||
// if we are to resolve the document we need to go through all results and replace the results with the resolved values | ||
const shouldResolve = resolve && | ||
requestClazz === types.SearchRequestIndexed && | ||
!this.indexedTypeIsDocumentType && | ||
results) { | ||
results; | ||
if (results) { | ||
for (const set of results) { | ||
let coercedResult = []; | ||
for (const value of set.results) { | ||
const resolved = value instanceof types.ResultIndexedValue | ||
? (await this.resolveDocument({ | ||
indexed: value.value, | ||
head: value.context.head, | ||
}))?.value | ||
: value.value; | ||
let missingValues = false; | ||
for (let i = 0; i < set.results.length; i++) { | ||
let value = set.results[i]; | ||
let resolved; | ||
if (shouldResolve) { | ||
resolved = | ||
value instanceof types.ResultIndexedValue | ||
? (await this.resolveDocument({ | ||
indexed: value.value, | ||
head: value.context.head, | ||
}))?.value | ||
: value.value; | ||
} | ||
else { | ||
resolved = value.value; | ||
} | ||
if (resolved) { | ||
coercedResult.push(new types.ResultValue({ | ||
context: value.context, | ||
value: resolved, | ||
})); | ||
set.results[i]._value = coerceWithContext(resolved, value.context); | ||
} | ||
else { | ||
missingValues = true; | ||
} | ||
} | ||
set.results = coercedResult; | ||
if (missingValues) { | ||
set.results = set.results.filter((x) => !!x); | ||
} | ||
} | ||
@@ -800,4 +821,4 @@ } | ||
from, | ||
indexed: result.indexed || | ||
(await this.transformer(result.value, result.context)), | ||
indexed: coerceWithContext(result.indexed || | ||
(await this.transformer(result.value, result.context)), result.context), | ||
}); | ||
@@ -815,3 +836,3 @@ } | ||
from, | ||
indexed: result.indexed || result.value, | ||
indexed: coerceWithContext(result.indexed || result.value, result.context), | ||
}); | ||
@@ -891,4 +912,5 @@ } | ||
from: this.node.identity.publicKey, | ||
indexed: result.indexed || | ||
(await this.transformer(result.value, result.context)), | ||
indexed: coerceWithContext(result.indexed || | ||
(await this.transformer(result.value, // TODO tyes | ||
result.context)), result.context), | ||
}); | ||
@@ -942,5 +964,5 @@ } | ||
from: from, | ||
indexed: result instanceof types.ResultIndexedValue | ||
indexed: coerceWithContext(result instanceof types.ResultIndexedValue | ||
? result.value | ||
: await this.transformer(result.value, result.context), | ||
: await this.transformer(result.value, result.context), result.context), | ||
}); | ||
@@ -996,3 +1018,3 @@ } | ||
if (resolve) { | ||
coercedBatch = (await Promise.all(batch.map(async (x) => x.value instanceof this.documentType | ||
coercedBatch = (await Promise.all(batch.map(async (x) => coerceWithContext(x.value instanceof this.documentType | ||
? x.value | ||
@@ -1002,6 +1024,6 @@ : (await this.resolveDocument({ | ||
indexed: x.indexed, | ||
}))?.value))).filter((x) => !!x); | ||
}))?.value, x.context)))).filter((x) => !!x); | ||
} | ||
else { | ||
coercedBatch = batch.map((x) => x.value); | ||
coercedBatch = batch.map((x) => coerceWithContext(x.value, x.context)); | ||
} | ||
@@ -1008,0 +1030,0 @@ return dedup(coercedBatch, this.indexByResolver); |
{ | ||
"name": "@peerbit/document", | ||
"version": "9.0.26", | ||
"version": "9.1.0", | ||
"description": "Document store implementation", | ||
@@ -63,11 +63,11 @@ "type": "module", | ||
"@peerbit/program": "5.1.6", | ||
"@peerbit/rpc": "5.0.36", | ||
"@peerbit/shared-log": "10.3.21", | ||
"@peerbit/rpc": "5.0.37", | ||
"@peerbit/shared-log": "10.3.22", | ||
"@peerbit/indexer-interface": "^2.0.6", | ||
"@peerbit/indexer-simple": "^1.1.9", | ||
"@peerbit/indexer-sqlite3": "^1.2.10", | ||
"@peerbit/document-interface": "^2.0.11" | ||
"@peerbit/indexer-sqlite3": "^1.2.11", | ||
"@peerbit/document-interface": "^2.0.12" | ||
}, | ||
"devDependencies": { | ||
"@peerbit/test-utils": "2.1.26", | ||
"@peerbit/test-utils": "2.1.27", | ||
"@peerbit/time": "2.0.8", | ||
@@ -74,0 +74,0 @@ "@types/pidusage": "^2.0.5", |
@@ -8,3 +8,3 @@ export * from "@peerbit/indexer-interface"; | ||
DocumentIndex, | ||
IDocumentWithContext, | ||
WithIndexedContext, | ||
OpenOptions, | ||
@@ -11,0 +11,0 @@ QueryOptions, |
@@ -37,3 +37,3 @@ import { type AbstractType, field, serialize, variant } from "@dao-xyz/borsh"; | ||
value: T; | ||
indexed: I; | ||
indexed: WithContext<I>; | ||
context: types.Context; | ||
@@ -149,3 +149,5 @@ from: PublicSignKey; | ||
options?: QueryDetailedOptions<T, D, any>, | ||
): Promise<RPCResponse<types.Results<types.ResultTypeFromRequest<R>>>[]> => { | ||
): Promise< | ||
RPCResponse<types.Results<types.ResultTypeFromRequest<R, T, I>>>[] | ||
> => { | ||
const results: RPCResponse<types.Results<any>>[] = []; | ||
@@ -239,3 +241,3 @@ for (const response of responses) { | ||
I, | ||
> = Resolve extends false ? I : T; | ||
> = Resolve extends false ? WithContext<I> : WithContext<T>; | ||
@@ -283,6 +285,10 @@ @variant(0) | ||
export type IDocumentWithContext<I> = { | ||
export type WithIndexedContext<I> = { | ||
__context: IndexableContext; | ||
} & I; | ||
export type WithContext<I> = { | ||
__context: types.Context; | ||
} & I; | ||
export type TransformerAsConstructor<T, I> = { | ||
@@ -320,3 +326,5 @@ type?: new (arg: T, context: types.Context) => I; | ||
types.ResultTypeFromRequest< | ||
types.SearchRequest | types.SearchRequestIndexed | ||
types.SearchRequest | types.SearchRequestIndexed, | ||
T, | ||
I | ||
> | ||
@@ -335,4 +343,17 @@ >, | ||
context: IndexableContext, | ||
) => IDocumentWithContext<I>; | ||
) => WithIndexedContext<I>; | ||
const coerceWithContext = <T>( | ||
value: T | WithContext<T>, | ||
context: types.Context, | ||
): WithContext<T> => { | ||
if ((value as WithContext<T>).__context) { | ||
return value as WithContext<T>; | ||
} | ||
let valueWithContext: WithContext<T> = value as any; | ||
valueWithContext.__context = context; | ||
return valueWithContext; | ||
}; | ||
@variant("documents_index") | ||
@@ -364,4 +385,4 @@ export class DocumentIndex< | ||
private indexByResolver: (obj: any) => string | Uint8Array; | ||
index: indexerTypes.Index<IDocumentWithContext<I>>; | ||
private _resumableIterators: ResumableIterators<IDocumentWithContext<I>>; | ||
index: indexerTypes.Index<WithIndexedContext<I>>; | ||
private _resumableIterators: ResumableIterators<WithIndexedContext<I>>; | ||
@@ -393,3 +414,3 @@ compatibility: 6 | 7 | 8 | undefined; | ||
timeout: ReturnType<typeof setTimeout>; | ||
queue: indexerTypes.IndexedResult<IDocumentWithContext<I>>[]; | ||
queue: indexerTypes.IndexedResult<WithIndexedContext<I>>[]; | ||
fromQuery: types.SearchRequest | types.SearchRequestIndexed; | ||
@@ -438,3 +459,3 @@ } | ||
context: types.Context, | ||
) => IDocumentWithContext<I>; | ||
) => WithIndexedContext<I>; | ||
@@ -485,3 +506,3 @@ // if this.type is a class that extends Program we want to do special functionality | ||
/* maxBatchSize: MAX_BATCH_SIZE */ | ||
})) || new HashmapIndex<IDocumentWithContext<I>>(); | ||
})) || new HashmapIndex<WithIndexedContext<I>>(); | ||
@@ -597,3 +618,3 @@ this._resumableIterators = new ResumableIterators(this.index); | ||
options?: Options, | ||
): Promise<T>; | ||
): Promise<WithContext<T>>; | ||
@@ -603,3 +624,3 @@ public async get<Options extends QueryOptions<T, D, false>>( | ||
options?: Options, | ||
): Promise<I>; | ||
): Promise<WithContext<I>>; | ||
@@ -610,3 +631,3 @@ public async get< | ||
>(key: indexerTypes.Ideable | indexerTypes.IdKey, options?: Options) { | ||
return ( | ||
const result = ( | ||
await this.getDetailed( | ||
@@ -616,3 +637,4 @@ key instanceof indexerTypes.IdKey ? key : indexerTypes.toId(key), | ||
) | ||
)?.[0]?.results[0]?.value; | ||
)?.[0]?.results[0]; | ||
return result?.value; | ||
} | ||
@@ -676,4 +698,4 @@ | ||
RT extends types.Result = Resolve extends true | ||
? types.ResultValue<T> | ||
: types.ResultIndexedValue<I>, | ||
? types.ResultValue<WithContext<T>> | ||
: types.ResultIndexedValue<WithContext<I>>, | ||
>( | ||
@@ -684,3 +706,6 @@ key: indexerTypes.IdKey | indexerTypes.IdPrimitive, | ||
let results: | ||
| types.Results<types.ResultValue<T> | types.ResultIndexedValue<I>>[] | ||
| types.Results< | ||
| types.ResultValue<WithContext<T>> | ||
| types.ResultIndexedValue<WithContext<I>> | ||
>[] | ||
| undefined; | ||
@@ -746,31 +771,38 @@ const resolve = options?.resolve || options?.resolve == null; | ||
if ( | ||
// if we are to resolve the document we need to go through all results and replace the results with the resolved values | ||
const shouldResolve = | ||
resolve && | ||
requestClazz === types.SearchRequestIndexed && | ||
!this.indexedTypeIsDocumentType && | ||
results | ||
) { | ||
results; | ||
if (results) { | ||
for (const set of results) { | ||
let coercedResult: types.ResultValue<T>[] = []; | ||
for (const value of set.results) { | ||
const resolved = | ||
value instanceof types.ResultIndexedValue | ||
? ( | ||
await this.resolveDocument({ | ||
indexed: value.value, | ||
head: value.context.head, | ||
}) | ||
)?.value | ||
: value.value; | ||
let missingValues = false; | ||
for (let i = 0; i < set.results.length; i++) { | ||
let value = set.results[i]; | ||
let resolved: T | undefined; | ||
if (shouldResolve) { | ||
resolved = | ||
value instanceof types.ResultIndexedValue | ||
? ( | ||
await this.resolveDocument({ | ||
indexed: value.value, | ||
head: value.context.head, | ||
}) | ||
)?.value | ||
: value.value; | ||
} else { | ||
resolved = value.value as T; | ||
} | ||
if (resolved) { | ||
coercedResult.push( | ||
new types.ResultValue({ | ||
context: value.context, | ||
value: resolved, | ||
}), | ||
); | ||
set.results[i]._value = coerceWithContext(resolved, value.context); | ||
} else { | ||
missingValues = true; | ||
} | ||
} | ||
set.results = coercedResult; | ||
if (missingValues) { | ||
set.results = set.results.filter((x) => !!x); | ||
} | ||
} | ||
@@ -842,3 +874,3 @@ } | ||
}, | ||
): Promise<types.Results<types.ResultTypeFromRequest<R>>> { | ||
): Promise<types.Results<types.ResultTypeFromRequest<R, T, I>>> { | ||
// We do special case for querying the id as we can do it faster than iterating | ||
@@ -854,3 +886,3 @@ | ||
let indexedResult: | ||
| indexerTypes.IndexedResults<IDocumentWithContext<I>> | ||
| indexerTypes.IndexedResults<WithIndexedContext<I>> | ||
| undefined = undefined; | ||
@@ -1019,5 +1051,3 @@ | ||
R extends types.SearchRequest | types.SearchRequestIndexed, | ||
RT extends types.Result = R extends types.SearchRequest | ||
? types.ResultValue<T> | ||
: types.ResultIndexedValue<I>, | ||
RT extends types.Result = types.ResultTypeFromRequest<R, T, I>, | ||
>( | ||
@@ -1051,3 +1081,4 @@ queryRequest: R, | ||
} | ||
const allResults: types.Results<types.ResultTypeFromRequest<R>>[] = []; | ||
const allResults: types.Results<types.ResultTypeFromRequest<R, T, I>>[] = | ||
[]; | ||
@@ -1067,3 +1098,3 @@ if (local) { | ||
let resolved: types.Results<types.ResultTypeFromRequest<R>>[] = []; | ||
let resolved: types.Results<types.ResultTypeFromRequest<R, T, I>>[] = []; | ||
if (remote) { | ||
@@ -1258,6 +1289,3 @@ const replicatorGroups = await this._log.getCover( | ||
kept: number; | ||
buffer: BufferedResult< | ||
types.ResultValue<T> | types.ResultIndexedValue<I>, | ||
I | ||
>[]; | ||
buffer: BufferedResult<types.ResultTypeFromRequest<R, T, I> | I, I>[]; | ||
} | ||
@@ -1274,4 +1302,4 @@ > = new Map(); | ||
const peerBuffers = (): { | ||
indexed: I; | ||
value: types.ResultValue<T> | types.ResultIndexedValue<I>; | ||
indexed: WithContext<I>; | ||
value: types.ResultTypeFromRequest<R, T, I> | I; | ||
from: PublicSignKey; | ||
@@ -1298,3 +1326,3 @@ context: types.Context; | ||
const results = response as types.Results< | ||
types.ResultTypeFromRequest<R> | ||
types.ResultTypeFromRequest<R, T, I> | ||
>; | ||
@@ -1308,4 +1336,6 @@ if (results.kept === 0n && results.results.length === 0) { | ||
} | ||
const buffer: BufferedResult<types.ResultTypeFromRequest<R>, I>[] = | ||
[]; | ||
const buffer: BufferedResult< | ||
types.ResultTypeFromRequest<R, T, I> | I, | ||
I | ||
>[] = []; | ||
@@ -1322,8 +1352,10 @@ for (const result of results.results) { | ||
buffer.push({ | ||
value: result.value, | ||
value: result.value as types.ResultTypeFromRequest<R, T, I>, | ||
context: result.context, | ||
from, | ||
indexed: | ||
indexed: coerceWithContext( | ||
(result.indexed as I) || | ||
(await this.transformer(result.value, result.context)), | ||
(await this.transformer(result.value, result.context)), | ||
result.context, | ||
), | ||
}); | ||
@@ -1343,3 +1375,6 @@ } else { | ||
from, | ||
indexed: result.indexed || result.value, | ||
indexed: coerceWithContext( | ||
result.indexed || result.value, | ||
result.context, | ||
), | ||
}); | ||
@@ -1442,8 +1477,10 @@ } | ||
from: this.node.identity.publicKey, | ||
indexed: | ||
indexed: coerceWithContext( | ||
result.indexed || | ||
(await this.transformer( | ||
result.value, | ||
result.context, | ||
)), | ||
(await this.transformer( | ||
result.value as any as T, // TODO tyes | ||
result.context, | ||
)), | ||
result.context, | ||
), | ||
}); | ||
@@ -1508,6 +1545,11 @@ } | ||
peerBuffer.buffer.push({ | ||
value: result.value, | ||
value: | ||
result.value as types.ResultTypeFromRequest< | ||
R, | ||
T, | ||
I | ||
>, | ||
context: result.context, | ||
from: from!, | ||
indexed: | ||
indexed: coerceWithContext( | ||
result instanceof types.ResultIndexedValue | ||
@@ -1519,2 +1561,4 @@ ? result.value | ||
), | ||
result.context, | ||
), | ||
}); | ||
@@ -1591,10 +1635,13 @@ } | ||
batch.map(async (x) => | ||
x.value instanceof this.documentType | ||
? x.value | ||
: ( | ||
await this.resolveDocument({ | ||
head: x.context.head, | ||
indexed: x.indexed, | ||
}) | ||
)?.value, | ||
coerceWithContext( | ||
x.value instanceof this.documentType | ||
? x.value | ||
: ( | ||
await this.resolveDocument({ | ||
head: x.context.head, | ||
indexed: x.indexed, | ||
}) | ||
)?.value, | ||
x.context, | ||
), | ||
), | ||
@@ -1604,7 +1651,5 @@ ) | ||
} else { | ||
coercedBatch = batch.map((x) => x.value) as ValueTypeFromRequest< | ||
Resolve, | ||
T, | ||
I | ||
>[]; | ||
coercedBatch = batch.map((x) => | ||
coerceWithContext(x.value, x.context), | ||
) as ValueTypeFromRequest<Resolve, T, I>[]; | ||
} | ||
@@ -1611,0 +1656,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
303005
5501
+ Added@peerbit/log@4.0.39(transitive)
+ Added@peerbit/rpc@5.0.37(transitive)
+ Added@peerbit/shared-log@10.3.22(transitive)
- Removed@peerbit/log@4.0.38(transitive)
- Removed@peerbit/rpc@5.0.36(transitive)
- Removed@peerbit/shared-log@10.3.21(transitive)
Updated@peerbit/rpc@5.0.37
Updated@peerbit/shared-log@10.3.22