@platformatic/sql-mapper 1.45.1 → 1.47.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.
@@ -64,43 +64,48 @@ async function insertMany (db, sql, table, schema, inputs, inputToFieldMap, prim
64
64
  }
65
65
 
66
66
  function insertPrep (inputs, inputToFieldMap, fields, sql) {
67
- const inputSet = new Set()
68
- const values = []
69
- for (const input of inputs) {
70
- const inputValues = []
71
- for (const key of Object.keys(input)) {
72
- let newKey = key
73
- if (inputToFieldMap[key] === undefined) {
74
- if (fields[key] === undefined) {
75
- throw new errors.UnknownFieldError(key)
76
- }
77
- } else {
78
- newKey = inputToFieldMap[key]
79
- }
67
+ const tableFields = Object.keys(fields)
68
+ const inputRaws = []
80
69
 
81
- inputSet.add(newKey)
70
+ for (const input of inputs) {
71
+ for (const entityKey of Object.keys(input)) {
72
+ const field = inputToFieldMap[entityKey]
82
73
 
83
- let value = input[key] ?? input[newKey]
74
+ if (field === undefined && fields[entityKey] === undefined) {
75
+ throw new errors.UnknownFieldError(entityKey)
76
+ }
77
+ }
84
78
 
85
- if (value && !fields[newKey].isArray && typeof value === 'object' && !(value instanceof Date)) {
79
+ const inputValues = []
80
+ for (const field of tableFields) {
81
+ const fieldMetadata = fields[field]
82
+ const inputKey = fieldMetadata.camelcase
83
+
84
+ let inputValue = input[inputKey] ?? input[field]
85
+ if (
86
+ inputValue &&
87
+ typeof inputValue === 'object' &&
88
+ !fieldMetadata.isArray &&
89
+ !(inputValue instanceof Date)
90
+ ) {
86
91
  // This is a JSON field
87
- value = JSON.stringify(value)
92
+ inputValue = JSON.stringify(inputValue)
88
93
  }
89
94
 
90
- inputValues.push(sql.value(value))
95
+ if (inputValue !== undefined) {
96
+ inputValues.push(sql.value(inputValue))
97
+ } else {
98
+ inputValues.push(sql`DEFAULT`)
99
+ }
91
100
  }
92
-
93
- values.push(sql` (${sql.join(
94
- inputValues,
95
- sql`, `
96
- )})`)
101
+ inputRaws.push(sql` (${sql.join(inputValues, sql`, `)})`)
97
102
  }
98
- const inputKeys = Array.from(inputSet)
99
103
  const keys = sql.join(
100
- inputKeys.map((key) => sql.ident(key)),
104
+ tableFields.map((key) => sql.ident(key)),
101
105
  sql`, `
102
106
  )
103
- return { keys, values }
107
+
108
+ return { keys, values: inputRaws }
104
109
  }
105
110
 
106
111
  async function updateMany (db, sql, table, schema, criteria, input, fieldsToRetrieve) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platformatic/sql-mapper",
3
- "version": "1.45.1",
3
+ "version": "1.47.0",
4
4
  "description": "A data mapper utility for SQL databases",
5
5
  "main": "mapper.js",
6
6
  "types": "mapper.d.ts",
@@ -16,7 +16,7 @@
16
16
  "homepage": "https://github.com/platformatic/platformatic#readme",
17
17
  "devDependencies": {
18
18
  "@matteo.collina/tspl": "^0.1.1",
19
- "borp": "^0.14.0",
19
+ "borp": "^0.15.0",
20
20
  "fastify": "^4.26.2",
21
21
  "snazzy": "^9.0.0",
22
22
  "standard": "^17.1.0",
@@ -33,7 +33,7 @@
33
33
  "camelcase": "^6.3.0",
34
34
  "fastify-plugin": "^4.5.1",
35
35
  "inflected": "^2.1.0",
36
- "@platformatic/utils": "1.45.1"
36
+ "@platformatic/utils": "1.47.0"
37
37
  },
38
38
  "tsd": {
39
39
  "directory": "test/types"