@platformatic/sql-mapper 3.4.1 → 3.5.1

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.
@@ -1,11 +1,2 @@
1
- 'use strict'
2
-
3
- const shared = require('./shared')
4
- const mysql = require('./mysql-shared')
5
-
6
- module.exports = {
7
- ...mysql,
8
- insertOne: shared.insertOne,
9
- insertMany: shared.insertMany,
10
- deleteAll: shared.deleteAll,
11
- }
1
+ export { listColumns, listConstraints, listTables, updateMany, updateOne } from './mysql-shared.js'
2
+ export { deleteAll, insertMany, insertOne } from './shared.js'
@@ -1,8 +1,6 @@
1
- 'use strict'
1
+ import { tableName } from '../utils.js'
2
2
 
3
- const { tableName } = require('../utils')
4
-
5
- async function listTables (db, sql, schemas) {
3
+ export async function listTables (db, sql, schemas) {
6
4
  if (schemas) {
7
5
  const schemaList = sql.__dangerous__rawValue(schemas.map(s => `'${s}'`))
8
6
  const res = await db.query(sql`
@@ -21,7 +19,7 @@ async function listTables (db, sql, schemas) {
21
19
  }
22
20
  }
23
21
 
24
- async function listColumns (db, sql, table, schema) {
22
+ export async function listColumns (db, sql, table, schema) {
25
23
  const query = sql`
26
24
  SELECT column_name as column_name, data_type as udt_name, is_nullable as is_nullable, column_type as column_type, extra as is_generated
27
25
  FROM information_schema.columns
@@ -31,7 +29,7 @@ async function listColumns (db, sql, table, schema) {
31
29
  return db.query(query)
32
30
  }
33
31
 
34
- async function listConstraints (db, sql, table, schema) {
32
+ export async function listConstraints (db, sql, table, schema) {
35
33
  const query = sql`
36
34
  SELECT TABLE_NAME as table_name, TABLE_SCHEMA as table_schema, COLUMN_NAME as column_name, CONSTRAINT_TYPE as constraint_type, referenced_table_name AS foreign_table_name, referenced_table_schema AS foreign_table_schema, referenced_column_name AS foreign_column_name
37
35
  FROM information_schema.table_constraints t
@@ -43,8 +41,8 @@ async function listConstraints (db, sql, table, schema) {
43
41
  return db.query(query)
44
42
  }
45
43
 
46
- async function updateOne (db, sql, table, schema, input, primaryKeys, fieldsToRetrieve) {
47
- const pairs = Object.keys(input).map((key) => {
44
+ export async function updateOne (db, sql, table, schema, input, primaryKeys, fieldsToRetrieve) {
45
+ const pairs = Object.keys(input).map(key => {
48
46
  let value = input[key]
49
47
  /* istanbul ignore next */
50
48
  if (value && typeof value === 'object' && !(value instanceof Date)) {
@@ -73,8 +71,8 @@ async function updateOne (db, sql, table, schema, input, primaryKeys, fieldsToRe
73
71
  return res[0]
74
72
  }
75
73
 
76
- async function updateMany (db, sql, table, schema, criteria, input, fieldsToRetrieve) {
77
- const pairs = Object.keys(input).map((key) => {
74
+ export async function updateMany (db, sql, table, schema, criteria, input, fieldsToRetrieve) {
75
+ const pairs = Object.keys(input).map(key => {
78
76
  let value = input[key]
79
77
  /* istanbul ignore next */
80
78
  if (value && typeof value === 'object' && !(value instanceof Date)) {
@@ -108,12 +106,4 @@ async function updateMany (db, sql, table, schema, criteria, input, fieldsToRetr
108
106
  return res
109
107
  }
110
108
 
111
- module.exports = {
112
- listTables,
113
- listColumns,
114
- listConstraints,
115
- updateOne,
116
- updateMany,
117
- }
118
-
119
- module.exports.hasILIKE = false
109
+ export const hasILIKE = false
@@ -1,18 +1,14 @@
1
- 'use strict'
1
+ import { MissingValueForPrimaryKeyError } from '../errors.js'
2
+ import { tableName } from '../utils.js'
3
+ import { insertPrep } from './shared.js'
2
4
 
3
- const { insertPrep } = require('./shared')
4
- const shared = require('./mysql-shared')
5
- const { tableName } = require('../utils')
6
- const errors = require('../errors')
5
+ export * from './mysql-shared.js'
7
6
 
8
- function insertOne (db, sql, table, schema, input, primaryKeys, fieldsToRetrieve) {
9
- const keysToSql = Object.keys(input).map((key) => sql.ident(key))
10
- const keys = sql.join(
11
- keysToSql,
12
- sql`, `
13
- )
7
+ export function insertOne (db, sql, table, schema, input, primaryKeys, fieldsToRetrieve) {
8
+ const keysToSql = Object.keys(input).map(key => sql.ident(key))
9
+ const keys = sql.join(keysToSql, sql`, `)
14
10
 
15
- const valuesToSql = Object.keys(input).map((key) => {
11
+ const valuesToSql = Object.keys(input).map(key => {
16
12
  /* istanbul ignore next */
17
13
  if (input[key] && typeof input[key] === 'object' && !(input[key] instanceof Date)) {
18
14
  // This is a JSON field
@@ -20,10 +16,7 @@ function insertOne (db, sql, table, schema, input, primaryKeys, fieldsToRetrieve
20
16
  }
21
17
  return sql.value(input[key])
22
18
  })
23
- const values = sql.join(
24
- valuesToSql,
25
- sql`, `
26
- )
19
+ const values = sql.join(valuesToSql, sql`, `)
27
20
 
28
21
  if (primaryKeys.length === 1 && input[primaryKeys[0].key] === undefined) {
29
22
  return db.tx(async function (db) {
@@ -49,7 +42,7 @@ function insertOne (db, sql, table, schema, input, primaryKeys, fieldsToRetrieve
49
42
  // TODO write a test that cover this
50
43
  /* istanbul ignore next */
51
44
  if (!input[key]) {
52
- throw new errors.MissingValueForPrimaryKeyError(key)
45
+ throw new MissingValueForPrimaryKeyError(key)
53
46
  }
54
47
  where.push(sql`${sql.ident(key)} = ${input[key]}`)
55
48
  }
@@ -72,7 +65,7 @@ function insertOne (db, sql, table, schema, input, primaryKeys, fieldsToRetrieve
72
65
  }
73
66
  }
74
67
 
75
- function insertMany (db, sql, table, schema, inputs, inputToFieldMap, primaryKeys, fieldsToRetrieve, fields) {
68
+ export function insertMany (db, sql, table, schema, inputs, inputToFieldMap, primaryKeys, fieldsToRetrieve, fields) {
76
69
  return db.tx(async function (db) {
77
70
  const { keys, values } = insertPrep(inputs, inputToFieldMap, fields, sql)
78
71
  const insert = sql`
@@ -111,7 +104,7 @@ function insertMany (db, sql, table, schema, inputs, inputToFieldMap, primaryKey
111
104
  })
112
105
  }
113
106
 
114
- function deleteAll (db, sql, table, schema, criteria, fieldsToRetrieve) {
107
+ export function deleteAll (db, sql, table, schema, criteria, fieldsToRetrieve) {
115
108
  return db.tx(async function (db) {
116
109
  let selectQuery = sql`
117
110
  SELECT ${sql.join(fieldsToRetrieve, sql`, `)}
@@ -144,10 +137,3 @@ function deleteAll (db, sql, table, schema, criteria, fieldsToRetrieve) {
144
137
  return res
145
138
  })
146
139
  }
147
-
148
- module.exports = {
149
- ...shared,
150
- insertOne,
151
- insertMany,
152
- deleteAll,
153
- }
package/lib/queries/pg.js CHANGED
@@ -1,9 +1,8 @@
1
- 'use strict'
1
+ import { tableName } from '../utils.js'
2
+ import { insertOne as sharedInsertOne } from './shared.js'
3
+ export { deleteAll, insertMany, updateMany } from './shared.js'
2
4
 
3
- const shared = require('./shared')
4
- const { tableName } = require('../utils')
5
-
6
- async function insertOne (db, sql, table, schema, input, primaryKeys, fieldsToRetrieve) {
5
+ export async function insertOne (db, sql, table, schema, input, primaryKeys, fieldsToRetrieve) {
7
6
  const inputKeys = Object.keys(input)
8
7
  if (inputKeys.length === 0) {
9
8
  const insert = sql`
@@ -14,14 +13,10 @@ async function insertOne (db, sql, table, schema, input, primaryKeys, fieldsToRe
14
13
  const res = await db.query(insert)
15
14
  return res[0]
16
15
  }
17
- return shared.insertOne(db, sql, table, schema, input, primaryKeys, fieldsToRetrieve)
16
+ return sharedInsertOne(db, sql, table, schema, input, primaryKeys, fieldsToRetrieve)
18
17
  }
19
18
 
20
- module.exports.insertOne = insertOne
21
- module.exports.deleteAll = shared.deleteAll
22
- module.exports.insertMany = shared.insertMany
23
-
24
- async function listTables (db, sql, schemas) {
19
+ export async function listTables (db, sql, schemas) {
25
20
  if (schemas) {
26
21
  const schemaList = sql.__dangerous__rawValue(schemas.map(s => `'${s}'`))
27
22
  const res = await db.query(sql`
@@ -40,9 +35,7 @@ async function listTables (db, sql, schemas) {
40
35
  return res.map(r => ({ schema: r.schemaname, table: r.tablename }))
41
36
  }
42
37
 
43
- module.exports.listTables = listTables
44
-
45
- async function listColumns (db, sql, table, schema) {
38
+ export async function listColumns (db, sql, table, schema) {
46
39
  /*
47
40
  return db.query(sql`
48
41
  SELECT column_name, udt_name, is_nullable, is_generated
@@ -71,9 +64,7 @@ async function listColumns (db, sql, table, schema) {
71
64
  return res
72
65
  }
73
66
 
74
- module.exports.listColumns = listColumns
75
-
76
- async function listConstraints (db, sql, table, schema) {
67
+ export async function listConstraints (db, sql, table, schema) {
77
68
  const query = sql`
78
69
  SELECT constraints.*, usage.*, usage2.table_name AS foreign_table_name, usage2.column_name AS foreign_column_name, usage2.table_schema AS foreign_table_schema
79
70
  FROM information_schema.table_constraints constraints
@@ -89,10 +80,8 @@ async function listConstraints (db, sql, table, schema) {
89
80
  return constraintsList
90
81
  }
91
82
 
92
- module.exports.listConstraints = listConstraints
93
-
94
- async function updateOne (db, sql, table, schema, input, primaryKeys, fieldsToRetrieve) {
95
- const pairs = Object.keys(input).map((key) => {
83
+ export async function updateOne (db, sql, table, schema, input, primaryKeys, fieldsToRetrieve) {
84
+ const pairs = Object.keys(input).map(key => {
96
85
  const value = input[key]
97
86
  return sql`${sql.ident(key)} = ${value}`
98
87
  })
@@ -110,11 +99,7 @@ async function updateOne (db, sql, table, schema, input, primaryKeys, fieldsToRe
110
99
  return res[0]
111
100
  }
112
101
 
113
- module.exports.updateOne = updateOne
114
-
115
- module.exports.updateMany = shared.updateMany
116
-
117
- async function listEnumValues (db, sql, table, schema) {
102
+ export async function listEnumValues (db, sql, table, schema) {
118
103
  const query = sql`
119
104
  SELECT udt_name, enumlabel, column_name
120
105
  FROM pg_enum e
@@ -126,6 +111,4 @@ async function listEnumValues (db, sql, table, schema) {
126
111
  return db.query(query)
127
112
  }
128
113
 
129
- module.exports.listEnumValues = listEnumValues
130
-
131
- module.exports.hasILIKE = true
114
+ export const hasILIKE = true
@@ -1,11 +1,9 @@
1
- 'use strict'
2
-
3
- const { tableName } = require('../utils')
4
- const errors = require('../errors')
1
+ import { UnknownFieldError } from '../errors.js'
2
+ import { tableName } from '../utils.js'
5
3
 
6
4
  /* istanbul ignore file */
7
5
 
8
- async function insertOne (db, sql, table, schema, input, primaryKeysTypes, fieldsToRetrieve) {
6
+ export async function insertOne (db, sql, table, schema, input, primaryKeysTypes, fieldsToRetrieve) {
9
7
  const inputKeys = Object.keys(input)
10
8
  if (inputKeys.length === 0) {
11
9
  const insert = sql`
@@ -19,14 +17,16 @@ async function insertOne (db, sql, table, schema, input, primaryKeysTypes, field
19
17
  }
20
18
 
21
19
  const keys = sql.join(
22
- inputKeys.map((key) => sql.ident(key)),
20
+ inputKeys.map(key => sql.ident(key)),
23
21
  sql`, `
24
22
  )
25
23
  const values = sql.join(
26
- Object.keys(input).map((key) => {
24
+ Object.keys(input).map(key => {
27
25
  const val = input[key]
28
26
  return sql.value(val)
29
- }), sql`, `)
27
+ }),
28
+ sql`, `
29
+ )
30
30
 
31
31
  const insert = sql`
32
32
  INSERT INTO ${tableName(sql, table, schema)} (${keys})
@@ -37,7 +37,7 @@ async function insertOne (db, sql, table, schema, input, primaryKeysTypes, field
37
37
  return res[0]
38
38
  }
39
39
 
40
- async function deleteAll (db, sql, table, schema, criteria, fieldsToRetrieve) {
40
+ export async function deleteAll (db, sql, table, schema, criteria, fieldsToRetrieve) {
41
41
  let query = sql`
42
42
  DELETE FROM ${tableName(sql, table, schema)}
43
43
  `
@@ -51,7 +51,17 @@ async function deleteAll (db, sql, table, schema, criteria, fieldsToRetrieve) {
51
51
  return res
52
52
  }
53
53
 
54
- async function insertMany (db, sql, table, schema, inputs, inputToFieldMap, primaryKey, fieldsToRetrieve, fields) {
54
+ export async function insertMany (
55
+ db,
56
+ sql,
57
+ table,
58
+ schema,
59
+ inputs,
60
+ inputToFieldMap,
61
+ primaryKey,
62
+ fieldsToRetrieve,
63
+ fields
64
+ ) {
55
65
  const { keys, values } = insertPrep(inputs, inputToFieldMap, fields, sql)
56
66
  const insert = sql`
57
67
  insert into ${tableName(sql, table, schema)} (${keys})
@@ -63,7 +73,7 @@ async function insertMany (db, sql, table, schema, inputs, inputToFieldMap, prim
63
73
  return res
64
74
  }
65
75
 
66
- function insertPrep (inputs, inputToFieldMap, fields, sql) {
76
+ export function insertPrep (inputs, inputToFieldMap, fields, sql) {
67
77
  const tableFields = Object.keys(fields)
68
78
  const inputRaws = []
69
79
 
@@ -72,7 +82,7 @@ function insertPrep (inputs, inputToFieldMap, fields, sql) {
72
82
  const field = inputToFieldMap[entityKey]
73
83
 
74
84
  if (field === undefined && fields[entityKey] === undefined) {
75
- throw new errors.UnknownFieldError(entityKey)
85
+ throw new UnknownFieldError(entityKey)
76
86
  }
77
87
  }
78
88
 
@@ -82,12 +92,7 @@ function insertPrep (inputs, inputToFieldMap, fields, sql) {
82
92
  const inputKey = fieldMetadata.camelcase
83
93
 
84
94
  let inputValue = input[inputKey] ?? input[field]
85
- if (
86
- inputValue &&
87
- typeof inputValue === 'object' &&
88
- !fieldMetadata.isArray &&
89
- !(inputValue instanceof Date)
90
- ) {
95
+ if (inputValue && typeof inputValue === 'object' && !fieldMetadata.isArray && !(inputValue instanceof Date)) {
91
96
  // This is a JSON field
92
97
  inputValue = JSON.stringify(inputValue)
93
98
  }
@@ -101,15 +106,15 @@ function insertPrep (inputs, inputToFieldMap, fields, sql) {
101
106
  inputRaws.push(sql` (${sql.join(inputValues, sql`, `)})`)
102
107
  }
103
108
  const keys = sql.join(
104
- tableFields.map((key) => sql.ident(key)),
109
+ tableFields.map(key => sql.ident(key)),
105
110
  sql`, `
106
111
  )
107
112
 
108
113
  return { keys, values: inputRaws }
109
114
  }
110
115
 
111
- async function updateMany (db, sql, table, schema, criteria, input, fieldsToRetrieve) {
112
- const pairs = Object.keys(input).map((key) => {
116
+ export async function updateMany (db, sql, table, schema, criteria, input, fieldsToRetrieve) {
117
+ const pairs = Object.keys(input).map(key => {
113
118
  const value = input[key]
114
119
  return sql`${sql.ident(key)} = ${value}`
115
120
  })
@@ -122,11 +127,3 @@ async function updateMany (db, sql, table, schema, criteria, input, fieldsToRetr
122
127
  const res = await db.query(update)
123
128
  return res
124
129
  }
125
-
126
- module.exports = {
127
- insertOne,
128
- insertPrep,
129
- deleteAll,
130
- insertMany,
131
- updateMany,
132
- }
@@ -1,7 +1,5 @@
1
- 'use strict'
2
-
3
- const { randomUUID } = require('crypto')
4
- const errors = require('../errors')
1
+ import { randomUUID } from 'crypto'
2
+ import { SQLiteOnlySupportsAutoIncrementOnOneColumnError } from '../errors.js'
5
3
 
6
4
  function fixValue (value) {
7
5
  if (value instanceof Date) {
@@ -12,7 +10,7 @@ function fixValue (value) {
12
10
  return value
13
11
  }
14
12
 
15
- async function listTables (db, sql) {
13
+ export async function listTables (db, sql) {
16
14
  const res = await db.query(sql`
17
15
  SELECT name FROM sqlite_master
18
16
  WHERE type='table'
@@ -21,9 +19,7 @@ async function listTables (db, sql) {
21
19
  return res.map(r => ({ schema: null, table: r.name }))
22
20
  }
23
21
 
24
- module.exports.listTables = listTables
25
-
26
- async function listColumns (db, sql, table) {
22
+ export async function listColumns (db, sql, table) {
27
23
  // pragma_table_info is not returning hidden column which tells if the column is generated or not
28
24
  // therefore it is changed to pragma_table_xinfo
29
25
  const columns = await db.query(sql`
@@ -36,14 +32,12 @@ async function listColumns (db, sql, table) {
36
32
  // convert is_nullable
37
33
  column.is_nullable = column.notnull === 0 && column.pk === 0 ? 'YES' : 'NO'
38
34
  // convert hidden to is_generated
39
- column.is_generated = (column.hidden === 2 || column.hidden === 3) ? 'YES' : 'NO'
35
+ column.is_generated = column.hidden === 2 || column.hidden === 3 ? 'YES' : 'NO'
40
36
  }
41
37
  return columns
42
38
  }
43
39
 
44
- module.exports.listColumns = listColumns
45
-
46
- async function listConstraints (db, sql, table) {
40
+ export async function listConstraints (db, sql, table) {
47
41
  const constraints = []
48
42
  const pks = await db.query(sql`
49
43
  SELECT *
@@ -54,7 +48,7 @@ async function listConstraints (db, sql, table) {
54
48
  for (const pk of pks) {
55
49
  constraints.push({
56
50
  column_name: pk.name,
57
- constraint_type: 'PRIMARY KEY',
51
+ constraint_type: 'PRIMARY KEY'
58
52
  })
59
53
  }
60
54
 
@@ -69,7 +63,7 @@ async function listConstraints (db, sql, table) {
69
63
  if (index.unique === 1) {
70
64
  constraints.push({
71
65
  column_name: index.name,
72
- constraint_type: 'UNIQUE',
66
+ constraint_type: 'UNIQUE'
73
67
  })
74
68
  }
75
69
  }
@@ -85,15 +79,13 @@ async function listConstraints (db, sql, table) {
85
79
  column_name: foreignKey.from,
86
80
  constraint_type: 'FOREIGN KEY',
87
81
  foreign_table_name: foreignKey.table,
88
- foreign_column_name: foreignKey.to,
82
+ foreign_column_name: foreignKey.to
89
83
  })
90
84
  }
91
85
  return constraints
92
86
  }
93
87
 
94
- module.exports.listConstraints = listConstraints
95
-
96
- async function insertOne (db, sql, table, schema, input, primaryKeys, fieldsToRetrieve) {
88
+ export async function insertOne (db, sql, table, schema, input, primaryKeys, fieldsToRetrieve) {
97
89
  const primaryKeyValues = {}
98
90
 
99
91
  let hasAutoIncrementPK = false
@@ -109,7 +101,7 @@ async function insertOne (db, sql, table, schema, input, primaryKeys, fieldsToRe
109
101
  primaryKeyValue = null
110
102
  hasAutoIncrementPK = true
111
103
  } else {
112
- throw new errors.SQLiteOnlySupportsAutoIncrementOnOneColumnError()
104
+ throw new SQLiteOnlySupportsAutoIncrementOnOneColumnError()
113
105
  }
114
106
  input[key] = primaryKeyValue
115
107
  }
@@ -170,10 +162,8 @@ async function insertOne (db, sql, table, schema, input, primaryKeys, fieldsToRe
170
162
  }
171
163
  }
172
164
 
173
- module.exports.insertOne = insertOne
174
-
175
- async function updateOne (db, sql, table, schema, input, primaryKeys, fieldsToRetrieve) {
176
- const pairs = Object.keys(input).map((key) => {
165
+ export async function updateOne (db, sql, table, schema, input, primaryKeys, fieldsToRetrieve) {
166
+ const pairs = Object.keys(input).map(key => {
177
167
  const value = input[key]
178
168
  return sql`${sql.ident(key)} = ${fixValue(value)}`
179
169
  })
@@ -199,9 +189,7 @@ async function updateOne (db, sql, table, schema, input, primaryKeys, fieldsToRe
199
189
  return res[0]
200
190
  }
201
191
 
202
- module.exports.updateOne = updateOne
203
-
204
- async function deleteAll (db, sql, table, schema, criteria, fieldsToRetrieve) {
192
+ export async function deleteAll (db, sql, table, schema, criteria, fieldsToRetrieve) {
205
193
  let query = sql`
206
194
  SELECT ${sql.join(fieldsToRetrieve, sql`, `)}
207
195
  FROM ${sql.ident(table)}
@@ -228,10 +216,8 @@ async function deleteAll (db, sql, table, schema, criteria, fieldsToRetrieve) {
228
216
  return data
229
217
  }
230
218
 
231
- module.exports.deleteAll = deleteAll
232
-
233
- async function updateMany (db, sql, table, schema, criteria, input, fieldsToRetrieve) {
234
- const pairs = Object.keys(input).map((key) => {
219
+ export async function updateMany (db, sql, table, schema, criteria, input, fieldsToRetrieve) {
220
+ const pairs = Object.keys(input).map(key => {
235
221
  const value = input[key]
236
222
  return sql`${sql.ident(key)} = ${fixValue(value)}`
237
223
  })
@@ -245,6 +231,4 @@ async function updateMany (db, sql, table, schema, criteria, input, fieldsToRetr
245
231
  return res
246
232
  }
247
233
 
248
- module.exports.updateMany = updateMany
249
-
250
- module.exports.hasILIKE = false
234
+ export const hasILIKE = false
package/lib/telemetry.js CHANGED
@@ -1,6 +1,4 @@
1
- 'use strict'
2
-
3
- function wrapQuery (app, db, request) {
1
+ export function wrapQuery (app, db, request) {
4
2
  const { startSpan, endSpan, SpanKind } = app.openTelemetry
5
3
  async function wrappedQuery () {
6
4
  const query = arguments[0]
@@ -18,8 +16,8 @@ function wrapQuery (app, db, request) {
18
16
  }
19
17
 
20
18
  const format = {
21
- escapeIdentifier: (str) => (str),
22
- formatValue: (value, index) => ({ placeholder: `$${index + 1}`, value }),
19
+ escapeIdentifier: str => str,
20
+ formatValue: (value, index) => ({ placeholder: `$${index + 1}`, value })
23
21
  }
24
22
  const { text: queryText } = query.format(format)
25
23
  // We get the name form the first 20 characters of the query
@@ -33,7 +31,7 @@ function wrapQuery (app, db, request) {
33
31
  const telemetryAttributes = {
34
32
  'db.statement': queryText,
35
33
  'db.system': dbSystem,
36
- 'db.name': database,
34
+ 'db.name': database
37
35
  }
38
36
 
39
37
  if (!db.isSQLite) {
@@ -57,12 +55,12 @@ function wrapQuery (app, db, request) {
57
55
  return wrappedQuery
58
56
  }
59
57
 
60
- function wrapDB (app, db, request) {
58
+ export function wrapDB (app, db, request) {
61
59
  const newDb = Object.create(db)
62
60
  const connectionInfo = db.connectionInfo
63
61
  newDb.query = wrapQuery(app, db, request)
64
62
  newDb.tx = function wrappedTx (func) {
65
- return db.tx((db) => {
63
+ return db.tx(db => {
66
64
  db.connectionInfo = connectionInfo
67
65
  const _newDb = Object.create(db)
68
66
  _newDb.query = wrapQuery(app, db, request)
@@ -72,7 +70,7 @@ function wrapDB (app, db, request) {
72
70
  return newDb
73
71
  }
74
72
 
75
- const setupTelemetry = app => {
73
+ export function setupTelemetry (app) {
76
74
  // Decorate the request with the wrapped DB.
77
75
  // We need that for the queries written directly using `db`
78
76
  if (app.platformatic.db) {
@@ -81,8 +79,3 @@ const setupTelemetry = app => {
81
79
  })
82
80
  }
83
81
  }
84
-
85
- module.exports = {
86
- setupTelemetry,
87
- wrapDB,
88
- }
package/lib/utils.js CHANGED
@@ -1,20 +1,18 @@
1
- 'use strict'
1
+ import camelcase from 'camelcase'
2
+ import { singularize } from 'inflected'
3
+ import { ParamLimitMustBeNotNegativeError, ParamLimitNotAllowedError } from './errors.js'
2
4
 
3
- const { singularize } = require('inflected')
4
- const camelcase = require('camelcase')
5
- const errors = require('./errors')
6
-
7
- function toUpperFirst (str) {
5
+ export function toUpperFirst (str) {
8
6
  str = str.toString()
9
7
  return str[0].toUpperCase() + str.slice(1)
10
8
  }
11
9
 
12
- function toLowerFirst (str) {
10
+ export function toLowerFirst (str) {
13
11
  str = str.toString()
14
12
  return str.charAt(0).toLowerCase() + str.slice(1)
15
13
  }
16
14
 
17
- function toSingular (str) {
15
+ export function toSingular (str) {
18
16
  str = camelcase(singularize(str))
19
17
  str = toUpperFirst(str)
20
18
  return str
@@ -24,36 +22,27 @@ function toSingular (str) {
24
22
  * If limit is not defined or invalid
25
23
  * let's set a safe default value preventing to load huge amount of data in memory
26
24
  */
27
- function sanitizeLimit (unsafeLimit, conf) {
25
+ export function sanitizeLimit (unsafeLimit, conf) {
28
26
  const defaultLimit = conf?.default ?? 10
29
- const limit = (unsafeLimit !== undefined) ? unsafeLimit : defaultLimit
27
+ const limit = unsafeLimit !== undefined ? unsafeLimit : defaultLimit
30
28
  const max = conf?.max ?? 100
31
29
 
32
30
  if (limit > max) {
33
- throw new errors.ParamLimitNotAllowedError(limit, max)
31
+ throw new ParamLimitNotAllowedError(limit, max)
34
32
  }
35
33
 
36
34
  if (limit < 0) {
37
- throw new errors.ParamLimitMustBeNotNegativeError(limit)
35
+ throw new ParamLimitMustBeNotNegativeError(limit)
38
36
  }
39
37
 
40
38
  return limit
41
39
  }
42
40
 
43
- function tableName (sql, table, schema) {
41
+ export function tableName (sql, table, schema) {
44
42
  /* istanbul ignore next */
45
43
  return schema ? sql.ident(schema, table) : sql.ident(table)
46
44
  }
47
45
 
48
- function areSchemasSupported (sql) {
46
+ export function areSchemasSupported (sql) {
49
47
  return !sql.isSQLite
50
48
  }
51
-
52
- module.exports = {
53
- toSingular,
54
- toUpperFirst,
55
- toLowerFirst,
56
- sanitizeLimit,
57
- tableName,
58
- areSchemasSupported,
59
- }