@jcbuisson/express-x-plugins 4.0.8 → 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/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
  # express-x-plugins
3
3
 
4
4
 
5
- IMPORTANT
5
+ IMPORTANT FOR ELECTRIC
6
6
 
7
7
  # set lc_messages=C for the PostgreSQL role so Electric receives recognizable English errors
8
8
  ALTER ROLE chris SET lc_messages = 'C';
@@ -13,9 +13,10 @@ ALTER SYSTEM SET max_replication_slots = 10;
13
13
  ALTER SYSTEM SET max_wal_senders = 10;
14
14
  ALTER ROLE chris WITH REPLICATION;
15
15
 
16
- ## restart postgres
17
- sudo systemctl restart postgresql
16
+ (restart postgres: `sudo systemctl restart postgresql`)
18
17
 
18
+ ## Use HTTP2
19
+ nginx: `listen 443 ssl http2;`
19
20
 
20
21
 
21
22
  Currently includes:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jcbuisson/express-x-plugins",
3
- "version": "4.0.8",
3
+ "version": "4.0.10",
4
4
  "description": "Plugins for express-x",
5
5
  "type": "module",
6
6
  "main": "./src/electric-server-plugin.mjs",
@@ -2,17 +2,6 @@ import { Shape, ShapeStream } from '@electric-sql/client'
2
2
  import { firstValueFrom, Observable, Subject, takeUntil } from 'rxjs'
3
3
  import { getCurrentScope, onScopeDispose, ref } from 'vue'
4
4
 
5
- export class DisposableShape extends Shape {
6
- subscribe(callback) {
7
- const unsubscribe = super.subscribe(callback)
8
- return () => {
9
- unsubscribe()
10
- if (this.numSubscribers === 0) this.stream.unsubscribeAll()
11
- }
12
- }
13
- }
14
-
15
-
16
5
  /**
17
6
  * Add Electric-backed reactive models to an Express-X client.
18
7
  *
@@ -100,6 +89,16 @@ export function electricClientPlugin(app, options = {}) {
100
89
 
101
90
  ////////////////////// UTILITIES //////////////////////
102
91
 
92
+ export class DisposableShape extends Shape {
93
+ subscribe(callback) {
94
+ const unsubscribe = super.subscribe(callback)
95
+ return () => {
96
+ unsubscribe()
97
+ if (this.numSubscribers === 0) this.stream.unsubscribeAll()
98
+ }
99
+ }
100
+ }
101
+
103
102
  const IDENTIFIER = /^[A-Za-z_][A-Za-z0-9_]*$/
104
103
  const RANGE_OPERATORS = { gt: '>', gte: '>=', lt: '<', lte: '<=' }
105
104
 
@@ -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(', '),
@@ -6,20 +6,18 @@
6
6
  */
7
7
  export async function reloadPlugin(app) {
8
8
 
9
- const cnxid = useSessionStorage('cnxid', '')
10
- const cnxtoken = useSessionStorage('cnxtoken', '')
11
9
  const handleTransferToken = token => {
12
- if (typeof token === 'string') cnxtoken.value = token
10
+ if (typeof token === 'string') sessionStorage.setItem('cnxtoken', token)
13
11
  }
14
12
 
15
13
  app.addConnectListener(async (socket) => {
16
14
  const socketId = socket.id
17
15
  console.log('connect', socketId)
18
- const prevSocketId = cnxid.value
19
- const prevTransferToken = cnxtoken.value
16
+ const prevSocketId = sessionStorage.getItem('cnxid') ?? ''
17
+ const prevTransferToken = sessionStorage.getItem('cnxtoken') ?? ''
20
18
  socket.off('cnx-transfer-token', handleTransferToken)
21
19
  socket.on('cnx-transfer-token', handleTransferToken)
22
- cnxid.value = socketId
20
+ sessionStorage.setItem('cnxid', socketId)
23
21
  if (prevSocketId && prevTransferToken) {
24
22
  console.log('cnx-transfer', prevSocketId, 'to', socketId)
25
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(