@nicia-ai/typegraph 0.9.2 → 0.11.0
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/dist/index.cjs +279 -114
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +279 -114
- package/dist/index.js.map +1 -1
- package/dist/interchange/index.d.cts +1 -1
- package/dist/interchange/index.d.ts +1 -1
- package/dist/profiler/index.d.cts +1 -1
- package/dist/profiler/index.d.ts +1 -1
- package/dist/{store-DgzIgrmn.d.cts → store-DyGdpDFr.d.cts} +103 -3
- package/dist/{store-wqOO3GSy.d.ts → store-nQ1ATBlN.d.ts} +103 -3
- package/package.json +5 -5
package/dist/index.d.cts
CHANGED
|
@@ -3,8 +3,8 @@ import { N as NodeType, E as EdgeType } from './types-DMzKq0d5.cjs';
|
|
|
3
3
|
export { A as AnyEdgeType, C as Cardinality, a as Collation, D as DeleteBehavior, b as EdgeId, c as EdgeProps, d as EdgeRegistration, e as EdgeTypeWithEndpoints, f as EndpointExistence, G as GraphDefaults, g as NodeId, h as NodeProps, i as NodeRegistration, S as SqlDialect, T as TemporalMode, U as UniqueConstraint, j as UniquenessScope, k as isEdgeType, l as isEdgeTypeWithEndpoints, m as isNodeType } from './types-DMzKq0d5.cjs';
|
|
4
4
|
import { I as InferenceType, M as MetaEdge, O as OntologyRelation } from './types-GLkwvQvS.cjs';
|
|
5
5
|
export { D as DEFAULT_SQL_SCHEMA, G as GraphBackend, a as MetaEdgeProperties, S as SqlSchema, b as SqlTableNames, T as TransactionBackend, c as createSqlSchema, i as isMetaEdge } from './types-GLkwvQvS.cjs';
|
|
6
|
-
import { A as AliasMap, E as EdgeAliasMap, Q as QueryBuilder, T as TraversalBuilder, C as CreateQueryBuilderOptions } from './store-
|
|
7
|
-
export { a as AggregateResult, b as
|
|
6
|
+
import { A as AliasMap, E as EdgeAliasMap, Q as QueryBuilder, T as TraversalBuilder, C as CreateQueryBuilderOptions } from './store-DyGdpDFr.cjs';
|
|
7
|
+
export { a as AggregateResult, b as AnyEdge, c as AnyNode, d as ConstraintNames, e as CreateEdgeInput, f as CreateNodeInput, g as Edge, h as EdgeAccessor, i as EdgeCollection, j as EdgeFindByEndpointsOptions, k as EdgeGetOrCreateByEndpointsOptions, l as EdgeGetOrCreateByEndpointsResult, m as EmbeddingSchema, n as EmbeddingValue, o as ExecutableAggregateQuery, p as ExecutableQuery, F as FieldAccessor, G as GetOrCreateAction, H as HookContext, I as IfExistsMode, N as Node, q as NodeAccessor, r as NodeAlias, s as NodeCollection, t as NodeGetOrCreateByConstraintOptions, u as NodeGetOrCreateByConstraintResult, v as NodeRef, O as OperationHookContext, P as PaginateOptions, w as PaginatedResult, x as Predicate, y as PreparedQuery, z as PropsAccessor, B as QueryHookContext, D as QueryOptions, R as RecursiveTraversalOptions, S as SelectContext, J as SelectableEdge, K as SelectableNode, L as Store, M as StoreHooks, U as StoreOptions, V as StreamOptions, W as SubgraphOptions, X as SubgraphResult, Y as SubsetEdge, Z as SubsetNode, _ as TransactionContext, $ as TypedEdgeCollection, a0 as UnionableQuery, a1 as UpdateEdgeInput, a2 as UpdateNodeInput, a3 as createStore, a4 as createStoreWithSchema, a5 as embedding, a6 as exists, a7 as fieldRef, a8 as getEmbeddingDimensions, a9 as inSubquery, aa as isEmbeddingSchema, ab as isParameterRef, ac as notExists, ad as notInSubquery, ae as param } from './store-DyGdpDFr.cjs';
|
|
8
8
|
import { G as GraphDef, K as KindRegistry } from './manager-DGSnJa1v.cjs';
|
|
9
9
|
export { A as AllEdgeTypes, a as AllNodeTypes, E as EdgeKinds, b as GetEdgeType, c as GetNodeType, N as NodeKinds, d as defineGraph, g as getEdgeKinds, e as getNodeKinds, i as isGraphDef } from './manager-DGSnJa1v.cjs';
|
|
10
10
|
export { C as CardinalityError, a as CompilerInvariantError, b as ConfigurationError, D as DatabaseOperationError, c as DisjointError, E as EdgeNotFoundError, d as EndpointError, e as EndpointNotFoundError, f as ErrorCategory, K as KindNotFoundError, M as MigrationError, N as NodeConstraintNotFoundError, g as NodeNotFoundError, R as RestrictedDeleteError, S as SchemaMismatchError, T as TypeGraphError, h as TypeGraphErrorOptions, U as UniquenessError, i as UnsupportedPredicateError, V as ValidationError, j as ValidationErrorDetails, k as ValidationIssue, l as VersionConflictError, m as getErrorSuggestion, n as isConstraintError, o as isSystemError, p as isTypeGraphError, q as isUserRecoverable } from './index-QTnQwakS.cjs';
|
package/dist/index.d.ts
CHANGED
|
@@ -3,8 +3,8 @@ import { N as NodeType, E as EdgeType } from './types-DMzKq0d5.js';
|
|
|
3
3
|
export { A as AnyEdgeType, C as Cardinality, a as Collation, D as DeleteBehavior, b as EdgeId, c as EdgeProps, d as EdgeRegistration, e as EdgeTypeWithEndpoints, f as EndpointExistence, G as GraphDefaults, g as NodeId, h as NodeProps, i as NodeRegistration, S as SqlDialect, T as TemporalMode, U as UniqueConstraint, j as UniquenessScope, k as isEdgeType, l as isEdgeTypeWithEndpoints, m as isNodeType } from './types-DMzKq0d5.js';
|
|
4
4
|
import { I as InferenceType, M as MetaEdge, O as OntologyRelation } from './types-1YJKodRv.js';
|
|
5
5
|
export { D as DEFAULT_SQL_SCHEMA, G as GraphBackend, a as MetaEdgeProperties, S as SqlSchema, b as SqlTableNames, T as TransactionBackend, c as createSqlSchema, i as isMetaEdge } from './types-1YJKodRv.js';
|
|
6
|
-
import { A as AliasMap, E as EdgeAliasMap, Q as QueryBuilder, T as TraversalBuilder, C as CreateQueryBuilderOptions } from './store-
|
|
7
|
-
export { a as AggregateResult, b as
|
|
6
|
+
import { A as AliasMap, E as EdgeAliasMap, Q as QueryBuilder, T as TraversalBuilder, C as CreateQueryBuilderOptions } from './store-nQ1ATBlN.js';
|
|
7
|
+
export { a as AggregateResult, b as AnyEdge, c as AnyNode, d as ConstraintNames, e as CreateEdgeInput, f as CreateNodeInput, g as Edge, h as EdgeAccessor, i as EdgeCollection, j as EdgeFindByEndpointsOptions, k as EdgeGetOrCreateByEndpointsOptions, l as EdgeGetOrCreateByEndpointsResult, m as EmbeddingSchema, n as EmbeddingValue, o as ExecutableAggregateQuery, p as ExecutableQuery, F as FieldAccessor, G as GetOrCreateAction, H as HookContext, I as IfExistsMode, N as Node, q as NodeAccessor, r as NodeAlias, s as NodeCollection, t as NodeGetOrCreateByConstraintOptions, u as NodeGetOrCreateByConstraintResult, v as NodeRef, O as OperationHookContext, P as PaginateOptions, w as PaginatedResult, x as Predicate, y as PreparedQuery, z as PropsAccessor, B as QueryHookContext, D as QueryOptions, R as RecursiveTraversalOptions, S as SelectContext, J as SelectableEdge, K as SelectableNode, L as Store, M as StoreHooks, U as StoreOptions, V as StreamOptions, W as SubgraphOptions, X as SubgraphResult, Y as SubsetEdge, Z as SubsetNode, _ as TransactionContext, $ as TypedEdgeCollection, a0 as UnionableQuery, a1 as UpdateEdgeInput, a2 as UpdateNodeInput, a3 as createStore, a4 as createStoreWithSchema, a5 as embedding, a6 as exists, a7 as fieldRef, a8 as getEmbeddingDimensions, a9 as inSubquery, aa as isEmbeddingSchema, ab as isParameterRef, ac as notExists, ad as notInSubquery, ae as param } from './store-nQ1ATBlN.js';
|
|
8
8
|
import { G as GraphDef, K as KindRegistry } from './manager-BCLhWysp.js';
|
|
9
9
|
export { A as AllEdgeTypes, a as AllNodeTypes, E as EdgeKinds, b as GetEdgeType, c as GetNodeType, N as NodeKinds, d as defineGraph, g as getEdgeKinds, e as getNodeKinds, i as isGraphDef } from './manager-BCLhWysp.js';
|
|
10
10
|
export { C as CardinalityError, a as CompilerInvariantError, b as ConfigurationError, D as DatabaseOperationError, c as DisjointError, E as EdgeNotFoundError, d as EndpointError, e as EndpointNotFoundError, f as ErrorCategory, K as KindNotFoundError, M as MigrationError, N as NodeConstraintNotFoundError, g as NodeNotFoundError, R as RestrictedDeleteError, S as SchemaMismatchError, T as TypeGraphError, h as TypeGraphErrorOptions, U as UniquenessError, i as UnsupportedPredicateError, V as ValidationError, j as ValidationErrorDetails, k as ValidationIssue, l as VersionConflictError, m as getErrorSuggestion, n as isConstraintError, o as isSystemError, p as isTypeGraphError, q as isUserRecoverable } from './index-QTnQwakS.js';
|
package/dist/index.js
CHANGED
|
@@ -3035,7 +3035,7 @@ function compileRecursiveCte(ast, traversal, graphId, ctx, requiredColumnsByAlia
|
|
|
3035
3035
|
recursiveBaseWhereClauses.push(cycleCheck);
|
|
3036
3036
|
}
|
|
3037
3037
|
recursiveBaseWhereClauses.push(...edgePredicates, ...targetNodePredicates);
|
|
3038
|
-
function
|
|
3038
|
+
function compileRecursiveBranch2(branch) {
|
|
3039
3039
|
const recursiveFilterClauses = [
|
|
3040
3040
|
...recursiveBaseWhereClauses,
|
|
3041
3041
|
compileKindFilter2(branch.edgeKinds, "e.kind"),
|
|
@@ -3090,7 +3090,7 @@ function compileRecursiveCte(ast, traversal, graphId, ctx, requiredColumnsByAlia
|
|
|
3090
3090
|
const directTargetField = direction === "out" ? "to_id" : "from_id";
|
|
3091
3091
|
const directJoinKindField = direction === "out" ? "from_kind" : "to_kind";
|
|
3092
3092
|
const directTargetKindField = direction === "out" ? "to_kind" : "from_kind";
|
|
3093
|
-
const directBranch =
|
|
3093
|
+
const directBranch = compileRecursiveBranch2({
|
|
3094
3094
|
joinField: directJoinField,
|
|
3095
3095
|
targetField: directTargetField,
|
|
3096
3096
|
joinKindField: directJoinKindField,
|
|
@@ -3106,7 +3106,7 @@ function compileRecursiveCte(ast, traversal, graphId, ctx, requiredColumnsByAlia
|
|
|
3106
3106
|
(kind) => directEdgeKinds.includes(kind)
|
|
3107
3107
|
);
|
|
3108
3108
|
const duplicateGuard = overlappingKinds.length > 0 ? sql`NOT (e.from_id = e.to_id AND ${compileKindFilter2(overlappingKinds, "e.kind")})` : void 0;
|
|
3109
|
-
const inverseBranch =
|
|
3109
|
+
const inverseBranch = compileRecursiveBranch2({
|
|
3110
3110
|
joinField: inverseJoinField,
|
|
3111
3111
|
targetField: inverseTargetField,
|
|
3112
3112
|
joinKindField: inverseJoinKindField,
|
|
@@ -8269,21 +8269,29 @@ function narrowEdge(edge) {
|
|
|
8269
8269
|
function narrowEdges(edges) {
|
|
8270
8270
|
return edges;
|
|
8271
8271
|
}
|
|
8272
|
+
function buildCreateEdgeInput(kind, from, to, props, options) {
|
|
8273
|
+
const input = {
|
|
8274
|
+
kind,
|
|
8275
|
+
fromKind: from.kind,
|
|
8276
|
+
fromId: from.id,
|
|
8277
|
+
toKind: to.kind,
|
|
8278
|
+
toId: to.id,
|
|
8279
|
+
props
|
|
8280
|
+
};
|
|
8281
|
+
if (options?.id !== void 0) input.id = options.id;
|
|
8282
|
+
if (options?.validFrom !== void 0) input.validFrom = options.validFrom;
|
|
8283
|
+
if (options?.validTo !== void 0) input.validTo = options.validTo;
|
|
8284
|
+
return input;
|
|
8285
|
+
}
|
|
8286
|
+
function buildUpdateEdgeInput(id, props, options) {
|
|
8287
|
+
const input = { id, props };
|
|
8288
|
+
if (options?.validTo !== void 0) input.validTo = options.validTo;
|
|
8289
|
+
return input;
|
|
8290
|
+
}
|
|
8272
8291
|
function mapBulkEdgeInputs(kind, items) {
|
|
8273
|
-
return items.map(
|
|
8274
|
-
|
|
8275
|
-
|
|
8276
|
-
fromKind: item.from.kind,
|
|
8277
|
-
fromId: item.from.id,
|
|
8278
|
-
toKind: item.to.kind,
|
|
8279
|
-
toId: item.to.id,
|
|
8280
|
-
props: item.props ?? {}
|
|
8281
|
-
};
|
|
8282
|
-
if (item.id !== void 0) input.id = item.id;
|
|
8283
|
-
if (item.validFrom !== void 0) input.validFrom = item.validFrom;
|
|
8284
|
-
if (item.validTo !== void 0) input.validTo = item.validTo;
|
|
8285
|
-
return input;
|
|
8286
|
-
});
|
|
8292
|
+
return items.map(
|
|
8293
|
+
(item) => buildCreateEdgeInput(kind, item.from, item.to, item.props ?? {}, item)
|
|
8294
|
+
);
|
|
8287
8295
|
}
|
|
8288
8296
|
function createEdgeCollection(config) {
|
|
8289
8297
|
const {
|
|
@@ -8303,18 +8311,16 @@ function createEdgeCollection(config) {
|
|
|
8303
8311
|
} = config;
|
|
8304
8312
|
return {
|
|
8305
8313
|
async create(from, to, props, options) {
|
|
8306
|
-
const
|
|
8307
|
-
|
|
8308
|
-
|
|
8309
|
-
|
|
8310
|
-
|
|
8311
|
-
|
|
8312
|
-
|
|
8313
|
-
|
|
8314
|
-
|
|
8315
|
-
|
|
8316
|
-
if (options?.validTo !== void 0) input.validTo = options.validTo;
|
|
8317
|
-
const result = await executeEdgeCreate2(input, backend);
|
|
8314
|
+
const result = await executeEdgeCreate2(
|
|
8315
|
+
buildCreateEdgeInput(
|
|
8316
|
+
kind,
|
|
8317
|
+
from,
|
|
8318
|
+
to,
|
|
8319
|
+
props ?? {},
|
|
8320
|
+
options
|
|
8321
|
+
),
|
|
8322
|
+
backend
|
|
8323
|
+
);
|
|
8318
8324
|
return narrowEdge(result);
|
|
8319
8325
|
},
|
|
8320
8326
|
async getById(id, options) {
|
|
@@ -8351,12 +8357,10 @@ function createEdgeCollection(config) {
|
|
|
8351
8357
|
);
|
|
8352
8358
|
},
|
|
8353
8359
|
async update(id, props, options) {
|
|
8354
|
-
const
|
|
8355
|
-
id,
|
|
8356
|
-
|
|
8357
|
-
|
|
8358
|
-
if (options?.validTo !== void 0) input.validTo = options.validTo;
|
|
8359
|
-
const result = await executeEdgeUpdate2(input, backend);
|
|
8360
|
+
const result = await executeEdgeUpdate2(
|
|
8361
|
+
buildUpdateEdgeInput(id, props, options),
|
|
8362
|
+
backend
|
|
8363
|
+
);
|
|
8360
8364
|
return narrowEdge(result);
|
|
8361
8365
|
},
|
|
8362
8366
|
async findFrom(from) {
|
|
@@ -8469,29 +8473,26 @@ function createEdgeCollection(config) {
|
|
|
8469
8473
|
for (const item of items) {
|
|
8470
8474
|
const existing = existingMap.get(item.id);
|
|
8471
8475
|
if (existing) {
|
|
8472
|
-
const input = {
|
|
8473
|
-
id: item.id,
|
|
8474
|
-
props: item.props
|
|
8475
|
-
};
|
|
8476
|
-
if (item.validTo !== void 0) input.validTo = item.validTo;
|
|
8477
8476
|
toUpdate.push({
|
|
8478
8477
|
index: itemIndex,
|
|
8479
|
-
input
|
|
8478
|
+
input: buildUpdateEdgeInput(
|
|
8479
|
+
item.id,
|
|
8480
|
+
item.props ?? {},
|
|
8481
|
+
item
|
|
8482
|
+
),
|
|
8480
8483
|
clearDeleted: existing.deleted_at !== void 0
|
|
8481
8484
|
});
|
|
8482
8485
|
} else {
|
|
8483
|
-
|
|
8484
|
-
|
|
8485
|
-
|
|
8486
|
-
|
|
8487
|
-
|
|
8488
|
-
|
|
8489
|
-
|
|
8490
|
-
|
|
8491
|
-
|
|
8492
|
-
|
|
8493
|
-
if (item.validTo !== void 0) input.validTo = item.validTo;
|
|
8494
|
-
toCreate.push({ index: itemIndex, input });
|
|
8486
|
+
toCreate.push({
|
|
8487
|
+
index: itemIndex,
|
|
8488
|
+
input: buildCreateEdgeInput(
|
|
8489
|
+
kind,
|
|
8490
|
+
item.from,
|
|
8491
|
+
item.to,
|
|
8492
|
+
item.props ?? {},
|
|
8493
|
+
item
|
|
8494
|
+
)
|
|
8495
|
+
});
|
|
8495
8496
|
}
|
|
8496
8497
|
itemIndex++;
|
|
8497
8498
|
}
|
|
@@ -8620,17 +8621,20 @@ function narrowNode(node) {
|
|
|
8620
8621
|
function narrowNodes(nodes) {
|
|
8621
8622
|
return nodes;
|
|
8622
8623
|
}
|
|
8624
|
+
function buildCreateInput(kind, props, options) {
|
|
8625
|
+
const input = { kind, props };
|
|
8626
|
+
if (options?.id !== void 0) input.id = options.id;
|
|
8627
|
+
if (options?.validFrom !== void 0) input.validFrom = options.validFrom;
|
|
8628
|
+
if (options?.validTo !== void 0) input.validTo = options.validTo;
|
|
8629
|
+
return input;
|
|
8630
|
+
}
|
|
8631
|
+
function buildUpdateInput(kind, id, props, options) {
|
|
8632
|
+
const input = { kind, id, props };
|
|
8633
|
+
if (options?.validTo !== void 0) input.validTo = options.validTo;
|
|
8634
|
+
return input;
|
|
8635
|
+
}
|
|
8623
8636
|
function mapBulkNodeInputs(kind, items) {
|
|
8624
|
-
return items.map((item) =>
|
|
8625
|
-
const input = {
|
|
8626
|
-
kind,
|
|
8627
|
-
props: item.props
|
|
8628
|
-
};
|
|
8629
|
-
if (item.id !== void 0) input.id = item.id;
|
|
8630
|
-
if (item.validFrom !== void 0) input.validFrom = item.validFrom;
|
|
8631
|
-
if (item.validTo !== void 0) input.validTo = item.validTo;
|
|
8632
|
-
return input;
|
|
8633
|
-
});
|
|
8637
|
+
return items.map((item) => buildCreateInput(kind, item.props, item));
|
|
8634
8638
|
}
|
|
8635
8639
|
function createNodeCollection(config) {
|
|
8636
8640
|
const {
|
|
@@ -8655,14 +8659,13 @@ function createNodeCollection(config) {
|
|
|
8655
8659
|
} = config;
|
|
8656
8660
|
return {
|
|
8657
8661
|
async create(props, options) {
|
|
8658
|
-
|
|
8659
|
-
|
|
8660
|
-
|
|
8661
|
-
|
|
8662
|
-
|
|
8663
|
-
|
|
8664
|
-
|
|
8665
|
-
const result = await executeNodeCreate2(input, backend);
|
|
8662
|
+
return this.createFromRecord(props, options);
|
|
8663
|
+
},
|
|
8664
|
+
async createFromRecord(data, options) {
|
|
8665
|
+
const result = await executeNodeCreate2(
|
|
8666
|
+
buildCreateInput(kind, data, options),
|
|
8667
|
+
backend
|
|
8668
|
+
);
|
|
8666
8669
|
return narrowNode(result);
|
|
8667
8670
|
},
|
|
8668
8671
|
async getById(id, options) {
|
|
@@ -8700,13 +8703,10 @@ function createNodeCollection(config) {
|
|
|
8700
8703
|
);
|
|
8701
8704
|
},
|
|
8702
8705
|
async update(id, props, options) {
|
|
8703
|
-
const
|
|
8704
|
-
kind,
|
|
8705
|
-
|
|
8706
|
-
|
|
8707
|
-
};
|
|
8708
|
-
if (options?.validTo !== void 0) input.validTo = options.validTo;
|
|
8709
|
-
const result = await executeNodeUpdate2(input, backend);
|
|
8706
|
+
const result = await executeNodeUpdate2(
|
|
8707
|
+
buildUpdateInput(kind, id, props, options),
|
|
8708
|
+
backend
|
|
8709
|
+
);
|
|
8710
8710
|
return narrowNode(result);
|
|
8711
8711
|
},
|
|
8712
8712
|
async delete(id) {
|
|
@@ -8762,31 +8762,28 @@ function createNodeCollection(config) {
|
|
|
8762
8762
|
return backend.countNodesByKind(params);
|
|
8763
8763
|
},
|
|
8764
8764
|
async upsertById(id, props, options) {
|
|
8765
|
+
return this.upsertByIdFromRecord(
|
|
8766
|
+
id,
|
|
8767
|
+
props,
|
|
8768
|
+
options
|
|
8769
|
+
);
|
|
8770
|
+
},
|
|
8771
|
+
async upsertByIdFromRecord(id, data, options) {
|
|
8765
8772
|
const existing = await backend.getNode(graphId, kind, id);
|
|
8766
8773
|
if (existing) {
|
|
8767
|
-
const input = {
|
|
8768
|
-
kind,
|
|
8769
|
-
id,
|
|
8770
|
-
props
|
|
8771
|
-
};
|
|
8772
|
-
if (options?.validTo !== void 0) input.validTo = options.validTo;
|
|
8773
8774
|
const clearDeleted = existing.deleted_at !== void 0;
|
|
8774
|
-
const
|
|
8775
|
-
|
|
8776
|
-
|
|
8777
|
-
|
|
8778
|
-
|
|
8779
|
-
|
|
8780
|
-
kind,
|
|
8781
|
-
id,
|
|
8782
|
-
props
|
|
8783
|
-
};
|
|
8784
|
-
if (options?.validFrom !== void 0)
|
|
8785
|
-
input.validFrom = options.validFrom;
|
|
8786
|
-
if (options?.validTo !== void 0) input.validTo = options.validTo;
|
|
8787
|
-
const result = await executeNodeCreate2(input, backend);
|
|
8788
|
-
return narrowNode(result);
|
|
8775
|
+
const result2 = await executeNodeUpdate2(
|
|
8776
|
+
buildUpdateInput(kind, id, data, options),
|
|
8777
|
+
backend,
|
|
8778
|
+
{ clearDeleted }
|
|
8779
|
+
);
|
|
8780
|
+
return narrowNode(result2);
|
|
8789
8781
|
}
|
|
8782
|
+
const result = await executeNodeCreate2(
|
|
8783
|
+
buildCreateInput(kind, data, { ...options, id }),
|
|
8784
|
+
backend
|
|
8785
|
+
);
|
|
8786
|
+
return narrowNode(result);
|
|
8790
8787
|
},
|
|
8791
8788
|
async bulkCreate(items) {
|
|
8792
8789
|
const batchInputs = mapBulkNodeInputs(
|
|
@@ -8830,26 +8827,25 @@ function createNodeCollection(config) {
|
|
|
8830
8827
|
for (const item of items) {
|
|
8831
8828
|
const existing = existingMap.get(item.id);
|
|
8832
8829
|
if (existing) {
|
|
8833
|
-
const input = {
|
|
8834
|
-
kind,
|
|
8835
|
-
id: item.id,
|
|
8836
|
-
props: item.props
|
|
8837
|
-
};
|
|
8838
|
-
if (item.validTo !== void 0) input.validTo = item.validTo;
|
|
8839
8830
|
toUpdate.push({
|
|
8840
8831
|
index: itemIndex,
|
|
8841
|
-
input
|
|
8832
|
+
input: buildUpdateInput(
|
|
8833
|
+
kind,
|
|
8834
|
+
item.id,
|
|
8835
|
+
item.props,
|
|
8836
|
+
item
|
|
8837
|
+
),
|
|
8842
8838
|
clearDeleted: existing.deleted_at !== void 0
|
|
8843
8839
|
});
|
|
8844
8840
|
} else {
|
|
8845
|
-
|
|
8846
|
-
|
|
8847
|
-
|
|
8848
|
-
|
|
8849
|
-
|
|
8850
|
-
|
|
8851
|
-
|
|
8852
|
-
|
|
8841
|
+
toCreate.push({
|
|
8842
|
+
index: itemIndex,
|
|
8843
|
+
input: buildCreateInput(
|
|
8844
|
+
kind,
|
|
8845
|
+
item.props,
|
|
8846
|
+
item
|
|
8847
|
+
)
|
|
8848
|
+
});
|
|
8853
8849
|
}
|
|
8854
8850
|
itemIndex++;
|
|
8855
8851
|
}
|
|
@@ -11190,6 +11186,142 @@ async function executeNodeBulkGetOrCreateByConstraint(ctx, kind, constraintName,
|
|
|
11190
11186
|
}
|
|
11191
11187
|
return results;
|
|
11192
11188
|
}
|
|
11189
|
+
var DEFAULT_SUBGRAPH_MAX_DEPTH = 10;
|
|
11190
|
+
function normalizeProps(value) {
|
|
11191
|
+
return typeof value === "string" ? value : JSON.stringify(value ?? {});
|
|
11192
|
+
}
|
|
11193
|
+
async function executeSubgraph(params) {
|
|
11194
|
+
const { options } = params;
|
|
11195
|
+
if (options.edges.length === 0) {
|
|
11196
|
+
return { nodes: [], edges: [] };
|
|
11197
|
+
}
|
|
11198
|
+
const maxDepth = Math.min(
|
|
11199
|
+
options.maxDepth ?? DEFAULT_SUBGRAPH_MAX_DEPTH,
|
|
11200
|
+
MAX_RECURSIVE_DEPTH
|
|
11201
|
+
);
|
|
11202
|
+
const ctx = {
|
|
11203
|
+
graphId: params.graphId,
|
|
11204
|
+
rootId: params.rootId,
|
|
11205
|
+
edgeKinds: options.edges,
|
|
11206
|
+
maxDepth,
|
|
11207
|
+
includeKinds: options.includeKinds,
|
|
11208
|
+
excludeRoot: options.excludeRoot ?? false,
|
|
11209
|
+
direction: options.direction ?? "out",
|
|
11210
|
+
cyclePolicy: options.cyclePolicy ?? "prevent",
|
|
11211
|
+
dialect: params.dialect,
|
|
11212
|
+
schema: params.schema ?? DEFAULT_SQL_SCHEMA,
|
|
11213
|
+
backend: params.backend
|
|
11214
|
+
};
|
|
11215
|
+
const reachableCte = buildReachableCte(ctx);
|
|
11216
|
+
const includedIdsCte = buildIncludedIdsCte(ctx);
|
|
11217
|
+
const [nodeRows, edgeRows] = await Promise.all([
|
|
11218
|
+
fetchSubgraphNodes(ctx, reachableCte, includedIdsCte),
|
|
11219
|
+
fetchSubgraphEdges(ctx, reachableCte, includedIdsCte)
|
|
11220
|
+
]);
|
|
11221
|
+
const nodes = nodeRows.map(
|
|
11222
|
+
(row) => rowToNode({ ...row, props: normalizeProps(row.props) })
|
|
11223
|
+
);
|
|
11224
|
+
const edges = edgeRows.map(
|
|
11225
|
+
(row) => rowToEdge({ ...row, props: normalizeProps(row.props) })
|
|
11226
|
+
);
|
|
11227
|
+
return {
|
|
11228
|
+
nodes,
|
|
11229
|
+
edges
|
|
11230
|
+
};
|
|
11231
|
+
}
|
|
11232
|
+
function buildReachableCte(ctx) {
|
|
11233
|
+
const shouldTrackPath = ctx.cyclePolicy === "prevent";
|
|
11234
|
+
const edgeKindFilter = compileKindFilter(sql.raw("e.kind"), ctx.edgeKinds);
|
|
11235
|
+
const initialPath = shouldTrackPath ? ctx.dialect.initializePath(sql.raw("n.id")) : void 0;
|
|
11236
|
+
const pathExtension = shouldTrackPath ? ctx.dialect.extendPath(sql.raw("r.path"), sql.raw("n.id")) : void 0;
|
|
11237
|
+
const cycleCheck = shouldTrackPath ? ctx.dialect.cycleCheck(sql.raw("n.id"), sql.raw("r.path")) : void 0;
|
|
11238
|
+
const baseColumns = [sql`n.id`, sql`n.kind`, sql`0 AS depth`];
|
|
11239
|
+
if (initialPath !== void 0) {
|
|
11240
|
+
baseColumns.push(sql`${initialPath} AS path`);
|
|
11241
|
+
}
|
|
11242
|
+
const baseCase = sql`SELECT ${sql.join(baseColumns, sql`, `)} FROM ${ctx.schema.nodesTable} n WHERE n.graph_id = ${ctx.graphId} AND n.id = ${ctx.rootId} AND n.deleted_at IS NULL`;
|
|
11243
|
+
const recursiveColumns = [
|
|
11244
|
+
sql`n.id`,
|
|
11245
|
+
sql`n.kind`,
|
|
11246
|
+
sql`r.depth + 1 AS depth`
|
|
11247
|
+
];
|
|
11248
|
+
if (pathExtension !== void 0) {
|
|
11249
|
+
recursiveColumns.push(sql`${pathExtension} AS path`);
|
|
11250
|
+
}
|
|
11251
|
+
const recursiveWhereClauses = [
|
|
11252
|
+
sql`e.graph_id = ${ctx.graphId}`,
|
|
11253
|
+
edgeKindFilter,
|
|
11254
|
+
sql`e.deleted_at IS NULL`,
|
|
11255
|
+
sql`n.deleted_at IS NULL`,
|
|
11256
|
+
sql`r.depth < ${ctx.maxDepth}`
|
|
11257
|
+
];
|
|
11258
|
+
if (cycleCheck !== void 0) {
|
|
11259
|
+
recursiveWhereClauses.push(cycleCheck);
|
|
11260
|
+
}
|
|
11261
|
+
const forceWorktableOuterJoinOrder = ctx.dialect.capabilities.forceRecursiveWorktableOuterJoinOrder;
|
|
11262
|
+
const recursiveCase = ctx.direction === "both" ? compileBidirectionalBranch({
|
|
11263
|
+
recursiveColumns,
|
|
11264
|
+
whereClauses: recursiveWhereClauses,
|
|
11265
|
+
forceWorktableOuterJoinOrder,
|
|
11266
|
+
schema: ctx.schema
|
|
11267
|
+
}) : compileRecursiveBranch({
|
|
11268
|
+
recursiveColumns,
|
|
11269
|
+
whereClauses: recursiveWhereClauses,
|
|
11270
|
+
joinField: "from_id",
|
|
11271
|
+
targetField: "to_id",
|
|
11272
|
+
targetKindField: "to_kind",
|
|
11273
|
+
forceWorktableOuterJoinOrder,
|
|
11274
|
+
schema: ctx.schema
|
|
11275
|
+
});
|
|
11276
|
+
return sql`WITH RECURSIVE reachable AS (${baseCase} UNION ALL ${recursiveCase})`;
|
|
11277
|
+
}
|
|
11278
|
+
function compileRecursiveBranch(params) {
|
|
11279
|
+
const columns = [...params.recursiveColumns];
|
|
11280
|
+
const selectClause = sql`SELECT ${sql.join(columns, sql`, `)}`;
|
|
11281
|
+
const nodeJoin = sql`JOIN ${params.schema.nodesTable} n ON n.graph_id = e.graph_id AND n.id = e.${sql.raw(params.targetField)} AND n.kind = e.${sql.raw(params.targetKindField)}`;
|
|
11282
|
+
if (params.forceWorktableOuterJoinOrder) {
|
|
11283
|
+
const allWhere = [
|
|
11284
|
+
...params.whereClauses,
|
|
11285
|
+
sql`e.${sql.raw(params.joinField)} = r.id`
|
|
11286
|
+
];
|
|
11287
|
+
return sql`${selectClause} FROM reachable r CROSS JOIN ${params.schema.edgesTable} e ${nodeJoin} WHERE ${sql.join(allWhere, sql` AND `)}`;
|
|
11288
|
+
}
|
|
11289
|
+
const where = [...params.whereClauses];
|
|
11290
|
+
return sql`${selectClause} FROM reachable r JOIN ${params.schema.edgesTable} e ON e.${sql.raw(params.joinField)} = r.id ${nodeJoin} WHERE ${sql.join(where, sql` AND `)}`;
|
|
11291
|
+
}
|
|
11292
|
+
function compileBidirectionalBranch(params) {
|
|
11293
|
+
const columns = [...params.recursiveColumns];
|
|
11294
|
+
const selectClause = sql`SELECT ${sql.join(columns, sql`, `)}`;
|
|
11295
|
+
const nodeJoin = sql`JOIN ${params.schema.nodesTable} n ON n.graph_id = e.graph_id AND ((e.to_id = r.id AND n.id = e.from_id AND n.kind = e.from_kind) OR (e.from_id = r.id AND n.id = e.to_id AND n.kind = e.to_kind))`;
|
|
11296
|
+
if (params.forceWorktableOuterJoinOrder) {
|
|
11297
|
+
const allWhere = [
|
|
11298
|
+
...params.whereClauses,
|
|
11299
|
+
sql`(e.from_id = r.id OR e.to_id = r.id)`
|
|
11300
|
+
];
|
|
11301
|
+
return sql`${selectClause} FROM reachable r CROSS JOIN ${params.schema.edgesTable} e ${nodeJoin} WHERE ${sql.join(allWhere, sql` AND `)}`;
|
|
11302
|
+
}
|
|
11303
|
+
return sql`${selectClause} FROM reachable r JOIN ${params.schema.edgesTable} e ON (e.from_id = r.id OR e.to_id = r.id) ${nodeJoin} WHERE ${sql.join([...params.whereClauses], sql` AND `)}`;
|
|
11304
|
+
}
|
|
11305
|
+
function buildIncludedIdsCte(ctx) {
|
|
11306
|
+
const filters = [];
|
|
11307
|
+
if (ctx.includeKinds !== void 0 && ctx.includeKinds.length > 0) {
|
|
11308
|
+
filters.push(compileKindFilter(sql.raw("kind"), ctx.includeKinds));
|
|
11309
|
+
}
|
|
11310
|
+
if (ctx.excludeRoot) {
|
|
11311
|
+
filters.push(sql`id != ${ctx.rootId}`);
|
|
11312
|
+
}
|
|
11313
|
+
const whereClause = filters.length > 0 ? sql` WHERE ${sql.join(filters, sql` AND `)}` : sql``;
|
|
11314
|
+
return sql`, included_ids AS (SELECT DISTINCT id FROM reachable${whereClause})`;
|
|
11315
|
+
}
|
|
11316
|
+
async function fetchSubgraphNodes(ctx, reachableCte, includedIdsCte) {
|
|
11317
|
+
const query = sql`${reachableCte}${includedIdsCte} SELECT n.kind, n.id, n.props, n.version, n.valid_from, n.valid_to, n.created_at, n.updated_at, n.deleted_at FROM ${ctx.schema.nodesTable} n WHERE n.graph_id = ${ctx.graphId} AND n.id IN (SELECT id FROM included_ids)`;
|
|
11318
|
+
return ctx.backend.execute(query);
|
|
11319
|
+
}
|
|
11320
|
+
async function fetchSubgraphEdges(ctx, reachableCte, includedIdsCte) {
|
|
11321
|
+
const edgeKindFilter = compileKindFilter(sql.raw("e.kind"), ctx.edgeKinds);
|
|
11322
|
+
const query = sql`${reachableCte}${includedIdsCte} SELECT e.id, e.kind, e.from_kind, e.from_id, e.to_kind, e.to_id, e.props, e.valid_from, e.valid_to, e.created_at, e.updated_at, e.deleted_at FROM ${ctx.schema.edgesTable} e WHERE e.graph_id = ${ctx.graphId} AND ${edgeKindFilter} AND e.deleted_at IS NULL AND e.from_id IN (SELECT id FROM included_ids) AND e.to_id IN (SELECT id FROM included_ids)`;
|
|
11323
|
+
return ctx.backend.execute(query);
|
|
11324
|
+
}
|
|
11193
11325
|
|
|
11194
11326
|
// src/store/store.ts
|
|
11195
11327
|
var Store = class {
|
|
@@ -11393,6 +11525,39 @@ var Store = class {
|
|
|
11393
11525
|
query() {
|
|
11394
11526
|
return this.#createQueryForBackend(this.#backend);
|
|
11395
11527
|
}
|
|
11528
|
+
// === Subgraph Extraction ===
|
|
11529
|
+
/**
|
|
11530
|
+
* Extracts a typed subgraph by traversing from a root node.
|
|
11531
|
+
*
|
|
11532
|
+
* Performs a BFS traversal from `rootId` following the specified edge kinds,
|
|
11533
|
+
* then returns all reachable nodes and the edges connecting them.
|
|
11534
|
+
*
|
|
11535
|
+
* @example
|
|
11536
|
+
* ```typescript
|
|
11537
|
+
* const result = await store.subgraph(run.id, {
|
|
11538
|
+
* edges: ["has_task", "runs_agent", "uses_skill"],
|
|
11539
|
+
* maxDepth: 4,
|
|
11540
|
+
* includeKinds: ["Run", "Task", "Agent", "Skill"],
|
|
11541
|
+
* });
|
|
11542
|
+
*
|
|
11543
|
+
* for (const node of result.nodes) {
|
|
11544
|
+
* switch (node.kind) {
|
|
11545
|
+
* case "Task": console.log(node.name); break;
|
|
11546
|
+
* case "Agent": console.log(node.model); break;
|
|
11547
|
+
* }
|
|
11548
|
+
* }
|
|
11549
|
+
* ```
|
|
11550
|
+
*/
|
|
11551
|
+
async subgraph(rootId, options) {
|
|
11552
|
+
return executeSubgraph({
|
|
11553
|
+
graphId: this.graphId,
|
|
11554
|
+
rootId,
|
|
11555
|
+
backend: this.#backend,
|
|
11556
|
+
dialect: getDialect(this.#backend.dialect),
|
|
11557
|
+
schema: this.#schema,
|
|
11558
|
+
options
|
|
11559
|
+
});
|
|
11560
|
+
}
|
|
11396
11561
|
// === Transactions ===
|
|
11397
11562
|
/**
|
|
11398
11563
|
* Executes a function within a transaction.
|