@platformatic/sql-mapper 0.45.1 → 0.46.2
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 +9 -0
- package/mapper.js +13 -0
- package/package.json +3 -2
- package/test/composite.test.js +0 -162
- package/test/create-connection.test.js +0 -39
- package/test/entity.test.js +0 -1125
- package/test/entity_transaction.test.js +0 -132
- package/test/helper.js +0 -112
- package/test/hooks.test.js +0 -325
- package/test/inserted_at_updated_at.test.js +0 -132
- package/test/mapper.test.js +0 -325
- package/test/no-primary-key.test.js +0 -79
- package/test/or.test.js +0 -193
- package/test/schema.test.js +0 -474
- package/test/types/mapper.test-d.ts +0 -139
- package/test/updateMany.test.js +0 -319
- package/test/where.test.js +0 -889
package/lib/entity.js
CHANGED
|
@@ -8,6 +8,7 @@ const {
|
|
|
8
8
|
sanitizeLimit
|
|
9
9
|
} = require('./utils')
|
|
10
10
|
const { singularize } = require('inflected')
|
|
11
|
+
const { findNearestString } = require('@platformatic/utils')
|
|
11
12
|
const errors = require('./errors')
|
|
12
13
|
|
|
13
14
|
function lowerCaseFirst (str) {
|
|
@@ -369,6 +370,14 @@ function createMapper (defaultDb, sql, log, table, fields, primaryKeys, relation
|
|
|
369
370
|
}
|
|
370
371
|
|
|
371
372
|
function buildEntity (db, sql, log, table, queries, autoTimestamp, schema, useSchemaInName, ignore, limitConfig, schemaList, columns, constraintsList) {
|
|
373
|
+
const columnsNames = columns.map(c => c.column_name)
|
|
374
|
+
for (const ignoredColumn of Object.keys(ignore)) {
|
|
375
|
+
if (!columnsNames.includes(ignoredColumn)) {
|
|
376
|
+
const nearestColumn = findNearestString(columnsNames, ignoredColumn)
|
|
377
|
+
log.warn(`Ignored column "${ignoredColumn}" not found. Did you mean "${nearestColumn}"?`)
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
|
|
372
381
|
// Compute the columns
|
|
373
382
|
columns = columns.filter((c) => !ignore[c.column_name])
|
|
374
383
|
const fields = columns.reduce((acc, column) => {
|
package/mapper.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
+
const { findNearestString } = require('@platformatic/utils')
|
|
3
4
|
const buildEntity = require('./lib/entity')
|
|
4
5
|
const buildCleanUp = require('./lib/clean-up')
|
|
5
6
|
const queriesFactory = require('./lib/queries')
|
|
@@ -158,6 +159,18 @@ async function connect ({ connectionString, log, onDatabaseLoad, poolSize, ignor
|
|
|
158
159
|
}
|
|
159
160
|
}
|
|
160
161
|
|
|
162
|
+
const schemaTables = dbschema.map(table => table.table)
|
|
163
|
+
for (const ignoredTable of Object.keys(ignore)) {
|
|
164
|
+
if (!schemaTables.includes(ignoredTable)) {
|
|
165
|
+
const nearestTable = findNearestString(schemaTables, ignoredTable)
|
|
166
|
+
let warningMessage = `Ignored table "${ignoredTable}" not found.`
|
|
167
|
+
if (nearestTable) {
|
|
168
|
+
warningMessage += ` Did you mean "${nearestTable}"?`
|
|
169
|
+
}
|
|
170
|
+
log.warn(warningMessage)
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
161
174
|
for (const { table, schema, columns, constraints } of dbschema) {
|
|
162
175
|
// The following line is a safety net when developing this module,
|
|
163
176
|
// it should never happen.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@platformatic/sql-mapper",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.46.2",
|
|
4
4
|
"description": "A data mapper utility for SQL databases",
|
|
5
5
|
"main": "mapper.js",
|
|
6
6
|
"types": "mapper.d.ts",
|
|
@@ -30,7 +30,8 @@
|
|
|
30
30
|
"@matteo.collina/sqlite-pool": "^0.3.0",
|
|
31
31
|
"camelcase": "^6.3.0",
|
|
32
32
|
"fastify-plugin": "^4.5.0",
|
|
33
|
-
"inflected": "^2.1.0"
|
|
33
|
+
"inflected": "^2.1.0",
|
|
34
|
+
"@platformatic/utils": "0.46.2"
|
|
34
35
|
},
|
|
35
36
|
"tsd": {
|
|
36
37
|
"directory": "test/types"
|
package/test/composite.test.js
DELETED
|
@@ -1,162 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const { clear, connInfo, isSQLite, isMysql, isPg } = require('./helper')
|
|
4
|
-
const { test } = require('tap')
|
|
5
|
-
const { connect } = require('..')
|
|
6
|
-
const fakeLogger = {
|
|
7
|
-
trace: () => {},
|
|
8
|
-
// trace: console.log,
|
|
9
|
-
error: () => {}
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
test('composite primary keys', async ({ equal, same, teardown, rejects }) => {
|
|
13
|
-
/* https://github.com/platformatic/platformatic/issues/299 */
|
|
14
|
-
async function onDatabaseLoad (db, sql) {
|
|
15
|
-
await clear(db, sql)
|
|
16
|
-
teardown(() => db.dispose())
|
|
17
|
-
|
|
18
|
-
if (isSQLite) {
|
|
19
|
-
await db.query(sql`CREATE TABLE pages (
|
|
20
|
-
id INTEGER PRIMARY KEY,
|
|
21
|
-
the_title VARCHAR(42)
|
|
22
|
-
);`)
|
|
23
|
-
|
|
24
|
-
await db.query(sql`CREATE TABLE users (
|
|
25
|
-
id INTEGER PRIMARY KEY,
|
|
26
|
-
username VARCHAR(255) NOT NULL
|
|
27
|
-
);`)
|
|
28
|
-
|
|
29
|
-
await db.query(sql`CREATE TABLE editors (
|
|
30
|
-
page_id INTEGER NOT NULL,
|
|
31
|
-
user_id INTEGER NOT NULL,
|
|
32
|
-
role VARCHAR(255) NOT NULL,
|
|
33
|
-
CONSTRAINT fk_editor_pages FOREIGN KEY (page_id) REFERENCES pages(id),
|
|
34
|
-
CONSTRAINT fk_editor_users FOREIGN KEY (user_id) REFERENCES users(id),
|
|
35
|
-
PRIMARY KEY (page_id, user_id)
|
|
36
|
-
);`)
|
|
37
|
-
} else if (isPg) {
|
|
38
|
-
await db.query(sql`CREATE TABLE pages (
|
|
39
|
-
id SERIAL PRIMARY KEY,
|
|
40
|
-
the_title VARCHAR(255) NOT NULL
|
|
41
|
-
);`)
|
|
42
|
-
|
|
43
|
-
await db.query(sql`CREATE TABLE users (
|
|
44
|
-
id SERIAL PRIMARY KEY,
|
|
45
|
-
username VARCHAR(255) NOT NULL
|
|
46
|
-
);`)
|
|
47
|
-
|
|
48
|
-
await db.query(sql`CREATE TABLE editors (
|
|
49
|
-
page_id INTEGER NOT NULL,
|
|
50
|
-
user_id INTEGER NOT NULL,
|
|
51
|
-
role VARCHAR(255) NOT NULL,
|
|
52
|
-
CONSTRAINT fk_editor_pages FOREIGN KEY (page_id) REFERENCES pages(id),
|
|
53
|
-
CONSTRAINT fk_editor_users FOREIGN KEY (user_id) REFERENCES users(id),
|
|
54
|
-
PRIMARY KEY (page_id, user_id)
|
|
55
|
-
);`)
|
|
56
|
-
} else if (isMysql) {
|
|
57
|
-
await db.query(sql`CREATE TABLE pages (
|
|
58
|
-
id INTEGER PRIMARY KEY AUTO_INCREMENT,
|
|
59
|
-
the_title VARCHAR(255) NOT NULL
|
|
60
|
-
);`)
|
|
61
|
-
|
|
62
|
-
await db.query(sql`CREATE TABLE users (
|
|
63
|
-
id INTEGER PRIMARY KEY AUTO_INCREMENT,
|
|
64
|
-
username VARCHAR(255) NOT NULL
|
|
65
|
-
);`)
|
|
66
|
-
|
|
67
|
-
await db.query(sql`CREATE TABLE editors (
|
|
68
|
-
page_id INTEGER NOT NULL,
|
|
69
|
-
user_id INTEGER NOT NULL,
|
|
70
|
-
role VARCHAR(255) NOT NULL,
|
|
71
|
-
CONSTRAINT \`fk_editor_pages\` FOREIGN KEY (page_id) REFERENCES pages (id) ON DELETE CASCADE ON UPDATE RESTRICT,
|
|
72
|
-
CONSTRAINT \`fk_editor_users\` FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE ON UPDATE RESTRICT,
|
|
73
|
-
PRIMARY KEY (page_id, user_id)
|
|
74
|
-
);`)
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
const mapper = await connect({
|
|
78
|
-
connectionString: connInfo.connectionString,
|
|
79
|
-
log: fakeLogger,
|
|
80
|
-
onDatabaseLoad,
|
|
81
|
-
ignore: {},
|
|
82
|
-
hooks: {}
|
|
83
|
-
})
|
|
84
|
-
const pageEntity = mapper.entities.page
|
|
85
|
-
const userEntity = mapper.entities.user
|
|
86
|
-
const editorEntity = mapper.entities.editor
|
|
87
|
-
|
|
88
|
-
const page = await pageEntity.save({
|
|
89
|
-
input: { theTitle: 'foobar' }
|
|
90
|
-
})
|
|
91
|
-
same(page, { id: '1', theTitle: 'foobar' })
|
|
92
|
-
|
|
93
|
-
const user = await userEntity.save({
|
|
94
|
-
input: { username: 'mcollina' }
|
|
95
|
-
})
|
|
96
|
-
same(user, { id: '1', username: 'mcollina' })
|
|
97
|
-
|
|
98
|
-
const user2 = await userEntity.save({
|
|
99
|
-
input: { username: 'lucamaraschi' }
|
|
100
|
-
})
|
|
101
|
-
same(user2, { id: '2', username: 'lucamaraschi' })
|
|
102
|
-
|
|
103
|
-
const editor1 = await editorEntity.save({
|
|
104
|
-
input: {
|
|
105
|
-
pageId: '1',
|
|
106
|
-
userId: '1',
|
|
107
|
-
role: 'admin'
|
|
108
|
-
}
|
|
109
|
-
})
|
|
110
|
-
same(editor1, { pageId: '1', userId: '1', role: 'admin' })
|
|
111
|
-
|
|
112
|
-
const editor2 = await editorEntity.save({
|
|
113
|
-
input: {
|
|
114
|
-
pageId: '1',
|
|
115
|
-
userId: '2',
|
|
116
|
-
role: 'author'
|
|
117
|
-
}
|
|
118
|
-
})
|
|
119
|
-
same(editor2, { pageId: '1', userId: '2', role: 'author' })
|
|
120
|
-
|
|
121
|
-
await editorEntity.save({
|
|
122
|
-
input: {
|
|
123
|
-
pageId: '1',
|
|
124
|
-
userId: '1',
|
|
125
|
-
role: 'captain'
|
|
126
|
-
}
|
|
127
|
-
})
|
|
128
|
-
|
|
129
|
-
const editors = await editorEntity.find({ orderBy: [{ field: 'userId', direction: 'ASC' }] })
|
|
130
|
-
same(editors, [{
|
|
131
|
-
pageId: '1',
|
|
132
|
-
userId: '1',
|
|
133
|
-
role: 'captain'
|
|
134
|
-
}, {
|
|
135
|
-
pageId: '1',
|
|
136
|
-
userId: '2',
|
|
137
|
-
role: 'author'
|
|
138
|
-
}])
|
|
139
|
-
|
|
140
|
-
await editorEntity.delete({})
|
|
141
|
-
|
|
142
|
-
const editorsInserted = await editorEntity.insert({
|
|
143
|
-
inputs: [{
|
|
144
|
-
pageId: '1',
|
|
145
|
-
userId: '1',
|
|
146
|
-
role: 'admin'
|
|
147
|
-
}, {
|
|
148
|
-
pageId: '1',
|
|
149
|
-
userId: '2',
|
|
150
|
-
role: 'author'
|
|
151
|
-
}]
|
|
152
|
-
})
|
|
153
|
-
same(editorsInserted, [{
|
|
154
|
-
pageId: '1',
|
|
155
|
-
userId: '1',
|
|
156
|
-
role: 'admin'
|
|
157
|
-
}, {
|
|
158
|
-
pageId: '1',
|
|
159
|
-
userId: '2',
|
|
160
|
-
role: 'author'
|
|
161
|
-
}])
|
|
162
|
-
})
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const { test } = require('tap')
|
|
4
|
-
const { clear, connInfo, isSQLite } = require('./helper')
|
|
5
|
-
const { createConnectionPool } = require('..')
|
|
6
|
-
const fakeLogger = {
|
|
7
|
-
trace: () => {},
|
|
8
|
-
error: () => {}
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
test('createConnectionPool', async ({ equal, same, teardown, rejects }) => {
|
|
12
|
-
const { db, sql } = await createConnectionPool({
|
|
13
|
-
connectionString: connInfo.connectionString,
|
|
14
|
-
log: fakeLogger
|
|
15
|
-
})
|
|
16
|
-
await clear(db, sql)
|
|
17
|
-
teardown(() => db.dispose())
|
|
18
|
-
if (isSQLite) {
|
|
19
|
-
await db.query(sql`CREATE TABLE pages (
|
|
20
|
-
id INTEGER PRIMARY KEY,
|
|
21
|
-
the_title VARCHAR(42),
|
|
22
|
-
is_published BOOLEAN NOT NULL
|
|
23
|
-
);`)
|
|
24
|
-
} else {
|
|
25
|
-
await db.query(sql`CREATE TABLE pages (
|
|
26
|
-
id SERIAL PRIMARY KEY,
|
|
27
|
-
the_title VARCHAR(255) NOT NULL,
|
|
28
|
-
is_published BOOLEAN NOT NULL
|
|
29
|
-
);`)
|
|
30
|
-
}
|
|
31
|
-
await db.query(sql`INSERT INTO pages (the_title, is_published) VALUES ('foo', true)`)
|
|
32
|
-
|
|
33
|
-
const res = await db.query(sql`SELECT * FROM pages`)
|
|
34
|
-
same(res, [{
|
|
35
|
-
id: 1,
|
|
36
|
-
the_title: 'foo',
|
|
37
|
-
is_published: true
|
|
38
|
-
}])
|
|
39
|
-
})
|