#Batch transaction in AstroDB

20 messages · Page 1 of 1 (latest)

floral tinsel
#

Im wondering why I keep getting an error while Im trying to do a batch transaction with AstroDB.
If I hardcoded this will work:

await db.batch([
  db.update(Instalments).set({ 
  verified: true
}).where(eq(Instalments.id, "123123")),
  db.update(Instalments).set({
  verified: true
}).where(eq(Instalments.id, "321321"))
])

But when I try to do with variables ids and values wont work:

const queries = instalments.map((item) => 
  db.update(Instalments)
  .set({ verified: item.verified })
  .where(eq(Instalments.id, item.id))
);
        
const promise = await db.batch(queries);
flint belfry
#

What is the value of queries after its mapped over?

floral tinsel
#

I tried checking with console.log to see if there was any difference, here, I show you:

    const instalments = [{ id: "123", verified: true }, { id: "321", verified: false }]

    const queries = instalments.map((item) => 
        db.update(Instalments)
          .set({ verified: item.verified })
          .where(eq(Instalments.id, item.id))
    );
[
  SQLiteUpdateBase {
    config: {
      set: [Object],
      table: [SQLiteTable],
      withList: undefined,
      where: [SQL]
    },
    run: [Function: run],
    all: [Function: all],
    get: [Function: get],
    values: [Function: values],
    session: LibSQLSession {
      dialect: SQLiteAsyncDialect {},
      logger: NoopLogger {},
      client: [Sqlite3Client],
      schema: undefined,
      options: [Object],
      tx: undefined
    },
    dialect: SQLiteAsyncDialect {},
    [Symbol(Symbol.toStringTag)]: 'QueryPromise'
  },
  SQLiteUpdateBase {
    config: {
      set: [Object],
      table: [SQLiteTable],
      withList: undefined,
      where: [SQL]
    },
    run: [Function: run],
    all: [Function: all],
    get: [Function: get],
    values: [Function: values],
    session: LibSQLSession {
      dialect: SQLiteAsyncDialect {},
      logger: NoopLogger {},
      client: [Sqlite3Client],
      schema: undefined,
      options: [Object],
      tx: undefined
    },
    dialect: SQLiteAsyncDialect {},
    [Symbol(Symbol.toStringTag)]: 'QueryPromise'
  }
]
#
console.log("SECOND", [
    db.update(Instalments).set({ 
        verified: true
    }).where(eq(Instalments.id, "123123")),
    db.update(Instalments).set({
        verified: true
    }).where(eq(Instalments.id, "321321"))
])
[
  SQLiteUpdateBase {
    config: {
      set: [Object],
      table: [SQLiteTable],
      withList: undefined,
      where: [SQL]
    },
    run: [Function: run],
    all: [Function: all],
    get: [Function: get],
    values: [Function: values],
    session: LibSQLSession {
      dialect: SQLiteAsyncDialect {},
      logger: NoopLogger {},
      client: [Sqlite3Client],
      schema: undefined,
      options: [Object],
      tx: undefined
    },
    dialect: SQLiteAsyncDialect {},
    [Symbol(Symbol.toStringTag)]: 'QueryPromise'
  },
  SQLiteUpdateBase {
    config: {
      set: [Object],
      table: [SQLiteTable],
      withList: undefined,
      where: [SQL]
    },
    run: [Function: run],
    all: [Function: all],
    get: [Function: get],
    values: [Function: values],
    session: LibSQLSession {
      dialect: SQLiteAsyncDialect {},
      logger: NoopLogger {},
      client: [Sqlite3Client],
      schema: undefined,
      options: [Object],
      tx: undefined
    },
    dialect: SQLiteAsyncDialect {},
    [Symbol(Symbol.toStringTag)]: 'QueryPromise'
  }
]
#

Sorry for the length, the first message is with the code with a .map, and the second is a hardcoded console.log. In both logs, there are just two objects in the array and looks the same

flint belfry
#

Can you try something like this

const instalments = [{ id: "123", verified: true }, { id: "321", verified: false }]
const queries = []
for(const [index,value] of instalments){
  queries.push(db.update(Instalments).set({verfied: value.verified}).where(ew(Instalments.id, value.id));
}
await db.batch(queries);
floral tinsel
#

Yeah, let me try

floral tinsel
#
Argument of type 'Omit<SQLiteUpdateBase<Table<"Instalments", { id: { type: "text"; schema: { unique: false; deprecated: false; name: "id"; collection: "Instalments"; primaryKey: true; }; }; paymentId: { type: "text"; schema: { unique: false; ... 5 more ...; references: { ...; }; }; }; paid_amount: { ...; }; verified: { ...; }; create...' is not assignable to parameter of type 'readonly [BatchItem<"sqlite">, ...BatchItem<"sqlite">[]]'.
  Source provides no match for required element at position 0 in target.
flint belfry
#

@minor dawn

minor dawn
#

Drizzle problem

#

Workaround in the issue 🙂

floral tinsel
#

The solution suggested works

#

The weird thing to me is that even with an error, the code works and updates the rows

#

idk

minor dawn
#

It's a type error, the code is correct

#

Just TS validation will complain

flint belfry
#

You can do a //@ts-ignore ? Of some of them I believe to surpress it.