@platformatic/sql-events 0.5.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/.nyc_output/216765ce-d0ee-4972-a798-60b27dee3411.json +1 -0
- package/.nyc_output/b818e2e5-299b-49c9-ade4-114a2b3e4fe5.json +1 -0
- package/.nyc_output/bd3970ed-e41b-4f49-8852-b8436108783a.json +1 -0
- package/.nyc_output/c2aec7f4-8b75-4b4a-8d27-d834cf4b0a33.json +1 -0
- package/.nyc_output/processinfo/216765ce-d0ee-4972-a798-60b27dee3411.json +1 -0
- package/.nyc_output/processinfo/b818e2e5-299b-49c9-ade4-114a2b3e4fe5.json +1 -0
- package/.nyc_output/processinfo/bd3970ed-e41b-4f49-8852-b8436108783a.json +1 -0
- package/.nyc_output/processinfo/c2aec7f4-8b75-4b4a-8d27-d834cf4b0a33.json +1 -0
- package/.nyc_output/processinfo/index.json +1 -0
- package/.taprc +1 -0
- package/LICENSE +201 -0
- package/index.d.ts +30 -0
- package/index.js +131 -0
- package/package.json +42 -0
- package/test/helper.js +66 -0
- package/test/hooks.test.js +317 -0
- package/test/redis.test.js +116 -0
- package/test/simple.test.js +305 -0
- package/test/types/index.test-d.ts +17 -0
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { test } = require('tap')
|
|
4
|
+
const sqlMapper = require('@platformatic/sql-mapper')
|
|
5
|
+
const { connect } = sqlMapper
|
|
6
|
+
const { clear, connInfo, isSQLite } = require('./helper')
|
|
7
|
+
const sqlEvents = require('..')
|
|
8
|
+
const { setupEmitter } = sqlEvents
|
|
9
|
+
const MQEmitter = require('mqemitter')
|
|
10
|
+
const fastify = require('fastify')
|
|
11
|
+
|
|
12
|
+
const fakeLogger = {
|
|
13
|
+
trace () {},
|
|
14
|
+
error () {}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
test('emit events', async ({ equal, same, teardown }) => {
|
|
18
|
+
async function onDatabaseLoad (db, sql) {
|
|
19
|
+
await clear(db, sql)
|
|
20
|
+
teardown(() => db.dispose())
|
|
21
|
+
|
|
22
|
+
if (isSQLite) {
|
|
23
|
+
await db.query(sql`CREATE TABLE pages (
|
|
24
|
+
id INTEGER PRIMARY KEY,
|
|
25
|
+
title VARCHAR(42)
|
|
26
|
+
);`)
|
|
27
|
+
} else {
|
|
28
|
+
await db.query(sql`CREATE TABLE pages (
|
|
29
|
+
id SERIAL PRIMARY KEY,
|
|
30
|
+
title VARCHAR(255) NOT NULL
|
|
31
|
+
);`)
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
const mapper = await connect({
|
|
35
|
+
log: fakeLogger,
|
|
36
|
+
...connInfo,
|
|
37
|
+
onDatabaseLoad
|
|
38
|
+
})
|
|
39
|
+
const pageEntity = mapper.entities.page
|
|
40
|
+
|
|
41
|
+
const mq = MQEmitter()
|
|
42
|
+
equal(setupEmitter({ mapper, mq, log: fakeLogger }), undefined)
|
|
43
|
+
const queue = await mapper.subscribe([
|
|
44
|
+
'/entity/page/save/+',
|
|
45
|
+
'/entity/page/delete/+'
|
|
46
|
+
])
|
|
47
|
+
equal(mapper.mq, mq)
|
|
48
|
+
|
|
49
|
+
const expected = []
|
|
50
|
+
|
|
51
|
+
// save - new record
|
|
52
|
+
const page = await pageEntity.save({
|
|
53
|
+
input: { title: 'fourth page' }
|
|
54
|
+
})
|
|
55
|
+
expected.push({
|
|
56
|
+
topic: '/entity/page/save/' + page.id,
|
|
57
|
+
payload: {
|
|
58
|
+
id: page.id
|
|
59
|
+
}
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
// save - update record
|
|
63
|
+
const page2 = await pageEntity.save({
|
|
64
|
+
input: {
|
|
65
|
+
id: page.id,
|
|
66
|
+
title: 'fifth page'
|
|
67
|
+
}
|
|
68
|
+
})
|
|
69
|
+
expected.push({
|
|
70
|
+
topic: '/entity/page/save/' + page.id,
|
|
71
|
+
payload: {
|
|
72
|
+
id: page2.id
|
|
73
|
+
}
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
await pageEntity.delete({
|
|
77
|
+
where: {
|
|
78
|
+
id: {
|
|
79
|
+
eq: page.id
|
|
80
|
+
}
|
|
81
|
+
},
|
|
82
|
+
fields: ['id', 'title']
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
expected.push({
|
|
86
|
+
topic: '/entity/page/delete/' + page.id,
|
|
87
|
+
payload: {
|
|
88
|
+
id: page.id
|
|
89
|
+
}
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
for await (const ev of queue) {
|
|
93
|
+
same(ev, expected.shift())
|
|
94
|
+
if (expected.length === 0) {
|
|
95
|
+
break
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
test('return entities', async ({ pass, teardown, equal, same }) => {
|
|
101
|
+
async function onDatabaseLoad (db, sql) {
|
|
102
|
+
await clear(db, sql)
|
|
103
|
+
|
|
104
|
+
if (isSQLite) {
|
|
105
|
+
await db.query(sql`CREATE TABLE pages (
|
|
106
|
+
id INTEGER PRIMARY KEY,
|
|
107
|
+
title VARCHAR(42)
|
|
108
|
+
);`)
|
|
109
|
+
} else {
|
|
110
|
+
await db.query(sql`CREATE TABLE pages (
|
|
111
|
+
id SERIAL PRIMARY KEY,
|
|
112
|
+
title VARCHAR(255) NOT NULL
|
|
113
|
+
);`)
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
const app = fastify()
|
|
117
|
+
teardown(() => app.close())
|
|
118
|
+
app.register(sqlMapper, {
|
|
119
|
+
connectionString: connInfo.connectionString,
|
|
120
|
+
onDatabaseLoad
|
|
121
|
+
})
|
|
122
|
+
app.register(sqlEvents)
|
|
123
|
+
|
|
124
|
+
await app.ready()
|
|
125
|
+
const pageEntity = app.platformatic.entities.page
|
|
126
|
+
const queue = await app.platformatic.subscribe([
|
|
127
|
+
'/entity/page/save/+',
|
|
128
|
+
'/entity/page/delete/+'
|
|
129
|
+
])
|
|
130
|
+
|
|
131
|
+
const expected = []
|
|
132
|
+
|
|
133
|
+
// save - new record
|
|
134
|
+
const page = await pageEntity.save({
|
|
135
|
+
input: { title: 'fourth page' }
|
|
136
|
+
})
|
|
137
|
+
expected.push({
|
|
138
|
+
topic: '/entity/page/save/' + page.id,
|
|
139
|
+
payload: {
|
|
140
|
+
id: page.id
|
|
141
|
+
}
|
|
142
|
+
})
|
|
143
|
+
|
|
144
|
+
// save - update record
|
|
145
|
+
await pageEntity.save({
|
|
146
|
+
input: {
|
|
147
|
+
id: page.id,
|
|
148
|
+
title: 'fifth page'
|
|
149
|
+
}
|
|
150
|
+
})
|
|
151
|
+
expected.push({
|
|
152
|
+
topic: '/entity/page/save/' + page.id,
|
|
153
|
+
payload: {
|
|
154
|
+
id: page.id
|
|
155
|
+
}
|
|
156
|
+
})
|
|
157
|
+
|
|
158
|
+
await pageEntity.delete({
|
|
159
|
+
where: {
|
|
160
|
+
id: {
|
|
161
|
+
eq: page.id
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
expected.push({
|
|
167
|
+
topic: '/entity/page/delete/' + page.id,
|
|
168
|
+
payload: {
|
|
169
|
+
id: page.id
|
|
170
|
+
}
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
for await (const ev of queue) {
|
|
174
|
+
same(ev, expected.shift())
|
|
175
|
+
if (expected.length === 0) {
|
|
176
|
+
break
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
})
|
|
180
|
+
|
|
181
|
+
test('insert', async ({ equal, same, teardown }) => {
|
|
182
|
+
async function onDatabaseLoad (db, sql) {
|
|
183
|
+
await clear(db, sql)
|
|
184
|
+
teardown(() => db.dispose())
|
|
185
|
+
|
|
186
|
+
if (isSQLite) {
|
|
187
|
+
await db.query(sql`CREATE TABLE pages (
|
|
188
|
+
id INTEGER PRIMARY KEY,
|
|
189
|
+
title VARCHAR(42)
|
|
190
|
+
);`)
|
|
191
|
+
} else {
|
|
192
|
+
await db.query(sql`CREATE TABLE pages (
|
|
193
|
+
id SERIAL PRIMARY KEY,
|
|
194
|
+
title VARCHAR(255) NOT NULL
|
|
195
|
+
);`)
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
const mapper = await connect({
|
|
199
|
+
log: fakeLogger,
|
|
200
|
+
...connInfo,
|
|
201
|
+
onDatabaseLoad
|
|
202
|
+
})
|
|
203
|
+
const pageEntity = mapper.entities.page
|
|
204
|
+
|
|
205
|
+
const mq = MQEmitter()
|
|
206
|
+
equal(setupEmitter({ mapper, mq, log: fakeLogger }), undefined)
|
|
207
|
+
const queue = await mapper.subscribe('/entity/page/save/+')
|
|
208
|
+
equal(mapper.mq, mq)
|
|
209
|
+
|
|
210
|
+
const expected = []
|
|
211
|
+
|
|
212
|
+
// save - new record
|
|
213
|
+
const pages = await pageEntity.insert({
|
|
214
|
+
inputs: [{
|
|
215
|
+
title: 'fourth page'
|
|
216
|
+
}, {
|
|
217
|
+
title: 'fifth page'
|
|
218
|
+
}]
|
|
219
|
+
})
|
|
220
|
+
|
|
221
|
+
for (const page of pages) {
|
|
222
|
+
expected.push({
|
|
223
|
+
topic: '/entity/page/save/' + page.id,
|
|
224
|
+
payload: {
|
|
225
|
+
id: page.id
|
|
226
|
+
}
|
|
227
|
+
})
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
for await (const ev of queue) {
|
|
231
|
+
same(ev, expected.shift())
|
|
232
|
+
if (expected.length === 0) {
|
|
233
|
+
break
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
})
|
|
237
|
+
|
|
238
|
+
test('more than one element for delete', async ({ equal, same, teardown }) => {
|
|
239
|
+
async function onDatabaseLoad (db, sql) {
|
|
240
|
+
await clear(db, sql)
|
|
241
|
+
teardown(() => db.dispose())
|
|
242
|
+
|
|
243
|
+
if (isSQLite) {
|
|
244
|
+
await db.query(sql`CREATE TABLE pages (
|
|
245
|
+
id INTEGER PRIMARY KEY,
|
|
246
|
+
title VARCHAR(42)
|
|
247
|
+
);`)
|
|
248
|
+
} else {
|
|
249
|
+
await db.query(sql`CREATE TABLE pages (
|
|
250
|
+
id SERIAL PRIMARY KEY,
|
|
251
|
+
title VARCHAR(255) NOT NULL
|
|
252
|
+
);`)
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
const mapper = await connect({
|
|
256
|
+
log: fakeLogger,
|
|
257
|
+
...connInfo,
|
|
258
|
+
onDatabaseLoad
|
|
259
|
+
})
|
|
260
|
+
const pageEntity = mapper.entities.page
|
|
261
|
+
|
|
262
|
+
const mq = MQEmitter()
|
|
263
|
+
equal(setupEmitter({ mapper, mq, log: fakeLogger }), undefined)
|
|
264
|
+
const queue = await mapper.subscribe([
|
|
265
|
+
'/entity/page/delete/+'
|
|
266
|
+
])
|
|
267
|
+
equal(mapper.mq, mq)
|
|
268
|
+
|
|
269
|
+
const expected = []
|
|
270
|
+
|
|
271
|
+
const page1 = await pageEntity.save({
|
|
272
|
+
input: { title: 'fourth page' }
|
|
273
|
+
})
|
|
274
|
+
|
|
275
|
+
const page2 = await pageEntity.save({
|
|
276
|
+
input: { title: 'fifth page' }
|
|
277
|
+
})
|
|
278
|
+
|
|
279
|
+
// delete all pages
|
|
280
|
+
await pageEntity.delete({
|
|
281
|
+
where: {},
|
|
282
|
+
fields: ['id', 'title']
|
|
283
|
+
})
|
|
284
|
+
|
|
285
|
+
expected.push({
|
|
286
|
+
topic: '/entity/page/delete/' + page1.id,
|
|
287
|
+
payload: {
|
|
288
|
+
id: page1.id
|
|
289
|
+
}
|
|
290
|
+
})
|
|
291
|
+
|
|
292
|
+
expected.push({
|
|
293
|
+
topic: '/entity/page/delete/' + page2.id,
|
|
294
|
+
payload: {
|
|
295
|
+
id: page2.id
|
|
296
|
+
}
|
|
297
|
+
})
|
|
298
|
+
|
|
299
|
+
for await (const ev of queue) {
|
|
300
|
+
same(ev, expected.shift())
|
|
301
|
+
if (expected.length === 0) {
|
|
302
|
+
break
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
})
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { expectType } from 'tsd'
|
|
2
|
+
import { fastify, FastifyInstance } from 'fastify'
|
|
3
|
+
import plugin, { setupEmitter } from '../../index'
|
|
4
|
+
import { Readable } from 'stream'
|
|
5
|
+
import {SQLMapperPluginInterface} from '@platformatic/sql-mapper'
|
|
6
|
+
|
|
7
|
+
const instance: FastifyInstance = fastify()
|
|
8
|
+
instance.register(plugin)
|
|
9
|
+
|
|
10
|
+
instance.register(async (instance) => {
|
|
11
|
+
expectType<Promise<Readable>>(instance.platformatic.subscribe('/test'))
|
|
12
|
+
expectType<Promise<Readable>>(instance.platformatic.subscribe(['/test']))
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
setupEmitter({ mapper: {} as SQLMapperPluginInterface })
|
|
16
|
+
setupEmitter({ mapper: {} as SQLMapperPluginInterface, connectionString: 'redis://localhost:6379' })
|
|
17
|
+
setupEmitter({ mapper: {} as SQLMapperPluginInterface, mq: {} })
|