{"version":3,"file":"selection-proxy.cjs","names":["entityKind","ViewBaseConfig","is","Subquery","View","SQL","Column","ColumnTableAliasProxyHandler","TableAliasProxyHandler"],"sources":["../src/selection-proxy.ts"],"sourcesContent":["import { ColumnTableAliasProxyHandler, TableAliasProxyHandler } from './alias.ts';\nimport { Column } from './column.ts';\nimport { entityKind, is } from './entity.ts';\nimport { SQL, View } from './sql/sql.ts';\nimport { Subquery } from './subquery.ts';\nimport { ViewBaseConfig } from './view-common.ts';\n\nexport class SelectionProxyHandler<T extends Subquery | Record<string, unknown> | View>\n\timplements ProxyHandler<Subquery | Record<string, unknown> | View>\n{\n\tstatic readonly [entityKind]: string = 'SelectionProxyHandler';\n\n\tprivate config: {\n\t\t/**\n\t\t * Table alias for the columns\n\t\t */\n\t\talias?: string;\n\t\t/**\n\t\t * What to do when a field is an instance of `SQL.Aliased` and it's not a selection field (from a subquery)\n\t\t *\n\t\t * `sql` - return the underlying SQL expression\n\t\t *\n\t\t * `alias` - return the field alias\n\t\t */\n\t\tsqlAliasedBehavior: 'sql' | 'alias';\n\t\t/**\n\t\t * What to do when a field is an instance of `SQL` and it doesn't have an alias declared\n\t\t *\n\t\t * `sql` - return the underlying SQL expression\n\t\t *\n\t\t * `error` - return a DrizzleTypeError on type level and throw an error on runtime\n\t\t */\n\t\tsqlBehavior: 'sql' | 'error';\n\n\t\t/**\n\t\t * Whether to replace the original name of the column with the alias\n\t\t * Should be set to `true` for views creation\n\t\t * @default false\n\t\t */\n\t\treplaceOriginalName?: boolean;\n\t};\n\n\tconstructor(config: SelectionProxyHandler<T>['config']) {\n\t\tthis.config = { ...config };\n\t}\n\n\tget(subquery: T, prop: string | symbol): any {\n\t\tif (prop === '_') {\n\t\t\treturn {\n\t\t\t\t...subquery['_' as keyof typeof subquery],\n\t\t\t\tselectedFields: new Proxy(\n\t\t\t\t\t(subquery as Subquery)._.selectedFields,\n\t\t\t\t\tthis as ProxyHandler<Record<string, unknown>>,\n\t\t\t\t),\n\t\t\t};\n\t\t}\n\n\t\tif (prop === ViewBaseConfig) {\n\t\t\treturn {\n\t\t\t\t...subquery[ViewBaseConfig as keyof typeof subquery],\n\t\t\t\tselectedFields: new Proxy(\n\t\t\t\t\t(subquery as View)[ViewBaseConfig].selectedFields,\n\t\t\t\t\tthis as ProxyHandler<Record<string, unknown>>,\n\t\t\t\t),\n\t\t\t};\n\t\t}\n\n\t\tif (typeof prop === 'symbol') {\n\t\t\treturn subquery[prop as keyof typeof subquery];\n\t\t}\n\n\t\tconst columns = is(subquery, Subquery)\n\t\t\t? subquery._.selectedFields\n\t\t\t: is(subquery, View)\n\t\t\t? subquery[ViewBaseConfig].selectedFields\n\t\t\t: subquery;\n\t\tconst value: unknown = columns[prop as keyof typeof columns];\n\n\t\tif (is(value, SQL.Aliased)) {\n\t\t\t// Never return the underlying SQL expression for a field previously selected in a subquery\n\t\t\tif (this.config.sqlAliasedBehavior === 'sql' && !value.isSelectionField) {\n\t\t\t\treturn value.sql;\n\t\t\t}\n\n\t\t\tconst newValue = value.clone();\n\t\t\tnewValue.isSelectionField = true;\n\t\t\tnewValue.origin = this.config.alias;\n\t\t\treturn newValue;\n\t\t}\n\n\t\tif (is(value, SQL)) {\n\t\t\tif (this.config.sqlBehavior === 'sql') {\n\t\t\t\treturn value;\n\t\t\t}\n\n\t\t\tthrow new Error(\n\t\t\t\t`You tried to reference \"${prop}\" field from a subquery, which is a raw SQL field, but it doesn't have an alias declared. Please add an alias to the field using \".as('alias')\" method.`,\n\t\t\t);\n\t\t}\n\n\t\tif (is(value, Column)) {\n\t\t\tif (this.config.alias) {\n\t\t\t\treturn new Proxy(\n\t\t\t\t\tvalue,\n\t\t\t\t\tnew ColumnTableAliasProxyHandler(\n\t\t\t\t\t\tnew Proxy(\n\t\t\t\t\t\t\tvalue.table,\n\t\t\t\t\t\t\tnew TableAliasProxyHandler(this.config.alias, this.config.replaceOriginalName ?? false, true),\n\t\t\t\t\t\t),\n\t\t\t\t\t\ttrue,\n\t\t\t\t\t),\n\t\t\t\t);\n\t\t\t}\n\t\t\treturn value;\n\t\t}\n\n\t\tif (typeof value !== 'object' || value === null) {\n\t\t\treturn value;\n\t\t}\n\n\t\treturn new Proxy(value, new SelectionProxyHandler(this.config));\n\t}\n}\n"],"mappings":";;;;;;;;;AAOA,IAAa,wBAAb,MAAa,sBAEb;CACC,QAAiBA,6BAAsB;CAEvC,AAAQ;CA8BR,YAAY,QAA4C;AACvD,OAAK,SAAS,EAAE,GAAG,QAAQ;;CAG5B,IAAI,UAAa,MAA4B;AAC5C,MAAI,SAAS,IACZ,QAAO;GACN,GAAG,SAAS;GACZ,gBAAgB,IAAI,MAClB,SAAsB,EAAE,gBACzB,KACA;GACD;AAGF,MAAI,SAASC,mCACZ,QAAO;GACN,GAAG,SAASA;GACZ,gBAAgB,IAAI,MAClB,SAAkBA,oCAAgB,gBACnC,KACA;GACD;AAGF,MAAI,OAAO,SAAS,SACnB,QAAO,SAAS;EAQjB,MAAM,SALUC,kBAAG,UAAUC,0BAAS,GACnC,SAAS,EAAE,iBACXD,kBAAG,UAAUE,qBAAK,GAClB,SAASH,oCAAgB,iBACzB,UAC4B;AAE/B,MAAIC,kBAAG,OAAOG,oBAAI,QAAQ,EAAE;AAE3B,OAAI,KAAK,OAAO,uBAAuB,SAAS,CAAC,MAAM,iBACtD,QAAO,MAAM;GAGd,MAAM,WAAW,MAAM,OAAO;AAC9B,YAAS,mBAAmB;AAC5B,YAAS,SAAS,KAAK,OAAO;AAC9B,UAAO;;AAGR,MAAIH,kBAAG,OAAOG,oBAAI,EAAE;AACnB,OAAI,KAAK,OAAO,gBAAgB,MAC/B,QAAO;AAGR,SAAM,IAAI,MACT,2BAA2B,KAAK,yJAChC;;AAGF,MAAIH,kBAAG,OAAOI,sBAAO,EAAE;AACtB,OAAI,KAAK,OAAO,MACf,QAAO,IAAI,MACV,OACA,IAAIC,2CACH,IAAI,MACH,MAAM,OACN,IAAIC,qCAAuB,KAAK,OAAO,OAAO,KAAK,OAAO,uBAAuB,OAAO,KAAK,CAC7F,EACD,KACA,CACD;AAEF,UAAO;;AAGR,MAAI,OAAO,UAAU,YAAY,UAAU,KAC1C,QAAO;AAGR,SAAO,IAAI,MAAM,OAAO,IAAI,sBAAsB,KAAK,OAAO,CAAC"}