@lunora/codegen 1.0.0-alpha.21 → 1.0.0-alpha.210
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/README.md +1 -1
- package/dist/index.d.mts +3324 -1151
- package/dist/index.d.ts +3324 -1151
- package/dist/index.mjs +1 -34
- package/dist/packem_shared/AGENTS_FILENAME-DPIMTD_h.mjs +1 -0
- package/dist/packem_shared/CONTAINERS_FILENAME-wDZs0S4p.mjs +1 -0
- package/dist/packem_shared/CodegenDiagnosticError-BUXlG3P-.mjs +1 -0
- package/dist/packem_shared/DEFAULT_TARGET-DtzlGawL.mjs +1 -0
- package/dist/packem_shared/FLAGS_FILENAME-CQofXgIf.mjs +1 -0
- package/dist/packem_shared/GENERATED_HEADER-Clwlg6jB.mjs +1 -0
- package/dist/packem_shared/LUNORA_ERROR_CODES-C2s0xWQD.mjs +1 -0
- package/dist/packem_shared/MUTATORS_FILENAME-CE7K7DfT.mjs +1 -0
- package/dist/packem_shared/NOTIFY_FILENAME-DbuOgja2.mjs +1 -0
- package/dist/packem_shared/OPENRPC_VERSION-BRCNaNPW.mjs +3 -0
- package/dist/packem_shared/PROJECT_CONFIG_FILENAMES-D29yBg5X.mjs +1 -0
- package/dist/packem_shared/QUEUES_FILENAME-Dd781zOQ.mjs +1 -0
- package/dist/packem_shared/SCHEMA_SNAPSHOT_FILENAME-D2xc36Bb.mjs +9 -0
- package/dist/packem_shared/SCHEMA_SNAPSHOT_VERSION-C0oHwiJZ.mjs +2 -0
- package/dist/packem_shared/SDK_LANGUAGES-CFyux-74.mjs +371 -0
- package/dist/packem_shared/SHAPES_FILENAME-BtuI2r7-.mjs +1 -0
- package/dist/packem_shared/SchemaSnapshotParseError-lUWe6lPo.mjs +18 -0
- package/dist/packem_shared/WORKFLOWS_FILENAME-v3AJcUjy.mjs +1 -0
- package/dist/packem_shared/buildOpenApiDocument-Ck7uULMF.mjs +3 -0
- package/dist/packem_shared/chain-u-8_P10m.mjs +1 -0
- package/dist/packem_shared/classify-procedure-call-RjsZg8AH.mjs +1 -0
- package/dist/packem_shared/context-property-calls-DV-Pt4KS.mjs +1 -0
- package/dist/packem_shared/declared-output-Iaar469J.mjs +1 -0
- package/dist/packem_shared/describeErrorLevelFindings-D23VSkFP.mjs +1 -0
- package/dist/packem_shared/discoverAuthApiCalls-EHm1O8uG.mjs +1 -0
- package/dist/packem_shared/discoverCrons-B2GXsB-m.mjs +1 -0
- package/dist/packem_shared/discoverFunctions-7IsdPbJJ.mjs +1 -0
- package/dist/packem_shared/discoverHttpRoutes-HeBP7vhK.mjs +1 -0
- package/dist/packem_shared/discoverHyperdriveCalls-CzRlqv_N.mjs +1 -0
- package/dist/packem_shared/discoverInserts-BP0Gl37o.mjs +1 -0
- package/dist/packem_shared/discoverMaskProcedures-Cl2I3ycl.mjs +1 -0
- package/dist/packem_shared/discoverMigrations-BX86Fv9G.mjs +1 -0
- package/dist/packem_shared/discoverNondeterministicCalls-CJYIXWPw.mjs +1 -0
- package/dist/packem_shared/discoverQueries-COGaQPE5.mjs +1 -0
- package/dist/packem_shared/discoverR2sqlCalls-emIgm3TF.mjs +1 -0
- package/dist/packem_shared/discoverRlsMetadata-3Gs8U3ng.mjs +1 -0
- package/dist/packem_shared/discoverRlsProcedures-CU7YMR3P.mjs +1 -0
- package/dist/packem_shared/discoverSandboxUsage-pJ42YuvL.mjs +1 -0
- package/dist/packem_shared/discoverSchema-ClYGs0dx.mjs +1 -0
- package/dist/packem_shared/discoverStorageRulesMetadata-BFiKUo5m.mjs +1 -0
- package/dist/packem_shared/emit-_LUzQG_K.mjs +2681 -0
- package/dist/packem_shared/emitApp-CgW3zBjL.mjs +838 -0
- package/dist/packem_shared/exported-procedure-chains-CjlBrFjo.mjs +1 -0
- package/dist/packem_shared/formatAdvisories-DwmUKYuc.mjs +2 -0
- package/dist/packem_shared/index-D6xfnMEE.mjs +1 -0
- package/dist/packem_shared/isD1GlobalTable-udvHAAzD.mjs +1 -0
- package/dist/packem_shared/isTypedSchema-B_qlBbE-.mjs +2 -0
- package/dist/packem_shared/listLunoraSourceFiles-DsxeRYvh.mjs +1 -0
- package/dist/packem_shared/module-specifiers-WF8iAydP.mjs +1 -0
- package/dist/packem_shared/parse-validator-DW5RNi_6.mjs +1 -0
- package/dist/packem_shared/paths-C-9fwEgQ.mjs +1 -0
- package/dist/packem_shared/queries-TZaLWhWq.mjs +1 -0
- package/dist/packem_shared/readPackageDependencies-C6h6FZuP.mjs +1 -0
- package/dist/packem_shared/redact-Cy14LEnc.mjs +1 -0
- package/dist/packem_shared/schemaFromIr-BWFnvCVg.mjs +1 -0
- package/dist/packem_shared/unwrap-handler-return-BEdusskg.mjs +1 -0
- package/package.json +16 -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/FLAGS_FILENAME-B1vE0LIo.mjs +0 -139
- package/dist/packem_shared/GENERATED_HEADER-CI272wRm.mjs +0 -3245
- package/dist/packem_shared/LUNORA_ERROR_CODES-CySpQPD3.mjs +0 -61
- package/dist/packem_shared/LUNORA_SOLUTION_RULES-BTejmZp-.mjs +0 -131
- package/dist/packem_shared/MUTATORS_FILENAME-BhqdPtKp.mjs +0 -81
- package/dist/packem_shared/OPENRPC_VERSION-t5pV2NIN.mjs +0 -60
- package/dist/packem_shared/QUEUES_FILENAME-B5_eWCRe.mjs +0 -119
- package/dist/packem_shared/SCHEMA_SNAPSHOT_FILENAME-BMez-jgr.mjs +0 -1039
- package/dist/packem_shared/SCHEMA_SNAPSHOT_VERSION-wWGP2_5E.mjs +0 -245
- package/dist/packem_shared/SHAPES_FILENAME-ChV7MqgE.mjs +0 -94
- package/dist/packem_shared/WORKFLOWS_FILENAME-CCisG0Vy.mjs +0 -124
- package/dist/packem_shared/buildOpenApiDocument-Dx5QftUn.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-KYq55qu3.mjs +0 -864
- package/dist/packem_shared/discoverStorageRulesMetadata-Da8BKXcI.mjs +0 -97
- package/dist/packem_shared/emitApp-cOSypPuO.mjs +0 -659
- package/dist/packem_shared/formatAdvisories-DdjK7sgh.mjs +0 -81
- 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{d as r}from"./context-property-calls-DV-Pt4KS.mjs";const t=(s,o)=>r(s,o,"r2sql");export{t as default};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{Node as o}from"ts-morph";import{listLunoraSourceFiles as d,lunoraRelativePath as x,stringPropertyOf as c}from"./listLunoraSourceFiles-DsxeRYvh.mjs";import{e as h}from"./exported-procedure-chains-CjlBrFjo.mjs";import{r as A}from"./chain-u-8_P10m.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-DsxeRYvh.mjs";import{c as p}from"./classify-procedure-call-RjsZg8AH.mjs";import{r as m}from"./chain-u-8_P10m.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-DsxeRYvh.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 D}from"@lunora/errors";import{Node as i,SyntaxKind as z}from"ts-morph";import{diagnosticAt as f}from"./CodegenDiagnosticError-BUXlG3P-.mjs";import{createRequire as M}from"node:module";import{join as R}from"node:path";import{r as C,p as F}from"./parse-validator-DW5RNi_6.mjs";const B=(e,n)=>`table "${e}" is .global() and declares vector index "${n}". Vector sync runs on the shard write path, which a global (D1/Hyperdrive) table's writes never take — the index would stay empty. Drop .global(), or keep the index yourself with ctx.vectors.upsert/deleteByIds.`,V=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}},q=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 n=e?._meta??{},t=e?.kind??"unknown",s={column:q(n.column),kind:t};switch(t){case"array":case"optional":{s.inner=g(n.inner);break}case"from":break;case"id":{s.tableName=n.tableName;break}case"literal":{s.literalValue=V(n.value);break}case"object":{const r=n.shape??{};s.shape=Object.fromEntries(Object.entries(r).map(([o,a])=>[o,g(a)]));break}case"record":{s.valueType=g(n.valueValidator),n.keyValidator!==void 0&&(s.keyType=g(n.keyValidator));break}case"storage":{typeof n.bucket=="string"&&(s.bucket=n.bucket);break}case"union":{const r=n.members??[];s.members=r.map(o=>g(o));break}}return s},J=e=>e?.kind==="global"?"global":e?.kind==="shardBy"&&typeof e.field=="string"?{field:e.field,kind:"shardBy"}:"root",U=(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.`)},K=(e,n)=>{const t=e.shape??{},s=(e.indexes??[]).map(r=>({fields:r.fields,name:r.name,...r.unique?{unique:!0}:{}}));return U(e,n),{...e.isExternallyManaged?{externallyManaged:!0}:{},geoIndexes:[],indexes:s,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,g(o)])),shardMode:J(e.shardMode),vectorIndexes:[]}},Z=e=>{const{key:n,tables:t}=e;if(typeof n!="string"||typeof t!="object"||t===null)return;const s=Object.entries(t).map(([a,l])=>K(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:s,bareVectorIndexes:o,key:n}},W=e=>{if(i.isIdentifier(e))return e.getText();if(i.isPropertyAccessExpression(e)){const n=e.getExpression();return i.isIdentifier(n)?n.getText():void 0}},G=e=>{const n=W(e);if(n===void 0)return;const t=i.isPropertyAccessExpression(e)?e.getName():void 0,s=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:s};if(r.getDefaultImport()?.getText()===n)return{importedName:"default",moduleSpecifier:o,propertyPath:s};if(r.getNamespaceImport()?.getText()===n&&t!==void 0)return{importedName:t,moduleSpecifier:o,propertyPath:[]}}},H=(e,n)=>{let t=e[n.importedName];for(const s of n.propertyPath){if(typeof t!="object"||t===null)return;t=t[s]}return t},Q=(e,n)=>{const t=G(e);if(t)try{const s=M(R(n,"noop.cjs")),r=s.resolve(t.moduleSpecifier),o=s(r),a=H(o,t);return typeof a!="object"||a===null?void 0:Z(a)}catch{return}},X=new Set(["cosine","dot-product","euclidean"]),T=e=>e.replaceAll(/^["']|["']$/gu,""),m=(e,n)=>{const t=e.getProperty(n);if(t&&i.isPropertyAssignment(t))return t.getInitializer()},d=(e,n)=>{const t=m(e,n);if(t&&i.isStringLiteral(t))return t.getLiteralText()},x=(e,n)=>{const t=m(e,n);if(t&&i.isNumericLiteral(t))return Number(t.getLiteralText())},Y=(e,n)=>{const t=m(e,n);if(t&&(i.isTrueLiteral(t)||i.isFalseLiteral(t)))return t.getLiteralValue()},b=(e,n)=>{const t=m(e,n);if(t&&i.isArrayLiteralExpression(t))return t.getElements().filter(s=>i.isStringLiteral(s)).map(s=>s.getLiteralText())},L=e=>e&&X.has(e)?e:void 0,h=e=>e&&i.isStringLiteral(e)?e.getLiteralText():"_unnamed_",ee=(e,n)=>{const t=b(e,n);return t!==void 0&&t.length>0?t:void 0},O=(e,n,t,s,r)=>{let o=e;for(;;){const a=o.getParent();if(!a||!i.isPropertyAccessExpression(a))break;const l=a.getParent();if(!l||!i.isCallExpression(l))break;if(a.getName()===n){const c=l.getArguments()[0];if(!c||!i.isStringLiteral(c))throw f(l,`\`.${n}(...)\` expects a string literal (${r})`);const p=c.getLiteralText();if(!s.has(p))throw f(c,`unknown ${t} ${JSON.stringify(p)} — expected ${r}`);return p}o=l}},te=new Set(["cascade","restrict","set null"]),ne=new Set(["aggregate","asId","cdcChangedTables","count","delete","deleteAll","deleteMany","deleteWhere","findFirst","findFirstOrThrow","findMany","get","groupBy","insert","insertMany","insertManyUnsafe","lookupById","normalizeId","patch","patchMany","patchWhere","query","rank","rankBefore","rankPage","rankPageRows","related","relationEdges","replace","restore","system","wipeShard"]),re=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"]),P=/^[A-Za-z_$][\w$]*$/u,E=(e,n)=>{const t=T(e);if(ne.has(t))throw f(n,`table name "${t}" is reserved — the generated shard binds each table's facade onto the same object that carries \`ctx.db\`'s own members, so this table would overwrite \`ctx.db.${t}\` and every use of it would break. Rename the table.`);if(re.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(!P.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.`)},se=e=>e&&te.has(e)?e:void 0,ie=e=>{if(!i.isArrowFunction(e))return;let n=e.getBody();if(i.isParenthesizedExpression(n))n=n.getExpression();else if(i.isBlock(n)){const t=n.getStatements().find(s=>i.isReturnStatement(s));n=t&&i.isReturnStatement(t)?t.getExpression()??n:n}return i.isObjectLiteralExpression(n)?n:void 0},oe=e=>{if(!i.isPropertyAssignment(e))return;const n=e.getInitializer();if(!n||!i.isCallExpression(n))return;const t=n.getExpression();if(!i.isPropertyAccessExpression(t))return;const s=t.getName();if(s!=="one"&&s!=="many")return;const[r,o]=n.getArguments();if(!r||!i.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",p;return o&&i.isObjectLiteralExpression(o)&&(l=d(o,"field")??l,c=d(o,"references")??c,p=s==="one"?se(d(o,"onDelete")):void 0),{field:l,kind:s,name:e.getName(),onDelete:p,references:c,table:a}},ae=e=>{const n=ie(e);if(!n)return[];const t=[];for(const s of n.getProperties()){const r=oe(s);r&&t.push(r)}return t},le=(e,n,t)=>{if(!n.includes("."))return;const[s="",...r]=n.split("."),o=`${s}${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.`)},ce=e=>{if(!e||!i.isObjectLiteralExpression(e))return!1;const n=e.getProperty("unique");if(!n||!i.isPropertyAssignment(n))return!1;const t=n.getInitializer();if(t===void 0)return!1;if(!i.isTrueLiteral(t)&&!i.isFalseLiteral(t))throw f(t,`\`unique\` must be a literal \`true\` or \`false\`, got ${JSON.stringify(t.getText())}`);return i.isTrueLiteral(t)},de=e=>{const[n,t,s]=e,r=ce(s),o=h(n),a=t&&i.isArrayLiteralExpression(t)?t.getElements():[];for(const c of a)if(!i.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=>i.isStringLiteral(c));for(const c of l)le(c,c.getLiteralText(),o);return{fields:l.map(c=>c.getLiteralText()),name:o,unique:r}},fe=e=>{const[n,t]=e;let s="_unknown_",r,o,a,l;return t&&i.isObjectLiteralExpression(t)&&(s=d(t,"field")??s,r=b(t,"filterFields"),o=d(t,"language"),a=Y(t,"staged"),l=d(t,"strategy")),{field:s,filterFields:r,language:o,name:h(n),staged:a,strategy:l}},ue=e=>{const[n,t]=e;let s="_unknown_",r;return t&&i.isObjectLiteralExpression(t)&&(s=d(t,"field")??s,r=x(t,"precision")),{field:s,name:h(n),precision:r}},pe=e=>{const[n,t]=e,s=n&&i.isStringLiteral(n)?n.getLiteralText():"_unknown_";let r;return t&&i.isObjectLiteralExpression(t)&&(r=x(t,"after")),{after:r,field:s}},ge=e=>{if(!i.isObjectLiteralExpression(e))return;const n=d(e,"field");return n===void 0?void 0:{direction:d(e,"direction")==="desc"?"desc":"asc",field:n}},me=e=>{const[n,t]=e;let s=[],r;if(t&&i.isObjectLiteralExpression(t)){const o=t.getProperty("sortBy");if(o&&i.isPropertyAssignment(o)){const a=o.getInitializer();a&&i.isArrayLiteralExpression(a)&&(s=a.getElements().map(l=>ge(l)).filter(l=>l!==void 0))}r=b(t,"partitionBy")}return{name:h(n),partitionBy:r,sortBy:s}},xe=(e,n)=>{const[t,s]=e,r=t&&i.isStringLiteral(t)?t.getLiteralText():"_unknown_";if(!(!s||!i.isObjectLiteralExpression(s)))return{dimensions:x(s,"dimensions"),field:r,metadata:b(s,"metadata"),metric:L(d(s,"metric")),name:d(s,"index")??"_unnamed_",table:n}},be=e=>{const n=e[0];return n&&i.isObjectLiteralExpression(n)&&d(n,"backend")==="hyperdrive"?"hyperdrive":"d1"},he=e=>{if(e&&i.isObjectLiteralExpression(e)){const n=e.getProperty("field");if(n&&i.isPropertyAssignment(n)){const t=n.getInitializer();if(t&&i.isStringLiteral(t))return t.getLiteralText()}}return"deletedAt"},ye=e=>{const n=e[0];return!n||!i.isObjectLiteralExpression(n)?{binding:"",hasTenantBy:!1,unanalyzable:!0}:{binding:d(n,"binding")??"",columns:ee(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")}},ve=(e,n,t,s)=>{switch(n){case"commitOrdered":{e.commitOrdered=!0;break}case"externallyManaged":{e.externallyManaged=!0;break}case"geoIndex":{e.geoIndexes.push(ue(t));break}case"global":{e.shardMode="global",e.globalBackend=be(t);break}case"index":{e.indexes.push(de(t));break}case"memory":{e.memory=!0;break}case"public":{e.isPublic=!0;break}case"rankIndex":{e.rankIndexes.push(me(t));break}case"relations":{const r=t[0];r&&e.relations.push(...ae(r));break}case"searchIndex":{e.searchIndexes.push(fe(t));break}case"shardBy":{const r=t[0];e.shardMode={field:r&&i.isStringLiteral(r)?r.getLiteralText():"_unknown_",kind:"shardBy"};break}case"softDelete":{e.softDelete={field:he(t[0])};break}case"source":{e.externalSource=ye(t),e.externallyManaged=!0;break}case"ttl":{e.ttl=pe(t);break}case"vectorize":{const r=xe(t,s);r&&e.vectorIndexes.push(r);break}}},w=["__vocab","_config","_content","_data","_docsize","_idx"],Ee=(e,n,t)=>{const s=new Set(t.map(r=>r.name));for(const r of t)for(const o of w){const a=`${r.name}${o}`;if(!s.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 ${w.join(", ")}.`)}},N=(e,n)=>{const t={commitOrdered:!1,externallyManaged:!1,geoIndexes:[],indexes:[],isPublic:!1,memory:!1,rankIndexes:[],relations:[],searchIndexes:[],shardMode:"root",vectorIndexes:[]};let s={},r=e;for(;i.isCallExpression(r);){const o=r.getExpression(),a=r.getArguments();if(i.isPropertyAccessExpression(o))ve(t,o.getName(),a,n),r=o.getExpression();else if(i.isIdentifier(o)&&o.getText()==="defineTable"){const l=a[0];if(l&&i.isObjectLiteralExpression(l))s=F(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 s)&&(s={...s,[t.softDelete.field]:{inner:{kind:"number"},kind:"optional"}}),Ee(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:s,shardMode:t.shardMode,softDelete:t.softDelete,ttl:t.ttl,vectorIndexes:t.vectorIndexes}},A=(e,n=[],t=new Set,s=new Set)=>{s.add(e);for(const r of e.getProperties()){if(i.isSpreadAssignment(r)){const a=C(r.getExpression());a!==void 0&&!s.has(a)&&A(a,n,t,s);continue}if(!i.isPropertyAssignment(r))continue;const o=r.getInitializer();if(o){const a=r.getName();E(a,r.getNameNode());const l=T(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(N(o,a))}}return n},Ie=e=>{const n=e.getProperty("source");if(n&&i.isPropertyAssignment(n)){const t=n.getInitializer();if(t&&i.isObjectLiteralExpression(t))return d(t,"table")??"_unknown_"}return"_unknown_"},Se=e=>{if(!i.isPropertyAssignment(e))return;const n=e.getInitializer();if(!n||!i.isCallExpression(n))return;const t=n.getExpression();if(!i.isIdentifier(t)||t.getText()!=="defineVectorIndex")return;const s=n.getArguments()[0];if(!s||!i.isObjectLiteralExpression(s))return;const r=e.getNameNode(),o=i.isStringLiteral(r)?r.getLiteralText():r.getText();return{dimensions:x(s,"dimensions"),metric:L(d(s,"metric")),name:o,table:Ie(s)}},_=e=>{const n=[];for(const t of e.getProperties()){const s=Se(t);s&&n.push(s)}return n},I=(e,n)=>`${e}_${n}`,ke=(e,n)=>{if(!P.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.`)},v=(e,n,t)=>t.has(e)?I(n,e):e,we=(e,n,t)=>{const s=I(n,e.name);return{...e,extensionKey:n,name:s,relations:e.relations.map(r=>({...r,table:v(r.table,n,t)})),vectorIndexes:e.vectorIndexes.map(r=>({...r,table:v(r.table,n,t)}))}},Te=e=>{if(i.isVariableDeclaration(e)||i.isPropertyAssignment(e))return e.getInitializer();if(i.isShorthandPropertyAssignment(e))return e.getNameNode()},Le=e=>{if(!i.isCallExpression(e))return!1;const n=e.getExpression();return i.isIdentifier(n)&&n.getText()==="defineSchemaExtension"},Oe=e=>{if(i.isIdentifier(e))return e;if(i.isPropertyAccessExpression(e)){const n=e.getExpression();return i.isIdentifier(n)?n:void 0}},Pe=e=>{if(i.isObjectLiteralExpression(e))return e;if(i.isCallExpression(e)){const n=e.getExpression();let t;if(i.isIdentifier(n)?t=n.getText():i.isPropertyAccessExpression(n)&&(t=n.getName()),t==="definePlugin"){const s=e.getArguments()[1];return s&&i.isObjectLiteralExpression(s)?s:void 0}}},Ne=(e,n)=>{const t=Te(e);if(t){if(i.isPropertyAccessExpression(n)){const s=Pe(t);return s?m(s,n.getName()):void 0}return t}},Ae=e=>{let n=e,t=0;for(;n&&t<32;){if(t+=1,Le(n))return n;const s=Oe(n),r=s&&i.isIdentifier(s)?s.getSymbol():void 0,a=(r?.getAliasedSymbol()?.getDeclarations()??r?.getDeclarations()??[])[0];if(!a)return;const l=a.getSourceFile();if(l.isInNodeModules()||l.isDeclarationFile())return;n=Ne(a,n)}},_e=e=>{const[n,t]=e.getArguments();if(!(!n||!i.isStringLiteral(n))&&!(!t||!i.isObjectLiteralExpression(t)))return{key:n.getLiteralText(),keyNode:n,options:t}},$e=e=>{const n=e.getProperty("tables");if(!n||!i.isPropertyAssignment(n))return[];const t=n.getInitializer();if(!t||!i.isObjectLiteralExpression(t))return[];const s=[];for(const r of t.getProperties()){if(!i.isPropertyAssignment(r))continue;const o=r.getInitializer();if(!o)continue;const a=r.getName();E(a,r.getNameNode()),s.push(N(o,a))}return s},je=e=>{const n=e.getProperty("vectorIndexes");if(!n||!i.isPropertyAssignment(n))return[];const t=n.getInitializer();return!t||!i.isObjectLiteralExpression(t)?[]:_(t)},$=(e,n,t,s)=>{ke(e,s);const r=new Set(n.map(l=>l.name)),o=n.map(l=>we(l,e,r)),a=t.map(l=>({...l,name:I(e,l.name),table:v(l.table,e,r)}));return{tables:o,vectorIndexes:a}},De=(e,n,t)=>$(e,$e(t),je(t),n),ze=(e,n)=>{const t=e.getArguments()[0];if(!t)return;const s=Ae(t);if(!s){if(n!==void 0){const o=Q(t,n);if(o){for(const a of o.bareTables)E(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=_e(s);if(!r){console.warn("@lunora/codegen: skipping `.extend(...)` — `defineSchemaExtension` requires a string `key` and an options object literal.");return}return De(r.key,r.keyNode,r.options)},Me=e=>{const n=[];let t=e;for(;;){const s=t.getParent();if(!s||!i.isPropertyAccessExpression(s))break;const r=s.getParent();if(!r||!i.isCallExpression(r))break;s.getName()==="extend"&&n.push(r),t=r}return n},Re=(e,n,t)=>{const s=new Set(n.map(o=>o.name)),r=[];for(const o of Me(e)){const a=ze(o,t);if(a){for(const l of a.tables){if(s.has(l.name))throw f(o,`defineSchema(...).extend(...): table "${l.name}" already exists — another extension with the same key already contributed it.`);s.add(l.name),n.push(l)}r.push(...a.vectorIndexes)}}return r},Ce=new Set(["eu","fedramp","us"]),Fe=e=>O(e,"jurisdiction","jurisdiction",Ce,'"eu", "us", or "fedramp"'),Be=new Set(["required"]),Ve=e=>O(e,"rls","rls mode",Be,'"required"'),Ge=(e,n,t)=>{const r=e.addSourceFileAtPath(n).getDescendantsOfKind(z.CallExpression).find(u=>{const k=u.getExpression();return i.isIdentifier(k)&&k.getText()==="defineSchema"});if(!r)throw new D("INTERNAL",`defineSchema() not found in ${n}`);const o=r.getArguments()[0];if(!o||!i.isObjectLiteralExpression(o))throw f(r,"defineSchema() expects an object literal");const a=A(o),l=r.getArguments()[1],c=l&&i.isObjectLiteralExpression(l)?_(l):[],p=Re(r,a,t),S=[...a.flatMap(u=>u.vectorIndexes),...c,...p],j=new Set(a.filter(u=>u.shardMode==="global").map(u=>u.name)),y=S.find(u=>j.has(u.table));if(y)throw f(r,B(y.table,y.name));return{jurisdiction:Fe(r),rlsMode:Ve(r),tables:a,vectorIndexes:S}};export{Ge 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-DsxeRYvh.mjs";import{w as d}from"./classify-procedure-call-RjsZg8AH.mjs";import{e as m}from"./exported-procedure-chains-CjlBrFjo.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};
|