Skip to content

Commit 55a48bd

Browse files
committed
adding other operations
1 parent 49bbefa commit 55a48bd

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

bindings/postgres/postgres.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,15 @@ const (
4343
findByIdOperation bindings.OperationKind = "findById"
4444
findAllOperation bindings.OperationKind = "findByAll"
4545
deleteByIdOperation bindings.OperationKind = "deleteById"
46+
deleteAllOperation bindings.OperationKind = "deleteAll"
4647
existsByIdOperation bindings.OperationKind = "existsById"
4748
countOperation bindings.OperationKind = "count"
4849

4950
sqlInsert = "INSERT INTO %v (%v) VALUES (%v)"
50-
sqlDelete = "DELETE FROM %v"
5151
sqlUpdateById = "UPDATE '%v' SET %v WHERE %v = %d"
5252
sqlSelectById = "SELECT * FROM %v WHERE %v = %d"
5353
sqlSelectAll = "SELECT (%v) FROM %v"
54+
sqlDeleteAll = "DELETE FROM %v"
5455
sqlCountAll = "SELECT COUNT(id) FROM %v"
5556

5657
operationType = "type"
@@ -243,6 +244,14 @@ func (p *Postgres) Invoke(ctx context.Context, req *bindings.InvokeRequest) (res
243244
}
244245
resp.Data = d
245246

247+
case deleteAllOperation:
248+
sql := fmt.Sprintf(sqlDeleteAll, entityName)
249+
r, err := p.exec(ctx, sql, args...)
250+
if err != nil {
251+
return nil, err
252+
}
253+
resp.Metadata["rows-affected"] = strconv.FormatInt(r, 10) // 0 if error
254+
246255
case saveOperation:
247256
columns := strings.Join(entities[entityName].properties, ",")
248257
columnsWithoutId := strings.Replace(columns, fmt.Sprintf("%v,", entities[entityName].id), "", -1)

bindings/postgres/postgres_test.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ func TestPostgresEntityIntegration(t *testing.T) {
238238
t.Run("Invoke findAll", func(t *testing.T) {
239239
res, err := b.Invoke(ctx, req)
240240
assertResponse(t, res, err)
241+
assert.Equal(t, string(res.Data), "[]")
241242
})
242243

243244
req = &bindings.InvokeRequest{
@@ -246,7 +247,7 @@ func TestPostgresEntityIntegration(t *testing.T) {
246247
commandEntityName: "customers"},
247248
}
248249
t.Run("Invoke save", func(t *testing.T) {
249-
// The following payload is a json payload that's why it has weird quoting
250+
// @TODO: we should marshal a customer struct to JSON, but it will not be an array
250251
req.Metadata[commandEntityProps] = "[\"'salaboy'\",\"'salaboy'\",\"'chiswick'\", \"'london'\", \"'w4'\",\"'uk'\"]"
251252
res, err := b.Invoke(ctx, req)
252253
assertResponse(t, res, err)
@@ -260,6 +261,21 @@ func TestPostgresEntityIntegration(t *testing.T) {
260261
t.Run("Invoke findAll", func(t *testing.T) {
261262
res, err := b.Invoke(ctx, req)
262263
assertResponse(t, res, err)
264+
assert.NotNil(t, res.Data)
265+
//@TODO: we can marshal this into a customer struct and validate props
266+
assert.Contains(t, string(res.Data), "\"salaboy\",\"salaboy\",\"chiswick\",\"london\",\"w4\",\"uk\"")
267+
})
268+
269+
req = &bindings.InvokeRequest{
270+
Operation: deleteAllOperation,
271+
Metadata: map[string]string{operationType: "entity",
272+
commandEntityName: "customers"},
273+
}
274+
t.Run("Invoke delete all", func(t *testing.T) {
275+
276+
res, err := b.Invoke(ctx, req)
277+
assertResponse(t, res, err)
278+
assert.Equal(t, res.Metadata["rows-affected"], "1")
263279
})
264280

265281
t.Run("Close", func(t *testing.T) {

0 commit comments

Comments
 (0)