| 119 | * ``` |
| 120 | */ |
| 121 | export class CamelCasePlugin implements KyselyPlugin { |
| 122 | readonly #camelCase: StringMapper |
| 123 | readonly #snakeCase: StringMapper |
| 124 | readonly #snakeCaseTransformer: SnakeCaseTransformer |
| 125 | |
| 126 | constructor(readonly opt: CamelCasePluginOptions = {}) { |
| 127 | this.#camelCase = createCamelCaseMapper(opt) |
| 128 | this.#snakeCase = createSnakeCaseMapper(opt) |
| 129 | |
| 130 | this.#snakeCaseTransformer = new SnakeCaseTransformer( |
| 131 | this.snakeCase.bind(this), |
| 132 | ) |
| 133 | } |
| 134 | |
| 135 | transformQuery(args: PluginTransformQueryArgs): RootOperationNode { |
| 136 | return this.#snakeCaseTransformer.transformNode(args.node, args.queryId) |
| 137 | } |
| 138 | |
| 139 | async transformResult( |
| 140 | args: PluginTransformResultArgs, |
| 141 | ): Promise<QueryResult<UnknownRow>> { |
| 142 | if (args.result.rows && Array.isArray(args.result.rows)) { |
| 143 | return { |
| 144 | ...args.result, |
| 145 | rows: args.result.rows.map((row) => this.mapRow(row)), |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | return args.result |
| 150 | } |
| 151 | |
| 152 | protected mapRow(row: UnknownRow): UnknownRow { |
| 153 | return Object.keys(row).reduce<UnknownRow>((obj, key) => { |
| 154 | let value = row[key] |
| 155 | |
| 156 | if (Array.isArray(value)) { |
| 157 | value = value.map((it) => (canMap(it, this.opt) ? this.mapRow(it) : it)) |
| 158 | } else if (canMap(value, this.opt)) { |
| 159 | value = this.mapRow(value) |
| 160 | } |
| 161 | |
| 162 | obj[this.camelCase(key)] = value |
| 163 | return obj |
| 164 | }, {}) |
| 165 | } |
| 166 | |
| 167 | protected snakeCase(str: string): string { |
| 168 | return this.#snakeCase(str) |
| 169 | } |
| 170 | |
| 171 | protected camelCase(str: string): string { |
| 172 | return this.#camelCase(str) |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | function canMap( |
| 177 | obj: unknown, |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…