@lakeql/cli 0.5.0 → 0.6.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/cli.mjs CHANGED
@@ -1,10 +1,9 @@
1
1
  #!/usr/bin/env node
2
- import{a as e,c as t,i as n,l as r,n as i,o as a,r as o,s,t as c}from"./pull-metadata-D8Z0WHKQ.mjs";import{Command as l}from"@commander-js/extra-typings";import u from"node:path";import{readPackage as d}from"read-pkg";import{existsSync as f}from"node:fs";import{mkdir as p,readFile as m,rm as h,writeFile as g}from"node:fs/promises";import{confirm as _,multiselect as v,question as y,select as b,validators as ee}from"@topcli/prompts";import{loadConfig as x}from"c12";import{generateCode as S}from"@lakeql/file-generator";import{generateConfig as te}from"@lakeql/file-generator/config";import{generateInterface as ne}from"@lakeql/file-generator/interface";import{generateQuerySchema as re}from"@lakeql/file-generator/query-schema";import{generateModel as ie}from"@lakeql/schema-generator/graphql-schema";import{generateJsonSchemaFromFields as ae}from"@lakeql/schema-generator/json-schema";import{generateConfigReqistry as C}from"@lakeql/file-generator/config-registry";import{globby as w}from"globby";import{camelCase as T,upperFirst as E}from"lodash-es";import D from"typescript";import{endpointDefinitionSchema as O,findDuplicateFieldNames as k}from"@lakeql/schema-generator/endpoint-schema";import{TrinoClient as A}from"@lakeql/trino-client";import{ClimtTable as j}from"climt";import{createEnv as M}from"@t3-oss/env-core";import oe from"dotenv";import{enum as se,number as ce,string as N}from"zod/v4";import{error as le}from"@lakeql/logger/console";import{Listr as ue}from"listr2";import{parseColumns as de}from"@lakeql/column-parser";import{convertTrinoResponse as fe}from"@lakeql/response-transformer";import P from"kleur";const pe={sourcePath:`.`};async function me(){let{config:e}=await x({name:`lakeql`,cwd:t(),defaults:pe,packageJson:!1,globalRc:!1,rcFile:!1,dotenv:!1});return e}async function F(e){let n=await me(),r=t(),i=e??n.sourcePath;return u.isAbsolute(i)?i:u.resolve(r,i)}function he(){return a().action(async({sourcePath:e})=>{await I(e!==process.env.INIT_CWD&&e!==process.cwd()?e:void 0)})}async function I(e){let t=await F(e),n=await S({fileName:`config-registry.ts`,nodes:C({configPaths:(await w(`schemas/**/config.ts`,{cwd:t,onlyFiles:!0})).map(e=>{let t=u.parse(e);return u.join(t.dir,t.name)})})});f(u.join(t,`config-registry.ts`))&&await h(u.join(t,`config-registry.ts`),{force:!0,recursive:!0}),await g(u.join(t,`config-registry.ts`),n.text)}function ge(e,t){return{baseClassName:`${E(T(e))}_${E(T(t))}`,queryName:`${T(e)}${E(T(t))}`,mutationName:`create${T(e)}${E(T(t))}`}}const{factory:L}=D;function R(e){return L.createIdentifier(e)}function z(e){return R(e)}function B(e){return L.createStringLiteral(e)}function V(e,t){return L.createPropertyAssignment(R(e),t)}function H(e,t=!0){return L.createObjectLiteralExpression(e,t)}function U(e){return e?L.createTrue():L.createFalse()}function _e(e){return L.createParameterDeclaration(void 0,void 0,R(e))}function W(e,t,n){return L.createArrowFunction(n?.async?[L.createToken(D.SyntaxKind.AsyncKeyword)]:void 0,void 0,e.map(e=>typeof e==`string`?_e(e):e),void 0,L.createToken(D.SyntaxKind.EqualsGreaterThanToken),t)}function ve(e){return L.createParenthesizedExpression(e)}function ye(e,t=!0){return W([`t`],ve(H(e,t)))}function G(e,t){return L.createPropertyAccessExpression(typeof e==`string`?R(e):e,R(t))}function be(e,t=[],n){return L.createCallExpression(e,n,t)}function K(e,t,n=[],r){return be(G(e,t),n,r)}function xe(e,t=[],n){return K(`builder`,e,t,n)}function Se(e,t){return L.createVariableStatement(void 0,L.createVariableDeclarationList([L.createVariableDeclaration(R(e),void 0,void 0,t)],D.NodeFlags.Const))}function q(e,t,n){return L.createImportDeclaration(void 0,L.createImportClause(n?.typeOnly??!1,void 0,L.createNamedImports(t.map(e=>L.createImportSpecifier(!1,void 0,R(e))))),B(e))}function Ce(e,t,n){return L.createImportDeclaration(void 0,L.createImportClause(n?.typeOnly??!1,R(t),void 0),B(e))}function we({models:e,mutationName:t,mutationConfig:n,hasValidations:r,fieldDefinitions:i}){if(!n)return[];let a=Object.values(e).find(e=>e.root);if(!a)return[];let o=Te(i),s=Ee(i),c=Object.values(e).filter(e=>!e.root);return[...De(r),...c.map(t=>Oe(t,e,o,s)),Oe(a,e,o,s),Me(t,a,r)]}function Te(e){let t=new Map;if(!e)return t;for(let n of e){let e=n.options?.required===!0;t.set(n.name,e)}return t}function Ee(e){let t=new Set;if(!e)return t;for(let n of e)n.options?.readOnly===!0&&t.add(n.name);return t}function De(e){let t=[q(`@lakeql/adapters`,[`executeWritePipeline`]),q(`@lakeql/trino-client`,[`TrinoClient`]),q(`@lakeql/api/builder`,[`builder`]),q(`~/env`,[`env`]),q(`./config`,[`hiveConfig`,`storageConfig`]),Ce(`./json-schema.json`,`jsonSchema`)];return e&&t.push(q(`./validations`,[`validationSchema`])),t}function Oe(e,t,n,r){let i=`${e.modelName}Input`,a=Object.values(e.fields).filter(e=>!r.has(e.name)).map(e=>ke(e,t,n));return Se(i,xe(`inputType`,[B(i),H([V(`fields`,ye(a))])]))}function ke(e,t,n){let r=V(`required`,U(n.get(e.name)??!0));if(e.isArray){let t=e.graphqlType.replace(/^\[/u,``).replace(/\]$/u,``),n=je(t);if(n)return V(e.name,K(`t`,n,[H([r],!1)]));let i=`${t}Input`;return V(e.name,K(`t`,`field`,[H([V(`type`,D.factory.createArrayLiteralExpression([z(i)])),r],!1)]))}let i=Ae(e.graphqlType);if(i)return V(e.name,K(`t`,i,[H([r],!1)]));if(Object.keys(t).includes(e.graphqlType)){let t=`${e.graphqlType}Input`;return V(e.name,K(`t`,`field`,[H([V(`type`,z(t)),r],!1)]))}return V(e.name,K(`t`,`string`,[H([r],!1)]))}function Ae(e){switch(e){case`String`:return`string`;case`Int`:return`int`;case`Float`:return`float`;case`Boolean`:return`boolean`;default:return}}function je(e){switch(e){case`String`:return`stringList`;case`Int`:return`intList`;case`Float`:return`floatList`;case`Boolean`:return`booleanList`;default:return}}function Me(e,t,n){let r=`${t.modelName}Input`,i=[];n&&i.push(D.factory.createExpressionStatement(K(`validationSchema`,`parse`,[z(`input`)]))),i.push(D.factory.createVariableStatement(void 0,D.factory.createVariableDeclarationList([D.factory.createVariableDeclaration(z(`trinoClient`),void 0,void 0,D.factory.createNewExpression(z(`TrinoClient`),void 0,[H([V(`host`,G(`env`,`HIVE_HOST`)),V(`port`,G(`env`,`HIVE_PORT`)),V(`auth`,H([V(`type`,B(`basic`)),V(`username`,G(`env`,`HIVE_USERNAME`)),V(`password`,G(`env`,`HIVE_PASSWORD`))],!1)),V(`catalog`,G(`env`,`HIVE_CATALOG`)),V(`source`,G(`env`,`HIVE_SOURCE`))])]))],D.NodeFlags.Const)));let a=D.factory.createAwaitExpression(be(z(`executeWritePipeline`),[H([V(`records`,z(`input`)),V(`jsonSchema`,D.factory.createAsExpression(z(`jsonSchema`),D.factory.createKeywordTypeNode(D.SyntaxKind.AnyKeyword))),V(`config`,H([V(`loadStrategy`,G(`storageConfig`,`loadStrategy`)),V(`bucket`,G(`storageConfig`,`bucket`)),V(`basePath`,G(`storageConfig`,`basePath`)),V(`table`,H([V(`catalog`,G(`hiveConfig`,`catalog`)),V(`schema`,G(`hiveConfig`,`schema`)),V(`tableName`,G(`hiveConfig`,`tableName`))],!1)),V(`trinoClient`,z(`trinoClient`))]))])]));i.push(D.factory.createExpressionStatement(a)),i.push(D.factory.createReturnStatement(D.factory.createTrue()));let o=D.factory.createBlock(i,!0),s=W([_e(`_root`),D.factory.createParameterDeclaration(void 0,void 0,D.factory.createObjectBindingPattern([D.factory.createBindingElement(void 0,void 0,D.factory.createIdentifier(`input`))]))],o,{async:!0}),c=V(e,K(`t`,`boolean`,[H([V(`args`,H([V(`input`,K(`t`,`arg`,[H([V(`type`,z(r)),V(`required`,U(!0))],!1)]))],!1)),V(`resolve`,s)])]));return D.factory.createExpressionStatement(xe(`mutationFields`,[ye([c])]))}function J(e){if(typeof e!=`object`||!e)return e;if(Array.isArray(e))return e.map(J);let t={},n=Object.keys(e).toSorted();for(let r of n)t[r]=J(e[r]);return t}function Ne(e){let t=J(e);return`${JSON.stringify(t,null,2).replaceAll(`\r
2
+ import{a as e,c as t,i as n,l as r,n as i,o as a,r as o,s,t as c}from"./pull-metadata-D8Z0WHKQ.mjs";import{Command as l}from"@commander-js/extra-typings";import u from"node:path";import{error as d,info as f,success as p,warning as m}from"@lakeql/logger/console";import{readPackage as h}from"read-pkg";import{existsSync as g}from"node:fs";import{mkdir as _,readFile as v,rm as ee,writeFile as y}from"node:fs/promises";import{confirm as te,multiselect as ne,question as re,select as b,validators as ie}from"@topcli/prompts";import{loadConfig as x}from"c12";import{generateCode as S}from"@lakeql/file-generator";import{generateConfig as ae}from"@lakeql/file-generator/config";import{generateInterface as oe}from"@lakeql/file-generator/interface";import{generateQuerySchema as se}from"@lakeql/file-generator/query-schema";import{generateModel as ce}from"@lakeql/schema-generator/graphql-schema";import{generateJsonSchemaFromFields as le}from"@lakeql/schema-generator/json-schema";import{generateConfigReqistry as C}from"@lakeql/file-generator/config-registry";import{globby as w}from"globby";import{camelCase as T,upperFirst as E}from"lodash-es";import D from"typescript";import{endpointDefinitionSchema as O,findDuplicateFieldNames as ue}from"@lakeql/schema-generator/endpoint-schema";import{TrinoClient as k}from"@lakeql/trino-client";import{ClimtTable as A}from"climt";import{createEnv as de}from"@t3-oss/env-core";import{ZodError as fe,enum as pe,number as me,string as j,z as M}from"zod/v4";import{Listr as he}from"listr2";import{parseColumns as ge}from"@lakeql/column-parser";import{convertTrinoResponse as _e}from"@lakeql/response-transformer";var N=class extends Error{code;hint;details;exitCode;constructor(e,t={}){super(e,t.cause?{cause:t.cause}:void 0),this.name=`CliError`,this.code=t.code,this.hint=t.hint,this.details=t.details,this.exitCode=t.exitCode??1}};function ve(e=`Aborted by user.`){return new N(e,{code:`CLI_ABORTED`,exitCode:0})}function ye(e){if(typeof e!=`object`||!e)return!1;let t=e,n=`${t.message??``} ${t.cause?.message??``}`.toLowerCase();return t.name===`CancelError`||t.code===`ABORT_ERR`||n.includes(`cancelled`)||n.includes(`canceled`)||n.includes(`aborted`)||n.includes(`interrupted`)}function P(e,t,n){return new N(`Failed to ${e}.`,{code:`TRINO_REQUEST_FAILED`,hint:`Verify HIVE_HOST/HIVE_PORT, credentials and network reachability to Trino.`,details:[`Context: ${t}`],cause:n})}const be={sourcePath:`.`},xe=`lakeql.config.json`;function Se(e){if(typeof e!=`object`||!e)return!1;let t=e,n=`${t.message??``} ${t.cause?.message??``}`;return t.code===`ERR_IMPORT_ATTRIBUTE_MISSING`||t.cause?.code===`ERR_IMPORT_ATTRIBUTE_MISSING`||n.includes(`needs an import attribute of "type: json"`)}function Ce(e){return{...be,...e}}async function we(e){let t=await v(u.join(e,xe),`utf-8`);return Ce(JSON.parse(t))}async function Te(){let e=t();try{let{config:t}=await x({name:`lakeql`,cwd:e,defaults:be,packageJson:!1,globalRc:!1,rcFile:!1,dotenv:!1});return Ce(t)}catch(t){let n=u.join(e,xe);if(Se(t)&&g(n))return we(e);throw new N(`Failed to load LakeQL config: ${t instanceof Error?t.message:typeof t==`string`?t:`Unknown error`}`,{code:`LAKEQL_CONFIG_LOAD_FAILED`,hint:`Check lakeql.config.{ts,mjs,js,json} syntax and ensure exported values are valid.`,details:[`Context: config loading from ${e}`,`Expected files: lakeql.config.ts, lakeql.config.mjs, lakeql.config.js, lakeql.config.json`],cause:t})}}async function F(e){let n=await Te(),r=t(),i=e??n.sourcePath;return u.isAbsolute(i)?i:u.resolve(r,i)}function Ee(){return a().action(async({sourcePath:e})=>{await I(e!==process.env.INIT_CWD&&e!==process.cwd()?e:void 0)})}async function I(e){let t=await F(e),n=await S({fileName:`config-registry.ts`,nodes:C({configPaths:(await w(`schemas/**/config.ts`,{cwd:t,onlyFiles:!0})).map(e=>{let t=u.parse(e);return u.join(t.dir,t.name)})})});g(u.join(t,`config-registry.ts`))&&await ee(u.join(t,`config-registry.ts`),{force:!0,recursive:!0}),await y(u.join(t,`config-registry.ts`),n.text)}function De(e,t){return{baseClassName:`${E(T(e))}_${E(T(t))}`,queryName:`${T(e)}${E(T(t))}`,mutationName:`create${T(e)}${E(T(t))}`}}const{factory:L}=D;function R(e){return L.createIdentifier(e)}function z(e){return R(e)}function B(e){return L.createStringLiteral(e)}function V(e,t){return L.createPropertyAssignment(R(e),t)}function H(e,t=!0){return L.createObjectLiteralExpression(e,t)}function Oe(e){return e?L.createTrue():L.createFalse()}function U(e){return L.createParameterDeclaration(void 0,void 0,R(e))}function ke(e,t,n){return L.createArrowFunction(n?.async?[L.createToken(D.SyntaxKind.AsyncKeyword)]:void 0,void 0,e.map(e=>typeof e==`string`?U(e):e),void 0,L.createToken(D.SyntaxKind.EqualsGreaterThanToken),t)}function Ae(e){return L.createParenthesizedExpression(e)}function je(e,t=!0){return ke([`t`],Ae(H(e,t)))}function W(e,t){return L.createPropertyAccessExpression(typeof e==`string`?R(e):e,R(t))}function Me(e,t=[],n){return L.createCallExpression(e,n,t)}function G(e,t,n=[],r){return Me(W(e,t),n,r)}function Ne(e,t=[],n){return G(`builder`,e,t,n)}function Pe(e,t){return L.createVariableStatement(void 0,L.createVariableDeclarationList([L.createVariableDeclaration(R(e),void 0,void 0,t)],D.NodeFlags.Const))}function K(e,t,n){return L.createImportDeclaration(void 0,L.createImportClause(n?.typeOnly??!1,void 0,L.createNamedImports(t.map(e=>L.createImportSpecifier(!1,void 0,R(e))))),B(e))}function Fe(e,t,n){return L.createImportDeclaration(void 0,L.createImportClause(n?.typeOnly??!1,R(t),void 0),B(e))}function Ie({models:e,mutationName:t,mutationConfig:n,hasValidations:r,fieldDefinitions:i}){if(!n)return[];let a=Object.values(e).find(e=>e.root);if(!a)return[];let o=Le(i),s=Re(i),c=Object.values(e).filter(e=>!e.root);return[...ze(r),...c.map(t=>Be(t,e,o,s)),Be(a,e,o,s),We(t,a,r)]}function Le(e){let t=new Map;if(!e)return t;for(let n of e){let e=n.options?.required===!0;t.set(n.name,e)}return t}function Re(e){let t=new Set;if(!e)return t;for(let n of e)n.options?.readOnly===!0&&t.add(n.name);return t}function ze(e){let t=[K(`@lakeql/adapters`,[`executeWritePipeline`]),K(`@lakeql/trino-client`,[`TrinoClient`]),K(`@lakeql/api/builder`,[`builder`]),K(`~/env`,[`env`]),K(`./config`,[`hiveConfig`,`storageConfig`]),Fe(`./json-schema.json`,`jsonSchema`)];return e&&t.push(K(`./validations`,[`validationSchema`])),t}function Be(e,t,n,r){let i=`${e.modelName}Input`,a=Object.values(e.fields).filter(e=>!r.has(e.name)).map(e=>Ve(e,t,n));return Pe(i,Ne(`inputType`,[B(i),H([V(`fields`,je(a))])]))}function Ve(e,t,n){let r=V(`required`,Oe(n.get(e.name)??!0));if(e.isArray){let t=e.graphqlType.replace(/^\[/u,``).replace(/\]$/u,``),n=Ue(t);if(n)return V(e.name,G(`t`,n,[H([r],!1)]));let i=`${t}Input`;return V(e.name,G(`t`,`field`,[H([V(`type`,D.factory.createArrayLiteralExpression([z(i)])),r],!1)]))}let i=He(e.graphqlType);if(i)return V(e.name,G(`t`,i,[H([r],!1)]));if(Object.keys(t).includes(e.graphqlType)){let t=`${e.graphqlType}Input`;return V(e.name,G(`t`,`field`,[H([V(`type`,z(t)),r],!1)]))}return V(e.name,G(`t`,`string`,[H([r],!1)]))}function He(e){switch(e){case`String`:return`string`;case`Int`:return`int`;case`Float`:return`float`;case`Boolean`:return`boolean`;default:return}}function Ue(e){switch(e){case`String`:return`stringList`;case`Int`:return`intList`;case`Float`:return`floatList`;case`Boolean`:return`booleanList`;default:return}}function We(e,t,n){let r=`${t.modelName}Input`,i=[];n&&i.push(D.factory.createExpressionStatement(G(`validationSchema`,`parse`,[z(`input`)]))),i.push(D.factory.createVariableStatement(void 0,D.factory.createVariableDeclarationList([D.factory.createVariableDeclaration(z(`trinoClient`),void 0,void 0,D.factory.createNewExpression(z(`TrinoClient`),void 0,[H([V(`host`,W(`env`,`HIVE_HOST`)),V(`port`,W(`env`,`HIVE_PORT`)),V(`auth`,H([V(`type`,B(`basic`)),V(`username`,W(`env`,`HIVE_USERNAME`)),V(`password`,W(`env`,`HIVE_PASSWORD`))],!1)),V(`catalog`,W(`env`,`HIVE_CATALOG`)),V(`source`,W(`env`,`HIVE_SOURCE`))])]))],D.NodeFlags.Const)));let a=D.factory.createAwaitExpression(Me(z(`executeWritePipeline`),[H([V(`records`,z(`input`)),V(`jsonSchema`,D.factory.createAsExpression(z(`jsonSchema`),D.factory.createKeywordTypeNode(D.SyntaxKind.AnyKeyword))),V(`config`,H([V(`loadStrategy`,W(`storageConfig`,`loadStrategy`)),V(`bucket`,W(`storageConfig`,`bucket`)),V(`basePath`,W(`storageConfig`,`basePath`)),V(`table`,H([V(`catalog`,W(`hiveConfig`,`catalog`)),V(`schema`,W(`hiveConfig`,`schema`)),V(`tableName`,W(`hiveConfig`,`tableName`))],!1)),V(`trinoClient`,z(`trinoClient`))]))])]));i.push(D.factory.createExpressionStatement(a)),i.push(D.factory.createReturnStatement(D.factory.createTrue()));let o=D.factory.createBlock(i,!0),s=ke([U(`_root`),D.factory.createParameterDeclaration(void 0,void 0,D.factory.createObjectBindingPattern([D.factory.createBindingElement(void 0,void 0,D.factory.createIdentifier(`input`))]))],o,{async:!0}),c=V(e,G(`t`,`boolean`,[H([V(`args`,H([V(`input`,G(`t`,`arg`,[H([V(`type`,z(r)),V(`required`,Oe(!0))],!1)]))],!1)),V(`resolve`,s)])]));return D.factory.createExpressionStatement(Ne(`mutationFields`,[je([c])]))}function q(e){if(typeof e!=`object`||!e)return e;if(Array.isArray(e))return e.map(q);let t={},n=Object.keys(e).toSorted();for(let r of n)t[r]=q(e[r]);return t}function Ge(e){let t=q(e);return`${JSON.stringify(t,null,2).replaceAll(`\r
3
3
  `,`
4
- `)}\n`}async function Pe(e){let{definition:t,outputDir:n,skipRegistry:r,sourcePathOverride:i}=e,{catalog:a,schema:o,tableName:s,fields:c}=t,{baseClassName:l,queryName:d,mutationName:m}=ge(o,s),_=ae(c),v=ie({isRoot:!0,models:{},name:l,source:_}),y=Object.values(v).find(e=>e.root===!0),b=y?.fields?Object.values(y.fields).filter(e=>e.filter===!0).map(e=>({name:e.name,type:e.graphqlType})):[],ee=[...new Set(b.map(e=>e.type))],x=[...new Set(Object.values(v).filter(e=>e.transformFields.length>0).flatMap(e=>e.transformFields))],C=[...new Set(Object.values(v).filter(e=>e.dateTimeFields.length>0).flatMap(e=>e.dateTimeFields))],w=await S({fileName:`config.ts`,nodes:te({catalog:a,queryName:d,schema:o,tableName:s,mutationName:[m],storageConfig:t.mutation?{loadStrategy:t.mutation.loadStrategy,type:t.mutation.type,bucket:t.mutation.bucket,basePath:t.mutation.basePath,region:t.mutation.region,endpoint:t.mutation.endpoint,partitioning:t.mutation.loadStrategy===`full_load`?void 0:t.mutation.partitioning,partitioningFormat:t.mutation.loadStrategy===`full_load`?void 0:t.mutation.partitioningFormat}:void 0})}),T=await S({fileName:`interface.ts`,nodes:ne(v)}),E=await S({fileName:`query-schema.ts`,nodes:re({dateTimeFields:C,filterFields:b,filterTypes:ee,models:v,queryName:d,transformFields:x})}),D=c.some(e=>e.options?.validations&&e.options.validations.length>0),O=we({models:v,mutationName:m,mutationConfig:t.mutation,hasValidations:D,fieldDefinitions:c}),k=``;O.length>0&&(k=(await S({fileName:`mutation-schema.ts`,nodes:O})).text);let A=Ne(_),j=Ne(t),M=[{fileName:`config.ts`,content:w.text},{fileName:`interface.ts`,content:T.text},{fileName:`query-schema.ts`,content:E.text},{fileName:`json-schema.json`,content:A},{fileName:`endpoint.json`,content:j}];k&&M.push({fileName:`mutation-schema.ts`,content:k}),f(n)&&await h(n,{force:!0,recursive:!0}),await p(n,{recursive:!0});for(let e of M)await g(u.join(n,e.fileName),e.content);return r||await I(i),{files:M,outputDir:n}}function Y(e,t=0){let n=[],r=` `.repeat(t+1);for(let i of e)if(i.type===`Object`)n.push(`${r}${i.name}: Object`),i.fields&&n.push(Y(i.fields,t+1));else if(i.type===`Array`){let e=i.items?.type??`Unknown`;n.push(`${r}${i.name}: Array<${e}>`),i.items?.type===`Object`&&i.items.fields&&n.push(Y(i.items.fields,t+1))}else n.push(`${r}${i.name}: ${i.type}`);return n.filter(e=>e.length>0).join(`
5
- `)}function Fe(){return s().action(async e=>{let{fromFile:t,sourcePath:n,skipRegistry:r,force:i}=e,a=u.isAbsolute(t)?t:u.resolve(process.cwd(),t);f(a)||(console.error(`Error: File not found: ${a}`),process.exit(1));let o;try{o=await m(a,`utf-8`)}catch(e){let t=e instanceof Error?e.message:String(e);console.error(`Error: Cannot read file: ${a}: ${t}`),process.exit(1)}let s;try{s=JSON.parse(o)}catch(e){let t=e instanceof Error?e.message:String(e);console.error(`Error: Invalid JSON in ${a}: ${t}`),process.exit(1)}let c=O.safeParse(s);if(!c.success){let e=c.error.issues.map(e=>` - ${e.path.length>0?e.path.join(`.`):`root`}: ${e.message}`).join(`
6
- `);console.error(`Error: Validation failed for ${a}:\n${e}`),process.exit(1)}let l=c.data,d=k(l.fields);if(d.length>0){let e=d.map(e=>{let t=e.path.length>0?e.path.join(`.`):`root`;return` - Duplicate field "${e.name}" at level: ${t}`}).join(`
7
- `);console.error(`Error: Validation failed for ${a}:\n${e}`),process.exit(1)}console.log(`
8
- Loaded definition summary:`),console.log(` tableName: ${l.tableName}`),console.log(` catalog: ${l.catalog}`),console.log(` schema: ${l.schema}`);let p=l.mutation?l.mutation.loadStrategy:`disabled`;console.log(` mutation: ${p}`),console.log(`
9
- Fields:`),console.log(Y(l.fields)),console.log(``);let h=await F(n===process.cwd()?void 0:n),g=u.join(h,`schemas/custom`,l.catalog,l.schema,l.tableName);f(g)&&!i&&(await _(`Directory "${g}" already exists. Overwrite?`)||(console.log(`Aborted.`),process.exit(0))),await Pe({definition:l,outputDir:g,skipRegistry:r,sourcePathOverride:n===process.cwd()?void 0:n}),console.log(`Endpoint generated successfully at: ${g}`)})}let Ie;function Le(){return oe.config({path:`../../.env`}),M({runtimeEnv:process.env,server:{HIVE_CATALOG:N().min(1),HIVE_HOST:N(),HIVE_PASSWORD:N().min(1),HIVE_PORT:N().transform(e=>Number.parseInt(e,10)).pipe(ce()),HIVE_SOURCE:N().optional(),HIVE_USERNAME:N().min(1),LOG_LEVEL:se([`info`,`warn`,`error`,`debug`]).default(`warn`)}})}function X(){return Ie||=Le(),Ie}function Re(){let t=e();return t.action(async({catalog:e,schema:t,table:n})=>{let r=X(),i=e??r.HIVE_CATALOG,a=new A({auth:{password:r.HIVE_PASSWORD,type:`basic`,username:r.HIVE_USERNAME},catalog:i,host:r.HIVE_HOST,port:r.HIVE_PORT}),o=t;if(o===void 0){let e=await a.schemas({catalog:i});o=await b(`Choose a schema from the ${i} catalog`,{autocomplete:!0,choices:e})}let s=n;if(s===void 0){let e=await a.tables({catalog:i,schema:o});s=await b(`Choose a table from "${i}.${o}"`,{autocomplete:!0,choices:e})}let c=(await a.columns({catalog:i,schema:o,table:s})).map(([e,t,n,r])=>({description:r,extra:n,name:e,type:t})),l=new j;l.column(`Column Name`,`name`),l.column(`Type`,`type`),l.column(`Extra`,`extra`),l.column(`Description`,`description`),l.render(c)}),t}function ze(){let e=n();return e.action(async({catalog:e})=>{let t=X(),n=e??t.HIVE_CATALOG,r=await new A({auth:{password:t.HIVE_PASSWORD,type:`basic`,username:t.HIVE_USERNAME},catalog:n,host:t.HIVE_HOST,port:t.HIVE_PORT}).schemas({catalog:n}),i=new j;i.column(`Schema Name`,`s`),i.render(r.map(e=>({s:e})))}),e}function Be(){let e=o();return e.action(async({catalog:e,schema:t})=>{let n=X(),r=e??n.HIVE_CATALOG,i=new A({auth:{password:n.HIVE_PASSWORD,type:`basic`,username:n.HIVE_USERNAME},catalog:r,host:n.HIVE_HOST,port:n.HIVE_PORT}),a=t;if(a===void 0){let e=await i.schemas({catalog:r});a=await b(`Choose a schema from the ${r} catalog`,{autocomplete:!0,choices:e})}let o=await i.tables({catalog:r,schema:a}),s=new j;s.column(`Table Name`,`t`),s.render(o.map(e=>({t:e})))}),e}function Ve(){let e=i();return e.action(async({catalog:e,schema:t})=>{let n=X(),r=e??n.HIVE_CATALOG,i=new A({auth:{password:n.HIVE_PASSWORD,type:`basic`,username:n.HIVE_USERNAME},catalog:r,host:n.HIVE_HOST,port:n.HIVE_PORT}),a=t;if(a===void 0){let e=await i.schemas({catalog:r});a=await b(`Choose a schema from the ${r} catalog`,{choices:e})}let o=await i.views({catalog:r,schema:a}),s=new j;s.column(`View Name`,`v`),s.render(o.map(e=>({v:e})))}),e}function Z(e){switch(e.replaceAll(/[^a-zA-Z]/gu,``)){case`varchar`:return`String`;case`decimal`:case`double`:case`float`:return`Float`;case`integer`:case`bigint`:return`Integer`;case`boolean`:return`Boolean`;case`timestamp`:return`DateTime`;case`date`:return`Date`;default:return`String`}}function He(e,t){return typeof t==`string`?{name:e,type:Z(t)}:Array.isArray(t)?{name:e,type:`Array`,items:Ue(t)}:typeof t==`object`&&t?{name:e,type:`Object`,fields:Q(t)}:{name:e,type:`String`}}function Ue(e){if(e.length===0)return{type:`String`};let[t]=e;return typeof t==`string`?{type:Z(t)}:typeof t==`object`&&t&&!Array.isArray(t)?{type:`Object`,fields:Q(t)}:{type:`String`}}function Q(e){let t=[];for(let[n,r]of Object.entries(e))t.push(He(n,r));return t}function We(e){let{tableName:t,catalog:n,schema:r,parsedColumns:i}=e;return{version:`1.0`,tableName:t,catalog:n,schema:r,fields:Q(i),mutation:!1}}async function $(e){let{trinoClient:t,catalog:n,schema:r,tables:i,resolvedTargetPath:a,skipRegistry:o,sourcePathOverride:s}=e;for(let e of i)await Pe({definition:We({tableName:e,catalog:n,schema:r,parsedColumns:de((await t.columns({catalog:n,schema:r,table:e})).map(e=>fe({keys:[`name`,`type`,`extra`,`description`],values:e})))}),outputDir:u.join(a,`schemas/generated`,n,r,e),skipRegistry:o,sourcePathOverride:s})}async function Ge(e){let n=t(),r;if(e){let t=u.isAbsolute(e)?e:u.resolve(n,e),i=u.parse(t);r=u.join(i.dir,i.name)}let{config:i}=await x({name:`import`,cwd:n,configFile:r,defaults:{default:[]},packageJson:!1,globalRc:!1,rcFile:!1,dotenv:!1}),a=i;return Array.isArray(a)?a:a&&typeof a==`object`&&`default`in a&&Array.isArray(a.default)?a.default:[]}async function Ke(e){let{configPath:t,catalog:n,sourcePathOverride:r,skipRegistry:i}=e,a=X(),o=await F(r),s=await Ge(t);if(s.length===0){console.log(`No entries found in bulk config.`);return}await new ue([{title:`Pull data`,task:(e,t)=>t.newListr(s.map(e=>{let t=n??e.catalog??a.HIVE_CATALOG,i=(e.tables?.length??0)+(e.views?.length??0);return{title:`${t}/${e.schema} — ${i} item(s)`,task:async(n,s)=>{let c=new A({auth:{password:a.HIVE_PASSWORD,type:`basic`,username:a.HIVE_USERNAME},catalog:t,host:a.HIVE_HOST,port:a.HIVE_PORT});e.tables&&e.tables.length>0&&(s.output=`Pulling ${e.tables.length} table(s)...`,await $({trinoClient:c,catalog:t,schema:e.schema,tables:e.tables,resolvedTargetPath:o,skipRegistry:!0,sourcePathOverride:r})),e.views&&e.views.length>0&&(s.output=`Pulling ${e.views.length} view(s)...`,await $({trinoClient:c,catalog:t,schema:e.schema,tables:e.views,resolvedTargetPath:o,skipRegistry:!0,sourcePathOverride:r})),s.title=`${t}/${e.schema} — ${i} item(s) pulled`}}}),{concurrent:!0,exitOnError:!1})},{title:`Create registry`,enabled:!i,task:async()=>{await I(r)}}],{concurrent:!1,exitOnError:!0}).run()}function qe(){let e=c();return e.action(async t=>{let{skipRegistry:n,sourcePath:r,bulk:i,bulkConfig:a}=t,o=r===(process.env.INIT_CWD??process.cwd())?void 0:r;if(i){await Ke({configPath:a,catalog:t.catalog,sourcePathOverride:o,skipRegistry:n});return}let s=X(),c=t.catalog??s.HIVE_CATALOG,{schema:l,table:u,type:d}=t,f=await F(o),p=new A({auth:{password:s.HIVE_PASSWORD,type:`basic`,username:s.HIVE_USERNAME},catalog:c,host:s.HIVE_HOST,port:s.HIVE_PORT});if(!l){let e=await p.schemas({catalog:c});l=await b(`Choose a schema from the ${c} catalog`,{autocomplete:!0,choices:e})}if(u.length===0&&(d??=await b(`What do you want to see from ${c}/${l}`,{choices:[{label:`Show tables`,value:`tables`},{label:`Show views`,value:`views`}]})),u.length===0){d??=`tables`;let t=[];t=d===`views`?await p.views({catalog:c,schema:l}):await p.tables({catalog:c,schema:l}),t.length===0&&e.error(le(`There are no ${d} in schema '${c}.${l}'.`),{exitCode:0}),u=await v(`Choose the ${d} to pull`,{autocomplete:!0,choices:t,validators:[ee.required()]})}await $({trinoClient:p,catalog:c,schema:l,tables:[...u],resolvedTargetPath:f,skipRegistry:n,sourcePathOverride:o})}),e}const Je=[`lakeql.config.mjs`,`lakeql.config.ts`,`lakeql.config.json`];function Ye(e){return`/** @type {import('@lakeql/cli').LakeQLConfig} */\nexport default {\n${Object.entries(e).map(([e,t])=>` ${e}: ${JSON.stringify(t)},`).join(`
10
- `)}\n}\n`}function Xe(e){for(let t of Je)if(f(u.join(e,t)))return t;return null}function Ze(){return r().action(async()=>{let e=t(),n=Xe(e);if(n&&await b(`${n} already exists. Overwrite?`,{choices:[{label:`Yes`,value:`yes`},{label:`No`,value:`no`}]})===`no`){console.log(P.yellow(`Aborted.`));return}let r=await b(`Config format:`,{choices:[{label:`lakeql.config.mjs (recommended)`,value:`mjs`},{label:`lakeql.config.json`,value:`json`}]}),i=f(u.join(e,`src`)),a;if(i)a=`src`,console.log(P.cyan(`Detected src/ directory — generated code will be placed in src/`));else{let e=await b(`Where should generated code be placed?`,{choices:[{label:`./ (project root)`,value:`.`},{label:`Custom path`,value:`__custom__`}]});a=e,e===`__custom__`&&(a=await y(`Enter the source path (relative to project root):`,{defaultValue:`.`}))}let o={sourcePath:a},s=r===`mjs`?`lakeql.config.mjs`:`lakeql.config.json`,c=u.join(e,s);await g(c,r===`mjs`?Ye(o):`${JSON.stringify(o,null,2)}\n`),console.log(P.green(`Created ${s} at ${c}`))})}async function Qe(){let e=await d(),t=new l;t.configureHelp({sortSubcommands:!0}),t.name(`lakeql-cli`).description(`LakeQL CLI`).version(e.version).addCommand(ze()).addCommand(Be()).addCommand(Ve()).addCommand(Re()).addCommand(qe()).addCommand(Fe()).addCommand(he()).addCommand(Ze()),await t.parseAsync()}Qe().catch(()=>{process.exit(1)});export{};
4
+ `)}\n`}async function Ke(e){let{definition:t,outputDir:n,skipRegistry:r,sourcePathOverride:i}=e,{catalog:a,schema:o,tableName:s,fields:c}=t,{baseClassName:l,queryName:d,mutationName:f}=De(o,s),p=le(c),m=ce({isRoot:!0,models:{},name:l,source:p}),h=Object.values(m).find(e=>e.root===!0),v=h?.fields?Object.values(h.fields).filter(e=>e.filter===!0).map(e=>({name:e.name,type:e.graphqlType})):[],te=[...new Set(v.map(e=>e.type))],ne=[...new Set(Object.values(m).filter(e=>e.transformFields.length>0).flatMap(e=>e.transformFields))],re=[...new Set(Object.values(m).filter(e=>e.dateTimeFields.length>0).flatMap(e=>e.dateTimeFields))],b=await S({fileName:`config.ts`,nodes:ae({catalog:a,queryName:d,schema:o,tableName:s,mutationName:[f],storageConfig:t.mutation?{loadStrategy:t.mutation.loadStrategy,type:t.mutation.type,bucket:t.mutation.bucket,basePath:t.mutation.basePath,region:t.mutation.region,endpoint:t.mutation.endpoint,partitioning:t.mutation.loadStrategy===`full_load`?void 0:t.mutation.partitioning,partitioningFormat:t.mutation.loadStrategy===`full_load`?void 0:t.mutation.partitioningFormat}:void 0})}),ie=await S({fileName:`interface.ts`,nodes:oe(m)}),x=await S({fileName:`query-schema.ts`,nodes:se({dateTimeFields:re,filterFields:v,filterTypes:te,models:m,queryName:d,transformFields:ne})}),C=c.some(e=>e.options?.validations&&e.options.validations.length>0),w=Ie({models:m,mutationName:f,mutationConfig:t.mutation,hasValidations:C,fieldDefinitions:c}),T=``;w.length>0&&(T=(await S({fileName:`mutation-schema.ts`,nodes:w})).text);let E=Ge(p),D=Ge(t),O=[{fileName:`config.ts`,content:b.text},{fileName:`interface.ts`,content:ie.text},{fileName:`query-schema.ts`,content:x.text},{fileName:`json-schema.json`,content:E},{fileName:`endpoint.json`,content:D}];T&&O.push({fileName:`mutation-schema.ts`,content:T}),g(n)&&await ee(n,{force:!0,recursive:!0}),await _(n,{recursive:!0});for(let e of O)await y(u.join(n,e.fileName),e.content);return r||await I(i),{files:O,outputDir:n}}function J(e,t=0){let n=[],r=` `.repeat(t+1);for(let i of e)if(i.type===`Object`)n.push(`${r}${i.name}: Object`),i.fields&&n.push(J(i.fields,t+1));else if(i.type===`Array`){let e=i.items?.type??`Unknown`;n.push(`${r}${i.name}: Array<${e}>`),i.items?.type===`Object`&&i.items.fields&&n.push(J(i.items.fields,t+1))}else n.push(`${r}${i.name}: ${i.type}`);return n.filter(e=>e.length>0).join(`
5
+ `)}function qe(){return s().action(async e=>{let{fromFile:t,sourcePath:n,skipRegistry:r,force:i}=e,a=u.isAbsolute(t)?t:u.resolve(process.cwd(),t);if(!g(a))throw new N(`File not found: ${a}`,{code:`ENDPOINT_FILE_NOT_FOUND`});let o;try{o=await v(a,`utf-8`)}catch(e){throw new N(`Cannot read file: ${a}: ${e instanceof Error?e.message:String(e)}`,{code:`ENDPOINT_FILE_READ_FAILED`,cause:e})}let s;try{s=JSON.parse(o)}catch(e){throw new N(`Invalid JSON in ${a}: ${e instanceof Error?e.message:String(e)}`,{code:`ENDPOINT_INVALID_JSON`,cause:e})}let c=O.safeParse(s);if(!c.success){let e=c.error.issues.map(e=>` - ${e.path.length>0?e.path.join(`.`):`root`}: ${e.message}`).join(`
6
+ `);throw new N(`Validation failed for ${a}.`,{code:`ENDPOINT_VALIDATION_FAILED`,details:[e]})}let l=c.data,d=ue(l.fields);if(d.length>0){let e=d.map(e=>{let t=e.path.length>0?e.path.join(`.`):`root`;return` - Duplicate field "${e.name}" at level: ${t}`}).join(`
7
+ `);throw new N(`Validation failed for ${a}.`,{code:`ENDPOINT_DUPLICATE_FIELDS`,details:[e]})}console.log(`\n${f(`Loaded definition summary:`)}`),console.log(f(`tableName: ${l.tableName}`)),console.log(f(`catalog: ${l.catalog}`)),console.log(f(`schema: ${l.schema}`));let m=l.mutation?l.mutation.loadStrategy:`disabled`;console.log(f(`mutation: ${m}`)),console.log(`\n${f(`Fields:`)}`),console.log(f(J(l.fields))),console.log(``);let h=await F(n===process.cwd()?void 0:n),_=u.join(h,`schemas/custom`,l.catalog,l.schema,l.tableName);if(g(_)&&!i&&!await te(`Directory "${_}" already exists. Overwrite?`))throw ve(`Aborted: endpoint generation was not confirmed.`);await Ke({definition:l,outputDir:_,skipRegistry:r,sourcePathOverride:n===process.cwd()?void 0:n}),console.log(p(`Endpoint generated successfully at: ${_}`))})}let Je;function Ye(){return de({runtimeEnv:process.env,server:{HIVE_CATALOG:j().min(1),HIVE_HOST:j(),HIVE_PASSWORD:j().min(1),HIVE_PORT:j().transform(e=>Number.parseInt(e,10)).pipe(me()),HIVE_SOURCE:j().optional(),HIVE_USERNAME:j().min(1),LOG_LEVEL:pe([`info`,`warn`,`error`,`debug`]).default(`warn`)}})}function Y(){return Je||=Ye(),Je}function Xe(){let t=e();return t.action(async({catalog:e,schema:t,table:n})=>{let r=Y(),i=e??r.HIVE_CATALOG,a=new k({auth:{password:r.HIVE_PASSWORD,type:`basic`,username:r.HIVE_USERNAME},catalog:i,host:r.HIVE_HOST,port:r.HIVE_PORT}),o=t;if(o===void 0){let e;try{e=await a.schemas({catalog:i})}catch(e){throw P(`list schemas`,`list-columns (catalog=${i})`,e)}o=await b(`Choose a schema from the ${i} catalog`,{autocomplete:!0,choices:e})}let s=n;if(s===void 0){let e;try{e=await a.tables({catalog:i,schema:o})}catch(e){throw P(`list tables`,`list-columns (catalog=${i}, schema=${o})`,e)}s=await b(`Choose a table from "${i}.${o}"`,{autocomplete:!0,choices:e})}let c;try{c=await a.columns({catalog:i,schema:o,table:s})}catch(e){throw P(`list columns`,`list-columns (catalog=${i}, schema=${o}, table=${s})`,e)}let l=c.map(([e,t,n,r])=>({description:r,extra:n,name:e,type:t})),u=new A;u.column(`Column Name`,`name`),u.column(`Type`,`type`),u.column(`Extra`,`extra`),u.column(`Description`,`description`),u.render(l)}),t}function Ze(){let e=n();return e.action(async({catalog:e})=>{let t=Y(),n=e??t.HIVE_CATALOG,r=new k({auth:{password:t.HIVE_PASSWORD,type:`basic`,username:t.HIVE_USERNAME},catalog:n,host:t.HIVE_HOST,port:t.HIVE_PORT}),i;try{i=await r.schemas({catalog:n})}catch(e){throw P(`list schemas`,`list-schemas (catalog=${n})`,e)}let a=new A;a.column(`Schema Name`,`s`),a.render(i.map(e=>({s:e})))}),e}function Qe(){let e=o();return e.action(async({catalog:e,schema:t})=>{let n=Y(),r=e??n.HIVE_CATALOG,i=new k({auth:{password:n.HIVE_PASSWORD,type:`basic`,username:n.HIVE_USERNAME},catalog:r,host:n.HIVE_HOST,port:n.HIVE_PORT}),a=t;if(a===void 0){let e;try{e=await i.schemas({catalog:r})}catch(e){throw P(`list schemas`,`list-tables (catalog=${r})`,e)}a=await b(`Choose a schema from the ${r} catalog`,{autocomplete:!0,choices:e})}let o;try{o=await i.tables({catalog:r,schema:a})}catch(e){throw P(`list tables`,`list-tables (catalog=${r}, schema=${a})`,e)}let s=new A;s.column(`Table Name`,`t`),s.render(o.map(e=>({t:e})))}),e}function $e(){let e=i();return e.action(async({catalog:e,schema:t})=>{let n=Y(),r=e??n.HIVE_CATALOG,i=new k({auth:{password:n.HIVE_PASSWORD,type:`basic`,username:n.HIVE_USERNAME},catalog:r,host:n.HIVE_HOST,port:n.HIVE_PORT}),a=t;if(a===void 0){let e;try{e=await i.schemas({catalog:r})}catch(e){throw P(`list schemas`,`list-views (catalog=${r})`,e)}a=await b(`Choose a schema from the ${r} catalog`,{choices:e})}let o;try{o=await i.views({catalog:r,schema:a})}catch(e){throw P(`list views`,`list-views (catalog=${r}, schema=${a})`,e)}let s=new A;s.column(`View Name`,`v`),s.render(o.map(e=>({v:e})))}),e}const et=M.object({schema:M.string().min(1),catalog:M.string().min(1).optional(),tables:M.array(M.string().min(1)).optional(),views:M.array(M.string().min(1)).optional()}).superRefine((e,t)=>{let n=Array.isArray(e.tables)&&e.tables.length>0,r=Array.isArray(e.views)&&e.views.length>0;!n&&!r&&t.addIssue({code:`custom`,message:`At least one non-empty list is required: tables or views.`,path:[`tables`]})}),tt=M.array(et);function X(e){return tt.parse(e)}function nt(e){switch(e.replaceAll(/[^a-zA-Z]/gu,``)){case`varchar`:return`String`;case`decimal`:case`double`:case`float`:return`Float`;case`integer`:case`bigint`:return`Integer`;case`boolean`:return`Boolean`;case`timestamp`:return`DateTime`;case`date`:return`Date`;default:return`String`}}function rt(e,t){return typeof t==`string`?{name:e,type:nt(t)}:Array.isArray(t)?{name:e,type:`Array`,items:it(t)}:typeof t==`object`&&t?{name:e,type:`Object`,fields:Z(t)}:{name:e,type:`String`}}function it(e){if(e.length===0)return{type:`String`};let[t]=e;return typeof t==`string`?{type:nt(t)}:typeof t==`object`&&t&&!Array.isArray(t)?{type:`Object`,fields:Z(t)}:{type:`String`}}function Z(e){let t=[];for(let[n,r]of Object.entries(e))t.push(rt(n,r));return t}function at(e){let{tableName:t,catalog:n,schema:r,parsedColumns:i}=e;return{version:`1.0`,tableName:t,catalog:n,schema:r,fields:Z(i),mutation:!1}}async function Q(e){let{trinoClient:t,catalog:n,schema:r,tables:i,resolvedTargetPath:a,skipRegistry:o,sourcePathOverride:s}=e;for(let e of i)await Ke({definition:at({tableName:e,catalog:n,schema:r,parsedColumns:ge((await t.columns({catalog:n,schema:r,table:e})).map(e=>_e({keys:[`name`,`type`,`extra`,`description`],values:e})))}),outputDir:u.join(a,`schemas/generated`,n,r,e),skipRegistry:o,sourcePathOverride:s})}async function ot(e){let n=t(),r;if(e){let t=u.isAbsolute(e)?e:u.resolve(n,e),i=u.parse(t);r=u.join(i.dir,i.name)}let{config:i}=await x({name:`import`,cwd:n,configFile:r,defaults:{default:[]},packageJson:!1,globalRc:!1,rcFile:!1,dotenv:!1}),a=i;return Array.isArray(a)?X(a):a&&typeof a==`object`&&`default`in a&&Array.isArray(a.default)?X(a.default):X([])}async function st(e){let{configPath:t,catalog:n,sourcePathOverride:r,skipRegistry:i}=e,a=Y(),o=await F(r),s;try{s=await ot(t)}catch(e){throw e instanceof fe?new N(`Invalid bulk pull config.`,{code:`BULK_CONFIG_INVALID`,details:e.issues.map(e=>` - ${e.path.length>0?e.path.join(`.`):`root`}: ${e.message}`),hint:`Ensure each entry defines a schema and at least one non-empty list: tables or views.`,cause:e}):e}if(s.length===0){console.log(f(`No entries found in bulk config.`));return}await new he([{title:`Pull data`,task:(e,t)=>t.newListr(s.map(e=>{let t=n??e.catalog??a.HIVE_CATALOG,i=(e.tables?.length??0)+(e.views?.length??0);return{title:`${t}/${e.schema} — ${i} item(s)`,task:async(n,s)=>{let c=new k({auth:{password:a.HIVE_PASSWORD,type:`basic`,username:a.HIVE_USERNAME},catalog:t,host:a.HIVE_HOST,port:a.HIVE_PORT});if(e.tables&&e.tables.length>0){s.output=`Pulling ${e.tables.length} table(s)...`;try{await Q({trinoClient:c,catalog:t,schema:e.schema,tables:e.tables,resolvedTargetPath:o,skipRegistry:!0,sourcePathOverride:r})}catch(n){throw P(`pull tables`,`bulk pull (catalog=${t}, schema=${e.schema}, tables=${e.tables.join(`,`)})`,n)}}if(e.views&&e.views.length>0){s.output=`Pulling ${e.views.length} view(s)...`;try{await Q({trinoClient:c,catalog:t,schema:e.schema,tables:e.views,resolvedTargetPath:o,skipRegistry:!0,sourcePathOverride:r})}catch(n){throw P(`pull views`,`bulk pull (catalog=${t}, schema=${e.schema}, views=${e.views.join(`,`)})`,n)}}s.title=`${t}/${e.schema} — ${i} item(s) pulled`}}}),{concurrent:!0,exitOnError:!1})},{title:`Create registry`,enabled:!i,task:async()=>{await I(r)}}],{concurrent:!1,exitOnError:!0}).run()}async function ct(e,t,n){try{return await n()}catch(n){throw P(e,t,n)}}function lt(){let e=c();return e.action(async e=>{let{skipRegistry:t,sourcePath:n,bulk:r,bulkConfig:i}=e,a=n===(process.env.INIT_CWD??process.cwd())?void 0:n;if(r){await st({configPath:i,catalog:e.catalog,sourcePathOverride:a,skipRegistry:t});return}let o=Y(),s=e.catalog??o.HIVE_CATALOG,{schema:c,table:l,type:u}=e,d=await F(a),f=new k({auth:{password:o.HIVE_PASSWORD,type:`basic`,username:o.HIVE_USERNAME},catalog:s,host:o.HIVE_HOST,port:o.HIVE_PORT});if(!c){let e=await ct(`list schemas`,`pull (catalog=${s})`,()=>f.schemas({catalog:s}));c=await b(`Choose a schema from the ${s} catalog`,{autocomplete:!0,choices:e})}if(l.length===0&&(u??=await b(`What do you want to see from ${s}/${c}`,{choices:[{label:`Show tables`,value:`tables`},{label:`Show views`,value:`views`}]})),l.length===0){u??=`tables`;let e=[];if(e=await ct(u===`views`?`list views`:`list tables`,`pull (catalog=${s}, schema=${c})`,()=>u===`views`?f.views({catalog:s,schema:c}):f.tables({catalog:s,schema:c})),e.length===0)throw new N(`No ${u} found in schema '${s}.${c}'.`,{code:`PULL_NO_RESULTS`,hint:`Use a different schema or check permissions for '${s}.${c}'.`,exitCode:0});l=await ne(`Choose the ${u} to pull`,{autocomplete:!0,choices:e,validators:[ie.required()]})}await Q({trinoClient:f,catalog:s,schema:c,tables:[...l],resolvedTargetPath:d,skipRegistry:t,sourcePathOverride:a})}),e}const ut=[`lakeql.config.mjs`,`lakeql.config.ts`,`lakeql.config.json`];function dt(e){return`/** @type {import('@lakeql/cli').LakeQLConfig} */\nexport default {\n${Object.entries(e).map(([e,t])=>` ${e}: ${JSON.stringify(t)},`).join(`
8
+ `)}\n}\n`}function ft(e){for(let t of ut)if(g(u.join(e,t)))return t;return null}function pt(){return r().action(async()=>{let e=t(),n=ft(e);if(n&&await b(`${n} already exists. Overwrite?`,{choices:[{label:`Yes`,value:`yes`},{label:`No`,value:`no`}]})===`no`){console.log(f(`Aborted.`));return}let r=await b(`Config format:`,{choices:[{label:`lakeql.config.mjs (recommended)`,value:`mjs`},{label:`lakeql.config.json`,value:`json`}]}),i=g(u.join(e,`src`)),a;if(i)a=`src`,console.log(f(`Detected src/ directory - generated code will be placed in src/`));else{let e=await b(`Where should generated code be placed?`,{choices:[{label:`./ (project root)`,value:`.`},{label:`Custom path`,value:`__custom__`}]});a=e,e===`__custom__`&&(a=await re(`Enter the source path (relative to project root):`,{defaultValue:`.`}))}let o={sourcePath:a},s=r===`mjs`?`lakeql.config.mjs`:`lakeql.config.json`,c=u.join(e,s);await y(c,r===`mjs`?dt(o):`${JSON.stringify(o,null,2)}\n`),console.log(p(`Created ${s} at ${c}`))})}function mt(e){let t=new l;return t.configureHelp({sortSubcommands:!0}),t.name(`lakeql-cli`).description(`LakeQL CLI`).version(e.version).addCommand(Ze()).addCommand(Qe()).addCommand($e()).addCommand(Xe()).addCommand(lt()).addCommand(qe()).addCommand(Ee()).addCommand(pt()),t}async function ht(e=process.argv.slice(2),t){let n=mt(t??await h());return e.length===0?(n.outputHelp(),0):(await n.parseAsync(e,{from:`user`}),0)}function $(e){if(!(typeof e!=`object`||!e))return e}function gt(e){let t=$(e),n=t?.message;for(;t?.cause;){let e=$(t.cause);if(!e)break;e.message&&(n=e.message),t=e}return n}function _t(e){if(e instanceof N&&e.code)return e.code;let t=$(e);return t?.code?t.code:$(t?.cause)?.code}function vt(e){if(e instanceof N&&e.hint)return e.hint;let t=gt(e)?.toLowerCase()??``,n=_t(e);if(t.includes(`fetch failed`)||n===`ECONNREFUSED`||n===`ENOTFOUND`||n===`ETIMEDOUT`)return`Could not reach Trino. Verify HIVE_HOST/HIVE_PORT and that the Trino endpoint is reachable from your network.`;if(t.includes(`invalid environment variables`)||t.includes(`required`))return`Missing or invalid environment variables. Set HIVE_HOST, HIVE_PORT, HIVE_USERNAME, HIVE_PASSWORD and HIVE_CATALOG.`}function yt(e){let t=[e instanceof N&&e.exitCode===0?`LakeQL CLI aborted.`:`LakeQL CLI failed.`],n=e instanceof Error?e.message:typeof e==`string`?e:`Unknown error`;t.push(`Reason: ${n}`),e instanceof N&&e.details?.length&&t.push(...e.details);let r=gt(e);r&&r!==n&&t.push(`Root cause: ${r}`);let i=_t(e);i&&t.push(`Error code: ${i}`);let a=vt(e);return a&&t.push(`Hint: ${a}`),t}ht().catch(e=>{let t=ye(e)?ve():e,n=yt(t),r=t instanceof N&&t.exitCode===0?m:d,i=n.map((e,t)=>t===0?r(e):f(e)).join(`
9
+ `);console.error(i);let a=t instanceof N?t.exitCode:1;process.exit(a)});export{};
package/dist/index.d.mts CHANGED
@@ -1,3 +1,5 @@
1
+ import { z } from "zod/v4";
2
+
1
3
  //#region src/bulk-pull-config.d.ts
2
4
  /**
3
5
  * Configuration for a single pull entry in a bulk import.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lakeql/cli",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "description": "CLI for introspecting Trino schemas and generating type-safe GraphQL endpoints",
5
5
  "keywords": [
6
6
  "cli",
@@ -56,20 +56,18 @@
56
56
  "c12": "4.0.0-beta.5",
57
57
  "climt": "0.2.0",
58
58
  "commander": "15.0.0",
59
- "dotenv": "17.4.2",
60
59
  "globby": "16.2.0",
61
60
  "handlebars": "4.7.9",
62
- "kleur": "4.1.5",
63
61
  "listr2": "10.2.1",
64
62
  "lodash-es": "4.18.1",
65
63
  "read-pkg": "10.1.0",
66
64
  "zod": "4.4.3",
67
65
  "@lakeql/file-generator": "0.1.8",
66
+ "@lakeql/helpers": "0.1.4",
68
67
  "@lakeql/column-parser": "0.1.4",
69
- "@lakeql/logger": "0.1.3",
68
+ "@lakeql/logger": "0.2.0",
70
69
  "@lakeql/response-transformer": "0.1.4",
71
70
  "@lakeql/schema-generator": "0.4.2",
72
- "@lakeql/helpers": "0.1.4",
73
71
  "@lakeql/trino-client": "0.4.0"
74
72
  },
75
73
  "devDependencies": {