Skip to content

Commit fcc5da2

Browse files
committed
feat: improve boolean conversion handling in record processing
1 parent d381496 commit fcc5da2

2 files changed

Lines changed: 30 additions & 8 deletions

File tree

lib/private/machines/private/process-each-record.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ module.exports = function processEachRecord(options) {
4242
// Update the orm object with the keyed collections
4343
orm.collections = collections
4444

45-
// Process each record
45+
// Process each record using waterline-utils' eachRecordDeep
46+
// which handles recursively processing populated associations
4647
eachRecordDeep(
4748
records,
4849
(record, WLModel) => {
@@ -63,9 +64,17 @@ module.exports = function processEachRecord(options) {
6364
if (columnName in record) {
6465
switch (attrDef.type) {
6566
case 'boolean':
66-
// SQLite stores booleans as integers, so we need to convert them
67+
// SQLite stores booleans as integers (0 = false, 1 = true)
68+
// Values may come back as numbers (1, 1.0) or strings ('1', '1.0')
6769
if (typeof record[columnName] !== 'boolean') {
68-
record[columnName] = record[columnName] === 1
70+
const rawValue = record[columnName]
71+
if (rawValue !== undefined && rawValue !== null) {
72+
const numericValue =
73+
typeof rawValue === 'string'
74+
? parseFloat(rawValue)
75+
: rawValue
76+
record[columnName] = numericValue !== 0
77+
}
6978
}
7079
break
7180

lib/private/machines/private/process-native-record.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,18 @@ module.exports = function processNativeRecord(nativeRecord, WLModel, meta) {
2727

2828
// Check out each known attribute...
2929
Object.entries(WLModel.attributes).forEach(([attrName, attrDef]) => {
30-
const phRecordKey = attrDef.columnName
30+
// Use columnName if defined, otherwise fall back to attribute name
31+
const columnName = attrDef.columnName || attrName
32+
33+
// If columnName differs from attrName, we need to rename the key
34+
// SQLite returns data with column names, but Waterline expects attribute names
35+
if (columnName !== attrName && nativeRecord[columnName] !== undefined) {
36+
nativeRecord[attrName] = nativeRecord[columnName]
37+
delete nativeRecord[columnName]
38+
}
39+
40+
// Now work with the attribute name
41+
const phRecordKey = attrName
3142

3243
// Handle JSON type
3344
if (
@@ -69,12 +80,14 @@ module.exports = function processNativeRecord(nativeRecord, WLModel, meta) {
6980
// Handle Boolean type
7081
if (attrDef.type === 'boolean') {
7182
// SQLite stores booleans as integers (0 = false, 1 = true)
72-
// But they come back as strings, so we need to parse them first
83+
// Values may come back as numbers (1, 1.0) or strings ('1', '1.0')
7384
const rawValue = nativeRecord[phRecordKey]
7485
if (rawValue !== undefined && rawValue !== null) {
75-
const numericValue =
76-
typeof rawValue === 'string' ? parseFloat(rawValue) : rawValue
77-
nativeRecord[phRecordKey] = numericValue === 0 ? false : true
86+
if (typeof rawValue !== 'boolean') {
87+
const numericValue =
88+
typeof rawValue === 'string' ? parseFloat(rawValue) : rawValue
89+
nativeRecord[phRecordKey] = numericValue !== 0
90+
}
7891
}
7992
}
8093

0 commit comments

Comments
 (0)