Accessing changes & lastRowId fields from D1Result object

I’ve noticed that changes and lastRowId has been removed from the D1Result object somewhere between v3.18.0 and v4.20221111.1 of @cloudflare/workers-types. These fields are still present in a prepare().run() runtime result and contain the desired values when doing local development using wrangler (v2.6.2). However the same code on a Cloudflare worker has the fields present in the Object but explicitly set to null.

e.g. in wrangler

{
  results: null,
  duration: 0.8914030194282532,
  lastRowId: 14,
  changes: 1,
  success: true,
  served_by: 'x-miniflare.db3'
}

but same code on a worker the lastRowId and changes fields are null

        {
          "results": null,
          "duration": 16.87849497795105,
          "lastRowId": null,
          "changes": null,
          "success": true,
          "served_by": "primary-REDACTED.db3",
          "meta": {
            "duration": 16.87849497795105,
            "last_row_id": null,
            "changes": null,
            "served_by": "primary-REDACTED.db3",
            "internal_stats": null
          }
        }

Do you plan to restore these fields back to the D1 API? They are pretty standard in any framework that does database manipulation (e.g. INSERT, DELETE) so I am somewhat surprised that they have been removed. If there is no plans to restore these, what is the recommended way to access the metadata about the mutations in that sqllite transaction?

2 Likes

same problem, insert didn’t return AUTOINCREMENT id

In regular SQLite, you would use result.lastInsertRowid to get the ID of the last inserted row.

In Cloudflare D1, the equivalent is result.meta.last_row_id.