@jcbuisson/express-x-plugins 4.0.9 → 4.0.10

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jcbuisson/express-x-plugins",
3
- "version": "4.0.9",
3
+ "version": "4.0.10",
4
4
  "description": "Plugins for express-x",
5
5
  "type": "module",
6
6
  "main": "./src/electric-server-plugin.mjs",
@@ -91,7 +91,7 @@ export function electricOfflinePlugin(app, db, models, options = {}) {
91
91
 
92
92
  update: async function(id, data) {
93
93
  await authorize(this, model.name, 'update', [id, data])
94
- const set = buildSet(data)
94
+ const set = buildSet(data, model.primaryKey)
95
95
  return withTransaction(db, async client => {
96
96
  const result = await client.query(
97
97
  `UPDATE ${model.quotedTable} SET ${set.sql} WHERE ${model.quotedPrimaryKey} = $${set.values.length + 1} RETURNING *`,
@@ -208,9 +208,9 @@ function buildWhere(where, startIndex = 1) {
208
208
  return { sql: clauses.length ? clauses.join(' AND ') : 'TRUE', values }
209
209
  }
210
210
 
211
- function buildSet(data, startIndex = 1) {
211
+ function buildSet(data, primaryKey, startIndex = 1) {
212
212
  assertPlainObject(data, 'mutation data')
213
- const entries = Object.entries(data).filter(([key, value]) => key !== 'uid' && value !== undefined)
213
+ const entries = Object.entries(data).filter(([key, value]) => key !== primaryKey && value !== undefined)
214
214
  if (entries.length === 0) throw new TypeError('mutation data must contain at least one field')
215
215
  return {
216
216
  sql: entries.map(([column], index) => `${quoteIdentifier(column, 'data column')} = $${startIndex + index}`).join(', '),
@@ -1,5 +1,3 @@
1
- import { useSessionStorage } from '@vueuse/core'
2
-
3
1
  /**
4
2
  * Enrich `app` with listeners handling socket data transfer on page reload
5
3
  *
@@ -8,20 +6,18 @@ import { useSessionStorage } from '@vueuse/core'
8
6
  */
9
7
  export async function reloadPlugin(app) {
10
8
 
11
- const cnxid = useSessionStorage('cnxid', '')
12
- const cnxtoken = useSessionStorage('cnxtoken', '')
13
9
  const handleTransferToken = token => {
14
- if (typeof token === 'string') cnxtoken.value = token
10
+ if (typeof token === 'string') sessionStorage.setItem('cnxtoken', token)
15
11
  }
16
12
 
17
13
  app.addConnectListener(async (socket) => {
18
14
  const socketId = socket.id
19
15
  console.log('connect', socketId)
20
- const prevSocketId = cnxid.value
21
- const prevTransferToken = cnxtoken.value
16
+ const prevSocketId = sessionStorage.getItem('cnxid') ?? ''
17
+ const prevTransferToken = sessionStorage.getItem('cnxtoken') ?? ''
22
18
  socket.off('cnx-transfer-token', handleTransferToken)
23
19
  socket.on('cnx-transfer-token', handleTransferToken)
24
- cnxid.value = socketId
20
+ sessionStorage.setItem('cnxid', socketId)
25
21
  if (prevSocketId && prevTransferToken) {
26
22
  console.log('cnx-transfer', prevSocketId, 'to', socketId)
27
23
  let timeout
@@ -77,23 +77,6 @@ test('getObservable emits Shape rows and cleans up its subscription', () => {
77
77
  subscription.unsubscribe()
78
78
  })
79
79
 
80
- test('getVueRef returns Shape rows in a Vue ref and cleans up with its scope', () => {
81
- const app = { service: () => ({}) }
82
- electricClientPlugin(app, { ShapeStream: FakeStream, Shape: FakeShape })
83
- const todo = app.createElectricModel('todos')
84
- const scope = effectScope()
85
- let rows
86
-
87
- scope.run(() => { rows = todo.getVueRef({ completed: false }) })
88
-
89
- assert.equal(isRef(rows), true)
90
- assert.deepEqual(rows.value, [{ uid: 'one', completed: false }])
91
- const shape = FakeStream.instances.at(-1).shape
92
- assert.equal(shape.unsubscribed, undefined)
93
- scope.stop()
94
- assert.equal(shape.unsubscribed, true)
95
- })
96
-
97
80
  test('findMany resolves with the first Shape rows and cleans up its subscription', async () => {
98
81
  const app = { service: () => ({}) }
99
82
  electricClientPlugin(app, { ShapeStream: FakeStream, Shape: FakeShape })
@@ -48,6 +48,32 @@ test('allows the database to generate a primary key', async () => {
48
48
  assert.equal(queries[1].sql, 'INSERT INTO "todos" DEFAULT VALUES RETURNING *')
49
49
  })
50
50
 
51
+ test('protects the configured primary key during updates', async () => {
52
+ const services = new Map()
53
+ const queries = []
54
+ const db = {
55
+ async query(sql, values = []) {
56
+ queries.push({ sql, values })
57
+ return { rows: [] }
58
+ },
59
+ }
60
+ const app = {
61
+ createService(name, methods) { services.set(name, methods) },
62
+ get() {},
63
+ }
64
+ electricOfflinePlugin(app, db, [{ name: 'people', primaryKey: 'id' }], {
65
+ authorize: async () => true,
66
+ })
67
+
68
+ await services.get('people').update.call({}, 7, { id: 99, uid: 'editable', name: 'Ada' })
69
+
70
+ assert.equal(
71
+ queries[0].sql,
72
+ 'UPDATE "people" SET "uid" = $1, "name" = $2 WHERE "id" = $3 RETURNING *',
73
+ )
74
+ assert.deepEqual(queries[0].values, ['editable', 'Ada', 7])
75
+ })
76
+
51
77
  test('requires authorization and reports forbidden calls', async () => {
52
78
  const { services } = fixture({ authorize: async () => false })
53
79
  await assert.rejects(