@lunora/codegen 1.0.0-alpha.19 → 1.0.0-alpha.190

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.
Files changed (94) hide show
  1. package/LICENSE.md +6 -0
  2. package/README.md +1 -1
  3. package/dist/index.d.mts +3277 -1132
  4. package/dist/index.d.ts +3277 -1132
  5. package/dist/index.mjs +1 -34
  6. package/dist/packem_shared/AGENTS_FILENAME-B78F7DuQ.mjs +1 -0
  7. package/dist/packem_shared/CONTAINERS_FILENAME-CQoUu7n7.mjs +1 -0
  8. package/dist/packem_shared/CodegenDiagnosticError-BUXlG3P-.mjs +1 -0
  9. package/dist/packem_shared/DEFAULT_TARGET-DtzlGawL.mjs +1 -0
  10. package/dist/packem_shared/FLAGS_FILENAME-CoTJH0-7.mjs +1 -0
  11. package/dist/packem_shared/GENERATED_HEADER-DycP5oCG.mjs +1 -0
  12. package/dist/packem_shared/LUNORA_ERROR_CODES-C2s0xWQD.mjs +1 -0
  13. package/dist/packem_shared/MUTATORS_FILENAME-CTHlZ1j6.mjs +1 -0
  14. package/dist/packem_shared/NOTIFY_FILENAME-CZh4jN4q.mjs +1 -0
  15. package/dist/packem_shared/OPENRPC_VERSION-BNobmd0k.mjs +3 -0
  16. package/dist/packem_shared/PROJECT_CONFIG_FILENAMES-D29yBg5X.mjs +1 -0
  17. package/dist/packem_shared/QUEUES_FILENAME-Q3CF3uYr.mjs +1 -0
  18. package/dist/packem_shared/SCHEMA_SNAPSHOT_FILENAME-DLqvtzGf.mjs +9 -0
  19. package/dist/packem_shared/SCHEMA_SNAPSHOT_VERSION-C0oHwiJZ.mjs +2 -0
  20. package/dist/packem_shared/SDK_LANGUAGES-CFyux-74.mjs +371 -0
  21. package/dist/packem_shared/SHAPES_FILENAME-Bvd5kgsF.mjs +1 -0
  22. package/dist/packem_shared/SchemaSnapshotParseError-lUWe6lPo.mjs +18 -0
  23. package/dist/packem_shared/WORKFLOWS_FILENAME-BaSbufF2.mjs +1 -0
  24. package/dist/packem_shared/buildOpenApiDocument-BomWVHVV.mjs +3 -0
  25. package/dist/packem_shared/chain-Dp_6wzI9.mjs +1 -0
  26. package/dist/packem_shared/classify-procedure-call-C8jbo0IJ.mjs +1 -0
  27. package/dist/packem_shared/context-property-calls-Ce9xGl_o.mjs +1 -0
  28. package/dist/packem_shared/describeErrorLevelFindings-D23VSkFP.mjs +1 -0
  29. package/dist/packem_shared/discoverAuthApiCalls-DoblmzUW.mjs +1 -0
  30. package/dist/packem_shared/discoverCrons-CMu0sA8k.mjs +1 -0
  31. package/dist/packem_shared/discoverFunctions-DB9G_XrW.mjs +1 -0
  32. package/dist/packem_shared/discoverHttpRoutes-S81Iv_iB.mjs +1 -0
  33. package/dist/packem_shared/discoverHyperdriveCalls-DgAYcZRD.mjs +1 -0
  34. package/dist/packem_shared/discoverInserts-COk-Sm_n.mjs +1 -0
  35. package/dist/packem_shared/discoverMaskProcedures-BLAzNB90.mjs +1 -0
  36. package/dist/packem_shared/discoverMigrations-B_sYrTyE.mjs +1 -0
  37. package/dist/packem_shared/discoverNondeterministicCalls-DkVVHe3U.mjs +1 -0
  38. package/dist/packem_shared/discoverQueries-BQXP4BZ0.mjs +1 -0
  39. package/dist/packem_shared/discoverR2sqlCalls-6u0OKy2N.mjs +1 -0
  40. package/dist/packem_shared/discoverRlsMetadata-CwWRPb8N.mjs +1 -0
  41. package/dist/packem_shared/discoverRlsProcedures-vJZq3yhb.mjs +1 -0
  42. package/dist/packem_shared/discoverSandboxUsage-DskdDbkM.mjs +1 -0
  43. package/dist/packem_shared/discoverSchema-DoZtXwLg.mjs +1 -0
  44. package/dist/packem_shared/discoverStorageRulesMetadata-xhxu-mkS.mjs +1 -0
  45. package/dist/packem_shared/emit-B_n-PvH3.mjs +2639 -0
  46. package/dist/packem_shared/emitApp-DvDGeMCt.mjs +823 -0
  47. package/dist/packem_shared/exported-procedure-chains-C9FHwphy.mjs +1 -0
  48. package/dist/packem_shared/formatAdvisories-CkEu1eye.mjs +2 -0
  49. package/dist/packem_shared/index-D_Z8rX-f.mjs +1 -0
  50. package/dist/packem_shared/isD1GlobalTable-udvHAAzD.mjs +1 -0
  51. package/dist/packem_shared/isTypedSchema-B_qlBbE-.mjs +2 -0
  52. package/dist/packem_shared/listLunoraSourceFiles-D4xthUf-.mjs +1 -0
  53. package/dist/packem_shared/module-specifiers-WF8iAydP.mjs +1 -0
  54. package/dist/packem_shared/parse-validator-tdRHHDzh.mjs +1 -0
  55. package/dist/packem_shared/paths-C-9fwEgQ.mjs +1 -0
  56. package/dist/packem_shared/queries-CPlxXagL.mjs +1 -0
  57. package/dist/packem_shared/readPackageDependencies-C6h6FZuP.mjs +1 -0
  58. package/dist/packem_shared/redact-Cy14LEnc.mjs +1 -0
  59. package/dist/packem_shared/schemaFromIr-BWFnvCVg.mjs +1 -0
  60. package/dist/packem_shared/unwrap-handler-return-CmIWbykC.mjs +1 -0
  61. package/package.json +16 -7
  62. package/dist/packem_shared/CONTAINERS_FILENAME-DlP6YM_Q.mjs +0 -224
  63. package/dist/packem_shared/CodegenDiagnosticError-DeblMkzO.mjs +0 -23
  64. package/dist/packem_shared/FLAGS_FILENAME-B1vE0LIo.mjs +0 -139
  65. package/dist/packem_shared/GENERATED_HEADER-D-AbhmRh.mjs +0 -3134
  66. package/dist/packem_shared/LUNORA_ERROR_CODES-CySpQPD3.mjs +0 -61
  67. package/dist/packem_shared/LUNORA_SOLUTION_RULES-BTejmZp-.mjs +0 -131
  68. package/dist/packem_shared/MUTATORS_FILENAME-BhqdPtKp.mjs +0 -81
  69. package/dist/packem_shared/OPENRPC_VERSION-DDT4mIzD.mjs +0 -60
  70. package/dist/packem_shared/QUEUES_FILENAME-B5_eWCRe.mjs +0 -119
  71. package/dist/packem_shared/SCHEMA_SNAPSHOT_FILENAME-BZH-O4uh.mjs +0 -1039
  72. package/dist/packem_shared/SCHEMA_SNAPSHOT_VERSION-wWGP2_5E.mjs +0 -245
  73. package/dist/packem_shared/SHAPES_FILENAME-ChV7MqgE.mjs +0 -94
  74. package/dist/packem_shared/WORKFLOWS_FILENAME-CCisG0Vy.mjs +0 -124
  75. package/dist/packem_shared/buildOpenApiDocument-DoK7_x1g.mjs +0 -183
  76. package/dist/packem_shared/discover-ast-CT6BgBr4.mjs +0 -13
  77. package/dist/packem_shared/discoverAuthApiCalls-CoirYbg6.mjs +0 -62
  78. package/dist/packem_shared/discoverCrons-Cev7RRAf.mjs +0 -257
  79. package/dist/packem_shared/discoverFunctions-BWMczzBx.mjs +0 -465
  80. package/dist/packem_shared/discoverHttpRoutes-CfP6cMzt.mjs +0 -131
  81. package/dist/packem_shared/discoverInserts-C7zxXkUf.mjs +0 -61
  82. package/dist/packem_shared/discoverMaskProcedures-Cm81kwrb.mjs +0 -217
  83. package/dist/packem_shared/discoverMigrations-Bi5nJ0mJ.mjs +0 -97
  84. package/dist/packem_shared/discoverNondeterministicCalls-C4M8AXmQ.mjs +0 -122
  85. package/dist/packem_shared/discoverQueries-B0wGT-xe.mjs +0 -62
  86. package/dist/packem_shared/discoverR2sqlCalls-BVNMd428.mjs +0 -80
  87. package/dist/packem_shared/discoverRlsMetadata-BS9GOGC5.mjs +0 -280
  88. package/dist/packem_shared/discoverSchema-BnWHHJ4T.mjs +0 -822
  89. package/dist/packem_shared/discoverStorageRulesMetadata-Da8BKXcI.mjs +0 -97
  90. package/dist/packem_shared/emitApp-CV81L74c.mjs +0 -659
  91. package/dist/packem_shared/formatAdvisories-LM8-Ixjw.mjs +0 -75
  92. package/dist/packem_shared/parse-validator-Cabb60UV.mjs +0 -133
  93. package/dist/packem_shared/paths-BRd6JHuF.mjs +0 -11
  94. package/dist/packem_shared/schemaFromIr-DTYsLBaA.mjs +0 -57
@@ -0,0 +1 @@
1
+ import{Node as o}from"ts-morph";import{listLunoraSourceFiles as d,lunoraRelativePath as x,stringPropertyOf as c}from"./listLunoraSourceFiles-D4xthUf-.mjs";import{e as h}from"./exported-procedure-chains-C9FHwphy.mjs";import{r as A}from"./chain-Dp_6wzI9.mjs";const L=new Set(["delete","insert","read","update"]),P=(e,t,i)=>{const r=e.getArguments()[0];if(!r||!o.isArrayLiteralExpression(r))return[];const s=[];for(const n of r.getElements()){if(!o.isObjectLiteralExpression(n))continue;const a=c(n,"on");a===void 0||!L.has(a)||s.push({file:t,on:a,procedure:i,table:c(n,"table")??""})}return s},u=e=>{if(!o.isObjectLiteralExpression(e))return[];const t=e.getProperty("permissions");if(!t||!o.isPropertyAssignment(t))return[];const i=t.getInitializer();if(!i||!o.isArrayLiteralExpression(i))return[];const r=[];for(const s of i.getElements()){if(o.isStringLiteral(s)){r.push(s.getLiteralText());continue}if(o.isCallExpression(s)){const n=s.getArguments()[0];n&&o.isStringLiteral(n)&&r.push(n.getLiteralText())}}return r},f=(e,t)=>t===void 0?e:{...e,description:t},E=e=>{if(o.isCallExpression(e)){const t=e.getArguments()[0];if(!t||!o.isStringLiteral(t))return;const i=e.getArguments()[1],r=i?c(i,"description"):void 0,s=i?u(i):[];return f({name:t.getLiteralText(),permissions:s},r)}if(o.isObjectLiteralExpression(e)){const t=c(e,"name");return t===void 0?void 0:f({name:t,permissions:u(e)},c(e,"description"))}},v=e=>{const t=e.getArguments()[1];if(!t||!o.isObjectLiteralExpression(t))return[];const i=t.getProperty("roles");if(!i||!o.isPropertyAssignment(i))return[];const r=i.getInitializer();if(!r||!o.isArrayLiteralExpression(r))return[];const s=[];for(const n of r.getElements()){const a=E(n);a&&s.push(a)}return s},y=(e,t,i)=>{const r=A(e);return{policies:r.flatMap(s=>P(s,t,i)),roles:r.flatMap(s=>v(s))}},F=(e,t)=>{const i=[],r=new Map;for(const s of d(t)){const n=e.getSourceFile(s)??e.addSourceFileAtPath(s),a=x(t,s);for(const{name:m,receiver:g}of h(n)){const p=y(g,a,m);i.push(...p.policies);for(const l of p.roles)r.has(l.name)||r.set(l.name,l)}}return{policies:i,roles:[...r.values()]}};export{F as default};
@@ -0,0 +1 @@
1
+ import{Node as o}from"ts-morph";import{listLunoraSourceFiles as f,lunoraRelativePath as u,tablesAccessedIn as d}from"./listLunoraSourceFiles-D4xthUf-.mjs";import{c as p}from"./classify-procedure-call-C8jbo0IJ.mjs";import{r as m}from"./chain-Dp_6wzI9.mjs";const b=e=>{const t=e.getArguments()[0];if(!t||!o.isArrayLiteralExpression(t))return[];const s=[];for(const r of t.getElements()){if(!o.isObjectLiteralExpression(r))continue;const i=r.getProperty("table");if(!i||!o.isPropertyAssignment(i))continue;const n=i.getInitializer();n&&o.isStringLiteral(n)&&s.push(n.getLiteralText())}return s},g=e=>{const t=m(e);return{rlsTables:t.flatMap(s=>b(s)),usesRls:t.length>0}},h=new Set(["findFirst","findFirstOrThrow","findMany","get","query"]),y=new Set(["delete","deleteMany","insert","insertMany","patch","patchMany","replace"]),v=(e,t)=>{if(!o.isVariableDeclaration(e))return;const s=e.getInitializer();if(!s||!o.isCallExpression(s))return;const r=p(s);if(!r)return;const i=r.receiver?g(r.receiver):{rlsTables:[],usesRls:!1},{tablesRead:n,tablesWritten:l}=d(e,h,y);return{exportName:e.getName(),file:t,rlsTables:i.rlsTables,tablesRead:n,tablesWritten:l,usesRls:i.usesRls,visibility:r.visibility}},S=(e,t)=>{const s=[];for(const r of f(t)){const i=e.getSourceFile(r)??e.addSourceFileAtPath(r),n=u(t,r);for(const l of i.getVariableStatements())if(l.isExported())for(const c of l.getDeclarations()){const a=v(c,n);a&&s.push(a)}}return s};export{S as default};
@@ -0,0 +1 @@
1
+ import{listLunoraSourceFiles as l}from"./listLunoraSourceFiles-D4xthUf-.mjs";const i=new Set(["@lunora/agent","@lunora/agent/sandbox"]),a={browserTool:"usesSandboxBrowser",containerTool:"usesSandboxContainer",fsTool:"usesSandboxFs"},u=()=>({usesSandboxBrowser:!1,usesSandboxContainer:!1,usesSandboxFs:!1}),d=o=>{const s=u();if(!i.has(o.getModuleSpecifierValue())||o.isTypeOnly())return s;for(const e of o.getNamedImports()){if(e.isTypeOnly())continue;const n=a[e.getNameNode().getText()];n!==void 0&&(s[n]=!0)}return s},b=(o,s)=>{const e=u();for(const n of l(s)){const c=o.getSourceFile(n)??o.addSourceFileAtPath(n);for(const t of c.getImportDeclarations()){const f=d(t);for(const r of Object.values(a))e[r]||=f[r]}if(Object.values(a).every(t=>e[t]))break}return e};export{b as discoverSandboxUsage};
@@ -0,0 +1 @@
1
+ import{LunoraError as j}from"@lunora/errors";import{Node as s,SyntaxKind as z}from"ts-morph";import{diagnosticAt as f}from"./CodegenDiagnosticError-BUXlG3P-.mjs";import{createRequire as D}from"node:module";import{join as R}from"node:path";import{r as C,a as M}from"./parse-validator-tdRHHDzh.mjs";const F=e=>{switch(typeof e){case"bigint":return e.toString();case"boolean":case"number":return String(e);case"string":return JSON.stringify(e);default:return e===null?"null":void 0}},B=e=>({...e?.defaultValue!==void 0||e?.defaultFn!==void 0||e?.serverDefault!==void 0?{hasDefault:!0}:{},...e?.onUpdateFn===void 0?{}:{hasOnUpdate:!0},notNull:e?.notNull??!0,...e?.unique===!0?{unique:!0}:{}}),p=e=>{const n=e?._meta??{},t=e?.kind??"unknown",i={column:B(n.column),kind:t};switch(t){case"array":case"optional":{i.inner=p(n.inner);break}case"from":break;case"id":{i.tableName=n.tableName;break}case"literal":{i.literalValue=F(n.value);break}case"object":{const r=n.shape??{};i.shape=Object.fromEntries(Object.entries(r).map(([o,a])=>[o,p(a)]));break}case"record":{i.valueType=p(n.valueValidator),n.keyValidator!==void 0&&(i.keyType=p(n.keyValidator));break}case"storage":{typeof n.bucket=="string"&&(i.bucket=n.bucket);break}case"union":{const r=n.members??[];i.members=r.map(o=>p(o));break}}return i},V=e=>e?.kind==="global"?"global":e?.kind==="shardBy"&&typeof e.field=="string"?{field:e.field,kind:"shardBy"}:"root",q=(e,n)=>{const t=[];e.relationMap&&Object.keys(e.relationMap).length>0&&t.push("relations"),e.rankIndexes&&e.rankIndexes.length>0&&t.push("rank indexes"),e.vectorIndexes&&e.vectorIndexes.length>0&&t.push("vector indexes"),t.length>0&&console.warn(`@lunora/codegen: package schema-extension table "${n}" declares ${t.join(" + ")}, which are not yet introspected from a node_modules extension — they will be absent from the generated types.`)},J=(e,n)=>{const t=e.shape??{},i=(e.indexes??[]).map(r=>({fields:r.fields,name:r.name,...r.unique?{unique:!0}:{}}));return q(e,n),{...e.isExternallyManaged?{externallyManaged:!0}:{},geoIndexes:[],indexes:i,name:n,rankIndexes:[],relations:[],searchIndexes:(e.searchIndexes??[]).map(r=>({field:r.field,...r.filterFields?{filterFields:r.filterFields}:{},...r.language===void 0?{}:{language:r.language},...r.staged===void 0?{}:{staged:r.staged},...r.strategy===void 0?{}:{strategy:r.strategy},name:r.name})),shape:Object.fromEntries(Object.entries(t).map(([r,o])=>[r,p(o)])),shardMode:V(e.shardMode),vectorIndexes:[]}},U=e=>{const{key:n,tables:t}=e;if(typeof n!="string"||typeof t!="object"||t===null)return;const i=Object.entries(t).map(([a,l])=>J(l,a)),r=e.vectorIndexes??{},o=Object.entries(r??{}).map(([a,l])=>({...l.dimensions===void 0?{}:{dimensions:l.dimensions},...l.field===void 0?{}:{field:l.field},...l.metadata===void 0?{}:{metadata:l.metadata},...l.metric===void 0?{}:{metric:l.metric},name:a,table:l.table??""}));return{bareTables:i,bareVectorIndexes:o,key:n}},K=e=>{if(s.isIdentifier(e))return e.getText();if(s.isPropertyAccessExpression(e)){const n=e.getExpression();return s.isIdentifier(n)?n.getText():void 0}},Z=e=>{const n=K(e);if(n===void 0)return;const t=s.isPropertyAccessExpression(e)?e.getName():void 0,i=t===void 0?[]:[t];for(const r of e.getSourceFile().getImportDeclarations()){const o=r.getModuleSpecifierValue();for(const a of r.getNamedImports())if((a.getAliasNode()?.getText()??a.getName())===n)return{importedName:a.getName(),moduleSpecifier:o,propertyPath:i};if(r.getDefaultImport()?.getText()===n)return{importedName:"default",moduleSpecifier:o,propertyPath:i};if(r.getNamespaceImport()?.getText()===n&&t!==void 0)return{importedName:t,moduleSpecifier:o,propertyPath:[]}}},G=(e,n)=>{let t=e[n.importedName];for(const i of n.propertyPath){if(typeof t!="object"||t===null)return;t=t[i]}return t},Q=(e,n)=>{const t=Z(e);if(t)try{const i=D(R(n,"noop.cjs")),r=i.resolve(t.moduleSpecifier),o=i(r),a=G(o,t);return typeof a!="object"||a===null?void 0:U(a)}catch{return}},W=new Set(["cosine","dot-product","euclidean"]),w=e=>e.replaceAll(/^["']|["']$/gu,""),g=(e,n)=>{const t=e.getProperty(n);if(t&&s.isPropertyAssignment(t))return t.getInitializer()},d=(e,n)=>{const t=g(e,n);if(t&&s.isStringLiteral(t))return t.getLiteralText()},m=(e,n)=>{const t=g(e,n);if(t&&s.isNumericLiteral(t))return Number(t.getLiteralText())},X=(e,n)=>{const t=g(e,n);if(t&&(s.isTrueLiteral(t)||s.isFalseLiteral(t)))return t.getLiteralValue()},x=(e,n)=>{const t=g(e,n);if(t&&s.isArrayLiteralExpression(t))return t.getElements().filter(i=>s.isStringLiteral(i)).map(i=>i.getLiteralText())},T=e=>e&&W.has(e)?e:void 0,b=e=>e&&s.isStringLiteral(e)?e.getLiteralText():"_unnamed_",H=(e,n)=>{const t=x(e,n);return t!==void 0&&t.length>0?t:void 0},L=(e,n,t,i,r)=>{let o=e;for(;;){const a=o.getParent();if(!a||!s.isPropertyAccessExpression(a))break;const l=a.getParent();if(!l||!s.isCallExpression(l))break;if(a.getName()===n){const c=l.getArguments()[0];if(!c||!s.isStringLiteral(c))throw f(l,`\`.${n}(...)\` expects a string literal (${r})`);const u=c.getLiteralText();if(!i.has(u))throw f(c,`unknown ${t} ${JSON.stringify(u)} — expected ${r}`);return u}o=l}},Y=new Set(["cascade","restrict","set null"]),S=new Set(["delete","get","insert","normalizeId","patch","query","replace","system"]),ee=new Set(["arguments","await","break","case","catch","class","const","continue","debugger","default","delete","do","else","enum","eval","export","extends","false","finally","for","function","if","implements","import","in","instanceof","interface","let","new","null","package","private","protected","public","return","static","super","switch","this","throw","true","try","typeof","var","void","while","with","yield"]),O=/^[A-Za-z_$][\w$]*$/u,v=(e,n)=>{const t=w(e);if(S.has(t))throw f(n,`table name "${t}" is reserved — it collides with a \`ctx.db\` member (one of ${[...S].map(i=>`"${i}"`).join(", ")}). Rename the table.`);if(ee.has(t))throw f(n,`table name "${t}" is a reserved JavaScript word — codegen interpolates table names into a bare \`const ${t} = sqliteTable(...)\` binding in the generated Drizzle module, which is a syntax error for a keyword. Rename the table.`);if(!O.test(t))throw f(n,`table name ${JSON.stringify(t)} is not a valid JS identifier — table names are used in generated type names (Doc_<name>) and must match [A-Za-z_$][A-Za-z0-9_$]*. Rename the table.`)},te=e=>e&&Y.has(e)?e:void 0,ne=e=>{if(!s.isArrowFunction(e))return;let n=e.getBody();if(s.isParenthesizedExpression(n))n=n.getExpression();else if(s.isBlock(n)){const t=n.getStatements().find(i=>s.isReturnStatement(i));n=t&&s.isReturnStatement(t)?t.getExpression()??n:n}return s.isObjectLiteralExpression(n)?n:void 0},re=e=>{if(!s.isPropertyAssignment(e))return;const n=e.getInitializer();if(!n||!s.isCallExpression(n))return;const t=n.getExpression();if(!s.isPropertyAccessExpression(t))return;const i=t.getName();if(i!=="one"&&i!=="many")return;const[r,o]=n.getArguments();if(!r||!s.isStringLiteral(r))throw f(r??n,`relation target table must be a string literal — codegen resolves relation targets statically. Got ${r?r.getText():"no argument"}.`);const a=r.getLiteralText();let l="_unknown_",c="_id",u;return o&&s.isObjectLiteralExpression(o)&&(l=d(o,"field")??l,c=d(o,"references")??c,u=i==="one"?te(d(o,"onDelete")):void 0),{field:l,kind:i,name:e.getName(),onDelete:u,references:c,table:a}},ie=e=>{const n=ne(e);if(!n)return[];const t=[];for(const i of n.getProperties()){const r=re(i);r&&t.push(r)}return t},se=(e,n,t)=>{if(!n.includes("."))return;const[i="",...r]=n.split("."),o=`${i}${r.map(a=>a.charAt(0).toUpperCase()+a.slice(1)).join("")}`;throw f(e,`index "${t}" indexes the nested path ${JSON.stringify(n)} — Lunora indexes only top-level columns. Denormalise the value into its own column (e.g. \`${o}\`), index that instead, and keep it in sync with a table \`.triggers()\` beforeInsert/beforeUpdate.`)},oe=e=>{if(!e||!s.isObjectLiteralExpression(e))return!1;const n=e.getProperty("unique");if(!n||!s.isPropertyAssignment(n))return!1;const t=n.getInitializer();if(t===void 0)return!1;if(!s.isTrueLiteral(t)&&!s.isFalseLiteral(t))throw f(t,`\`unique\` must be a literal \`true\` or \`false\`, got ${JSON.stringify(t.getText())}`);return s.isTrueLiteral(t)},ae=e=>{const[n,t,i]=e,r=oe(i),o=b(n),a=t&&s.isArrayLiteralExpression(t)?t.getElements():[];for(const c of a)if(!s.isStringLiteral(c))throw f(c,`index "${o}" lists a non-literal field (${JSON.stringify(c.getText())}); codegen reads index fields statically, so every entry must be a string literal.`);const l=a.filter(c=>s.isStringLiteral(c));for(const c of l)se(c,c.getLiteralText(),o);return{fields:l.map(c=>c.getLiteralText()),name:o,unique:r}},le=e=>{const[n,t]=e;let i="_unknown_",r,o,a,l;return t&&s.isObjectLiteralExpression(t)&&(i=d(t,"field")??i,r=x(t,"filterFields"),o=d(t,"language"),a=X(t,"staged"),l=d(t,"strategy")),{field:i,filterFields:r,language:o,name:b(n),staged:a,strategy:l}},ce=e=>{const[n,t]=e;let i="_unknown_",r;return t&&s.isObjectLiteralExpression(t)&&(i=d(t,"field")??i,r=m(t,"precision")),{field:i,name:b(n),precision:r}},de=e=>{const[n,t]=e,i=n&&s.isStringLiteral(n)?n.getLiteralText():"_unknown_";let r;return t&&s.isObjectLiteralExpression(t)&&(r=m(t,"after")),{after:r,field:i}},fe=e=>{if(!s.isObjectLiteralExpression(e))return;const n=d(e,"field");return n===void 0?void 0:{direction:d(e,"direction")==="desc"?"desc":"asc",field:n}},ue=e=>{const[n,t]=e;let i=[],r;if(t&&s.isObjectLiteralExpression(t)){const o=t.getProperty("sortBy");if(o&&s.isPropertyAssignment(o)){const a=o.getInitializer();a&&s.isArrayLiteralExpression(a)&&(i=a.getElements().map(l=>fe(l)).filter(l=>l!==void 0))}r=x(t,"partitionBy")}return{name:b(n),partitionBy:r,sortBy:i}},pe=(e,n)=>{const[t,i]=e,r=t&&s.isStringLiteral(t)?t.getLiteralText():"_unknown_";if(!(!i||!s.isObjectLiteralExpression(i)))return{dimensions:m(i,"dimensions"),field:r,metadata:x(i,"metadata"),metric:T(d(i,"metric")),name:d(i,"index")??"_unnamed_",table:n}},ge=e=>{const n=e[0];return n&&s.isObjectLiteralExpression(n)&&d(n,"backend")==="hyperdrive"?"hyperdrive":"d1"},me=e=>{if(e&&s.isObjectLiteralExpression(e)){const n=e.getProperty("field");if(n&&s.isPropertyAssignment(n)){const t=n.getInitializer();if(t&&s.isStringLiteral(t))return t.getLiteralText()}}return"deletedAt"},xe=e=>{const n=e[0];return!n||!s.isObjectLiteralExpression(n)?{binding:"",hasTenantBy:!1,unanalyzable:!0}:{binding:d(n,"binding")??"",columns:H(n,"columns"),hasReconcile:n.getProperty("reconcileEveryMs")!==void 0,hasSoftDelete:n.getProperty("softDeleteColumn")!==void 0,hasTenantBy:n.getProperty("tenantBy")!==void 0,idColumn:d(n,"idColumn"),mode:d(n,"mode"),query:d(n,"query")}},be=(e,n,t,i)=>{switch(n){case"commitOrdered":{e.commitOrdered=!0;break}case"externallyManaged":{e.externallyManaged=!0;break}case"geoIndex":{e.geoIndexes.push(ce(t));break}case"global":{e.shardMode="global",e.globalBackend=ge(t);break}case"index":{e.indexes.push(ae(t));break}case"memory":{e.memory=!0;break}case"public":{e.isPublic=!0;break}case"rankIndex":{e.rankIndexes.push(ue(t));break}case"relations":{const r=t[0];r&&e.relations.push(...ie(r));break}case"searchIndex":{e.searchIndexes.push(le(t));break}case"shardBy":{const r=t[0];e.shardMode={field:r&&s.isStringLiteral(r)?r.getLiteralText():"_unknown_",kind:"shardBy"};break}case"softDelete":{e.softDelete={field:me(t[0])};break}case"source":{e.externalSource=xe(t),e.externallyManaged=!0;break}case"ttl":{e.ttl=de(t);break}case"vectorize":{const r=pe(t,i);r&&e.vectorIndexes.push(r);break}}},k=["__vocab","_config","_content","_data","_docsize","_idx"],he=(e,n,t)=>{const i=new Set(t.map(r=>r.name));for(const r of t)for(const o of k){const a=`${r.name}${o}`;if(!i.has(a))continue;const l=o==="__vocab"?`the \`fts5vocab\` companion for "${a}" is created \`IF NOT EXISTS\`, so it silently resolves to "${r.name}"'s — the second index then reads the wrong vocabulary and returns wrong results, with no error`:`creating "${a}" is rejected (\`object name reserved for internal use: SQLITE_ERROR\`), which aborts the shard migration and leaves every sharded table unreadable`;throw f(e,`table "${n}" declares search indexes "${r.name}" and "${a}", whose generated FTS5 tables collide: "${r.name}" already owns "${n}__fts_${a}" as its "${o}" companion, so ${l}. Rename one of them so neither ends with another's name plus ${k.join(", ")}.`)}},P=(e,n)=>{const t={commitOrdered:!1,externallyManaged:!1,geoIndexes:[],indexes:[],isPublic:!1,memory:!1,rankIndexes:[],relations:[],searchIndexes:[],shardMode:"root",vectorIndexes:[]};let i={},r=e;for(;s.isCallExpression(r);){const o=r.getExpression(),a=r.getArguments();if(s.isPropertyAccessExpression(o))be(t,o.getName(),a,n),r=o.getExpression();else if(s.isIdentifier(o)&&o.getText()==="defineTable"){const l=a[0];if(l&&s.isObjectLiteralExpression(l))i=M(l);else if(l)throw f(l,`table "${n}" calls defineTable(${JSON.stringify(l.getText())}), but codegen reads the field map syntactically and can only read an object literal. Inline the fields into the defineTable(...) call. To reuse them in an \`.input()\`, derive from the generated \`Doc_${n}\` / \`Insert_${n}\` type instead of sharing the runtime value.`);break}else break}return t.softDelete&&!(t.softDelete.field in i)&&(i={...i,[t.softDelete.field]:{inner:{kind:"number"},kind:"optional"}}),he(e,n,t.searchIndexes),{commitOrdered:t.commitOrdered,externallyManaged:t.externallyManaged,externalSource:t.externalSource,geoIndexes:t.geoIndexes,globalBackend:t.shardMode==="global"?t.globalBackend??"d1":void 0,indexes:t.indexes,isPublic:t.isPublic,memory:t.memory,name:n,rankIndexes:t.rankIndexes,relations:t.relations,searchIndexes:t.searchIndexes,shape:i,shardMode:t.shardMode,softDelete:t.softDelete,ttl:t.ttl,vectorIndexes:t.vectorIndexes}},N=(e,n=[],t=new Set,i=new Set)=>{i.add(e);for(const r of e.getProperties()){if(s.isSpreadAssignment(r)){const a=C(r.getExpression());a!==void 0&&!i.has(a)&&N(a,n,t,i);continue}if(!s.isPropertyAssignment(r))continue;const o=r.getInitializer();if(o){const a=r.getName();v(a,r.getNameNode());const l=w(a);if(t.has(l))throw f(r.getNameNode(),`defineSchema({...}): table "${l}" is declared more than once — the earlier declaration would be silently discarded. Remove the duplicate.`);t.add(l),n.push(P(o,a))}}return n},ye=e=>{const n=e.getProperty("source");if(n&&s.isPropertyAssignment(n)){const t=n.getInitializer();if(t&&s.isObjectLiteralExpression(t))return d(t,"table")??"_unknown_"}return"_unknown_"},ve=e=>{if(!s.isPropertyAssignment(e))return;const n=e.getInitializer();if(!n||!s.isCallExpression(n))return;const t=n.getExpression();if(!s.isIdentifier(t)||t.getText()!=="defineVectorIndex")return;const i=n.getArguments()[0];if(!i||!s.isObjectLiteralExpression(i))return;const r=e.getNameNode(),o=s.isStringLiteral(r)?r.getLiteralText():r.getText();return{dimensions:m(i,"dimensions"),metric:T(d(i,"metric")),name:o,table:ye(i)}},A=e=>{const n=[];for(const t of e.getProperties()){const i=ve(t);i&&n.push(i)}return n},E=(e,n)=>`${e}_${n}`,Ee=(e,n)=>{if(!O.test(e))throw f(n,`defineSchemaExtension key ${JSON.stringify(e)} is not a valid JS identifier — it is prefixed onto every table the extension contributes (\`${e}_<table>\`), and table names are used in generated type names (Doc_<name>) and must match [A-Za-z_$][A-Za-z0-9_$]*. Rename the extension key.`)},y=(e,n,t)=>t.has(e)?E(n,e):e,Ie=(e,n,t)=>{const i=E(n,e.name);return{...e,extensionKey:n,name:i,relations:e.relations.map(r=>({...r,table:y(r.table,n,t)})),vectorIndexes:e.vectorIndexes.map(r=>({...r,table:y(r.table,n,t)}))}},Se=e=>{if(s.isVariableDeclaration(e)||s.isPropertyAssignment(e))return e.getInitializer();if(s.isShorthandPropertyAssignment(e))return e.getNameNode()},ke=e=>{if(!s.isCallExpression(e))return!1;const n=e.getExpression();return s.isIdentifier(n)&&n.getText()==="defineSchemaExtension"},we=e=>{if(s.isIdentifier(e))return e;if(s.isPropertyAccessExpression(e)){const n=e.getExpression();return s.isIdentifier(n)?n:void 0}},Te=e=>{if(s.isObjectLiteralExpression(e))return e;if(s.isCallExpression(e)){const n=e.getExpression();let t;if(s.isIdentifier(n)?t=n.getText():s.isPropertyAccessExpression(n)&&(t=n.getName()),t==="definePlugin"){const i=e.getArguments()[1];return i&&s.isObjectLiteralExpression(i)?i:void 0}}},Le=(e,n)=>{const t=Se(e);if(t){if(s.isPropertyAccessExpression(n)){const i=Te(t);return i?g(i,n.getName()):void 0}return t}},Oe=e=>{let n=e,t=0;for(;n&&t<32;){if(t+=1,ke(n))return n;const i=we(n),r=i&&s.isIdentifier(i)?i.getSymbol():void 0,a=(r?.getAliasedSymbol()?.getDeclarations()??r?.getDeclarations()??[])[0];if(!a)return;const l=a.getSourceFile();if(l.isInNodeModules()||l.isDeclarationFile())return;n=Le(a,n)}},Pe=e=>{const[n,t]=e.getArguments();if(!(!n||!s.isStringLiteral(n))&&!(!t||!s.isObjectLiteralExpression(t)))return{key:n.getLiteralText(),keyNode:n,options:t}},Ne=e=>{const n=e.getProperty("tables");if(!n||!s.isPropertyAssignment(n))return[];const t=n.getInitializer();if(!t||!s.isObjectLiteralExpression(t))return[];const i=[];for(const r of t.getProperties()){if(!s.isPropertyAssignment(r))continue;const o=r.getInitializer();if(!o)continue;const a=r.getName();v(a,r.getNameNode()),i.push(P(o,a))}return i},Ae=e=>{const n=e.getProperty("vectorIndexes");if(!n||!s.isPropertyAssignment(n))return[];const t=n.getInitializer();return!t||!s.isObjectLiteralExpression(t)?[]:A(t)},_=(e,n,t,i)=>{Ee(e,i);const r=new Set(n.map(l=>l.name)),o=n.map(l=>Ie(l,e,r)),a=t.map(l=>({...l,name:E(e,l.name),table:y(l.table,e,r)}));return{tables:o,vectorIndexes:a}},_e=(e,n,t)=>_(e,Ne(t),Ae(t),n),$e=(e,n)=>{const t=e.getArguments()[0];if(!t)return;const i=Oe(t);if(!i){if(n!==void 0){const o=Q(t,n);if(o){for(const a of o.bareTables)v(a.name,t);return _(o.key,o.bareTables,o.bareVectorIndexes,t)}}console.warn(`@lunora/codegen: skipping \`.extend(${t.getText()})\` — its \`defineSchemaExtension(...)\` could not be resolved from local sources, and ${n===void 0?"no project root was available to resolve the package":"the package could not be imported/introspected"}. Extension tables will be absent from the generated types.`);return}const r=Pe(i);if(!r){console.warn("@lunora/codegen: skipping `.extend(...)` — `defineSchemaExtension` requires a string `key` and an options object literal.");return}return _e(r.key,r.keyNode,r.options)},je=e=>{const n=[];let t=e;for(;;){const i=t.getParent();if(!i||!s.isPropertyAccessExpression(i))break;const r=i.getParent();if(!r||!s.isCallExpression(r))break;i.getName()==="extend"&&n.push(r),t=r}return n},ze=(e,n,t)=>{const i=new Set(n.map(o=>o.name)),r=[];for(const o of je(e)){const a=$e(o,t);if(a){for(const l of a.tables){if(i.has(l.name))throw f(o,`defineSchema(...).extend(...): table "${l.name}" already exists — another extension with the same key already contributed it.`);i.add(l.name),n.push(l)}r.push(...a.vectorIndexes)}}return r},De=new Set(["eu","fedramp","us"]),Re=e=>L(e,"jurisdiction","jurisdiction",De,'"eu", "us", or "fedramp"'),Ce=new Set(["required"]),Me=e=>L(e,"rls","rls mode",Ce,'"required"'),Ke=(e,n,t)=>{const r=e.addSourceFileAtPath(n).getDescendantsOfKind(z.CallExpression).find(h=>{const I=h.getExpression();return s.isIdentifier(I)&&I.getText()==="defineSchema"});if(!r)throw new j("INTERNAL",`defineSchema() not found in ${n}`);const o=r.getArguments()[0];if(!o||!s.isObjectLiteralExpression(o))throw f(r,"defineSchema() expects an object literal");const a=N(o),l=r.getArguments()[1],c=l&&s.isObjectLiteralExpression(l)?A(l):[],u=ze(r,a,t),$=[...a.flatMap(h=>h.vectorIndexes),...c,...u];return{jurisdiction:Re(r),rlsMode:Me(r),tables:a,vectorIndexes:$}};export{Ke as default};
@@ -0,0 +1 @@
1
+ import{Node as u}from"ts-morph";import{listLunoraSourceFiles as f,lunoraRelativePath as p,stringPropertyOf as l}from"./listLunoraSourceFiles-D4xthUf-.mjs";import{w as d}from"./classify-procedure-call-C8jbo0IJ.mjs";import{e as m}from"./exported-procedure-chains-C9FHwphy.mjs";const h=new Set(["delete","list","read","write"]),g=t=>d(t,"use","storageRules"),x=(t,s,n)=>{const e=t.getArguments()[0];if(!e||!u.isArrayLiteralExpression(e))return[];const i=[];for(const o of e.getElements()){if(!u.isObjectLiteralExpression(o))continue;const r=l(o,"on");if(r===void 0||!h.has(r))continue;const a=l(o,"prefix"),c={bucket:l(o,"bucket")??"",file:s,on:r,procedure:n};a!==void 0&&(c.prefix=a),i.push(c)}return i},A=(t,s)=>{const n=[];for(const e of f(s)){const i=t.getSourceFile(e)??t.addSourceFileAtPath(e),o=p(s,e);for(const{name:r,receiver:a}of m(i))for(const c of g(a))n.push(...x(c,o,r))}return{rules:n}};export{A as default};