{"version":3,"sources":["../src/indexes/ddl.ts"],"names":["sql","compileNodeIndexKeys","compileEdgeIndexKeys","renderSqlInline","compileIndexWhere","getDialect","requireDefined"],"mappings":";;;;;;;;AA+BA,IAAM,kBAAA,GAAqB;AAAA,EACzB,QAAA,EAAU,EAAE,gBAAA,EAAkB,IAAA,EAAM,mBAAmB,IAAA,EAAK;AAAA,EAC5D,MAAA,EAAQ,EAAE,gBAAA,EAAkB,KAAA,EAAO,mBAAmB,KAAA;AACxD,CAAA;AAeO,SAAS,gBAAA,CACd,KAAA,EACA,OAAA,EACA,OAAA,GAAmC,EAAC,EAC5B;AACR,EAAA,IAAI,KAAA,CAAM,WAAW,MAAA,EAAQ;AAC3B,IAAA,OAAO,oBAAA,CAAqB,KAAA,EAAO,OAAA,EAAS,OAAO,CAAA;AAAA,EACrD;AACA,EAAA,OAAO,oBAAA,CAAqB,KAAA,EAAO,OAAA,EAAS,OAAO,CAAA;AACrD;AAEO,SAAS,oBAAA,CACd,KAAA,EACA,OAAA,EACA,OAAA,GAAmC,EAAC,EAC5B;AACR,EAAA,MAAM,SAAA,GAAY,QAAQ,cAAA,IAAkB,iBAAA;AAC5C,EAAA,OAAO,qBAAA,CAAsB,KAAA,EAAO,OAAA,EAAS,SAAA,EAAW,OAAO,CAAA;AACjE;AAEO,SAAS,oBAAA,CACd,KAAA,EACA,OAAA,EACA,OAAA,GAAmC,EAAC,EAC5B;AACR,EAAA,MAAM,SAAA,GAAY,QAAQ,cAAA,IAAkB,iBAAA;AAC5C,EAAA,OAAO,qBAAA,CAAsB,KAAA,EAAO,OAAA,EAAS,SAAA,EAAW,OAAO,CAAA;AACjE;AAEA,SAAS,qBAAA,CACP,KAAA,EACA,OAAA,EACA,SAAA,EACA,OAAA,EACQ;AACR,EAAA,IAAI,KAAA,CAAM,WAAW,MAAA,EAAW;AAC9B,IAAA,OAAO,yBAAA,CAA0B,KAAA,EAAO,OAAA,EAAS,SAAA,EAAW,OAAO,CAAA;AAAA,EACrE;AACA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AS,gBAAgB,UAAA,EAA4B;AACnD,EAAA,OAAO,CAAA,CAAA,EAAI,UAAA,CAAW,UAAA,CAAW,GAAA,EAAK,IAAI,CAAC,CAAA,CAAA,CAAA;AAC7C","file":"chunk-RNMA3NLE.cjs","sourcesContent":["import { getDialect } from \"../query/dialect\";\nimport { type SqlDialect } from \"../query/dialect/types\";\nimport { renderSqlInline, sql, type SqlFragment } from \"../query/sql-fragment\";\nimport { requireDefined } from \"../utils/presence\";\nimport {\n  compileEdgeIndexKeys,\n  compileIndexWhere,\n  compileNodeIndexKeys,\n  type IndexCompilationContext,\n} from \"./compiler\";\nimport {\n  type EdgeIndexDeclaration,\n  type NodeIndexDeclaration,\n  type RelationalIndexDeclaration,\n  type SystemColumnName,\n} from \"./types\";\n\nexport type GenerateIndexDdlOptions = Readonly<{\n  nodesTableName?: string | undefined;\n  edgesTableName?: string | undefined;\n  ifNotExists?: boolean | undefined;\n  /**\n   * Emit `CREATE INDEX CONCURRENTLY` (Postgres only). Required for\n   * `store.materializeIndexes()` so live tables don't take an\n   * `AccessExclusiveLock`. Ignored on SQLite (no equivalent; SQLite is\n   * single-writer regardless). When true on Postgres, the resulting\n   * statement cannot be executed inside a transaction.\n   */\n  concurrent?: boolean | undefined;\n}>;\n\nconst INDEX_DDL_BEHAVIOR = {\n  postgres: { concurrentBuilds: true, supportsGinFamily: true },\n  sqlite: { concurrentBuilds: false, supportsGinFamily: false },\n} as const satisfies Record<\n  SqlDialect,\n  Readonly<{ concurrentBuilds: boolean; supportsGinFamily: boolean }>\n>;\n\n/**\n * Generate `CREATE INDEX` DDL for a single relational index declaration.\n *\n * Vector index declarations (`entity: \"vector\"`) are NOT handled here —\n * they go through `backend.createVectorIndex` instead because the DDL\n * is dialect-specific (`USING hnsw (...) WITH (m=..., ef_construction=...)`\n * on Postgres) and operates on the strategy's per-`(kind, field)` embedding\n * tables, not `typegraph_nodes` / `typegraph_edges`. Callers narrow to the\n * relational subset before calling this function.\n */\nexport function generateIndexDDL(\n  index: RelationalIndexDeclaration,\n  dialect: SqlDialect,\n  options: GenerateIndexDdlOptions = {},\n): string {\n  if (index.entity === \"node\") {\n    return generateNodeIndexDDL(index, dialect, options);\n  }\n  return generateEdgeIndexDDL(index, dialect, options);\n}\n\nexport function generateNodeIndexDDL(\n  index: NodeIndexDeclaration,\n  dialect: SqlDialect,\n  options: GenerateIndexDdlOptions = {},\n): string {\n  const tableName = options.nodesTableName ?? \"typegraph_nodes\";\n  return generateTableIndexDDL(index, dialect, tableName, options);\n}\n\nexport function generateEdgeIndexDDL(\n  index: EdgeIndexDeclaration,\n  dialect: SqlDialect,\n  options: GenerateIndexDdlOptions = {},\n): string {\n  const tableName = options.edgesTableName ?? \"typegraph_edges\";\n  return generateTableIndexDDL(index, dialect, tableName, options);\n}\n\nfunction generateTableIndexDDL(\n  index: RelationalIndexDeclaration,\n  dialect: SqlDialect,\n  tableName: string,\n  options: GenerateIndexDdlOptions,\n): string {\n  if (index.method !== undefined) {\n    return generateGinFamilyIndexDDL(index, dialect, tableName, options);\n  }\n  const ifNotExists = options.ifNotExists ?? true;\n  const propsColumn = sql.identifier(\"props\");\n  const systemColumn = (column: SystemColumnName): SqlFragment =>\n    sql.identifier(column);\n\n  const keys =\n    index.entity === \"node\" ?\n      compileNodeIndexKeys(index, dialect, propsColumn, systemColumn).keys\n    : compileEdgeIndexKeys(index, dialect, propsColumn, systemColumn).keys;\n\n  const whereSql =\n    index.where ?\n      renderSqlInline(\n        compileIndexWhere(\n          {\n            dialect,\n            propsColumn,\n            systemColumn,\n          } satisfies IndexCompilationContext,\n          index.where,\n        ),\n        dialect,\n      )\n    : undefined;\n\n  const keySql = keys.map((key) => renderSqlInline(key, dialect)).join(\", \");\n  const unique = index.unique ? \"UNIQUE \" : \"\";\n  const concurrent =\n    (\n      options.concurrent === true &&\n      INDEX_DDL_BEHAVIOR[dialect].concurrentBuilds\n    ) ?\n      \"CONCURRENTLY \"\n    : \"\";\n  const ifNotExistsSql = ifNotExists ? \"IF NOT EXISTS \" : \"\";\n\n  const whereClause = whereSql ? ` WHERE ${whereSql}` : \"\";\n\n  return `CREATE ${unique}INDEX ${concurrent}${ifNotExistsSql}${quoteIdentifier(index.name)} ON ${quoteIdentifier(tableName)} (${keySql})${whereClause};`;\n}\n\n/**\n * DDL for the GIN-family methods (`\"gin\"` / `\"trigram\"`) — a PostgreSQL\n * expression GIN over the declaration's single field.\n *\n * The indexed expression is built from the SAME dialect extraction the\n * query compiler emits for the field (`jsonExtract` → `\"props\" #> ARRAY[…]`\n * for containment, `jsonExtractText` → `#>>` for text), because Postgres\n * matches expression indexes structurally: `(props #> ARRAY['tags']) @> $1`\n * and `(props #>> ARRAY['name']) ILIKE $1 ESCAPE '\\'` both hit their GIN\n * (verified against parameterized prepared statements). No scope prefix\n * columns, uniqueness, or partial clause: a GIN indexes one expression, and\n * the query's `graph_id` / `kind` equality filters apply as residual\n * conditions over the candidate rows.\n */\nfunction generateGinFamilyIndexDDL(\n  index: RelationalIndexDeclaration,\n  dialect: SqlDialect,\n  tableName: string,\n  options: GenerateIndexDdlOptions,\n): string {\n  if (!INDEX_DDL_BEHAVIOR[dialect].supportsGinFamily) {\n    throw new Error(\n      `Index \"${index.name}\" declares method \"${index.method}\", which ` +\n        \"requires PostgreSQL. materializeIndexes() reports such \" +\n        \"declarations as skipped on SQLite instead of calling this.\",\n    );\n  }\n  const adapter = getDialect(dialect);\n  const propsColumn = sql.identifier(\"props\");\n  const pointer = requireDefined(index.fields[0]);\n  const expression =\n    index.method === \"gin\" ?\n      adapter.jsonExtract(propsColumn, pointer)\n    : adapter.jsonExtractText(propsColumn, pointer);\n  const operatorClass =\n    index.method === \"gin\" ? \"jsonb_path_ops\" : \"gin_trgm_ops\";\n  const concurrent = options.concurrent === true ? \"CONCURRENTLY \" : \"\";\n  const ifNotExistsSql = (options.ifNotExists ?? true) ? \"IF NOT EXISTS \" : \"\";\n  return `CREATE INDEX ${concurrent}${ifNotExistsSql}${quoteIdentifier(index.name)} ON ${quoteIdentifier(tableName)} USING GIN ((${renderSqlInline(expression, dialect)}) ${operatorClass});`;\n}\n\nfunction quoteIdentifier(identifier: string): string {\n  return `\"${identifier.replaceAll('\"', '\"\"')}\"`;\n}\n"]}