@platformatic/sql-mapper 0.9.2 → 0.11.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/entity.js CHANGED
@@ -85,9 +85,9 @@ function createMapper (defaultDb, sql, log, table, fields, primaryKeys, relation
85
85
  }
86
86
 
87
87
  let now
88
- if (autoTimestamp && fields.updated_at) {
88
+ if (autoTimestamp && fields[autoTimestamp.updatedAt]) {
89
89
  now = new Date()
90
- input.updated_at = now
90
+ input[autoTimestamp.updatedAt] = now
91
91
  }
92
92
  if (hasPrimaryKeys) { // update
93
93
  const res = await queries.updateOne(db, sql, table, schema, input, primaryKeys, fieldsToRetrieve)
@@ -100,10 +100,10 @@ function createMapper (defaultDb, sql, log, table, fields, primaryKeys, relation
100
100
  }
101
101
 
102
102
  // insert
103
- if (autoTimestamp && fields.inserted_at) {
103
+ if (autoTimestamp && fields[autoTimestamp.createdAt]) {
104
104
  /* istanbul ignore next */
105
105
  now = now || new Date()
106
- input.inserted_at = now
106
+ input[autoTimestamp.createdAt] = now
107
107
  }
108
108
  const res = await queries.insertOne(db, sql, table, schema, input, primaryKeysTypes, fieldsToRetrieve)
109
109
  return fixOutput(res)
@@ -118,11 +118,11 @@ function createMapper (defaultDb, sql, log, table, fields, primaryKeys, relation
118
118
  if (autoTimestamp) {
119
119
  const now = new Date()
120
120
  for (const input of inputs) {
121
- if (fields.inserted_at) {
122
- input.insertedAt = now
121
+ if (fields[autoTimestamp.createdAt]) {
122
+ input[autoTimestamp.createdAt] = now
123
123
  }
124
- if (fields.updated_at) {
125
- input.updatedAt = now
124
+ if (fields[autoTimestamp.updatedAt]) {
125
+ input[autoTimestamp.updatedAt] = now
126
126
  }
127
127
  }
128
128
  }
@@ -151,10 +151,9 @@ function createMapper (defaultDb, sql, log, table, fields, primaryKeys, relation
151
151
  throw new Error('Input not provided.')
152
152
  }
153
153
  const input = fixInput(args.input)
154
- let now
155
- if (autoTimestamp && fields.updated_at) {
156
- now = new Date()
157
- input.updated_at = now
154
+ if (autoTimestamp && fields[autoTimestamp.updatedAt]) {
155
+ const now = new Date()
156
+ input[autoTimestamp.updatedAt] = now
158
157
  }
159
158
  const criteria = computeCriteria(args)
160
159
 
@@ -340,7 +339,7 @@ async function buildEntity (db, sql, log, table, queries, autoTimestamp, schema,
340
339
  acc[column.column_name].enum = column.column_type.match(/'(.+?)'/g).map(enumValue => enumValue.slice(1, enumValue.length - 1))
341
340
  }
342
341
 
343
- if (autoTimestamp && (column.column_name === 'updated_at' || column.column_name === 'inserted_at')) {
342
+ if (autoTimestamp && (column.column_name === autoTimestamp.createdAt || column.column_name === autoTimestamp.updatedAt)) {
344
343
  acc[column.column_name].autoTimestamp = true
345
344
  }
346
345
  return acc
package/mapper.js CHANGED
@@ -1,3 +1,5 @@
1
+ 'use strict'
2
+
1
3
  const buildEntity = require('./lib/entity')
2
4
  const queriesFactory = require('./lib/queries')
3
5
  const fp = require('fastify-plugin')
@@ -39,7 +41,15 @@ async function buildConnection (log, createConnectionPool, connectionString, poo
39
41
  return db
40
42
  }
41
43
 
44
+ const defaultAutoTimestampFields = {
45
+ createdAt: 'created_at',
46
+ updatedAt: 'updated_at'
47
+ }
48
+
42
49
  async function connect ({ connectionString, log, onDatabaseLoad, poolSize = 10, ignore = {}, autoTimestamp = true, hooks = {}, schema, limit = {} }) {
50
+ if (typeof autoTimestamp === 'boolean' && autoTimestamp === true) {
51
+ autoTimestamp = defaultAutoTimestampFields
52
+ }
43
53
  // TODO validate config using the schema
44
54
  if (!connectionString) {
45
55
  throw new Error('connectionString is required')
@@ -115,7 +125,7 @@ async function connect ({ connectionString, log, onDatabaseLoad, poolSize = 10,
115
125
  const entity = await buildEntity(db, sql, log, table, queries, autoTimestamp, schema, useSchema, ignore[table] || {}, limit)
116
126
  // Check for primary key of all entities
117
127
  if (entity.primaryKeys.size === 0) {
118
- throw Error(`Cannot find any primary keys for ${entity.name} entity`)
128
+ throw Error(`Cannot find any primary keys for ${entity.name} entity: ${JSON.stringify(entity)}`)
119
129
  }
120
130
 
121
131
  entities[entity.singularName] = entity
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platformatic/sql-mapper",
3
- "version": "0.9.2",
3
+ "version": "0.11.0",
4
4
  "description": "A data mapper utility for SQL databases",
5
5
  "main": "mapper.js",
6
6
  "repository": {
package/test/helper.js CHANGED
@@ -4,7 +4,12 @@
4
4
  // See https://node-postgres.com/features/types/
5
5
  process.env.TZ = 'UTC'
6
6
 
7
- const connInfo = {}
7
+ const connInfo = {
8
+ autoTimestamp: {
9
+ createdAt: 'inserted_at',
10
+ updatedAt: 'updated_at'
11
+ }
12
+ }
8
13
 
9
14
  if (!process.env.DB || process.env.DB === 'postgresql') {
10
15
  connInfo.connectionString = 'postgres://postgres:postgres@127.0.0.1/postgres'
@@ -248,7 +248,7 @@ test('updateMany successful and update updated_at', async ({ pass, teardown, sam
248
248
  title VARCHAR(42),
249
249
  long_text TEXT,
250
250
  counter INTEGER,
251
- inserted_at TIMESTAMP,
251
+ created_at TIMESTAMP,
252
252
  updated_at TIMESTAMP
253
253
  );`)
254
254
  } else if (isMysql) {
@@ -257,7 +257,7 @@ test('updateMany successful and update updated_at', async ({ pass, teardown, sam
257
257
  title VARCHAR(42),
258
258
  long_text TEXT,
259
259
  counter INTEGER,
260
- inserted_at TIMESTAMP NULL DEFAULT NULL,
260
+ created_at TIMESTAMP NULL DEFAULT NULL,
261
261
  updated_at TIMESTAMP NULL DEFAULT NULL
262
262
  );`)
263
263
  } else {
@@ -266,7 +266,7 @@ test('updateMany successful and update updated_at', async ({ pass, teardown, sam
266
266
  title VARCHAR(42),
267
267
  long_text TEXT,
268
268
  counter INTEGER,
269
- inserted_at TIMESTAMP,
269
+ created_at TIMESTAMP,
270
270
  updated_at TIMESTAMP
271
271
  );`)
272
272
  }
@@ -313,6 +313,6 @@ test('updateMany successful and update updated_at', async ({ pass, teardown, sam
313
313
 
314
314
  const updatedPost3 = (await entity.find({ where: { id: { eq: '3' } } }))[0]
315
315
  same(updatedPost3.title, 'Updated title')
316
- same(createdPost3.insertedAt, updatedPost3.insertedAt)
316
+ same(createdPost3.createdAt, updatedPost3.createdAt)
317
317
  notSame(createdPost3.updatedAt, updatedPost3.updatedAt)
318
318
  })