@platformatic/sql-mapper 0.23.1 → 0.24.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/queries/shared.js +1 -1
- package/package.json +1 -1
- package/test/entity.test.js +29 -20
package/lib/queries/shared.js
CHANGED
|
@@ -77,7 +77,7 @@ function insertPrep (inputs, inputToFieldMap, fields, sql) {
|
|
|
77
77
|
|
|
78
78
|
inputSet.add(newKey)
|
|
79
79
|
|
|
80
|
-
let value = input[key]
|
|
80
|
+
let value = input[key] ?? input[newKey]
|
|
81
81
|
|
|
82
82
|
if (value && typeof value === 'object' && !(value instanceof Date)) {
|
|
83
83
|
// This is a JSON field
|
package/package.json
CHANGED
package/test/entity.test.js
CHANGED
|
@@ -50,16 +50,18 @@ test('entity API', async ({ equal, same, teardown, rejects }) => {
|
|
|
50
50
|
if (isSQLite) {
|
|
51
51
|
await db.query(sql`CREATE TABLE pages (
|
|
52
52
|
id INTEGER PRIMARY KEY,
|
|
53
|
-
the_title VARCHAR(42)
|
|
53
|
+
the_title VARCHAR(42),
|
|
54
|
+
is_published BOOLEAN NOT NULL
|
|
54
55
|
);`)
|
|
55
56
|
} else {
|
|
56
57
|
await db.query(sql`CREATE TABLE pages (
|
|
57
58
|
id SERIAL PRIMARY KEY,
|
|
58
|
-
the_title VARCHAR(255) NOT NULL
|
|
59
|
+
the_title VARCHAR(255) NOT NULL,
|
|
60
|
+
is_published BOOLEAN NOT NULL
|
|
59
61
|
);`)
|
|
60
62
|
}
|
|
61
|
-
await db.query(sql`INSERT INTO pages (the_title) VALUES ('foo')`)
|
|
62
|
-
await db.query(sql`INSERT INTO pages (the_title) VALUES ('bar')`)
|
|
63
|
+
await db.query(sql`INSERT INTO pages (the_title, is_published) VALUES ('foo', true)`)
|
|
64
|
+
await db.query(sql`INSERT INTO pages (the_title, is_published) VALUES ('bar', false)`)
|
|
63
65
|
}
|
|
64
66
|
const mapper = await connect({
|
|
65
67
|
connectionString: connInfo.connectionString,
|
|
@@ -70,16 +72,17 @@ test('entity API', async ({ equal, same, teardown, rejects }) => {
|
|
|
70
72
|
})
|
|
71
73
|
const pageEntity = mapper.entities.page
|
|
72
74
|
// fixInput
|
|
73
|
-
const fixedInput = pageEntity.fixInput({ id: 42, theTitle: 'Fixme' })
|
|
74
|
-
same(fixedInput, { id: 42, the_title: 'Fixme' })
|
|
75
|
+
const fixedInput = pageEntity.fixInput({ id: 42, theTitle: 'Fixme', isPublished: true })
|
|
76
|
+
same(fixedInput, { id: 42, the_title: 'Fixme', is_published: true })
|
|
75
77
|
|
|
76
78
|
// fixOutput
|
|
77
79
|
const fixedOutput = pageEntity.fixOutput({
|
|
78
80
|
id: 42,
|
|
79
|
-
the_title: 'Fixme'
|
|
81
|
+
the_title: 'Fixme',
|
|
82
|
+
is_published: true
|
|
80
83
|
})
|
|
81
84
|
|
|
82
|
-
same(fixedOutput, { id: 42, theTitle: 'Fixme' })
|
|
85
|
+
same(fixedOutput, { id: 42, theTitle: 'Fixme', isPublished: true })
|
|
83
86
|
|
|
84
87
|
// empty fixOutput
|
|
85
88
|
same(pageEntity.fixOutput(undefined), undefined)
|
|
@@ -94,29 +97,35 @@ test('entity API', async ({ equal, same, teardown, rejects }) => {
|
|
|
94
97
|
|
|
95
98
|
// insert - single
|
|
96
99
|
const insertResult = await pageEntity.insert({
|
|
97
|
-
inputs: [{ theTitle: 'foobar' }],
|
|
98
|
-
fields: ['id', 'theTitle']
|
|
100
|
+
inputs: [{ theTitle: 'foobar', isPublished: false }],
|
|
101
|
+
fields: ['id', 'theTitle', 'isPublished']
|
|
99
102
|
})
|
|
100
|
-
same(insertResult, [{ id: '3', theTitle: 'foobar' }])
|
|
103
|
+
same(insertResult, [{ id: '3', theTitle: 'foobar', isPublished: false }])
|
|
101
104
|
|
|
102
105
|
// insert - multiple
|
|
103
106
|
const insertMultipleResult = await pageEntity.insert({
|
|
104
|
-
inputs: [
|
|
105
|
-
|
|
107
|
+
inputs: [
|
|
108
|
+
{ theTitle: 'platformatic', isPublished: false },
|
|
109
|
+
{ theTitle: 'foobar', isPublished: true }
|
|
110
|
+
],
|
|
111
|
+
fields: ['id', 'theTitle', 'isPublished']
|
|
106
112
|
})
|
|
107
|
-
same(insertMultipleResult, [
|
|
113
|
+
same(insertMultipleResult, [
|
|
114
|
+
{ id: '4', theTitle: 'platformatic', isPublished: false },
|
|
115
|
+
{ id: '5', theTitle: 'foobar', isPublished: true }
|
|
116
|
+
])
|
|
108
117
|
|
|
109
118
|
// save - new record
|
|
110
119
|
same(await pageEntity.save({
|
|
111
|
-
input: { theTitle: 'fourth page' },
|
|
112
|
-
fields: ['id', 'theTitle']
|
|
113
|
-
}), { id: 6, theTitle: 'fourth page' })
|
|
120
|
+
input: { theTitle: 'fourth page', isPublished: false },
|
|
121
|
+
fields: ['id', 'theTitle', 'isPublished']
|
|
122
|
+
}), { id: 6, theTitle: 'fourth page', isPublished: false })
|
|
114
123
|
|
|
115
124
|
// save - update record
|
|
116
125
|
same(await pageEntity.save({
|
|
117
|
-
input: { id: 4, theTitle: 'foofoo' },
|
|
118
|
-
fields: ['id', 'theTitle']
|
|
119
|
-
}), { id: '4', theTitle: 'foofoo' })
|
|
126
|
+
input: { id: 4, theTitle: 'foofoo', isPublished: true },
|
|
127
|
+
fields: ['id', 'theTitle', 'isPublished']
|
|
128
|
+
}), { id: '4', theTitle: 'foofoo', isPublished: true })
|
|
120
129
|
|
|
121
130
|
// save - empty object
|
|
122
131
|
rejects(async () => {
|