@finos/legend-cube

中文文档
0.0.10发布于 2026/11/12Apache-2.0TypeScriptES Module零依赖近 30 天下载 2155

Legend Cube core: the host-free domain model of the Legend Cube query builder

原始使用文档
文档目录5 节
  1. How it fits together
  2. Saved spec
  3. Host-free rule
  4. Development
  5. Documentation

@finos/legend-cube

The host-free core of Legend Cube, a canvas-based visual query builder (not Legend DataCube, which is @finos/legend-data-cube). It holds the domain model: precise types and values, schemas, the query graph with its schema inference and validation, the transforms, filters, the Cube IR and its emitter, and the saved-spec codec.

The UI, the stores and the Legend engine adapter live in @finos/legend-cube-builder.

Status: work in progress, version 0.0.x. It has relational tables, data product access points and ingest data sets (both beta), Join, Sort, Group (with its aggregations), Pivot (a column's values into columns, through the engine's pivot(), refused on the databases that can't run it), Filter, Restrict, Rename, Distinct, Drop, Limit, Slice, Concat, Difference, Partition (window functions), Extend (computed columns, typed by the engine) and Filter by Expression (a Pure condition, checked by the engine). The API is not stable before 1.0: src/index.ts re-exports whole modules.

How it fits together

  • The query graph is the only source of truth. A CubeDocument holds the context (the model and the runtime), an immutable Query of QueryNodes and Connections, and presentation settings: the graph shown or not, column widths and number formats, and the grid's drill-down. Every edit makes a new Query.
  • Drill-down: checkDrilldown checks a saved drill-down against a run's columns, and deriveDrilldownLevel derives each level's query from the run's, as Cube's own nodes (a Filter of the expanded keys, a Group, a Sort), so every database workaround applies.
  • Inference: buildSchemasAndValidity gives each node its output schema and its errors.
  • Emitting: QueryEmitter turns a valid query into the Cube IR, a host-free Pure AST (printIR prints it for debugging). A Sort's order is written where it is used: before a Limit, Drop or Slice, and before the run's row limit. The builder's v1/ adapter writes the IR as the engine's protocol JSON. The engine's Pure text is never parsed back.
  • Saving: encodeCubeSpec and decodeCubeSpec (or serializeCubeSpec and parseCubeSpec, for text) write and read the saved spec.
  • Node types: createNodeRegistry() lists them, in palette order. The palette, the context menu and the codec all read it.
FolderHolds
types/CubeType, the registry of precise primitive types, type families, compatibility
values/literal values, read and checked per type
schema/Schema and SchemaColumn, schema diffs
graph/QueryNode and its base classes, Connection, Query, CubeDocument
inference/schema inference, which marks incomplete and upstream-invalid nodes; validation helpers; the rows' order through the graph (RowOrder.ts)
nodes/the node registry; sources/ (relational table, data product access point, ingest data set, and the one-kind rule), transforms/ (Join, Sort, Group and its aggregations, Pivot, Filter, Restrict, Rename, Distinct, Drop, Limit, Slice, Concat, Difference, Partition and its window functions, Extend and its typing, Filter by Expression and its check, the row settings they share, the Join and Concat autofixes), Unknown
filter/the filter tree, operators by type family, helpers that build filters
messages/the validation messages, including ERR_INCOMPLETE and ERR_SCHEMAS for incomplete and upstream-invalid nodes
ir/the Cube IR, an emitter per node type (emitters/), the query emitter that isolates windows with lets, the debug printer
drilldown/the grid's drill-down: its levels and group-row aggregations, checked against a run, and each level's derived query
presentation/number column formats, by column or for every number column
spec/the saved-spec codec, a codec per node type (codecs/), migrations
utils/assertions and JSON helpers

Saved spec

A cube is saved as JSON, format version 1 (CURRENT_FORMAT_VERSION): formatVersion, name, context (the model, kept as given, and the runtime), query (selected and nodes) and meta. Each node has kind, id, its inputs (left out for a source, which has none) and its own fields.

  • Literal values are typed, with numbers written as strings, so no digit is lost; a node's settings, such as a Limit's size, are JSON numbers. A resolved source keeps a snapshot of its schema; an unresolved one is saved without it, and typed again when the cube is imported.
  • Keys this version doesn't know are kept and written back, and a node of an unknown kind is kept as an Unknown node.
  • A spec is at most 1 MiB (MAX_SPEC_BYTES). One saved by a newer version is read as read-only. There are no migrations yet (CUBE_SPEC_MIGRATIONS), since version 1 is the first.
  • The format is a draft until the Cube store exists: it may still change, with migrations.

Host-free rule

This package runs anywhere JavaScript runs: no browser, no Node and no Legend host code. In code outside tests:

  • Only relative imports. The package has no runtime dependencies.
  • Only ECMAScript globals. window, document, fetch, console, setTimeout, structuredClone, URL, TextEncoder and the like fail the build.

Four guards enforce it:

  1. package.json declares no dependencies, and only tooling dev dependencies.
  2. The legendCubeHostFree block of the root ESLint config forbids other imports and flags the common browser globals while you type.
  3. tsconfig.build.json compiles with no ambient types ("types": []).
  4. src/__tests__/LegendCubeHostFree.test.ts checks the dependencies and the imports, and compiles the package against the ECMAScript library alone.

Tests, test utilities and mocks (__tests__, __test-utils__ and __mocks__ folders) are exempt: they may use Node and Jest.

Types come only from resolveCubeType, PrimitiveType.get and new EnumType. That keeps a planned change internal: Cube's types are to move onto legend-graph's metamodel, and the core may then depend on legend-graph's metamodel, but never on its V1_* protocol classes.

Development

  • yarn workspace @finos/legend-cube test runs the tests (test:watch to watch). They run under Node and need no engine.
  • yarn workspace @finos/legend-cube dev, or yarn dev:ts at the root, rebuilds on change.

Documentation

版本与发布记录

@finos/legend-cube 共发布 10 个版本,首次发布于 2026/10/08;过去 12 个月发布了 10 个版本。

版本发布日期距今
0.0.10latest2026/11/122 天前
0.0.92026/11/104 天前
0.0.82026/11/0212 天前
0.0.72026/10/1926 天前
0.0.62026/10/1629 天前
0.0.52026/10/1530 天前

关于 @finos/legend-cube 的常见问题

@finos/legend-cube 是什么?

Legend Cube core: the host-free domain model of the Legend Cube query builder

如何安装 @finos/legend-cube?

在项目目录运行 npm install @finos/legend-cube,或使用 pnpm add / yarn add / bun add。

@finos/legend-cube 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@finos/legend-cube 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@finos/legend-cube 可以商用吗?

@finos/legend-cube 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@finos/legend-cube 的下载量有多少?

根据 npm 官方下载统计,@finos/legend-cube 在 2026/10/15 – 2026/11/13 这 30 天内被下载 2,155 次,日均约 72 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@finos/legend-cube 还在维护吗?

最新版本 0.0.10 发布于 2026/11/12(2 天前);至今共发布 10 个版本,其中过去 12 个月发布了 10 个;首次发布于 2026/10/08。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。