chiselstrike icon indicating copy to clipboard operation
chiselstrike copied to clipboard

blob type to store images

Open 7flash opened this issue 1 year ago • 12 comments

7flash avatar Aug 31 '22 11:08 7flash

I need to store images in sqlite, and currently using String field for it, how can use blob instead?

7flash avatar Aug 31 '22 11:08 7flash

@7flash There's no API to do blobs, but I am actually working on it. But now we're debating with @honzasp whether the mapping type should be Blob or ArrayBuffer. What's your take on this @7flash?

penberg avatar Aug 31 '22 12:08 penberg

Deno defines Blob as

type BlobPart = BufferSource | Blob | string;
type BufferSource = ArrayBufferView | ArrayBuffer;

class Blob {
    constructor(blobParts?: BlobPart[], options?: BlobPropertyBag);

    stream(): ReadableStream<Uint8Array>;

    text(): Promise<string>;

	arrayBuffer(): Promise<ArrayBuffer>;

    // ...
}

that means it can be instantiated by passing an instance of ArrayBuffer in constructor and have it extracted as arrayBuffer()

But, Blob type in MySQL is actually just text, therefore ".text()" should be called on each field of Blob type asynchronously before saving the item into database?

7flash avatar Aug 31 '22 13:08 7flash

I see this as part of #1748 .

glommer avatar Sep 05 '22 17:09 glommer

@BearLemma do we depend on adding reflection to do this right ?

glommer avatar Sep 05 '22 19:09 glommer

Well if it's just about storing ArrayBuffers then I don't think so :thinking: . At least not in the general sense. We've been discussing that in that RFC on next-gen data model.

BearLemma avatar Sep 06 '22 18:09 BearLemma

@7flash We are working on Blob support right now. It's part of our Kafka producer effort, but should work for this use case too. Will update this ticket when the work lands in main hopefully in the next few weeks.

penberg avatar Sep 15 '22 11:09 penberg

@7flash with https://github.com/chiselstrike/chiselstrike/pull/1785 we've just landed the support for ArrayBuffer as entity field. I think it should be trivially possible to convert Blob to ArrayBuffer and store it that way. Is it going to solve your problem?

BearLemma avatar Oct 04 '22 14:10 BearLemma

@7flash with #1785 we've just landed the support for ArrayBuffer as entity field. I think it should be trivially possible to convert Blob to ArrayBuffer and store it that way. Is it going to solve your problem?

I am still storing images in String field.. is that very wrong? will it affect performance or anything if change to ArrayBuffer?

7flash avatar Oct 04 '22 14:10 7flash

@7flash with #1785 we've just landed the support for ArrayBuffer as entity field. I think it should be trivially possible to convert Blob to ArrayBuffer and store it that way. Is it going to solve your problem?

I am still storing images in String field.. is that very wrong? will it affect performance or anything if change to ArrayBuffer?

Not really very wrong :) just less efficient. ArrayBuffer will get stored as binary blob which is more efficient. In addition to that, you get the blobs base64 encoded when accessed via CRUD API which I think is particularly useful for images.

BearLemma avatar Oct 04 '22 14:10 BearLemma

I can see that Blobs were implemented internally, but should I still keep using string type for images? When I try to define a Blob field in model, it doesn't seem to recognize it:

field 'image' in class 'FortyEight' is of unknown entity type 'Blob'

7flash avatar Jan 22 '23 18:01 7flash

@7flash As I wrote, you need to use the ArrayBuffer type. That should work fine.

BearLemma avatar Jan 25 '23 07:01 BearLemma