@lunora/codegen 1.0.0-alpha.136 → 1.0.0-alpha.138
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.mjs +1 -1
- package/dist/packem_shared/AGENTS_FILENAME-D1qA0h5m.mjs +1 -0
- package/dist/packem_shared/CONTAINERS_FILENAME-oKaARMoA.mjs +1 -0
- package/dist/packem_shared/FLAGS_FILENAME-BasxlnVS.mjs +1 -0
- package/dist/packem_shared/{GENERATED_HEADER-Juc2KRT1.mjs → GENERATED_HEADER-Gajqa19J.mjs} +1 -1
- package/dist/packem_shared/MUTATORS_FILENAME-DJvAhDlD.mjs +1 -0
- package/dist/packem_shared/NOTIFY_FILENAME-B-eIjru_.mjs +1 -0
- package/dist/packem_shared/{OPENRPC_VERSION-CtO1oRu-.mjs → OPENRPC_VERSION-X4hs2-hb.mjs} +1 -1
- package/dist/packem_shared/QUEUES_FILENAME-D229WtRm.mjs +1 -0
- package/dist/packem_shared/SCHEMA_SNAPSHOT_FILENAME-D1R4OORJ.mjs +9 -0
- package/dist/packem_shared/WORKFLOWS_FILENAME-DRaiCNa6.mjs +1 -0
- package/dist/packem_shared/ast-CjWtsS6S.mjs +1 -0
- package/dist/packem_shared/{buildOpenApiDocument-B3l1QPs9.mjs → buildOpenApiDocument-CGl09_Pi.mjs} +1 -1
- package/dist/packem_shared/chain-CtC3-wwZ.mjs +1 -0
- package/dist/packem_shared/classify-procedure-call-AKeIK05y.mjs +1 -0
- package/dist/packem_shared/discoverAuthApiCalls-C35kkayg.mjs +1 -0
- package/dist/packem_shared/discoverCrons-wXBfQcfn.mjs +1 -0
- package/dist/packem_shared/discoverFunctions-BBDziFXy.mjs +1 -0
- package/dist/packem_shared/discoverHttpRoutes-Dmow2gql.mjs +1 -0
- package/dist/packem_shared/discoverInserts-C5l2INmE.mjs +1 -0
- package/dist/packem_shared/discoverMaskProcedures-Df4JQp_2.mjs +1 -0
- package/dist/packem_shared/discoverMigrations-CWH7T6qz.mjs +1 -0
- package/dist/packem_shared/discoverNondeterministicCalls-JsO91i57.mjs +1 -0
- package/dist/packem_shared/discoverQueries-iypfwfdZ.mjs +1 -0
- package/dist/packem_shared/discoverR2sqlCalls-BbfOpwE4.mjs +1 -0
- package/dist/packem_shared/discoverRlsMetadata-DGUGYgWs.mjs +1 -0
- package/dist/packem_shared/discoverRlsProcedures-CRshfYZE.mjs +1 -0
- package/dist/packem_shared/discoverSandboxUsage-DdewAOuj.mjs +1 -0
- package/dist/packem_shared/discoverSchema-Bp8DcNq7.mjs +1 -0
- package/dist/packem_shared/discoverStorageRulesMetadata-gyO60A5S.mjs +1 -0
- package/dist/packem_shared/{emit-khv5P1pH.mjs → emit-BQNGQnaS.mjs} +186 -186
- package/dist/packem_shared/{emitApp-CZ_yiNNO.mjs → emitApp-BQcIWzkh.mjs} +1 -1
- package/dist/packem_shared/exported-procedure-chains-MlZRKqSD.mjs +1 -0
- package/dist/packem_shared/index-CCDlo9L7.mjs +1 -0
- package/dist/packem_shared/queries-B_xEHIeL.mjs +1 -0
- package/dist/packem_shared/unwrap-handler-return-CmIWbykC.mjs +1 -0
- package/package.json +10 -10
- package/dist/packem_shared/AGENTS_FILENAME-CPHo8Se5.mjs +0 -1
- package/dist/packem_shared/CONTAINERS_FILENAME-Bvn6LZP6.mjs +0 -1
- package/dist/packem_shared/FLAGS_FILENAME-DtBWyUKq.mjs +0 -1
- package/dist/packem_shared/MUTATORS_FILENAME-VnV3vpJh.mjs +0 -1
- package/dist/packem_shared/NOTIFY_FILENAME-3xSHPaVK.mjs +0 -1
- package/dist/packem_shared/QUEUES_FILENAME-BnGLtAIu.mjs +0 -1
- package/dist/packem_shared/SCHEMA_SNAPSHOT_FILENAME-DIVmMUEJ.mjs +0 -9
- package/dist/packem_shared/WORKFLOWS_FILENAME-B8y51Frf.mjs +0 -1
- package/dist/packem_shared/discover-ast-D33zP-NZ.mjs +0 -1
- package/dist/packem_shared/discover-queries-Dfj_0WJG.mjs +0 -1
- package/dist/packem_shared/discoverAuthApiCalls-DUjcl0SB.mjs +0 -1
- package/dist/packem_shared/discoverCrons-BUJO2ipU.mjs +0 -1
- package/dist/packem_shared/discoverFunctions-2ajN70Q9.mjs +0 -1
- package/dist/packem_shared/discoverHttpRoutes-DDhmUyVN.mjs +0 -1
- package/dist/packem_shared/discoverInserts-YX8AmrzL.mjs +0 -1
- package/dist/packem_shared/discoverMaskProcedures-CjfyneNc.mjs +0 -1
- package/dist/packem_shared/discoverMigrations-4Jjf5DJE.mjs +0 -1
- package/dist/packem_shared/discoverNondeterministicCalls-CSI5ir3L.mjs +0 -1
- package/dist/packem_shared/discoverQueries-C5S9tFoD.mjs +0 -1
- package/dist/packem_shared/discoverR2sqlCalls-NtUJIKqo.mjs +0 -1
- package/dist/packem_shared/discoverRlsMetadata-BA9lVt-Q.mjs +0 -1
- package/dist/packem_shared/discoverSandboxUsage-L0lV17de.mjs +0 -1
- package/dist/packem_shared/discoverSchema-Hsu_1xIc.mjs +0 -1
- package/dist/packem_shared/discoverStorageRulesMetadata-DudvC-VM.mjs +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{LunoraError as _}from"@lunora/errors";import{Node as i,SyntaxKind as j}from"ts-morph";import{diagnosticAt as f}from"./CodegenDiagnosticError-BUXlG3P-.mjs";import{createRequire as $}from"node:module";import{join as z}from"node:path";import{a as D}from"./parse-validator-BFTp87yz.mjs";const C=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}},R=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",s={column:R(n.column),kind:t};switch(t){case"array":case"optional":{s.inner=p(n.inner);break}case"from":break;case"id":{s.tableName=n.tableName;break}case"literal":{s.literalValue=C(n.value);break}case"object":{const r=n.shape??{};s.shape=Object.fromEntries(Object.entries(r).map(([o,a])=>[o,p(a)]));break}case"record":{s.valueType=p(n.valueValidator),n.keyValidator!==void 0&&(s.keyType=p(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=>p(o));break}}return s},M=e=>e?.kind==="global"?"global":e?.kind==="shardBy"&&typeof e.field=="string"?{field:e.field,kind:"shardBy"}:"root",F=(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.`)},B=(e,n)=>{const t=e.shape??{},s=(e.indexes??[]).map(r=>({fields:r.fields,name:r.name,...r.unique?{unique:!0}:{}}));return F(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,p(o)])),shardMode:M(e.shardMode),vectorIndexes:[]}},V=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])=>B(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}},q=e=>{if(i.isIdentifier(e))return e.getText();if(i.isPropertyAccessExpression(e)){const n=e.getExpression();return i.isIdentifier(n)?n.getText():void 0}},J=e=>{const n=q(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:[]}}},U=(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},K=(e,n)=>{const t=J(e);if(t)try{const s=$(z(n,"noop.cjs")),r=s.resolve(t.moduleSpecifier),o=s(r),a=U(o,t);return typeof a!="object"||a===null?void 0:V(a)}catch{return}},G=new Set(["cosine","dot-product","euclidean"]),k=e=>e.replaceAll(/^["']|["']$/gu,""),g=(e,n)=>{const t=e.getProperty(n);if(t&&i.isPropertyAssignment(t))return t.getInitializer()},d=(e,n)=>{const t=g(e,n);if(t&&i.isStringLiteral(t))return t.getLiteralText()},m=(e,n)=>{const t=g(e,n);if(t&&i.isNumericLiteral(t))return Number(t.getLiteralText())},Z=(e,n)=>{const t=g(e,n);if(t&&(i.isTrueLiteral(t)||i.isFalseLiteral(t)))return t.getLiteralValue()},x=(e,n)=>{const t=g(e,n);if(t&&i.isArrayLiteralExpression(t))return t.getElements().filter(s=>i.isStringLiteral(s)).map(s=>s.getLiteralText())},w=e=>e&&G.has(e)?e:void 0,b=e=>e&&i.isStringLiteral(e)?e.getLiteralText():"_unnamed_",Q=(e,n)=>{const t=x(e,n);return t!==void 0&&t.length>0?t:void 0},T=(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 u=c.getLiteralText();if(!s.has(u))throw f(c,`unknown ${t} ${JSON.stringify(u)} — expected ${r}`);return u}o=l}},W=new Set(["cascade","restrict","set null"]),I=new Set(["delete","get","insert","normalizeId","patch","query","replace","system"]),X=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"]),H=/^[A-Za-z_$][\w$]*$/u,L=(e,n)=>{const t=k(e);if(I.has(t))throw f(n,`table name "${t}" is reserved — it collides with a \`ctx.db\` member (one of ${[...I].map(s=>`"${s}"`).join(", ")}). Rename the table.`);if(X.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(!H.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.`)},Y=e=>e&&W.has(e)?e:void 0,ee=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},te=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",u;return o&&i.isObjectLiteralExpression(o)&&(l=d(o,"field")??l,c=d(o,"references")??c,u=s==="one"?Y(d(o,"onDelete")):void 0),{field:l,kind:s,name:e.getName(),onDelete:u,references:c,table:a}},ne=e=>{const n=ee(e);if(!n)return[];const t=[];for(const s of n.getProperties()){const r=te(s);r&&t.push(r)}return t},re=(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.`)},se=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)},ie=e=>{const[n,t,s]=e,r=se(s),o=b(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)re(c,c.getLiteralText(),o);return{fields:l.map(c=>c.getLiteralText()),name:o,unique:r}},oe=e=>{const[n,t]=e;let s="_unknown_",r,o,a,l;return t&&i.isObjectLiteralExpression(t)&&(s=d(t,"field")??s,r=x(t,"filterFields"),o=d(t,"language"),a=Z(t,"staged"),l=d(t,"strategy")),{field:s,filterFields:r,language:o,name:b(n),staged:a,strategy:l}},ae=e=>{const[n,t]=e;let s="_unknown_",r;return t&&i.isObjectLiteralExpression(t)&&(s=d(t,"field")??s,r=m(t,"precision")),{field:s,name:b(n),precision:r}},le=e=>{const[n,t]=e,s=n&&i.isStringLiteral(n)?n.getLiteralText():"_unknown_";let r;return t&&i.isObjectLiteralExpression(t)&&(r=m(t,"after")),{after:r,field:s}},ce=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}},de=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=>ce(l)).filter(l=>l!==void 0))}r=x(t,"partitionBy")}return{name:b(n),partitionBy:r,sortBy:s}},fe=(e,n)=>{const[t,s]=e,r=t&&i.isStringLiteral(t)?t.getLiteralText():"_unknown_";if(!(!s||!i.isObjectLiteralExpression(s)))return{dimensions:m(s,"dimensions"),field:r,metadata:x(s,"metadata"),metric:w(d(s,"metric")),name:d(s,"index")??"_unnamed_",table:n}},ue=e=>{const n=e[0];return n&&i.isObjectLiteralExpression(n)&&d(n,"backend")==="hyperdrive"?"hyperdrive":"d1"},pe=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"},ge=e=>{const n=e[0];return!n||!i.isObjectLiteralExpression(n)?{binding:"",hasTenantBy:!1,unanalyzable:!0}:{binding:d(n,"binding")??"",columns:Q(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")}},me=(e,n,t,s)=>{switch(n){case"commitOrdered":{e.commitOrdered=!0;break}case"externallyManaged":{e.externallyManaged=!0;break}case"geoIndex":{e.geoIndexes.push(ae(t));break}case"global":{e.shardMode="global",e.globalBackend=ue(t);break}case"index":{e.indexes.push(ie(t));break}case"memory":{e.memory=!0;break}case"public":{e.isPublic=!0;break}case"rankIndex":{e.rankIndexes.push(de(t));break}case"relations":{const r=t[0];r&&e.relations.push(...ne(r));break}case"searchIndex":{e.searchIndexes.push(oe(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:pe(t[0])};break}case"source":{e.externalSource=ge(t),e.externallyManaged=!0;break}case"ttl":{e.ttl=le(t);break}case"vectorize":{const r=fe(t,s);r&&e.vectorIndexes.push(r);break}}},S=["__vocab","_config","_content","_data","_docsize","_idx"],xe=(e,n,t)=>{const s=new Set(t.map(r=>r.name));for(const r of t)for(const o of S){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 ${S.join(", ")}.`)}},O=(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))me(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=D(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"}}),xe(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}},be=e=>{const n=[],t=new Set;for(const s of e.getProperties()){if(!i.isPropertyAssignment(s))continue;const r=s.getInitializer();if(r){const o=s.getName();L(o,s.getNameNode());const a=k(o);if(t.has(a))throw f(s.getNameNode(),`defineSchema({...}): table "${a}" is declared more than once — the earlier declaration would be silently discarded. Remove the duplicate.`);t.add(a),n.push(O(r,o))}}return n},he=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_"},ye=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:m(s,"dimensions"),metric:w(d(s,"metric")),name:o,table:he(s)}},P=e=>{const n=[];for(const t of e.getProperties()){const s=ye(t);s&&n.push(s)}return n},v=(e,n)=>`${e}_${n}`,y=(e,n,t)=>t.has(e)?v(n,e):e,ve=(e,n,t)=>{const s=v(n,e.name);return{...e,extensionKey:n,name:s,relations:e.relations.map(r=>({...r,table:y(r.table,n,t)})),vectorIndexes:e.vectorIndexes.map(r=>({...r,table:y(r.table,n,t)}))}},Ee=e=>{if(i.isVariableDeclaration(e)||i.isPropertyAssignment(e))return e.getInitializer();if(i.isShorthandPropertyAssignment(e))return e.getNameNode()},Ie=e=>{if(!i.isCallExpression(e))return!1;const n=e.getExpression();return i.isIdentifier(n)&&n.getText()==="defineSchemaExtension"},Se=e=>{if(i.isIdentifier(e))return e;if(i.isPropertyAccessExpression(e)){const n=e.getExpression();return i.isIdentifier(n)?n:void 0}},ke=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}}},we=(e,n)=>{const t=Ee(e);if(t){if(i.isPropertyAccessExpression(n)){const s=ke(t);return s?g(s,n.getName()):void 0}return t}},Te=e=>{let n=e,t=0;for(;n&&t<32;){if(t+=1,Ie(n))return n;const s=Se(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=we(a,n)}},Le=e=>{const[n,t]=e.getArguments();if(!(!n||!i.isStringLiteral(n))&&!(!t||!i.isObjectLiteralExpression(t)))return{key:n.getLiteralText(),options:t}},Oe=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();L(a,r.getNameNode()),s.push(O(o,a))}return s},Pe=e=>{const n=e.getProperty("vectorIndexes");if(!n||!i.isPropertyAssignment(n))return[];const t=n.getInitializer();return!t||!i.isObjectLiteralExpression(t)?[]:P(t)},N=(e,n,t)=>{const s=new Set(n.map(a=>a.name)),r=n.map(a=>ve(a,e,s)),o=t.map(a=>({...a,name:v(e,a.name),table:y(a.table,e,s)}));return{tables:r,vectorIndexes:o}},Ne=(e,n)=>N(e,Oe(n),Pe(n)),Ae=(e,n)=>{const t=e.getArguments()[0];if(!t)return;const s=Te(t);if(!s){if(n!==void 0){const o=K(t,n);if(o)return N(o.key,o.bareTables,o.bareVectorIndexes)}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=Le(s);if(!r){console.warn("@lunora/codegen: skipping `.extend(...)` — `defineSchemaExtension` requires a string `key` and an options object literal.");return}return Ne(r.key,r.options)},_e=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},je=(e,n,t)=>{const s=new Set(n.map(o=>o.name)),r=[];for(const o of _e(e)){const a=Ae(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},$e=new Set(["eu","fedramp","us"]),ze=e=>T(e,"jurisdiction","jurisdiction",$e,'"eu", "us", or "fedramp"'),De=new Set(["required"]),Ce=e=>T(e,"rls","rls mode",De,'"required"'),Je=(e,n,t)=>{const r=e.addSourceFileAtPath(n).getDescendantsOfKind(j.CallExpression).find(h=>{const E=h.getExpression();return i.isIdentifier(E)&&E.getText()==="defineSchema"});if(!r)throw new _("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=be(o),l=r.getArguments()[1],c=l&&i.isObjectLiteralExpression(l)?P(l):[],u=je(r,a,t),A=[...a.flatMap(h=>h.vectorIndexes),...c,...u];return{jurisdiction:ze(r),rlsMode:Ce(r),tables:a,vectorIndexes:A}};export{Je as default};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{Node as u}from"ts-morph";import{l as f,a as p,c as l}from"./ast-CjWtsS6S.mjs";import{w as d}from"./classify-procedure-call-AKeIK05y.mjs";import{e as m}from"./exported-procedure-chains-MlZRKqSD.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 a=[];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 i=l(o,"prefix"),c={bucket:l(o,"bucket")??"",file:s,on:r,procedure:n};i!==void 0&&(c.prefix=i),a.push(c)}return a},A=(t,s)=>{const n=[];for(const e of f(s)){const a=t.getSourceFile(e)??t.addSourceFileAtPath(e),o=p(s,e);for(const{name:r,receiver:i}of m(a))for(const c of g(i))n.push(...x(c,o,r))}return{rules:n}};export{A as default};
|