@lunora/codegen 1.0.0-alpha.9 → 1.0.0-alpha.90
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/LICENSE.md +6 -0
- package/dist/index.d.mts +2722 -931
- package/dist/index.d.ts +2722 -931
- package/dist/index.mjs +1 -29
- package/dist/packem_shared/AGENTS_FILENAME-BesaDYkL.mjs +1 -0
- package/dist/packem_shared/CONTAINERS_FILENAME-CFwdyaCI.mjs +1 -0
- package/dist/packem_shared/CodegenDiagnosticError-DPezpZTz.mjs +1 -0
- package/dist/packem_shared/DEFAULT_TARGET-CH5disA3.mjs +1 -0
- package/dist/packem_shared/FLAGS_FILENAME-CHaH6obH.mjs +1 -0
- package/dist/packem_shared/GENERATED_HEADER-CUBdTCVk.mjs +1 -0
- package/dist/packem_shared/LUNORA_ERROR_CODES-Um9hC1gr.mjs +1 -0
- package/dist/packem_shared/MUTATORS_FILENAME-DY3rgWGl.mjs +1 -0
- package/dist/packem_shared/NOTIFY_FILENAME-CDvoTY3Y.mjs +1 -0
- package/dist/packem_shared/OPENRPC_VERSION-aq5eyIkd.mjs +3 -0
- package/dist/packem_shared/QUEUES_FILENAME-CSE6i-ld.mjs +1 -0
- package/dist/packem_shared/SCHEMA_SNAPSHOT_FILENAME-ChA6rpGP.mjs +9 -0
- package/dist/packem_shared/SCHEMA_SNAPSHOT_VERSION-CFhF_hmg.mjs +2 -0
- package/dist/packem_shared/SHAPES_FILENAME-BdLj5-mD.mjs +1 -0
- package/dist/packem_shared/SchemaSnapshotParseError-BJkhTEpi.mjs +16 -0
- package/dist/packem_shared/WORKFLOWS_FILENAME-ClsYRKXp.mjs +1 -0
- package/dist/packem_shared/buildOpenApiDocument-Bw3MChvT.mjs +3 -0
- package/dist/packem_shared/describeErrorLevelFindings-IhNx9gPr.mjs +1 -0
- package/dist/packem_shared/discover-ast-CezKqEhE.mjs +1 -0
- package/dist/packem_shared/discover-queries-BmZeJ--u.mjs +1 -0
- package/dist/packem_shared/discoverAuthApiCalls-BgL6nj03.mjs +1 -0
- package/dist/packem_shared/discoverCrons-WJscxQFl.mjs +1 -0
- package/dist/packem_shared/discoverFunctions-BgGmxEqW.mjs +1 -0
- package/dist/packem_shared/discoverHttpRoutes-eW8RbVOr.mjs +1 -0
- package/dist/packem_shared/discoverInserts-BCz6zpI0.mjs +1 -0
- package/dist/packem_shared/discoverMaskProcedures-DLfQRsQc.mjs +1 -0
- package/dist/packem_shared/discoverMigrations-Cbc4W765.mjs +1 -0
- package/dist/packem_shared/discoverNondeterministicCalls-x9IhWxPP.mjs +1 -0
- package/dist/packem_shared/discoverQueries-Cf0LGoQG.mjs +1 -0
- package/dist/packem_shared/discoverR2sqlCalls--MSfSeDm.mjs +1 -0
- package/dist/packem_shared/discoverRlsMetadata-DTRS0ROc.mjs +1 -0
- package/dist/packem_shared/discoverSandboxUsage-BDyeyuYo.mjs +1 -0
- package/dist/packem_shared/discoverSchema-DSJSX4Ss.mjs +1 -0
- package/dist/packem_shared/discoverStorageRulesMetadata-Cic97L-2.mjs +1 -0
- package/dist/packem_shared/emit-DMvG162j.mjs +2310 -0
- package/dist/packem_shared/emitApp--dJ6EnFn.mjs +524 -0
- package/dist/packem_shared/formatAdvisories-BCEfzLT0.mjs +2 -0
- package/dist/packem_shared/module-specifiers-8FEEiUcv.mjs +1 -0
- package/dist/packem_shared/parse-validator-CkLxY-Mr.mjs +1 -0
- package/dist/packem_shared/paths-BmX5O1sG.mjs +1 -0
- package/dist/packem_shared/readPackageDependencies-CogJX1Ia.mjs +1 -0
- package/dist/packem_shared/redact-6jD4lAhq.mjs +1 -0
- package/dist/packem_shared/schemaFromIr-R1ZFzVyy.mjs +1 -0
- package/package.json +12 -7
- package/dist/packem_shared/CONTAINERS_FILENAME-DlP6YM_Q.mjs +0 -224
- package/dist/packem_shared/CodegenDiagnosticError-DeblMkzO.mjs +0 -23
- package/dist/packem_shared/GENERATED_HEADER-LceqJ_qp.mjs +0 -2688
- package/dist/packem_shared/LUNORA_ERROR_CODES-CySpQPD3.mjs +0 -61
- package/dist/packem_shared/OPENRPC_VERSION-s7lhywUr.mjs +0 -60
- package/dist/packem_shared/SCHEMA_SNAPSHOT_FILENAME-Ct0NDRLY.mjs +0 -937
- package/dist/packem_shared/SCHEMA_SNAPSHOT_VERSION-DzLDbWk3.mjs +0 -233
- package/dist/packem_shared/WORKFLOWS_FILENAME-D62dcBGg.mjs +0 -84
- package/dist/packem_shared/buildOpenApiDocument-DSPTssJI.mjs +0 -183
- package/dist/packem_shared/discover-ast-CT6BgBr4.mjs +0 -13
- package/dist/packem_shared/discoverAuthApiCalls-CoirYbg6.mjs +0 -62
- package/dist/packem_shared/discoverCrons-Cev7RRAf.mjs +0 -257
- package/dist/packem_shared/discoverFunctions-BWMczzBx.mjs +0 -465
- package/dist/packem_shared/discoverHttpRoutes-CfP6cMzt.mjs +0 -131
- package/dist/packem_shared/discoverInserts-C7zxXkUf.mjs +0 -61
- package/dist/packem_shared/discoverMaskProcedures-Cm81kwrb.mjs +0 -217
- package/dist/packem_shared/discoverMigrations-Bi5nJ0mJ.mjs +0 -97
- package/dist/packem_shared/discoverNondeterministicCalls-C4M8AXmQ.mjs +0 -122
- package/dist/packem_shared/discoverQueries-B0wGT-xe.mjs +0 -62
- package/dist/packem_shared/discoverR2sqlCalls-BVNMd428.mjs +0 -80
- package/dist/packem_shared/discoverRlsMetadata-BS9GOGC5.mjs +0 -280
- package/dist/packem_shared/discoverSchema-BF6L4mrU.mjs +0 -795
- package/dist/packem_shared/discoverStorageRulesMetadata-Da8BKXcI.mjs +0 -97
- package/dist/packem_shared/emitApp-CDg72pVv.mjs +0 -599
- package/dist/packem_shared/formatAdvisories-8NIv1k0I.mjs +0 -69
- package/dist/packem_shared/parse-validator-Cabb60UV.mjs +0 -133
- package/dist/packem_shared/paths-BRd6JHuF.mjs +0 -11
- package/dist/packem_shared/schemaFromIr-DTYsLBaA.mjs +0 -57
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{Node as n,SyntaxKind as A}from"ts-morph";import{listLunoraSourceFiles as E,lunoraRelativePath as b,classifyProcedureCall as y}from"./discoverFunctions-BgGmxEqW.mjs";const S=t=>{if(!n.isCallExpression(t))return!1;const e=t.getExpression();return n.isIdentifier(e)?e.getText()==="rls":n.isPropertyAccessExpression(e)?e.getName()==="rls":!1},h=t=>{const e=t.getArguments()[0];if(!e||!n.isArrayLiteralExpression(e))return[];const r=[];for(const s of e.getElements()){if(!n.isObjectLiteralExpression(s))continue;const i=s.getProperty("table");if(!i||!n.isPropertyAssignment(i))continue;const o=i.getInitializer();o&&n.isStringLiteral(o)&&r.push(o.getLiteralText())}return r},L=t=>{const e=[];let r=t;for(;n.isCallExpression(r);){const s=r.getExpression();if(!n.isPropertyAccessExpression(s))break;if(s.getName()==="use"){const i=r.getArguments()[0];i&&S(i)&&e.push(i)}r=s.getExpression()}return e},v=t=>{const e=L(t);return{rlsTables:e.flatMap(r=>h(r)),usesRls:e.length>0}},P=new Set(["findFirst","findFirstOrThrow","findMany","get","query"]),T=new Set(["delete","deleteMany","insert","insertMany","patch","patchMany","replace"]),g=(t,e)=>{const r=t.getExpression();if(!n.isPropertyAccessExpression(r)||!e.has(r.getName()))return!1;const s=r.getExpression();return n.isPropertyAccessExpression(s)?s.getName()==="db":n.isIdentifier(s)&&s.getText()==="db"},d=t=>{const e=t.getArguments()[0];return e&&n.isStringLiteral(e)?e.getLiteralText():""},M=t=>{const e=new Set,r=new Set;for(const s of t.getDescendantsOfKind(A.CallExpression))if(g(s,P)){const i=d(s);i!==""&&e.add(i)}else if(g(s,T)){const i=d(s);i!==""&&r.add(i)}return{tablesRead:[...e],tablesWritten:[...r]}},R=(t,e)=>{if(!n.isVariableDeclaration(t))return;const r=t.getInitializer();if(!r||!n.isCallExpression(r))return;const s=y(r);if(!s)return;const i=s.receiver?v(s.receiver):{rlsTables:[],usesRls:!1},{tablesRead:o,tablesWritten:a}=M(t);return{exportName:t.getName(),file:e,rlsTables:i.rlsTables,tablesRead:o,tablesWritten:a,usesRls:i.usesRls,visibility:s.visibility}},D=(t,e)=>{const r=[];for(const s of E(e)){const i=t.getSourceFile(s)??t.addSourceFileAtPath(s),o=b(e,s);for(const a of i.getVariableStatements())if(a.isExported())for(const f of a.getDeclarations()){const c=R(f,o);c&&r.push(c)}}return r},C=new Set(["delete","insert","read","update"]),l=(t,e)=>{if(!n.isObjectLiteralExpression(t))return;const r=t.getProperty(e);if(!r||!n.isPropertyAssignment(r))return;const s=r.getInitializer();return s&&n.isStringLiteral(s)?s.getLiteralText():void 0},I=(t,e,r)=>{const s=t.getArguments()[0];if(!s||!n.isArrayLiteralExpression(s))return[];const i=[];for(const o of s.getElements()){if(!n.isObjectLiteralExpression(o))continue;const a=l(o,"on");a===void 0||!C.has(a)||i.push({file:e,on:a,procedure:r,table:l(o,"table")??""})}return i},m=t=>{if(!n.isObjectLiteralExpression(t))return[];const e=t.getProperty("permissions");if(!e||!n.isPropertyAssignment(e))return[];const r=e.getInitializer();if(!r||!n.isArrayLiteralExpression(r))return[];const s=[];for(const i of r.getElements()){if(n.isStringLiteral(i)){s.push(i.getLiteralText());continue}if(n.isCallExpression(i)){const o=i.getArguments()[0];o&&n.isStringLiteral(o)&&s.push(o.getLiteralText())}}return s},x=(t,e)=>e===void 0?t:{...t,description:e},N=t=>{if(n.isCallExpression(t)){const e=t.getArguments()[0];if(!e||!n.isStringLiteral(e))return;const r=t.getArguments()[1],s=r?l(r,"description"):void 0,i=r?m(r):[];return x({name:e.getLiteralText(),permissions:i},s)}if(n.isObjectLiteralExpression(t)){const e=l(t,"name");return e===void 0?void 0:x({name:e,permissions:m(t)},l(t,"description"))}},O=t=>{const e=t.getArguments()[1];if(!e||!n.isObjectLiteralExpression(e))return[];const r=e.getProperty("roles");if(!r||!n.isPropertyAssignment(r))return[];const s=r.getInitializer();if(!s||!n.isArrayLiteralExpression(s))return[];const i=[];for(const o of s.getElements()){const a=N(o);a&&i.push(a)}return i},w=(t,e,r)=>{const s=L(t);return{policies:s.flatMap(i=>I(i,e,r)),roles:s.flatMap(i=>O(i))}},F=t=>{const e=[];for(const r of t.getVariableStatements())if(r.isExported())for(const s of r.getDeclarations()){const i=s.getInitializer(),o=i&&n.isCallExpression(i)?y(i):void 0;o?.receiver&&e.push({name:s.getName(),receiver:o.receiver})}return e},V=(t,e)=>{const r=[],s=new Map;for(const i of E(e)){const o=t.getSourceFile(i)??t.addSourceFileAtPath(i),a=b(e,i);for(const{name:f,receiver:c}of F(o)){const p=w(c,a,f);r.push(...p.policies);for(const u of p.roles)s.has(u.name)||s.set(u.name,u)}}return{policies:r,roles:[...s.values()]}};export{D as default,V as discoverRlsMetadata};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{listLunoraSourceFiles as u}from"./discoverFunctions-BgGmxEqW.mjs";const i=new Set(["@lunora/agent","@lunora/agent/sandbox"]),d=o=>{const s={usesSandboxBrowser:!1,usesSandboxContainer:!1};if(!i.has(o.getModuleSpecifierValue())||o.isTypeOnly())return s;for(const e of o.getNamedImports()){if(e.isTypeOnly())continue;const n=e.getNameNode().getText();n==="browserTool"?s.usesSandboxBrowser=!0:n==="containerTool"&&(s.usesSandboxContainer=!0)}return s},b=(o,s)=>{const e={usesSandboxBrowser:!1,usesSandboxContainer:!1};for(const n of u(s)){const a=o.getSourceFile(n)??o.addSourceFileAtPath(n);for(const t of a.getImportDeclarations()){const r=d(t);e.usesSandboxBrowser||=r.usesSandboxBrowser,e.usesSandboxContainer||=r.usesSandboxContainer}if(e.usesSandboxBrowser&&e.usesSandboxContainer)break}return e};export{b as discoverSandboxUsage};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{LunoraError as A}from"@lunora/errors";import{Node as s,SyntaxKind as T}from"ts-morph";import{diagnosticAt as u}from"./CodegenDiagnosticError-DPezpZTz.mjs";import{f as j}from"./parse-validator-CkLxY-Mr.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}},z=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}:{}}),f=e=>{const r=e?._meta??{},t=e?.kind??"unknown",n={column:z(r.column),kind:t};switch(t){case"array":case"optional":{n.inner=f(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(([o,a])=>[o,f(a)]));break}case"record":{n.valueType=f(r.valueValidator),r.keyValidator!==void 0&&(n.keyType=f(r.keyValidator));break}case"storage":{typeof r.bucket=="string"&&(n.bucket=r.bucket);break}case"union":{const i=r.members??[];n.members=i.map(o=>f(o));break}}return n},D=e=>e?.kind==="global"?"global":e?.kind==="shardBy"&&typeof e.field=="string"?{field:e.field,kind:"shardBy"}:"root",M=(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 M(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,o])=>[i,f(o)])),shardMode:D(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(([a,l])=>B(l,a)),i=e.vectorIndexes??{},o=Object.entries(i??{}).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:n,bareVectorIndexes:o,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 o=i.getModuleSpecifierValue();for(const a of i.getNamedImports())if((a.getAliasNode()?.getText()??a.getName())===r)return{importedName:a.getName(),moduleSpecifier:o,propertyPath:n};if(i.getDefaultImport()?.getText()===r)return{importedName:"default",moduleSpecifier:o,propertyPath:n};if(i.getNamespaceImport()?.getText()===r&&t!==void 0)return{importedName:t,moduleSpecifier:o,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),o=n(i),a=C(o,t);return typeof a!="object"||a===null?void 0:F(a)}catch{return}},J=new Set(["cosine","dot-product","euclidean"]),U=new Set(["cascade","restrict","set null"]),v=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(v.has(t))throw u(r,`table name "${t}" is reserved — it collides with a \`ctx.db\` member (one of ${[...v].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.`)},d=(e,r)=>{const t=e.getProperty(r);if(t&&s.isPropertyAssignment(t)){const n=t.getInitializer();if(n&&s.isStringLiteral(n))return n.getLiteralText()}},m=(e,r)=>{const t=e.getProperty(r);if(t&&s.isPropertyAssignment(t)){const n=t.getInitializer();if(n&&s.isNumericLiteral(n))return Number(n.getLiteralText())}},X=(e,r)=>{const t=e.getProperty(r);if(t&&s.isPropertyAssignment(t)){const n=t.getInitializer();if(n&&(s.isTrueLiteral(n)||s.isFalseLiteral(n)))return n.getLiteralValue()}},h=(e,r)=>{const t=e.getProperty(r);if(t&&s.isPropertyAssignment(t)){const n=t.getInitializer();if(n&&s.isArrayLiteralExpression(n))return n.getElements().filter(i=>s.isStringLiteral(i)).map(i=>i.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,o]=r.getArguments(),a=i&&s.isStringLiteral(i)?i.getLiteralText():"_unknown_";let l="_unknown_",c="_id",g;return o&&s.isObjectLiteralExpression(o)&&(l=d(o,"field")??l,c=d(o,"references")??c,g=n==="one"?G(d(o,"onDelete")):void 0),{field:l,kind:n,name:e.getName(),onDelete:g,references:c,table:a}},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},x=e=>e&&s.isStringLiteral(e)?e.getLiteralText():"_unnamed_",ee=(e,r,t)=>{if(!r.includes("."))return;const[n="",...i]=r.split("."),o=`${n}${i.map(a=>a.charAt(0).toUpperCase()+a.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. \`${o}\`), 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),o=x(r),a=t&&s.isArrayLiteralExpression(t)?t.getElements():[];for(const c of a)if(!s.isStringLiteral(c))throw u(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)ee(c,c.getLiteralText(),o);return{fields:l.map(c=>c.getLiteralText()),name:o,unique:i}},ne=e=>{const[r,t]=e;let n="_unknown_",i,o,a,l;return t&&s.isObjectLiteralExpression(t)&&(n=d(t,"field")??n,i=h(t,"filterFields"),o=d(t,"language"),a=X(t,"staged"),l=d(t,"strategy")),{field:n,filterFields:i,language:o,name:x(r),staged:a,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:x(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}},oe=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}},ae=e=>{const[r,t]=e;let n=[],i;if(t&&s.isObjectLiteralExpression(t)){const o=t.getProperty("sortBy");if(o&&s.isPropertyAssignment(o)){const a=o.getInitializer();a&&s.isArrayLiteralExpression(a)&&(n=a.getElements().map(l=>oe(l)).filter(l=>l!==void 0))}i=h(t,"partitionBy")}return{name:x(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:h(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"},p=(e,r)=>{const t=e.getProperty(r);if(t&&s.isPropertyAssignment(t)){const n=t.getInitializer();if(n&&s.isStringLiteral(n))return n.getLiteralText()}},ue=(e,r)=>{const t=e.getProperty(r);if(t&&s.isPropertyAssignment(t)){const n=t.getInitializer();if(n&&s.isArrayLiteralExpression(n)){const i=n.getElements().filter(o=>s.isStringLiteral(o)).map(o=>o.getLiteralText());return i.length>0?i:void 0}}},ge=e=>{const r=e[0];return!r||!s.isObjectLiteralExpression(r)?{binding:"",hasTenantBy:!1,unanalyzable:!0}:{binding:p(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:p(r,"idColumn"),mode:p(r,"mode"),query:p(r,"query")}},fe=(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(ae(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=ge(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 o of E){const a=`${i.name}${o}`;if(!n.has(a))continue;const l=o==="__vocab"?`the \`fts5vocab\` companion for "${a}" 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 "${a}" 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 "${a}", whose generated FTS5 tables collide: "${i.name}" already owns "${r}__fts_${a}" as its "${o}" 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 o=i.getExpression(),a=i.getArguments();if(s.isPropertyAccessExpression(o))fe(t,o.getName(),a,r),i=o.getExpression();else if(s.isIdentifier(o)&&o.getText()==="defineTable"){const l=a[0];if(l&&s.isObjectLiteralExpression(l))n=j(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(),o=s.isStringLiteral(i)?i.getLiteralText():i.getText();return{dimensions:m(n,"dimensions"),metric:L(d(n,"metric")),name:o,table:me(n)}},P=e=>{const r=[];for(const t of e.getProperties()){const n=xe(t);n&&r.push(n)}return r},I=(e,r)=>`${e}_${r}`,y=(e,r,t)=>t.has(e)?I(r,e):e,be=(e,r,t)=>{const n=I(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)}))}},ye=e=>{if(s.isVariableDeclaration(e)||s.isPropertyAssignment(e))return e.getInitializer();if(s.isShorthandPropertyAssignment(e))return e.getNameNode()},he=(e,r)=>{const t=e.getProperty(r);if(t&&s.isPropertyAssignment(t))return t.getInitializer()},Ie=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}}},Ee=(e,r)=>{const t=ye(e);if(t){if(s.isPropertyAccessExpression(r)){const n=ve(t);return n?he(n,r.getName()):void 0}return t}},Le=e=>{let r=e,t=0;for(;r&&t<32;){if(t+=1,Ie(r))return r;const n=ke(r),i=n&&s.isIdentifier(n)?n.getSymbol():void 0,o=(i?.getAliasedSymbol()?.getDeclarations()??i?.getDeclarations()??[])[0];if(!o)return;const a=o.getSourceFile();if(a.isInNodeModules()||a.isDeclarationFile())return;r=Ee(o,r)}},Se=e=>{const[r,t]=e.getArguments();if(!(!r||!s.isStringLiteral(r))&&!(!t||!s.isObjectLiteralExpression(t)))return{key:r.getLiteralText(),options:t}},Pe=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 o=i.getInitializer();o&&n.push(S(o,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)?[]:P(t)},$=(e,r,t)=>{const n=new Set(r.map(a=>a.name)),i=r.map(a=>be(a,e,n)),o=t.map(a=>({...a,name:I(e,a.name),table:y(a.table,e,n)}));return{tables:i,vectorIndexes:o}},we=(e,r)=>$(e,Pe(r),$e(r)),Ae=(e,r)=>{const t=e.getArguments()[0];if(!t)return;const n=Le(t);if(!n){if(r!==void 0){const o=V(t,r);if(o)return $(o.key,o.bareTables,o.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=Se(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},w=(e,r,t,n,i)=>{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()===r){const c=l.getArguments()[0];if(!c||!s.isStringLiteral(c))throw u(l,`\`.${r}(...)\` expects a string literal (${i})`);const g=c.getLiteralText();if(!n.has(g))throw u(c,`unknown ${t} ${JSON.stringify(g)} — expected ${i}`);return g}o=l}},je=new Set(["eu","fedramp","us"]),_e=e=>w(e,"jurisdiction","jurisdiction",je,'"eu", "us", or "fedramp"'),Ne=new Set(["required"]),Oe=e=>w(e,"rls","rls mode",Ne,'"required"'),ze=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(o=>o.name)),i=[];for(const o of Te(e)){const a=Ae(o,t);if(a){for(const l of a.tables){if(n.has(l.name))throw u(o,`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(...a.vectorIndexes)}}return i},Ve=(e,r,t)=>{const n=e.addSourceFileAtPath(r).getDescendantsOfKind(T.CallExpression).find(b=>{const k=b.getExpression();return s.isIdentifier(k)&&k.getText()==="defineSchema"});if(!n)throw new A("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 o=ze(i),a=n.getArguments()[1],l=a&&s.isObjectLiteralExpression(a)?P(a):[],c=De(n,o,t),g=[...o.flatMap(b=>b.vectorIndexes),...l,...c];return{jurisdiction:_e(n),rlsMode:Oe(n),tables:o,vectorIndexes:g}};export{Ve as default};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{Node as i}from"ts-morph";import{listLunoraSourceFiles as f,lunoraRelativePath as p,classifyProcedureCall as g}from"./discoverFunctions-BgGmxEqW.mjs";const x=new Set(["delete","list","read","write"]),m=s=>{if(!i.isCallExpression(s))return!1;const r=s.getExpression();return i.isIdentifier(r)?r.getText()==="storageRules":i.isPropertyAccessExpression(r)&&r.getName()==="storageRules"},d=s=>{const r=[];let t=s;for(;i.isCallExpression(t);){const e=t.getExpression();if(!i.isPropertyAccessExpression(e))break;if(e.getName()==="use"){const o=t.getArguments()[0];o&&m(o)&&r.push(o)}t=e.getExpression()}return r},u=(s,r)=>{if(!i.isObjectLiteralExpression(s))return;const t=s.getProperty(r);if(!t||!i.isPropertyAssignment(t))return;const e=t.getInitializer();return e&&i.isStringLiteral(e)?e.getLiteralText():void 0},E=(s,r,t)=>{const e=s.getArguments()[0];if(!e||!i.isArrayLiteralExpression(e))return[];const o=[];for(const n of e.getElements()){if(!i.isObjectLiteralExpression(n))continue;const c=u(n,"on");if(c===void 0||!x.has(c))continue;const a=u(n,"prefix"),l={bucket:u(n,"bucket")??"",file:r,on:c,procedure:t};a!==void 0&&(l.prefix=a),o.push(l)}return o},v=s=>{const r=[];for(const t of s.getVariableStatements())if(t.isExported())for(const e of t.getDeclarations()){const o=e.getInitializer(),n=o&&i.isCallExpression(o)?g(o):void 0;n?.receiver&&r.push({name:e.getName(),receiver:n.receiver})}return r},h=(s,r)=>{const t=[];for(const e of f(r)){const o=s.getSourceFile(e)??s.addSourceFileAtPath(e),n=p(r,e);for(const{name:c,receiver:a}of v(o))for(const l of d(a))t.push(...E(l,n,c))}return{rules:t}};export{h as default};
|