summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Matthew Phillips <matthew@skypack.dev> 2024-05-21 17:12:39 -0400
committerGravatar GitHub <noreply@github.com> 2024-05-21 17:12:39 -0400
commite14ce5726df73e2988fe1a39e078ef2d66d2f4a8 (patch)
treec3ff9591130c35e4f8a9880f591279b50e8d3ef3
parentb78e83f448d142e83be592f6249c4822e7cd5726 (diff)
downloadastro-e14ce5726df73e2988fe1a39e078ef2d66d2f4a8.tar.gz
astro-e14ce5726df73e2988fe1a39e078ef2d66d2f4a8.tar.zst
astro-e14ce5726df73e2988fe1a39e078ef2d66d2f4a8.zip
Fix inconsistent results from raw SQL (#11091)
* Fix inconsistent results from raw SQL * Remove .only * Fix * Update packages/db/src/runtime/db-client.ts Co-authored-by: Ben Holmes <hey@bholmes.dev> --------- Co-authored-by: Ben Holmes <hey@bholmes.dev>
-rw-r--r--.changeset/honest-shirts-sell.md5
-rw-r--r--packages/db/src/runtime/db-client.ts18
-rw-r--r--packages/db/test/fixtures/static-remote/src/pages/run.astro17
-rw-r--r--packages/db/test/static-remote.test.js7
4 files changed, 46 insertions, 1 deletions
diff --git a/.changeset/honest-shirts-sell.md b/.changeset/honest-shirts-sell.md
new file mode 100644
index 000000000..e50be0516
--- /dev/null
+++ b/.changeset/honest-shirts-sell.md
@@ -0,0 +1,5 @@
+---
+"@astrojs/db": patch
+---
+
+Fix inconsistent result type using raw SQL
diff --git a/packages/db/src/runtime/db-client.ts b/packages/db/src/runtime/db-client.ts
index ecab7782d..3d62eba06 100644
--- a/packages/db/src/runtime/db-client.ts
+++ b/packages/db/src/runtime/db-client.ts
@@ -71,7 +71,23 @@ export function createRemoteDatabaseClient(appToken: string, remoteDbURL: string
});
}
- if (method === 'run') return remoteResult;
+ if (method === 'run') {
+ const rawRows = Array.from(remoteResult.rows);
+ // Implement basic `toJSON()` for Drizzle to serialize properly
+ (remoteResult as any).rows.toJSON = () => rawRows;
+ // Using `db.run()` drizzle massages the rows into an object.
+ // So in order to make dev/prod consistent, we need to do the same here.
+ // This creates an object and loops over each row replacing it with the object.
+ for(let i = 0; i < remoteResult.rows.length; i++) {
+ let row = remoteResult.rows[i];
+ let item: Record<string, any> = {};
+ remoteResult.columns.forEach((col, index) => {
+ item[col] = row[index];
+ });
+ (remoteResult as any).rows[i] = item;
+ }
+ return remoteResult;
+ }
// Drizzle expects each row as an array of its values
const rowValues: unknown[][] = [];
diff --git a/packages/db/test/fixtures/static-remote/src/pages/run.astro b/packages/db/test/fixtures/static-remote/src/pages/run.astro
new file mode 100644
index 000000000..2f2ac1cce
--- /dev/null
+++ b/packages/db/test/fixtures/static-remote/src/pages/run.astro
@@ -0,0 +1,17 @@
+---
+import { User, db, sql } from 'astro:db';
+
+const results = await db.run(sql`SELECT 1 as value`);
+const row = results.rows[0];
+---
+
+<html>
+ <head>
+ <title>Testing</title>
+ </head>
+ <body>
+ <h1>Testing</h1>
+
+ <span id="row">{row.value}</span>
+ </body>
+</html>
diff --git a/packages/db/test/static-remote.test.js b/packages/db/test/static-remote.test.js
index 7adcf6976..492a0d385 100644
--- a/packages/db/test/static-remote.test.js
+++ b/packages/db/test/static-remote.test.js
@@ -30,5 +30,12 @@ describe('astro:db', () => {
expect($('li').length).to.equal(1);
});
+
+ it('Returns correct shape from db.run()', async () => {
+ const html = await fixture.readFile('/run/index.html');
+ const $ = cheerioLoad(html);
+
+ expect($('#row').text()).to.equal('1');
+ });
});
});