I'm trying to save some images on a postgres db, but only 10 bytes of data are being saved.
The flow is something like this:
I receive a base64 encoded string on my server, then i load that to a Buffer, set it to my entity and save it. But then a try to restore that information from db and i'm getting only 10 bytes of data, verified with octet_length() on a query.
My entity attribute definition:
@Column({ "name": "entima_imagem", "type": "bytea", "nullable": false })
entima_imagem: Buffer;
The code where i receive the data and save it:
entity.entima_imagem = Buffer.from(base64String, "base64");
const repository = this.getRepositoryTarget(Entity);
const saved = await repository.save<any>(entity);
On the server, before saving, i'm writing the file on disc and i can visualize it without problem.
Based on that comment https://github.com/typeorm/typeorm/issues/2878#issuecomment-432725569 and the idea of bytea hex format from there https://www.postgresql.org/docs/9.0/datatype-binary.html I did the following:
Decoded the Buffer to a hex string, escaped it with \x and then loaded it to a Buffer again.
entity.entima_imagem = Buffer.from("\\x" + Buffer.from(base64String, "base64").toString("hex"));
Now the data is saved without problems and i can retrieve them just as it's supposed to be.
It didn't look so elegant, but solved the problem for now.