@lunora/codegen 1.0.0-alpha.10 → 1.0.0-alpha.101

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 (76) hide show
  1. package/LICENSE.md +6 -0
  2. package/dist/index.d.mts +2730 -947
  3. package/dist/index.d.ts +2730 -947
  4. package/dist/index.mjs +1 -29
  5. package/dist/packem_shared/AGENTS_FILENAME-Dk-k250h.mjs +1 -0
  6. package/dist/packem_shared/CONTAINERS_FILENAME-DQpyhY6c.mjs +1 -0
  7. package/dist/packem_shared/CodegenDiagnosticError-DPezpZTz.mjs +1 -0
  8. package/dist/packem_shared/DEFAULT_TARGET-CH5disA3.mjs +1 -0
  9. package/dist/packem_shared/FLAGS_FILENAME-Boqm0YCP.mjs +1 -0
  10. package/dist/packem_shared/GENERATED_HEADER-C5Dapn_Y.mjs +1 -0
  11. package/dist/packem_shared/LUNORA_ERROR_CODES-zfAI2OFs.mjs +1 -0
  12. package/dist/packem_shared/MUTATORS_FILENAME-Cp_JxLSz.mjs +1 -0
  13. package/dist/packem_shared/NOTIFY_FILENAME-DwyJ5I4Q.mjs +1 -0
  14. package/dist/packem_shared/OPENRPC_VERSION-DkNdVeNM.mjs +3 -0
  15. package/dist/packem_shared/QUEUES_FILENAME-QpMZsde0.mjs +1 -0
  16. package/dist/packem_shared/SCHEMA_SNAPSHOT_FILENAME-hQUs7FJj.mjs +9 -0
  17. package/dist/packem_shared/SCHEMA_SNAPSHOT_VERSION-CFhF_hmg.mjs +2 -0
  18. package/dist/packem_shared/SHAPES_FILENAME-WxXOsYfe.mjs +1 -0
  19. package/dist/packem_shared/SchemaSnapshotParseError-BkgxjBPQ.mjs +16 -0
  20. package/dist/packem_shared/WORKFLOWS_FILENAME-Dz80zqeT.mjs +1 -0
  21. package/dist/packem_shared/buildOpenApiDocument-DcpvOBMi.mjs +3 -0
  22. package/dist/packem_shared/describeErrorLevelFindings-IhNx9gPr.mjs +1 -0
  23. package/dist/packem_shared/discover-ast-7ABwvTVn.mjs +1 -0
  24. package/dist/packem_shared/discover-queries-i4Vd2si7.mjs +1 -0
  25. package/dist/packem_shared/discoverAuthApiCalls-XfNbxqlq.mjs +1 -0
  26. package/dist/packem_shared/discoverCrons-RlATa0_t.mjs +1 -0
  27. package/dist/packem_shared/discoverFunctions-4TNKUv7P.mjs +1 -0
  28. package/dist/packem_shared/discoverHttpRoutes-DQivLBqT.mjs +1 -0
  29. package/dist/packem_shared/discoverInserts-D6szWzrV.mjs +1 -0
  30. package/dist/packem_shared/discoverMaskProcedures-BBPqLXA5.mjs +1 -0
  31. package/dist/packem_shared/discoverMigrations-rbjxflR8.mjs +1 -0
  32. package/dist/packem_shared/discoverNondeterministicCalls-Bxr652Rz.mjs +1 -0
  33. package/dist/packem_shared/discoverQueries-uFtLDmn_.mjs +1 -0
  34. package/dist/packem_shared/discoverR2sqlCalls-DMxG6gHr.mjs +1 -0
  35. package/dist/packem_shared/discoverRlsMetadata-B63ARnKG.mjs +1 -0
  36. package/dist/packem_shared/discoverSandboxUsage-DBM98vVh.mjs +1 -0
  37. package/dist/packem_shared/discoverSchema-BNr7PVcZ.mjs +1 -0
  38. package/dist/packem_shared/discoverStorageRulesMetadata-_40MGqRX.mjs +1 -0
  39. package/dist/packem_shared/emit-B8FxCABN.mjs +2054 -0
  40. package/dist/packem_shared/emitApp-Dg-jOuj6.mjs +526 -0
  41. package/dist/packem_shared/formatAdvisories-CQvIhQ04.mjs +2 -0
  42. package/dist/packem_shared/module-specifiers-8FEEiUcv.mjs +1 -0
  43. package/dist/packem_shared/parse-validator-CusS8QwU.mjs +1 -0
  44. package/dist/packem_shared/paths-BmX5O1sG.mjs +1 -0
  45. package/dist/packem_shared/readPackageDependencies-CogJX1Ia.mjs +1 -0
  46. package/dist/packem_shared/redact-6jD4lAhq.mjs +1 -0
  47. package/dist/packem_shared/schemaFromIr-R1ZFzVyy.mjs +1 -0
  48. package/package.json +12 -7
  49. package/dist/packem_shared/CONTAINERS_FILENAME-DlP6YM_Q.mjs +0 -224
  50. package/dist/packem_shared/CodegenDiagnosticError-DeblMkzO.mjs +0 -23
  51. package/dist/packem_shared/GENERATED_HEADER-BiFXNUvo.mjs +0 -2692
  52. package/dist/packem_shared/LUNORA_ERROR_CODES-CySpQPD3.mjs +0 -61
  53. package/dist/packem_shared/OPENRPC_VERSION-B4i9Qp3v.mjs +0 -60
  54. package/dist/packem_shared/SCHEMA_SNAPSHOT_FILENAME-Bvv7qa_u.mjs +0 -939
  55. package/dist/packem_shared/SCHEMA_SNAPSHOT_VERSION-wWGP2_5E.mjs +0 -245
  56. package/dist/packem_shared/WORKFLOWS_FILENAME-D62dcBGg.mjs +0 -84
  57. package/dist/packem_shared/buildOpenApiDocument-Cu3mZl-8.mjs +0 -183
  58. package/dist/packem_shared/discover-ast-CT6BgBr4.mjs +0 -13
  59. package/dist/packem_shared/discoverAuthApiCalls-CoirYbg6.mjs +0 -62
  60. package/dist/packem_shared/discoverCrons-Cev7RRAf.mjs +0 -257
  61. package/dist/packem_shared/discoverFunctions-BWMczzBx.mjs +0 -465
  62. package/dist/packem_shared/discoverHttpRoutes-CfP6cMzt.mjs +0 -131
  63. package/dist/packem_shared/discoverInserts-C7zxXkUf.mjs +0 -61
  64. package/dist/packem_shared/discoverMaskProcedures-Cm81kwrb.mjs +0 -217
  65. package/dist/packem_shared/discoverMigrations-Bi5nJ0mJ.mjs +0 -97
  66. package/dist/packem_shared/discoverNondeterministicCalls-C4M8AXmQ.mjs +0 -122
  67. package/dist/packem_shared/discoverQueries-B0wGT-xe.mjs +0 -62
  68. package/dist/packem_shared/discoverR2sqlCalls-BVNMd428.mjs +0 -80
  69. package/dist/packem_shared/discoverRlsMetadata-BS9GOGC5.mjs +0 -280
  70. package/dist/packem_shared/discoverSchema-BnWHHJ4T.mjs +0 -822
  71. package/dist/packem_shared/discoverStorageRulesMetadata-Da8BKXcI.mjs +0 -97
  72. package/dist/packem_shared/emitApp-KfMaKXWe.mjs +0 -603
  73. package/dist/packem_shared/formatAdvisories-8NIv1k0I.mjs +0 -69
  74. package/dist/packem_shared/parse-validator-Cabb60UV.mjs +0 -133
  75. package/dist/packem_shared/paths-BRd6JHuF.mjs +0 -11
  76. package/dist/packem_shared/schemaFromIr-DTYsLBaA.mjs +0 -57
@@ -0,0 +1 @@
1
+ import{LunoraError as T}from"@lunora/errors";import{Node as s,SyntaxKind as j}from"ts-morph";import{diagnosticAt as u}from"./CodegenDiagnosticError-DPezpZTz.mjs";import{g as A}from"./parse-validator-CusS8QwU.mjs";import{createRequire as _}from"node:module";import{join as N}from"node:path";const O=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}},D=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}:{}}),g=e=>{const r=e?._meta??{},t=e?.kind??"unknown",n={column:D(r.column),kind:t};switch(t){case"array":case"optional":{n.inner=g(r.inner);break}case"from":break;case"id":{n.tableName=r.tableName;break}case"literal":{n.literalValue=O(r.value);break}case"object":{const i=r.shape??{};n.shape=Object.fromEntries(Object.entries(i).map(([a,o])=>[a,g(o)]));break}case"record":{n.valueType=g(r.valueValidator),r.keyValidator!==void 0&&(n.keyType=g(r.keyValidator));break}case"storage":{typeof r.bucket=="string"&&(n.bucket=r.bucket);break}case"union":{const i=r.members??[];n.members=i.map(a=>g(a));break}}return n},M=e=>e?.kind==="global"?"global":e?.kind==="shardBy"&&typeof e.field=="string"?{field:e.field,kind:"shardBy"}:"root",z=(e,r)=>{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 "${r}" declares ${t.join(" + ")}, which are not yet introspected from a node_modules extension — they will be absent from the generated types.`)},B=(e,r)=>{const t=e.shape??{},n=(e.indexes??[]).map(i=>({fields:i.fields,name:i.name,...i.unique?{unique:!0}:{}}));return z(e,r),{...e.isExternallyManaged?{externallyManaged:!0}:{},geoIndexes:[],indexes:n,name:r,rankIndexes:[],relations:[],searchIndexes:(e.searchIndexes??[]).map(i=>({field:i.field,...i.filterFields?{filterFields:i.filterFields}:{},...i.language===void 0?{}:{language:i.language},...i.staged===void 0?{}:{staged:i.staged},...i.strategy===void 0?{}:{strategy:i.strategy},name:i.name})),shape:Object.fromEntries(Object.entries(t).map(([i,a])=>[i,g(a)])),shardMode:M(e.shardMode),vectorIndexes:[]}},F=e=>{const{key:r,tables:t}=e;if(typeof r!="string"||typeof t!="object"||t===null)return;const n=Object.entries(t).map(([o,l])=>B(l,o)),i=e.vectorIndexes??{},a=Object.entries(i??{}).map(([o,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:o,table:l.table??""}));return{bareTables:n,bareVectorIndexes:a,key:r}},q=e=>{if(s.isIdentifier(e))return e.getText();if(s.isPropertyAccessExpression(e)){const r=e.getExpression();return s.isIdentifier(r)?r.getText():void 0}},R=e=>{const r=q(e);if(r===void 0)return;const t=s.isPropertyAccessExpression(e)?e.getName():void 0,n=t===void 0?[]:[t];for(const i of e.getSourceFile().getImportDeclarations()){const a=i.getModuleSpecifierValue();for(const o of i.getNamedImports())if((o.getAliasNode()?.getText()??o.getName())===r)return{importedName:o.getName(),moduleSpecifier:a,propertyPath:n};if(i.getDefaultImport()?.getText()===r)return{importedName:"default",moduleSpecifier:a,propertyPath:n};if(i.getNamespaceImport()?.getText()===r&&t!==void 0)return{importedName:t,moduleSpecifier:a,propertyPath:[]}}},C=(e,r)=>{let t=e[r.importedName];for(const n of r.propertyPath){if(typeof t!="object"||t===null)return;t=t[n]}return t},V=(e,r)=>{const t=R(e);if(t)try{const n=_(N(r,"noop.cjs")),i=n.resolve(t.moduleSpecifier),a=n(i),o=C(a,t);return typeof o!="object"||o===null?void 0:F(o)}catch{return}},J=new Set(["cosine","dot-product","euclidean"]),U=new Set(["cascade","restrict","set null"]),I=new Set(["delete","get","insert","normalizeId","patch","query","replace","system"]),K=/^[A-Za-z_$][\w$]*$/u,Z=e=>e.replaceAll(/^["']|["']$/gu,""),Q=(e,r)=>{const t=Z(e);if(I.has(t))throw u(r,`table name "${t}" is reserved — it collides with a \`ctx.db\` member (one of ${[...I].map(n=>`"${n}"`).join(", ")}). Rename the table.`);if(!K.test(t))throw u(r,`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.`)},p=(e,r)=>{const t=e.getProperty(r);if(t&&s.isPropertyAssignment(t))return t.getInitializer()},d=(e,r)=>{const t=p(e,r);if(t&&s.isStringLiteral(t))return t.getLiteralText()},m=(e,r)=>{const t=p(e,r);if(t&&s.isNumericLiteral(t))return Number(t.getLiteralText())},X=(e,r)=>{const t=p(e,r);if(t&&(s.isTrueLiteral(t)||s.isFalseLiteral(t)))return t.getLiteralValue()},x=(e,r)=>{const t=p(e,r);if(t&&s.isArrayLiteralExpression(t))return t.getElements().filter(n=>s.isStringLiteral(n)).map(n=>n.getLiteralText())},L=e=>e&&J.has(e)?e:void 0,G=e=>e&&U.has(e)?e:void 0,H=e=>{if(!s.isArrowFunction(e))return;let r=e.getBody();if(s.isParenthesizedExpression(r))r=r.getExpression();else if(s.isBlock(r)){const t=r.getStatements().find(n=>s.isReturnStatement(n));r=t&&s.isReturnStatement(t)?t.getExpression()??r:r}return s.isObjectLiteralExpression(r)?r:void 0},W=e=>{if(!s.isPropertyAssignment(e))return;const r=e.getInitializer();if(!r||!s.isCallExpression(r))return;const t=r.getExpression();if(!s.isPropertyAccessExpression(t))return;const n=t.getName();if(n!=="one"&&n!=="many")return;const[i,a]=r.getArguments(),o=i&&s.isStringLiteral(i)?i.getLiteralText():"_unknown_";let l="_unknown_",c="_id",f;return a&&s.isObjectLiteralExpression(a)&&(l=d(a,"field")??l,c=d(a,"references")??c,f=n==="one"?G(d(a,"onDelete")):void 0),{field:l,kind:n,name:e.getName(),onDelete:f,references:c,table:o}},Y=e=>{const r=H(e);if(!r)return[];const t=[];for(const n of r.getProperties()){const i=W(n);i&&t.push(i)}return t},b=e=>e&&s.isStringLiteral(e)?e.getLiteralText():"_unnamed_",ee=(e,r,t)=>{if(!r.includes("."))return;const[n="",...i]=r.split("."),a=`${n}${i.map(o=>o.charAt(0).toUpperCase()+o.slice(1)).join("")}`;throw u(e,`index "${t}" indexes the nested path ${JSON.stringify(r)} — Lunora indexes only top-level columns. Denormalise the value into its own column (e.g. \`${a}\`), index that instead, and keep it in sync with a table \`.triggers()\` beforeInsert/beforeUpdate.`)},te=e=>{if(!e||!s.isObjectLiteralExpression(e))return!1;const r=e.getProperty("unique");if(!r||!s.isPropertyAssignment(r))return!1;const t=r.getInitializer();if(t===void 0)return!1;if(!s.isTrueLiteral(t)&&!s.isFalseLiteral(t))throw u(t,`\`unique\` must be a literal \`true\` or \`false\`, got ${JSON.stringify(t.getText())}`);return s.isTrueLiteral(t)},re=e=>{const[r,t,n]=e,i=te(n),a=b(r),o=t&&s.isArrayLiteralExpression(t)?t.getElements():[];for(const c of o)if(!s.isStringLiteral(c))throw u(c,`index "${a}" lists a non-literal field (${JSON.stringify(c.getText())}); codegen reads index fields statically, so every entry must be a string literal.`);const l=o.filter(c=>s.isStringLiteral(c));for(const c of l)ee(c,c.getLiteralText(),a);return{fields:l.map(c=>c.getLiteralText()),name:a,unique:i}},ne=e=>{const[r,t]=e;let n="_unknown_",i,a,o,l;return t&&s.isObjectLiteralExpression(t)&&(n=d(t,"field")??n,i=x(t,"filterFields"),a=d(t,"language"),o=X(t,"staged"),l=d(t,"strategy")),{field:n,filterFields:i,language:a,name:b(r),staged:o,strategy:l}},ie=e=>{const[r,t]=e;let n="_unknown_",i;return t&&s.isObjectLiteralExpression(t)&&(n=d(t,"field")??n,i=m(t,"precision")),{field:n,name:b(r),precision:i}},se=e=>{const[r,t]=e,n=r&&s.isStringLiteral(r)?r.getLiteralText():"_unknown_";let i;return t&&s.isObjectLiteralExpression(t)&&(i=m(t,"after")),{after:i,field:n}},ae=e=>{if(!s.isObjectLiteralExpression(e))return;const r=d(e,"field");return r===void 0?void 0:{direction:d(e,"direction")==="desc"?"desc":"asc",field:r}},oe=e=>{const[r,t]=e;let n=[],i;if(t&&s.isObjectLiteralExpression(t)){const a=t.getProperty("sortBy");if(a&&s.isPropertyAssignment(a)){const o=a.getInitializer();o&&s.isArrayLiteralExpression(o)&&(n=o.getElements().map(l=>ae(l)).filter(l=>l!==void 0))}i=x(t,"partitionBy")}return{name:b(r),partitionBy:i,sortBy:n}},le=(e,r)=>{const[t,n]=e,i=t&&s.isStringLiteral(t)?t.getLiteralText():"_unknown_";if(!(!n||!s.isObjectLiteralExpression(n)))return{dimensions:m(n,"dimensions"),field:i,metadata:x(n,"metadata"),metric:L(d(n,"metric")),name:d(n,"index")??"_unnamed_",table:r}},ce=e=>{const r=e[0];return r&&s.isObjectLiteralExpression(r)&&d(r,"backend")==="hyperdrive"?"hyperdrive":"d1"},de=e=>{if(e&&s.isObjectLiteralExpression(e)){const r=e.getProperty("field");if(r&&s.isPropertyAssignment(r)){const t=r.getInitializer();if(t&&s.isStringLiteral(t))return t.getLiteralText()}}return"deletedAt"},ue=(e,r)=>{const t=x(e,r);return t!==void 0&&t.length>0?t:void 0},fe=e=>{const r=e[0];return!r||!s.isObjectLiteralExpression(r)?{binding:"",hasTenantBy:!1,unanalyzable:!0}:{binding:d(r,"binding")??"",columns:ue(r,"columns"),hasReconcile:r.getProperty("reconcileEveryMs")!==void 0,hasSoftDelete:r.getProperty("softDeleteColumn")!==void 0,hasTenantBy:r.getProperty("tenantBy")!==void 0,idColumn:d(r,"idColumn"),mode:d(r,"mode"),query:d(r,"query")}},ge=(e,r,t,n)=>{switch(r){case"externallyManaged":{e.externallyManaged=!0;break}case"geoIndex":{e.geoIndexes.push(ie(t));break}case"global":{e.shardMode="global",e.globalBackend=ce(t);break}case"index":{e.indexes.push(re(t));break}case"public":{e.isPublic=!0;break}case"rankIndex":{e.rankIndexes.push(oe(t));break}case"relations":{const i=t[0];i&&e.relations.push(...Y(i));break}case"searchIndex":{e.searchIndexes.push(ne(t));break}case"shardBy":{const i=t[0];e.shardMode={field:i&&s.isStringLiteral(i)?i.getLiteralText():"_unknown_",kind:"shardBy"};break}case"softDelete":{e.softDelete={field:de(t[0])};break}case"source":{e.externalSource=fe(t),e.externallyManaged=!0;break}case"ttl":{e.ttl=se(t);break}case"vectorize":{const i=le(t,n);i&&e.vectorIndexes.push(i);break}}},E=["__vocab","_config","_content","_data","_docsize","_idx"],pe=(e,r,t)=>{const n=new Set(t.map(i=>i.name));for(const i of t)for(const a of E){const o=`${i.name}${a}`;if(!n.has(o))continue;const l=a==="__vocab"?`the \`fts5vocab\` companion for "${o}" is created \`IF NOT EXISTS\`, so it silently resolves to "${i.name}"'s — the second index then reads the wrong vocabulary and returns wrong results, with no error`:`creating "${o}" is rejected (\`object name reserved for internal use: SQLITE_ERROR\`), which aborts the shard migration and leaves every sharded table unreadable`;throw u(e,`table "${r}" declares search indexes "${i.name}" and "${o}", whose generated FTS5 tables collide: "${i.name}" already owns "${r}__fts_${o}" as its "${a}" companion, so ${l}. Rename one of them so neither ends with another's name plus ${E.join(", ")}.`)}},S=(e,r)=>{const t={externallyManaged:!1,geoIndexes:[],indexes:[],isPublic:!1,rankIndexes:[],relations:[],searchIndexes:[],shardMode:"root",vectorIndexes:[]};let n={},i=e;for(;s.isCallExpression(i);){const a=i.getExpression(),o=i.getArguments();if(s.isPropertyAccessExpression(a))ge(t,a.getName(),o,r),i=a.getExpression();else if(s.isIdentifier(a)&&a.getText()==="defineTable"){const l=o[0];if(l&&s.isObjectLiteralExpression(l))n=A(l);else if(l)throw u(l,`table "${r}" 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_${r}\` / \`Insert_${r}\` type instead of sharing the runtime value.`);break}else break}return t.softDelete&&!(t.softDelete.field in n)&&(n={...n,[t.softDelete.field]:{inner:{kind:"number"},kind:"optional"}}),pe(e,r,t.searchIndexes),{externallyManaged:t.externallyManaged,externalSource:t.externalSource,geoIndexes:t.geoIndexes,globalBackend:t.shardMode==="global"?t.globalBackend??"d1":void 0,indexes:t.indexes,isPublic:t.isPublic,name:r,rankIndexes:t.rankIndexes,relations:t.relations,searchIndexes:t.searchIndexes,shape:n,shardMode:t.shardMode,softDelete:t.softDelete,ttl:t.ttl,vectorIndexes:t.vectorIndexes}},me=e=>{const r=e.getProperty("source");if(r&&s.isPropertyAssignment(r)){const t=r.getInitializer();if(t&&s.isObjectLiteralExpression(t))return d(t,"table")??"_unknown_"}return"_unknown_"},xe=e=>{if(!s.isPropertyAssignment(e))return;const r=e.getInitializer();if(!r||!s.isCallExpression(r))return;const t=r.getExpression();if(!s.isIdentifier(t)||t.getText()!=="defineVectorIndex")return;const n=r.getArguments()[0];if(!n||!s.isObjectLiteralExpression(n))return;const i=e.getNameNode(),a=s.isStringLiteral(i)?i.getLiteralText():i.getText();return{dimensions:m(n,"dimensions"),metric:L(d(n,"metric")),name:a,table:me(n)}},$=e=>{const r=[];for(const t of e.getProperties()){const n=xe(t);n&&r.push(n)}return r},k=(e,r)=>`${e}_${r}`,y=(e,r,t)=>t.has(e)?k(r,e):e,be=(e,r,t)=>{const n=k(r,e.name);return{...e,extensionKey:r,name:n,relations:e.relations.map(i=>({...i,table:y(i.table,r,t)})),vectorIndexes:e.vectorIndexes.map(i=>({...i,table:y(i.table,r,t)}))}},he=e=>{if(s.isVariableDeclaration(e)||s.isPropertyAssignment(e))return e.getInitializer();if(s.isShorthandPropertyAssignment(e))return e.getNameNode()},ye=e=>{if(!s.isCallExpression(e))return!1;const r=e.getExpression();return s.isIdentifier(r)&&r.getText()==="defineSchemaExtension"},ke=e=>{if(s.isIdentifier(e))return e;if(s.isPropertyAccessExpression(e)){const r=e.getExpression();return s.isIdentifier(r)?r:void 0}},ve=e=>{if(s.isObjectLiteralExpression(e))return e;if(s.isCallExpression(e)){const r=e.getExpression();let t;if(s.isIdentifier(r)?t=r.getText():s.isPropertyAccessExpression(r)&&(t=r.getName()),t==="definePlugin"){const n=e.getArguments()[1];return n&&s.isObjectLiteralExpression(n)?n:void 0}}},Ie=(e,r)=>{const t=he(e);if(t){if(s.isPropertyAccessExpression(r)){const n=ve(t);return n?p(n,r.getName()):void 0}return t}},Ee=e=>{let r=e,t=0;for(;r&&t<32;){if(t+=1,ye(r))return r;const n=ke(r),i=n&&s.isIdentifier(n)?n.getSymbol():void 0,a=(i?.getAliasedSymbol()?.getDeclarations()??i?.getDeclarations()??[])[0];if(!a)return;const o=a.getSourceFile();if(o.isInNodeModules()||o.isDeclarationFile())return;r=Ie(a,r)}},Le=e=>{const[r,t]=e.getArguments();if(!(!r||!s.isStringLiteral(r))&&!(!t||!s.isObjectLiteralExpression(t)))return{key:r.getLiteralText(),options:t}},Se=e=>{const r=e.getProperty("tables");if(!r||!s.isPropertyAssignment(r))return[];const t=r.getInitializer();if(!t||!s.isObjectLiteralExpression(t))return[];const n=[];for(const i of t.getProperties()){if(!s.isPropertyAssignment(i))continue;const a=i.getInitializer();a&&n.push(S(a,i.getName()))}return n},$e=e=>{const r=e.getProperty("vectorIndexes");if(!r||!s.isPropertyAssignment(r))return[];const t=r.getInitializer();return!t||!s.isObjectLiteralExpression(t)?[]:$(t)},w=(e,r,t)=>{const n=new Set(r.map(o=>o.name)),i=r.map(o=>be(o,e,n)),a=t.map(o=>({...o,name:k(e,o.name),table:y(o.table,e,n)}));return{tables:i,vectorIndexes:a}},we=(e,r)=>w(e,Se(r),$e(r)),Pe=(e,r)=>{const t=e.getArguments()[0];if(!t)return;const n=Ee(t);if(!n){if(r!==void 0){const a=V(t,r);if(a)return w(a.key,a.bareTables,a.bareVectorIndexes)}console.warn(`@lunora/codegen: skipping \`.extend(${t.getText()})\` — its \`defineSchemaExtension(...)\` could not be resolved from local sources, and ${r===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 i=Le(n);if(!i){console.warn("@lunora/codegen: skipping `.extend(...)` — `defineSchemaExtension` requires a string `key` and an options object literal.");return}return we(i.key,i.options)},Te=e=>{const r=[];let t=e;for(;;){const n=t.getParent();if(!n||!s.isPropertyAccessExpression(n))break;const i=n.getParent();if(!i||!s.isCallExpression(i))break;n.getName()==="extend"&&r.push(i),t=i}return r},P=(e,r,t,n,i)=>{let a=e;for(;;){const o=a.getParent();if(!o||!s.isPropertyAccessExpression(o))break;const l=o.getParent();if(!l||!s.isCallExpression(l))break;if(o.getName()===r){const c=l.getArguments()[0];if(!c||!s.isStringLiteral(c))throw u(l,`\`.${r}(...)\` expects a string literal (${i})`);const f=c.getLiteralText();if(!n.has(f))throw u(c,`unknown ${t} ${JSON.stringify(f)} — expected ${i}`);return f}a=l}},je=new Set(["eu","fedramp","us"]),Ae=e=>P(e,"jurisdiction","jurisdiction",je,'"eu", "us", or "fedramp"'),_e=new Set(["required"]),Ne=e=>P(e,"rls","rls mode",_e,'"required"'),Oe=e=>{const r=[];for(const t of e.getProperties()){if(!s.isPropertyAssignment(t))continue;const n=t.getInitializer();if(n){const i=t.getName();Q(i,t.getNameNode()),r.push(S(n,i))}}return r},De=(e,r,t)=>{const n=new Set(r.map(a=>a.name)),i=[];for(const a of Te(e)){const o=Pe(a,t);if(o){for(const l of o.tables){if(n.has(l.name))throw u(a,`defineSchema(...).extend(...): table "${l.name}" already exists — another extension with the same key already contributed it.`);n.add(l.name),r.push(l)}i.push(...o.vectorIndexes)}}return i},Ce=(e,r,t)=>{const n=e.addSourceFileAtPath(r).getDescendantsOfKind(j.CallExpression).find(h=>{const v=h.getExpression();return s.isIdentifier(v)&&v.getText()==="defineSchema"});if(!n)throw new T("INTERNAL",`defineSchema() not found in ${r}`);const i=n.getArguments()[0];if(!i||!s.isObjectLiteralExpression(i))throw u(n,"defineSchema() expects an object literal");const a=Oe(i),o=n.getArguments()[1],l=o&&s.isObjectLiteralExpression(o)?$(o):[],c=De(n,a,t),f=[...a.flatMap(h=>h.vectorIndexes),...l,...c];return{jurisdiction:Ae(n),rlsMode:Ne(n),tables:a,vectorIndexes:f}};export{Ce as default};
@@ -0,0 +1 @@
1
+ import{Node as n}from"ts-morph";import{g as u,p,D as l}from"./discover-ast-7ABwvTVn.mjs";import{classifyProcedureCall as g}from"./discoverFunctions-4TNKUv7P.mjs";const m=new Set(["delete","list","read","write"]),x=o=>{if(!n.isCallExpression(o))return!1;const e=o.getExpression();return n.isIdentifier(e)?e.getText()==="storageRules":n.isPropertyAccessExpression(e)&&e.getName()==="storageRules"},d=o=>{const e=[];let s=o;for(;n.isCallExpression(s);){const r=s.getExpression();if(!n.isPropertyAccessExpression(r))break;if(r.getName()==="use"){const t=s.getArguments()[0];t&&x(t)&&e.push(t)}s=r.getExpression()}return e},E=(o,e,s)=>{const r=o.getArguments()[0];if(!r||!n.isArrayLiteralExpression(r))return[];const t=[];for(const i of r.getElements()){if(!n.isObjectLiteralExpression(i))continue;const c=l(i,"on");if(c===void 0||!m.has(c))continue;const a=l(i,"prefix"),f={bucket:l(i,"bucket")??"",file:e,on:c,procedure:s};a!==void 0&&(f.prefix=a),t.push(f)}return t},v=o=>{const e=[];for(const s of o.getVariableStatements())if(s.isExported())for(const r of s.getDeclarations()){const t=r.getInitializer(),i=t&&n.isCallExpression(t)?g(t):void 0;i?.receiver&&e.push({name:r.getName(),receiver:i.receiver})}return e},S=(o,e)=>{const s=[];for(const r of u(e)){const t=o.getSourceFile(r)??o.addSourceFileAtPath(r),i=p(e,r);for(const{name:c,receiver:a}of v(t))for(const f of d(a))s.push(...E(f,i,c))}return{rules:s}};export{S as default};