@jcbuisson/express-x-plugins 4.1.1 → 4.1.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.
package/README.md
CHANGED
|
@@ -105,7 +105,7 @@ app.configure(electricClientPlugin, {
|
|
|
105
105
|
shapePath: '/electric/v1/shape',
|
|
106
106
|
})
|
|
107
107
|
|
|
108
|
-
// Client-generated UUID stored in the default `
|
|
108
|
+
// Client-generated UUID stored in the default `id` primary key:
|
|
109
109
|
const todo = app.createElectricModel('todos')
|
|
110
110
|
|
|
111
111
|
// Or, for a database-generated primary key such as SERIAL/IDENTITY:
|
|
@@ -121,8 +121,8 @@ const subscription = todo.getObservable({ completed: false }).subscribe(rows =>
|
|
|
121
121
|
|
|
122
122
|
// Mutations use the matching Express-X service and are reflected by Electric.
|
|
123
123
|
await todo.create({ title: 'Learn Shapes', completed: false })
|
|
124
|
-
await todo.update(
|
|
125
|
-
await todo.remove(
|
|
124
|
+
await todo.update(id, { completed: true })
|
|
125
|
+
await todo.remove(id)
|
|
126
126
|
|
|
127
127
|
const created = await numberedTodo.create({ title: 'Assigned by PostgreSQL' })
|
|
128
128
|
console.log(created.id)
|
|
@@ -143,7 +143,7 @@ configured table, and Electric source credentials stay server-side.
|
|
|
143
143
|
|
|
144
144
|
### Model configuration
|
|
145
145
|
|
|
146
|
-
Server models may be strings (table, service name, and default `
|
|
146
|
+
Server models may be strings (table, service name, and default `id` key) or objects:
|
|
147
147
|
|
|
148
148
|
```js
|
|
149
149
|
{ name: 'todo', table: 'todos', primaryKey: 'id' }
|
package/package.json
CHANGED
|
@@ -55,14 +55,14 @@ export function electricOfflinePlugin(app, db, models, options = {}) {
|
|
|
55
55
|
},
|
|
56
56
|
|
|
57
57
|
// create(data): the primary key is server-generated
|
|
58
|
-
// create(
|
|
59
|
-
create: async function(
|
|
58
|
+
// create(id, data): id (the primary key) is provided by the client
|
|
59
|
+
create: async function(idOrData, data) {
|
|
60
60
|
const hasClientId = data !== undefined
|
|
61
|
-
const mutationData = hasClientId ? data :
|
|
62
|
-
await authorize(this, model.name, 'create', hasClientId ? [
|
|
61
|
+
const mutationData = hasClientId ? data : idOrData
|
|
62
|
+
await authorize(this, model.name, 'create', hasClientId ? [idOrData, mutationData] : [mutationData])
|
|
63
63
|
assertPlainObject(mutationData, 'mutation data')
|
|
64
64
|
const safeData = hasClientId
|
|
65
|
-
? { ...mutationData, [model.primaryKey]:
|
|
65
|
+
? { ...mutationData, [model.primaryKey]: idOrData }
|
|
66
66
|
: { ...mutationData }
|
|
67
67
|
const entries = Object.entries(safeData).filter(([, value]) => value !== undefined)
|
|
68
68
|
const columns = entries.map(([column]) => quoteIdentifier(column, 'data column'))
|
|
@@ -164,7 +164,7 @@ function normalizeModels(models) {
|
|
|
164
164
|
}
|
|
165
165
|
const name = config.name
|
|
166
166
|
const table = config.table ?? name
|
|
167
|
-
const primaryKey = config.primaryKey ?? '
|
|
167
|
+
const primaryKey = config.primaryKey ?? 'id'
|
|
168
168
|
quoteIdentifier(name, 'model name')
|
|
169
169
|
return {
|
|
170
170
|
name,
|
|
@@ -10,7 +10,7 @@ function fixture({ authorize = async () => true, fetch } = {}) {
|
|
|
10
10
|
const db = {
|
|
11
11
|
async query(sql, values = []) {
|
|
12
12
|
queries.push({ sql, values })
|
|
13
|
-
return { rows: [{
|
|
13
|
+
return { rows: [{ id: values.at(-1) ?? 'one', label: 'row' }] }
|
|
14
14
|
},
|
|
15
15
|
}
|
|
16
16
|
const app = {
|
|
@@ -25,14 +25,18 @@ test('registers the mutation API', async () => {
|
|
|
25
25
|
const { services, queries } = fixture()
|
|
26
26
|
const service = services.get('todos')
|
|
27
27
|
assert.deepEqual(Object.keys(service), [
|
|
28
|
-
'create', 'update', 'delete',
|
|
28
|
+
'findUnique', 'findMany', 'create', 'update', 'delete',
|
|
29
29
|
])
|
|
30
30
|
|
|
31
31
|
const value = await service.create.call({}, 'one', { label: 'new' })
|
|
32
|
-
assert.equal(value.
|
|
32
|
+
assert.equal(value.id, 'one')
|
|
33
33
|
|
|
34
34
|
await service.create.call({}, 'only-id', {})
|
|
35
|
-
assert.match(queries[1].sql, /DO UPDATE SET "
|
|
35
|
+
assert.match(queries[1].sql, /DO UPDATE SET "id" = EXCLUDED\."id"/)
|
|
36
|
+
assert.equal(queries[1].values[0], 'only-id')
|
|
37
|
+
assert.deepEqual(fixture().registration.models, [
|
|
38
|
+
{ name: 'todos', table: 'todos', primaryKey: 'id' },
|
|
39
|
+
])
|
|
36
40
|
await assert.rejects(service.create.call({}, 'bad', []), /plain object/)
|
|
37
41
|
})
|
|
38
42
|
|