@platformatic/sql-mapper 0.10.0 → 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 +12 -13
- package/mapper.js +9 -1
- package/package.json +1 -1
- package/test/helper.js +6 -1
- package/test/updateMany.test.js +4 -4
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.
|
|
88
|
+
if (autoTimestamp && fields[autoTimestamp.updatedAt]) {
|
|
89
89
|
now = new Date()
|
|
90
|
-
input.
|
|
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.
|
|
103
|
+
if (autoTimestamp && fields[autoTimestamp.createdAt]) {
|
|
104
104
|
/* istanbul ignore next */
|
|
105
105
|
now = now || new Date()
|
|
106
|
-
input.
|
|
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.
|
|
122
|
-
input.
|
|
121
|
+
if (fields[autoTimestamp.createdAt]) {
|
|
122
|
+
input[autoTimestamp.createdAt] = now
|
|
123
123
|
}
|
|
124
|
-
if (fields.
|
|
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
|
-
|
|
155
|
-
|
|
156
|
-
|
|
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 ===
|
|
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
|
@@ -41,7 +41,15 @@ async function buildConnection (log, createConnectionPool, connectionString, poo
|
|
|
41
41
|
return db
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
+
const defaultAutoTimestampFields = {
|
|
45
|
+
createdAt: 'created_at',
|
|
46
|
+
updatedAt: 'updated_at'
|
|
47
|
+
}
|
|
48
|
+
|
|
44
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
|
+
}
|
|
45
53
|
// TODO validate config using the schema
|
|
46
54
|
if (!connectionString) {
|
|
47
55
|
throw new Error('connectionString is required')
|
|
@@ -117,7 +125,7 @@ async function connect ({ connectionString, log, onDatabaseLoad, poolSize = 10,
|
|
|
117
125
|
const entity = await buildEntity(db, sql, log, table, queries, autoTimestamp, schema, useSchema, ignore[table] || {}, limit)
|
|
118
126
|
// Check for primary key of all entities
|
|
119
127
|
if (entity.primaryKeys.size === 0) {
|
|
120
|
-
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)}`)
|
|
121
129
|
}
|
|
122
130
|
|
|
123
131
|
entities[entity.singularName] = entity
|
package/package.json
CHANGED
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'
|
package/test/updateMany.test.js
CHANGED
|
@@ -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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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.
|
|
316
|
+
same(createdPost3.createdAt, updatedPost3.createdAt)
|
|
317
317
|
notSame(createdPost3.updatedAt, updatedPost3.updatedAt)
|
|
318
318
|
})
|