import { abstractReturnShapeKey, brandWithType, ObjectRef, type SchemaTypes, typeBrandKey, } from '@pothos/core'; import type { WithBrand } from './types.js'; export const drizzleTableKey = Symbol.for('Pothos.drizzleTableKey'); export class DrizzleObjectRef< Types extends SchemaTypes, Table extends keyof Types['DrizzleRelations'] = keyof Types['DrizzleRelations'], T = {}, > extends ObjectRef< Types, | T | { $pothosQueryFor: Table | undefined; }, T > { [drizzleTableKey]!: Types['DrizzleRelations'][Table]; [abstractReturnShapeKey]!: WithBrand; tableName: string; constructor(name: string, tableName: string) { super(name); this.tableName = tableName; } addBrand( value: V, ): V extends T[] ? { [K in keyof V]: WithBrand } : WithBrand { if (Array.isArray(value)) { for (const val of value) { brandWithType(val, this.name as never); } return value as never; } brandWithType(value, this.name as never); return value as never; } hasBrand(value: unknown) { return ( typeof value === 'object' && value !== null && typeBrandKey in value && (value as { [typeBrandKey]?: unknown })[typeBrandKey] === this.name ); } }