@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.
@@ -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
- })
package/test/or.test.js DELETED
@@ -1,193 +0,0 @@
1
- 'use strict'
2
-
3
- const { test } = require('tap')
4
- const { connect } = require('..')
5
- const { clear, connInfo, isSQLite, isMysql } = require('./helper')
6
- const fakeLogger = {
7
- trace: () => { },
8
- error: () => { }
9
- }
10
-
11
- test('where clause with or operation', async ({ pass, teardown, same }) => {
12
- const mapper = await connect({
13
- ...connInfo,
14
- autoTimestamp: true,
15
- log: fakeLogger,
16
- async onDatabaseLoad (db, sql) {
17
- teardown(() => db.dispose())
18
- pass('onDatabaseLoad called')
19
-
20
- await clear(db, sql)
21
-
22
- if (isSQLite) {
23
- await db.query(sql`CREATE TABLE posts (
24
- id INTEGER PRIMARY KEY,
25
- title VARCHAR(42),
26
- long_text TEXT,
27
- counter INTEGER
28
- );`)
29
- } else if (isMysql) {
30
- await db.query(sql`CREATE TABLE posts (
31
- id SERIAL PRIMARY KEY,
32
- title VARCHAR(42),
33
- long_text TEXT,
34
- counter INTEGER
35
- );`)
36
- } else {
37
- await db.query(sql`CREATE TABLE posts (
38
- id SERIAL PRIMARY KEY,
39
- title VARCHAR(42),
40
- long_text TEXT,
41
- counter INTEGER
42
- );`)
43
- }
44
- }
45
- })
46
-
47
- const entity = mapper.entities.post
48
-
49
- const posts = [{
50
- title: 'Dog',
51
- longText: 'Foo',
52
- counter: 10
53
- }, {
54
- title: 'Cat',
55
- longText: 'Bar',
56
- counter: 20
57
- }, {
58
- title: 'Mouse',
59
- longText: 'Baz',
60
- counter: 30
61
- }, {
62
- title: 'Duck',
63
- longText: 'A duck tale',
64
- counter: 40
65
- }]
66
-
67
- await entity.insert({
68
- inputs: posts
69
- })
70
-
71
- {
72
- const data = await entity.find({
73
- where: {
74
- or: [
75
- {
76
- counter: {
77
- eq: 10
78
- }
79
- },
80
- {
81
- counter: {
82
- eq: 20
83
- }
84
- }
85
- ]
86
- }
87
- })
88
-
89
- same(data, [
90
- { id: '1', title: 'Dog', longText: 'Foo', counter: 10 },
91
- { id: '2', title: 'Cat', longText: 'Bar', counter: 20 }
92
- ])
93
- }
94
-
95
- {
96
- const data = await entity.find({
97
- where: {
98
- or: [
99
- {
100
- counter: {
101
- eq: 20
102
- }
103
- },
104
- {
105
- counter: {
106
- gte: 30
107
- }
108
- }
109
- ]
110
- }
111
- })
112
-
113
- same(data, [
114
- { id: '2', title: 'Cat', longText: 'Bar', counter: 20 },
115
- { id: '3', title: 'Mouse', longText: 'Baz', counter: 30 },
116
- { id: '4', title: 'Duck', longText: 'A duck tale', counter: 40 }
117
- ])
118
- }
119
-
120
- {
121
- const data = await entity.find({
122
- where: {
123
- or: [
124
- {
125
- title: {
126
- eq: 'Dog'
127
- }
128
- },
129
- {
130
- title: {
131
- eq: 'Duck'
132
- }
133
- }
134
- ]
135
- }
136
- })
137
-
138
- same(data, [
139
- { id: '1', title: 'Dog', longText: 'Foo', counter: 10 },
140
- { id: '4', title: 'Duck', longText: 'A duck tale', counter: 40 }
141
- ])
142
- }
143
-
144
- {
145
- const data = await entity.find({
146
- where: {
147
- or: [
148
- {
149
- title: {
150
- eq: 'Dog'
151
- }
152
- },
153
- {
154
- longText: {
155
- eq: 'Baz'
156
- }
157
- }
158
- ]
159
- }
160
- })
161
-
162
- same(data, [
163
- { id: '1', title: 'Dog', longText: 'Foo', counter: 10 },
164
- { id: '3', title: 'Mouse', longText: 'Baz', counter: 30 }
165
- ])
166
- }
167
-
168
- {
169
- const data = await entity.find({
170
- where: {
171
- counter: {
172
- in: [10, 20]
173
- },
174
- or: [
175
- {
176
- title: {
177
- eq: 'Dog'
178
- }
179
- },
180
- {
181
- longText: {
182
- eq: 'Baz'
183
- }
184
- }
185
- ]
186
- }
187
- })
188
-
189
- same(data, [
190
- { id: '1', title: 'Dog', longText: 'Foo', counter: 10 }
191
- ])
192
- }
193
- })