Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Value should be serializable #845

Open
BrandonDyer64 opened this issue Dec 8, 2024 · 0 comments
Open

Value should be serializable #845

BrandonDyer64 opened this issue Dec 8, 2024 · 0 comments

Comments

@BrandonDyer64
Copy link

BrandonDyer64 commented Dec 8, 2024

Motivation

For use with things like Cloudflare D1, I need to send the query string along with a serialized list of values across a wasm ffi boundary. In order to serialize the values, I have to break each variant apart and serialize it.

let (query, values) = Query::select()
    // ...
    .build(SqliteQueryBuilder);

let prepared: D1PreparedStatement = d1.prepare(query);

let serializer: Serializer = serde_wasm_bindgen::Serializer::new().serialize_missing_as_null(true);

let bindings: Vec<JsValue> = values
    .0
    .into_iter()
    .map(|value| {
        // Serialize the value
        match &value {
            Value::Bool(x) => x.serialize(&serializer),
            Value::TinyInt(x) => x.serialize(&serializer),
            Value::SmallInt(x) => x.serialize(&serializer),
            Value::Int(x) => x.serialize(&serializer),
            Value::BigInt(x) => x.serialize(&serializer),
            Value::TinyUnsigned(x) => x.serialize(&serializer),
            Value::SmallUnsigned(x) => x.serialize(&serializer),
            Value::Unsigned(x) => x.serialize(&serializer),
            Value::BigUnsigned(x) => x.serialize(&serializer),
            Value::Float(x) => x.serialize(&serializer),
            Value::Double(x) => x.serialize(&serializer),
            Value::String(x) => x.serialize(&serializer),
            Value::Char(x) => x.serialize(&serializer),
            Value::Bytes(x) => x.serialize(&serializer),
            Value::Json(x) => x.serialize(&serializer),
            Value::ChronoDate(x) => x.serialize(&serializer),
            Value::ChronoTime(x) => x.serialize(&serializer),
            Value::ChronoDateTime(x) => x.serialize(&serializer),
            Value::ChronoDateTimeUtc(x) => x.serialize(&serializer),
            Value::ChronoDateTimeLocal(x) => x.serialize(&serializer),
            Value::ChronoDateTimeWithTimeZone(x) => x.serialize(&serializer),
            Value::TimeDate(x) => x.serialize(&serializer),
            Value::TimeTime(x) => x.serialize(&serializer),
            Value::TimeDateTime(x) => x.serialize(&serializer),
            Value::TimeDateTimeWithTimeZone(x) => x.serialize(&serializer),
            Value::Uuid(x) => x.serialize(&serializer),
            Value::Decimal(x) => x.serialize(&serializer),
            Value::BigDecimal(x) => x.serialize(&serializer),
        }
        .map_err(|e| Error::Internal(e.into()))
    })
    .collect::<Result<Vec<_>>>()?;

let stmt: D1PreparedStatement = prepared.bind(&bindings)?;

let result: Vec<PostData> = stmt.run().await?.results::<PostData>()?;

Proposed Solutions

Add a serde feature that allows certain types, like Value and Values to be serialized.

let (query, values) = Query::select()
    // ...
    .build(SqliteQueryBuilder);

let prepared: D1PreparedStatement = d1.prepare(query);

let serializer: Serializer = serde_wasm_bindgen::Serializer::new().serialize_missing_as_null(true);

let bindings: Vec<JsValue> = values
    .0
    .into_iter()
    .map(|value| {
        // Serialize the value
        value.serialize(&serializer).map_err(|e| Error::Internal(e.into()))
    })
    .collect::<Result<Vec<_>>>()?;

let stmt: D1PreparedStatement = prepared.bind(&bindings)?;

let result: Vec<PostData> = stmt.run().await?.results::<PostData>()?;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant