@platformatic/sql-mapper 0.46.1 → 0.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.
- package/package.json +2 -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/ignore.test.js +0 -187
- 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 -138
- package/test/updateMany.test.js +0 -319
- package/test/where.test.js +0 -889
package/test/ignore.test.js
DELETED
|
@@ -1,187 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const { test } = require('tap')
|
|
4
|
-
const { connect } = require('..')
|
|
5
|
-
const { clear, connInfo, isSQLite } = require('./helper')
|
|
6
|
-
|
|
7
|
-
const fakeLogger = {
|
|
8
|
-
trace: () => {},
|
|
9
|
-
error: () => {},
|
|
10
|
-
warn: () => {}
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
async function createBasicPages (db, sql) {
|
|
14
|
-
if (isSQLite) {
|
|
15
|
-
await db.query(sql`CREATE TABLE pages (
|
|
16
|
-
id INTEGER PRIMARY KEY,
|
|
17
|
-
title VARCHAR(42)
|
|
18
|
-
);`)
|
|
19
|
-
await db.query(sql`CREATE TABLE categories (
|
|
20
|
-
id INTEGER PRIMARY KEY,
|
|
21
|
-
name VARCHAR(42)
|
|
22
|
-
);`)
|
|
23
|
-
} else {
|
|
24
|
-
await db.query(sql`CREATE TABLE pages (
|
|
25
|
-
id SERIAL PRIMARY KEY,
|
|
26
|
-
title VARCHAR(42)
|
|
27
|
-
);`)
|
|
28
|
-
await db.query(sql`CREATE TABLE categories (
|
|
29
|
-
id SERIAL PRIMARY KEY,
|
|
30
|
-
name VARCHAR(42)
|
|
31
|
-
);`)
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
test('ignore a table', async ({ pass, teardown, equal }) => {
|
|
36
|
-
async function onDatabaseLoad (db, sql) {
|
|
37
|
-
pass('onDatabaseLoad called')
|
|
38
|
-
teardown(() => db.dispose())
|
|
39
|
-
|
|
40
|
-
await clear(db, sql)
|
|
41
|
-
await createBasicPages(db, sql)
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
const mapper = await connect({
|
|
45
|
-
...connInfo,
|
|
46
|
-
log: fakeLogger,
|
|
47
|
-
onDatabaseLoad,
|
|
48
|
-
ignore: {
|
|
49
|
-
categories: true
|
|
50
|
-
}
|
|
51
|
-
})
|
|
52
|
-
|
|
53
|
-
const pageEntity = mapper.entities.page
|
|
54
|
-
equal(pageEntity.name, 'Page')
|
|
55
|
-
|
|
56
|
-
const categoryEntity = mapper.entities.category
|
|
57
|
-
equal(categoryEntity, undefined, 'category entity is ignored')
|
|
58
|
-
})
|
|
59
|
-
|
|
60
|
-
test('show a warning if there is no ignored table', async ({ plan, pass, teardown, equal }) => {
|
|
61
|
-
plan(4)
|
|
62
|
-
|
|
63
|
-
async function onDatabaseLoad (db, sql) {
|
|
64
|
-
pass('onDatabaseLoad called')
|
|
65
|
-
teardown(() => db.dispose())
|
|
66
|
-
|
|
67
|
-
await clear(db, sql)
|
|
68
|
-
await createBasicPages(db, sql)
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
const logger = {
|
|
72
|
-
trace: () => {},
|
|
73
|
-
error: () => {},
|
|
74
|
-
warn: (msg) => {
|
|
75
|
-
equal(msg, 'Ignored table "missing_table_pages" not found. Did you mean "pages"?')
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
const mapper = await connect({
|
|
80
|
-
...connInfo,
|
|
81
|
-
log: logger,
|
|
82
|
-
onDatabaseLoad,
|
|
83
|
-
ignore: {
|
|
84
|
-
missing_table_pages: true
|
|
85
|
-
}
|
|
86
|
-
})
|
|
87
|
-
|
|
88
|
-
const pageEntity = mapper.entities.page
|
|
89
|
-
equal(pageEntity.name, 'Page')
|
|
90
|
-
|
|
91
|
-
const categoryEntity = mapper.entities.category
|
|
92
|
-
equal(categoryEntity.name, 'Category')
|
|
93
|
-
})
|
|
94
|
-
|
|
95
|
-
test('show a warning if the database is empty', async ({ plan, pass, teardown, equal }) => {
|
|
96
|
-
// plan(4)
|
|
97
|
-
|
|
98
|
-
async function onDatabaseLoad (db, sql) {
|
|
99
|
-
pass('onDatabaseLoad called')
|
|
100
|
-
teardown(() => db.dispose())
|
|
101
|
-
|
|
102
|
-
await clear(db, sql)
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
const logger = {
|
|
106
|
-
trace: () => {},
|
|
107
|
-
error: () => {},
|
|
108
|
-
warn: (msg) => {
|
|
109
|
-
equal(msg, 'Ignored table "missing_table_pages" not found.')
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
await connect({
|
|
114
|
-
...connInfo,
|
|
115
|
-
log: logger,
|
|
116
|
-
onDatabaseLoad,
|
|
117
|
-
ignore: {
|
|
118
|
-
missing_table_pages: true
|
|
119
|
-
}
|
|
120
|
-
})
|
|
121
|
-
})
|
|
122
|
-
|
|
123
|
-
test('ignore a column', async ({ pass, teardown, equal }) => {
|
|
124
|
-
async function onDatabaseLoad (db, sql) {
|
|
125
|
-
pass('onDatabaseLoad called')
|
|
126
|
-
teardown(() => db.dispose())
|
|
127
|
-
|
|
128
|
-
await clear(db, sql)
|
|
129
|
-
await createBasicPages(db, sql)
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
const mapper = await connect({
|
|
133
|
-
...connInfo,
|
|
134
|
-
log: fakeLogger,
|
|
135
|
-
onDatabaseLoad,
|
|
136
|
-
ignore: {
|
|
137
|
-
categories: {
|
|
138
|
-
name: true
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
})
|
|
142
|
-
|
|
143
|
-
const pageEntity = mapper.entities.page
|
|
144
|
-
equal(pageEntity.name, 'Page')
|
|
145
|
-
|
|
146
|
-
const categoryEntity = mapper.entities.category
|
|
147
|
-
equal(categoryEntity.name, 'Category')
|
|
148
|
-
equal(categoryEntity.fields.id.camelcase, 'id')
|
|
149
|
-
equal(categoryEntity.fields.name, undefined, 'name column is ignored')
|
|
150
|
-
})
|
|
151
|
-
|
|
152
|
-
test('shows a warning if there is no ignored column', async ({ plan, pass, teardown, equal }) => {
|
|
153
|
-
plan(4)
|
|
154
|
-
|
|
155
|
-
async function onDatabaseLoad (db, sql) {
|
|
156
|
-
pass('onDatabaseLoad called')
|
|
157
|
-
teardown(() => db.dispose())
|
|
158
|
-
|
|
159
|
-
await clear(db, sql)
|
|
160
|
-
await createBasicPages(db, sql)
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
const logger = {
|
|
164
|
-
trace: () => {},
|
|
165
|
-
error: () => {},
|
|
166
|
-
warn: (msg) => {
|
|
167
|
-
equal(msg, 'Ignored column "missing_column_name" not found. Did you mean "name"?')
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
const mapper = await connect({
|
|
172
|
-
...connInfo,
|
|
173
|
-
log: logger,
|
|
174
|
-
onDatabaseLoad,
|
|
175
|
-
ignore: {
|
|
176
|
-
categories: {
|
|
177
|
-
missing_column_name: true
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
})
|
|
181
|
-
|
|
182
|
-
const pageEntity = mapper.entities.page
|
|
183
|
-
equal(pageEntity.name, 'Page')
|
|
184
|
-
|
|
185
|
-
const categoryEntity = mapper.entities.category
|
|
186
|
-
equal(categoryEntity.name, 'Category')
|
|
187
|
-
})
|
|
@@ -1,132 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const { test } = require('tap')
|
|
4
|
-
const { clear, connInfo, isSQLite, isMysql } = require('./helper')
|
|
5
|
-
const { setTimeout } = require('timers/promises')
|
|
6
|
-
const { connect } = require('..')
|
|
7
|
-
const fakeLogger = {
|
|
8
|
-
trace: () => {},
|
|
9
|
-
error: () => {}
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
async function createBasicPages (db, sql) {
|
|
13
|
-
if (isSQLite) {
|
|
14
|
-
await db.query(sql`CREATE TABLE pages (
|
|
15
|
-
id INTEGER PRIMARY KEY,
|
|
16
|
-
title VARCHAR(42),
|
|
17
|
-
inserted_at TIMESTAMP,
|
|
18
|
-
updated_at TIMESTAMP
|
|
19
|
-
);`)
|
|
20
|
-
} else if (isMysql) {
|
|
21
|
-
await db.query(sql`CREATE TABLE pages (
|
|
22
|
-
id SERIAL PRIMARY KEY,
|
|
23
|
-
title VARCHAR(42),
|
|
24
|
-
inserted_at TIMESTAMP NULL DEFAULT NULL,
|
|
25
|
-
updated_at TIMESTAMP NULL DEFAULT NULL
|
|
26
|
-
);`)
|
|
27
|
-
} else {
|
|
28
|
-
await db.query(sql`CREATE TABLE pages (
|
|
29
|
-
id SERIAL PRIMARY KEY,
|
|
30
|
-
title VARCHAR(42),
|
|
31
|
-
inserted_at TIMESTAMP,
|
|
32
|
-
updated_at TIMESTAMP
|
|
33
|
-
);`)
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
test('inserted_at updated_at happy path', async ({ pass, teardown, same, equal, not, comment, notSame }) => {
|
|
38
|
-
const mapper = await connect({
|
|
39
|
-
...connInfo,
|
|
40
|
-
log: fakeLogger,
|
|
41
|
-
async onDatabaseLoad (db, sql) {
|
|
42
|
-
teardown(() => db.dispose())
|
|
43
|
-
pass('onDatabaseLoad called')
|
|
44
|
-
|
|
45
|
-
await clear(db, sql)
|
|
46
|
-
await createBasicPages(db, sql)
|
|
47
|
-
}
|
|
48
|
-
})
|
|
49
|
-
|
|
50
|
-
const entity = mapper.entities.page
|
|
51
|
-
|
|
52
|
-
equal(entity.fields.inserted_at.autoTimestamp, true)
|
|
53
|
-
equal(entity.fields.updated_at.autoTimestamp, true)
|
|
54
|
-
|
|
55
|
-
const original = await entity.save({
|
|
56
|
-
input: { title: 'Hello' }
|
|
57
|
-
})
|
|
58
|
-
not(original.insertedAt, null, 'insertedAt')
|
|
59
|
-
not(original.updatedAt, null, 'updatedAt')
|
|
60
|
-
comment(`insertedAt: ${original.insertedAt}`)
|
|
61
|
-
comment(`updatedAt: ${original.updatedAt}`)
|
|
62
|
-
|
|
63
|
-
{
|
|
64
|
-
const [data] = await entity.find({ where: { id: { eq: original.id } } })
|
|
65
|
-
same(data.insertedAt, original.insertedAt, 'insertedAt')
|
|
66
|
-
same(data.updatedAt, original.updatedAt, 'updatedAt')
|
|
67
|
-
comment(`insertedAt: ${data.insertedAt}`)
|
|
68
|
-
comment(`updatedAt: ${data.updatedAt}`)
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
await setTimeout(1000) // await 1s
|
|
72
|
-
|
|
73
|
-
let updated
|
|
74
|
-
{
|
|
75
|
-
const data = await entity.save({
|
|
76
|
-
input: { id: original.id, title: 'Hello World' }
|
|
77
|
-
})
|
|
78
|
-
same(data.insertedAt, original.insertedAt, 'insertedAt')
|
|
79
|
-
notSame(data.updatedAt, original.updatedAt, 'updatedAt')
|
|
80
|
-
updated = data
|
|
81
|
-
comment(`insertedAt: ${data.insertedAt}`)
|
|
82
|
-
comment(`updatedAt: ${data.updatedAt}`)
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
{
|
|
86
|
-
const [data] = await entity.find({ where: { id: { eq: original.id } } })
|
|
87
|
-
same(data.insertedAt, updated.insertedAt, 'insertedAt')
|
|
88
|
-
same(data.updatedAt, updated.updatedAt, 'updatedAt')
|
|
89
|
-
comment(`insertedAt: ${data.insertedAt}`)
|
|
90
|
-
comment(`updatedAt: ${data.updatedAt}`)
|
|
91
|
-
}
|
|
92
|
-
})
|
|
93
|
-
|
|
94
|
-
test('bulk insert adds inserted_at updated_at', async ({ pass, teardown, same, equal, not, comment }) => {
|
|
95
|
-
const mapper = await connect({
|
|
96
|
-
...connInfo,
|
|
97
|
-
log: fakeLogger,
|
|
98
|
-
async onDatabaseLoad (db, sql) {
|
|
99
|
-
teardown(() => db.dispose())
|
|
100
|
-
pass('onDatabaseLoad called')
|
|
101
|
-
|
|
102
|
-
await clear(db, sql)
|
|
103
|
-
await createBasicPages(db, sql)
|
|
104
|
-
}
|
|
105
|
-
})
|
|
106
|
-
|
|
107
|
-
const entity = mapper.entities.page
|
|
108
|
-
|
|
109
|
-
{
|
|
110
|
-
const pages = await entity.insert({
|
|
111
|
-
inputs: [
|
|
112
|
-
{ title: 'Page 1' },
|
|
113
|
-
{ title: 'Page 2' },
|
|
114
|
-
{ title: 'Page 3' }
|
|
115
|
-
]
|
|
116
|
-
})
|
|
117
|
-
for (const page of pages) {
|
|
118
|
-
not(page.insertedAt, null, 'insertedAt')
|
|
119
|
-
not(page.updatedAt, null, 'updatedAt')
|
|
120
|
-
same(page.insertedAt, page.updatedAt, 'insertedAt === updatedAt')
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
{
|
|
125
|
-
const pages = await entity.find()
|
|
126
|
-
for (const page of pages) {
|
|
127
|
-
not(page.insertedAt, null, 'insertedAt')
|
|
128
|
-
not(page.updatedAt, null, 'updatedAt')
|
|
129
|
-
same(page.insertedAt, page.updatedAt, 'insertedAt === updatedAt')
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
})
|
package/test/mapper.test.js
DELETED
|
@@ -1,325 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const { test } = require('tap')
|
|
4
|
-
const { connect, plugin } = require('..')
|
|
5
|
-
const { clear, connInfo, isPg, isMysql, isSQLite } = require('./helper')
|
|
6
|
-
const fastify = require('fastify')
|
|
7
|
-
|
|
8
|
-
const fakeLogger = {
|
|
9
|
-
trace: () => {},
|
|
10
|
-
error: () => {},
|
|
11
|
-
warn: () => {}
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
test('should throw if no connection string is provided', async ({ equal }) => {
|
|
15
|
-
try {
|
|
16
|
-
await connect({
|
|
17
|
-
connectionString: false
|
|
18
|
-
})
|
|
19
|
-
} catch (err) {
|
|
20
|
-
equal(err.message, 'connectionString is required')
|
|
21
|
-
}
|
|
22
|
-
})
|
|
23
|
-
|
|
24
|
-
test('[PG] return entities', { skip: !isPg }, async ({ pass, teardown, equal }) => {
|
|
25
|
-
async function onDatabaseLoad (db, sql) {
|
|
26
|
-
await clear(db, sql)
|
|
27
|
-
teardown(() => db.dispose())
|
|
28
|
-
|
|
29
|
-
await db.query(sql`CREATE TABLE pages (
|
|
30
|
-
id SERIAL PRIMARY KEY,
|
|
31
|
-
title VARCHAR(255) NOT NULL
|
|
32
|
-
);`)
|
|
33
|
-
}
|
|
34
|
-
const mapper = await connect({
|
|
35
|
-
connectionString: connInfo.connectionString,
|
|
36
|
-
log: fakeLogger,
|
|
37
|
-
onDatabaseLoad,
|
|
38
|
-
ignore: {},
|
|
39
|
-
hooks: {}
|
|
40
|
-
})
|
|
41
|
-
const pageEntity = mapper.entities.page
|
|
42
|
-
equal(pageEntity.name, 'Page')
|
|
43
|
-
equal(pageEntity.singularName, 'page')
|
|
44
|
-
equal(pageEntity.pluralName, 'pages')
|
|
45
|
-
pass()
|
|
46
|
-
})
|
|
47
|
-
|
|
48
|
-
test('[mysql] return entities', { skip: !isMysql }, async ({ pass, teardown, equal }) => {
|
|
49
|
-
async function onDatabaseLoad (db, sql) {
|
|
50
|
-
await clear(db, sql)
|
|
51
|
-
teardown(() => db.dispose())
|
|
52
|
-
|
|
53
|
-
await db.query(sql`CREATE TABLE pages (
|
|
54
|
-
id SERIAL PRIMARY KEY,
|
|
55
|
-
title VARCHAR(255) NOT NULL
|
|
56
|
-
);`)
|
|
57
|
-
}
|
|
58
|
-
const mapper = await connect({
|
|
59
|
-
connectionString: connInfo.connectionString,
|
|
60
|
-
log: fakeLogger,
|
|
61
|
-
onDatabaseLoad,
|
|
62
|
-
ignore: {},
|
|
63
|
-
hooks: {}
|
|
64
|
-
})
|
|
65
|
-
const pageEntity = mapper.entities.page
|
|
66
|
-
equal(pageEntity.name, 'Page')
|
|
67
|
-
equal(pageEntity.singularName, 'page')
|
|
68
|
-
equal(pageEntity.pluralName, 'pages')
|
|
69
|
-
pass()
|
|
70
|
-
})
|
|
71
|
-
|
|
72
|
-
test('[sqlite] return entities', { skip: !isSQLite }, async ({ pass, teardown, equal }) => {
|
|
73
|
-
async function onDatabaseLoad (db, sql) {
|
|
74
|
-
teardown(async () => await clear(db, sql))
|
|
75
|
-
teardown(() => db.dispose())
|
|
76
|
-
|
|
77
|
-
await db.query(sql`CREATE TABLE IF NOT EXISTS pages (
|
|
78
|
-
id SERIAL PRIMARY KEY,
|
|
79
|
-
title VARCHAR(255) NOT NULL
|
|
80
|
-
);`)
|
|
81
|
-
}
|
|
82
|
-
const mapper = await connect({
|
|
83
|
-
connectionString: connInfo.connectionString,
|
|
84
|
-
log: fakeLogger,
|
|
85
|
-
onDatabaseLoad,
|
|
86
|
-
ignore: {},
|
|
87
|
-
hooks: {}
|
|
88
|
-
})
|
|
89
|
-
const pageEntity = mapper.entities.page
|
|
90
|
-
equal(pageEntity.name, 'Page')
|
|
91
|
-
equal(pageEntity.singularName, 'page')
|
|
92
|
-
equal(pageEntity.pluralName, 'pages')
|
|
93
|
-
pass()
|
|
94
|
-
})
|
|
95
|
-
|
|
96
|
-
test('ignore tables', async ({ teardown, has }) => {
|
|
97
|
-
async function onDatabaseLoad (db, sql) {
|
|
98
|
-
teardown(async () => await clear(db, sql))
|
|
99
|
-
teardown(() => db.dispose())
|
|
100
|
-
|
|
101
|
-
await db.query(sql`CREATE TABLE IF NOT EXISTS pages (
|
|
102
|
-
id SERIAL PRIMARY KEY,
|
|
103
|
-
title VARCHAR(255) NOT NULL
|
|
104
|
-
);`)
|
|
105
|
-
|
|
106
|
-
await db.query(sql`CREATE TABLE IF NOT EXISTS users (
|
|
107
|
-
id SERIAL PRIMARY KEY,
|
|
108
|
-
username VARCHAR(255) NOT NULL
|
|
109
|
-
);`)
|
|
110
|
-
}
|
|
111
|
-
const mapper = await connect({
|
|
112
|
-
connectionString: connInfo.connectionString,
|
|
113
|
-
log: fakeLogger,
|
|
114
|
-
onDatabaseLoad,
|
|
115
|
-
ignore: { users: true },
|
|
116
|
-
hooks: {}
|
|
117
|
-
})
|
|
118
|
-
has(mapper.entities.users, undefined)
|
|
119
|
-
})
|
|
120
|
-
|
|
121
|
-
test('[PG] return entities with Fastify', { skip: !isPg }, async ({ pass, teardown, equal }) => {
|
|
122
|
-
async function onDatabaseLoad (db, sql) {
|
|
123
|
-
teardown(async () => await clear(db, sql))
|
|
124
|
-
|
|
125
|
-
await db.query(sql`CREATE TABLE IF NOT EXISTS pages (
|
|
126
|
-
id SERIAL PRIMARY KEY,
|
|
127
|
-
title VARCHAR(255) NOT NULL
|
|
128
|
-
);`)
|
|
129
|
-
}
|
|
130
|
-
const app = fastify()
|
|
131
|
-
teardown(() => app.close())
|
|
132
|
-
app.register(plugin, {
|
|
133
|
-
connectionString: connInfo.connectionString,
|
|
134
|
-
log: fakeLogger,
|
|
135
|
-
onDatabaseLoad
|
|
136
|
-
})
|
|
137
|
-
await app.ready()
|
|
138
|
-
const pageEntity = app.platformatic.entities.page
|
|
139
|
-
equal(pageEntity.name, 'Page')
|
|
140
|
-
equal(pageEntity.singularName, 'page')
|
|
141
|
-
equal(pageEntity.pluralName, 'pages')
|
|
142
|
-
pass()
|
|
143
|
-
})
|
|
144
|
-
|
|
145
|
-
test('[mysql] return entities', { skip: !isMysql }, async ({ pass, teardown, equal }) => {
|
|
146
|
-
async function onDatabaseLoad (db, sql) {
|
|
147
|
-
teardown(async () => await clear(db, sql))
|
|
148
|
-
|
|
149
|
-
await db.query(sql`CREATE TABLE IF NOT EXISTS pages (
|
|
150
|
-
id SERIAL PRIMARY KEY,
|
|
151
|
-
title VARCHAR(255) NOT NULL
|
|
152
|
-
);`)
|
|
153
|
-
}
|
|
154
|
-
const app = fastify()
|
|
155
|
-
teardown(() => app.close())
|
|
156
|
-
app.register(plugin, {
|
|
157
|
-
connectionString: connInfo.connectionString,
|
|
158
|
-
onDatabaseLoad
|
|
159
|
-
})
|
|
160
|
-
await app.ready()
|
|
161
|
-
const pageEntity = app.platformatic.entities.page
|
|
162
|
-
equal(pageEntity.name, 'Page')
|
|
163
|
-
equal(pageEntity.singularName, 'page')
|
|
164
|
-
equal(pageEntity.pluralName, 'pages')
|
|
165
|
-
pass()
|
|
166
|
-
})
|
|
167
|
-
|
|
168
|
-
test('[sqlite] return entities', { skip: !isSQLite }, async ({ pass, teardown, equal }) => {
|
|
169
|
-
async function onDatabaseLoad (db, sql) {
|
|
170
|
-
teardown(async () => await clear(db, sql))
|
|
171
|
-
|
|
172
|
-
await db.query(sql`CREATE TABLE IF NOT EXISTS pages (
|
|
173
|
-
id SERIAL PRIMARY KEY,
|
|
174
|
-
title VARCHAR(255) NOT NULL
|
|
175
|
-
);`)
|
|
176
|
-
}
|
|
177
|
-
const app = fastify()
|
|
178
|
-
teardown(() => app.close())
|
|
179
|
-
app.register(plugin, {
|
|
180
|
-
connectionString: connInfo.connectionString,
|
|
181
|
-
onDatabaseLoad
|
|
182
|
-
})
|
|
183
|
-
await app.ready()
|
|
184
|
-
const pageEntity = app.platformatic.entities.page
|
|
185
|
-
equal(pageEntity.name, 'Page')
|
|
186
|
-
equal(pageEntity.singularName, 'page')
|
|
187
|
-
equal(pageEntity.pluralName, 'pages')
|
|
188
|
-
pass()
|
|
189
|
-
})
|
|
190
|
-
|
|
191
|
-
test('missing connectionString', async ({ rejects }) => {
|
|
192
|
-
const app = fastify()
|
|
193
|
-
app.register(plugin)
|
|
194
|
-
|
|
195
|
-
await rejects(app.ready(), /connectionString/)
|
|
196
|
-
})
|
|
197
|
-
|
|
198
|
-
test('platformaticContext', async ({ plan, equal, teardown }) => {
|
|
199
|
-
plan(3)
|
|
200
|
-
async function onDatabaseLoad (db, sql) {
|
|
201
|
-
teardown(async () => await clear(db, sql))
|
|
202
|
-
|
|
203
|
-
await db.query(sql`CREATE TABLE IF NOT EXISTS pages (
|
|
204
|
-
id SERIAL PRIMARY KEY,
|
|
205
|
-
title VARCHAR(255) NOT NULL
|
|
206
|
-
);`)
|
|
207
|
-
}
|
|
208
|
-
const app = fastify()
|
|
209
|
-
teardown(() => app.close())
|
|
210
|
-
app.register(plugin, {
|
|
211
|
-
connectionString: connInfo.connectionString,
|
|
212
|
-
onDatabaseLoad
|
|
213
|
-
})
|
|
214
|
-
|
|
215
|
-
app.get('/', function (req, reply) {
|
|
216
|
-
const ctx = req.platformaticContext
|
|
217
|
-
equal(app, ctx.app)
|
|
218
|
-
equal(reply, ctx.reply)
|
|
219
|
-
return 'hello world'
|
|
220
|
-
})
|
|
221
|
-
|
|
222
|
-
const res = await app.inject('/')
|
|
223
|
-
equal(res.statusCode, 200)
|
|
224
|
-
})
|
|
225
|
-
|
|
226
|
-
test('platformatic decorator already present', async ({ teardown }) => {
|
|
227
|
-
async function onDatabaseLoad (db, sql) {
|
|
228
|
-
}
|
|
229
|
-
const app = fastify()
|
|
230
|
-
app.decorate('platformatic', {})
|
|
231
|
-
teardown(() => app.close())
|
|
232
|
-
app.register(plugin, {
|
|
233
|
-
connectionString: connInfo.connectionString,
|
|
234
|
-
onDatabaseLoad
|
|
235
|
-
})
|
|
236
|
-
await app.ready()
|
|
237
|
-
})
|
|
238
|
-
|
|
239
|
-
test('clean up all tables', async ({ teardown, has, equal, same }) => {
|
|
240
|
-
async function onDatabaseLoad (db, sql) {
|
|
241
|
-
await clear(db, sql)
|
|
242
|
-
teardown(async () => await clear(db, sql))
|
|
243
|
-
teardown(() => db.dispose())
|
|
244
|
-
|
|
245
|
-
await db.query(sql`CREATE TABLE IF NOT EXISTS pages (
|
|
246
|
-
id SERIAL PRIMARY KEY,
|
|
247
|
-
title VARCHAR(255) NOT NULL
|
|
248
|
-
);`)
|
|
249
|
-
}
|
|
250
|
-
const mapper = await connect({
|
|
251
|
-
connectionString: connInfo.connectionString,
|
|
252
|
-
log: fakeLogger,
|
|
253
|
-
onDatabaseLoad
|
|
254
|
-
})
|
|
255
|
-
|
|
256
|
-
const res = await mapper.entities.page.save({ input: { title: 'hello' } })
|
|
257
|
-
|
|
258
|
-
same(await mapper.entities.page.find(), [res])
|
|
259
|
-
|
|
260
|
-
await mapper.cleanUpAllEntities()
|
|
261
|
-
|
|
262
|
-
const pages = await mapper.entities.page.find()
|
|
263
|
-
equal(pages.length, 0)
|
|
264
|
-
})
|
|
265
|
-
|
|
266
|
-
test('clean up all tables with foreign keys', async ({ teardown, has, equal, same }) => {
|
|
267
|
-
async function onDatabaseLoad (db, sql) {
|
|
268
|
-
await clear(db, sql)
|
|
269
|
-
teardown(async () => await clear(db, sql))
|
|
270
|
-
teardown(() => db.dispose())
|
|
271
|
-
|
|
272
|
-
if (db.isSQLite) {
|
|
273
|
-
await db.query(sql`CREATE TABLE IF NOT EXISTS pages (
|
|
274
|
-
id INTEGER PRIMARY KEY,
|
|
275
|
-
title VARCHAR(255) NOT NULL
|
|
276
|
-
);`)
|
|
277
|
-
await db.query(sql`CREATE TABLE IF NOT EXISTS comments (
|
|
278
|
-
id INTEGER PRIMARY KEY,
|
|
279
|
-
text VARCHAR(255) NOT NULL,
|
|
280
|
-
page_id INTEGER NOT NULL REFERENCES pages(id)
|
|
281
|
-
);`)
|
|
282
|
-
} else if (db.isPg) {
|
|
283
|
-
await db.query(sql`CREATE TABLE IF NOT EXISTS pages (
|
|
284
|
-
id SERIAL PRIMARY KEY,
|
|
285
|
-
title VARCHAR(255) NOT NULL
|
|
286
|
-
);`)
|
|
287
|
-
await db.query(sql`CREATE TABLE IF NOT EXISTS comments (
|
|
288
|
-
id SERIAL PRIMARY KEY,
|
|
289
|
-
text VARCHAR(255) NOT NULL,
|
|
290
|
-
page_id INTEGER NOT NULL REFERENCES pages(id)
|
|
291
|
-
);`)
|
|
292
|
-
} else {
|
|
293
|
-
await db.query(sql`CREATE TABLE IF NOT EXISTS pages (
|
|
294
|
-
id INTEGER PRIMARY KEY AUTO_INCREMENT,
|
|
295
|
-
title VARCHAR(255) NOT NULL
|
|
296
|
-
);`)
|
|
297
|
-
await db.query(sql`CREATE TABLE IF NOT EXISTS comments (
|
|
298
|
-
id INTEGER PRIMARY KEY AUTO_INCREMENT,
|
|
299
|
-
text VARCHAR(255) NOT NULL,
|
|
300
|
-
page_id INTEGER NOT NULL REFERENCES pages(id)
|
|
301
|
-
);`)
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
const mapper = await connect({
|
|
305
|
-
connectionString: connInfo.connectionString,
|
|
306
|
-
log: fakeLogger,
|
|
307
|
-
onDatabaseLoad
|
|
308
|
-
})
|
|
309
|
-
|
|
310
|
-
{
|
|
311
|
-
const p1 = await mapper.entities.page.save({ input: { title: 'hello' } })
|
|
312
|
-
same(await mapper.entities.page.find(), [p1])
|
|
313
|
-
|
|
314
|
-
const c1 = await mapper.entities.comment.save({ input: { text: 'foo', pageId: p1.id } })
|
|
315
|
-
same(await mapper.entities.comment.find(), [c1])
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
await mapper.cleanUpAllEntities()
|
|
319
|
-
|
|
320
|
-
const pages = await mapper.entities.page.find()
|
|
321
|
-
equal(pages.length, 0)
|
|
322
|
-
|
|
323
|
-
const comments = await mapper.entities.comment.find()
|
|
324
|
-
equal(comments.length, 0)
|
|
325
|
-
})
|
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const { test } = require('tap')
|
|
4
|
-
|
|
5
|
-
const { clear, connInfo, isMysql8, isSQLite } = require('./helper')
|
|
6
|
-
const { connect } = require('..')
|
|
7
|
-
const fakeLogger = {
|
|
8
|
-
trace: () => {},
|
|
9
|
-
warn: () => {},
|
|
10
|
-
error: () => {}
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
test('unique key', async ({ equal, not, same, teardown }) => {
|
|
14
|
-
async function onDatabaseLoad (db, sql) {
|
|
15
|
-
await clear(db, sql)
|
|
16
|
-
teardown(() => db.dispose())
|
|
17
|
-
|
|
18
|
-
const table = sql`
|
|
19
|
-
CREATE TABLE pages (
|
|
20
|
-
xx INTEGER DEFAULT NULL UNIQUE,
|
|
21
|
-
name varchar(75) DEFAULT NULL UNIQUE
|
|
22
|
-
);
|
|
23
|
-
`
|
|
24
|
-
|
|
25
|
-
await db.query(table)
|
|
26
|
-
}
|
|
27
|
-
const mapper = await connect({
|
|
28
|
-
connectionString: connInfo.connectionString,
|
|
29
|
-
log: fakeLogger,
|
|
30
|
-
onDatabaseLoad,
|
|
31
|
-
ignore: {},
|
|
32
|
-
hooks: {}
|
|
33
|
-
})
|
|
34
|
-
const pageEntity = mapper.entities.page
|
|
35
|
-
not(pageEntity, undefined)
|
|
36
|
-
equal(pageEntity.name, 'Page')
|
|
37
|
-
equal(pageEntity.singularName, 'page')
|
|
38
|
-
equal(pageEntity.pluralName, 'pages')
|
|
39
|
-
if (isMysql8 || isSQLite) {
|
|
40
|
-
same(pageEntity.primaryKeys, new Set(['name']))
|
|
41
|
-
equal(pageEntity.camelCasedFields.name.primaryKey, true)
|
|
42
|
-
} else {
|
|
43
|
-
same(pageEntity.primaryKeys, new Set(['xx']))
|
|
44
|
-
equal(pageEntity.camelCasedFields.xx.primaryKey, true)
|
|
45
|
-
}
|
|
46
|
-
equal(pageEntity.camelCasedFields.xx.unique, true)
|
|
47
|
-
equal(pageEntity.camelCasedFields.name.unique, true)
|
|
48
|
-
})
|
|
49
|
-
|
|
50
|
-
test('no key', async ({ same, teardown, pass, equal, plan }) => {
|
|
51
|
-
plan(3)
|
|
52
|
-
async function onDatabaseLoad (db, sql) {
|
|
53
|
-
await clear(db, sql)
|
|
54
|
-
teardown(() => db.dispose())
|
|
55
|
-
|
|
56
|
-
const table = sql`
|
|
57
|
-
CREATE TABLE pages (
|
|
58
|
-
xx INTEGER DEFAULT NULL,
|
|
59
|
-
name varchar(75) DEFAULT NULL
|
|
60
|
-
);
|
|
61
|
-
`
|
|
62
|
-
|
|
63
|
-
await db.query(table)
|
|
64
|
-
}
|
|
65
|
-
const log = {
|
|
66
|
-
trace: () => {},
|
|
67
|
-
warn: (obj, str) => {
|
|
68
|
-
same(obj, { table: 'pages' })
|
|
69
|
-
equal(str, 'Cannot find any primary keys for table')
|
|
70
|
-
},
|
|
71
|
-
error: () => {}
|
|
72
|
-
}
|
|
73
|
-
const mapper = await connect({
|
|
74
|
-
connectionString: connInfo.connectionString,
|
|
75
|
-
log,
|
|
76
|
-
onDatabaseLoad
|
|
77
|
-
})
|
|
78
|
-
same(mapper.entities, {})
|
|
79
|
-
})
|