memos/store/db/postgres/storage.go
Irving Ou 9c18960f47
feat: support Postgres (#2569)
* skeleton of postgres

skeleton

* Adding Postgres specific db schema sql

* user test passed

* memo store test passed

* tag is working

* update user setting test done

* activity test done

* idp test passed

* inbox test done

* memo_organizer, UNTESTED

* memo relation test passed

* webhook test passed

* system setting test passed

* passed storage test

* pass resource test

* migration_history done

* fix memo_relation_test

* fixing server memo_relation test

* passes memo relation server test

* paess memo test

* final manual testing done

* final fixes

* final fixes cleanup

* sync schema

* lint

* lint

* lint

* lint

* lint
2023-12-03 13:31:29 +08:00

125 lines
2.8 KiB
Go

package postgres
import (
"context"
"github.com/Masterminds/squirrel"
"github.com/usememos/memos/store"
)
func (d *DB) CreateStorage(ctx context.Context, create *store.Storage) (*store.Storage, error) {
qb := squirrel.Insert("storage").Columns("name", "type", "config")
values := []any{create.Name, create.Type, create.Config}
if create.ID != 0 {
qb = qb.Columns("id")
values = append(values, create.ID)
}
qb = qb.Values(values...).Suffix("RETURNING id")
query, args, err := qb.PlaceholderFormat(squirrel.Dollar).ToSql()
if err != nil {
return nil, err
}
err = d.db.QueryRowContext(ctx, query, args...).Scan(&create.ID)
if err != nil {
return nil, err
}
return create, nil
}
func (d *DB) ListStorages(ctx context.Context, find *store.FindStorage) ([]*store.Storage, error) {
qb := squirrel.Select("id", "name", "type", "config").From("storage").OrderBy("id DESC")
if find.ID != nil {
qb = qb.Where(squirrel.Eq{"id": *find.ID})
}
query, args, err := qb.PlaceholderFormat(squirrel.Dollar).ToSql()
if err != nil {
return nil, err
}
rows, err := d.db.QueryContext(ctx, query, args...)
if err != nil {
return nil, err
}
defer rows.Close()
list := []*store.Storage{}
for rows.Next() {
storage := &store.Storage{}
if err := rows.Scan(&storage.ID, &storage.Name, &storage.Type, &storage.Config); err != nil {
return nil, err
}
list = append(list, storage)
}
if err := rows.Err(); err != nil {
return nil, err
}
return list, nil
}
func (d *DB) UpdateStorage(ctx context.Context, update *store.UpdateStorage) (*store.Storage, error) {
qb := squirrel.Update("storage")
if update.Name != nil {
qb = qb.Set("name", *update.Name)
}
if update.Config != nil {
qb = qb.Set("config", *update.Config)
}
qb = qb.Where(squirrel.Eq{"id": update.ID})
query, args, err := qb.PlaceholderFormat(squirrel.Dollar).ToSql()
if err != nil {
return nil, err
}
_, err = d.db.ExecContext(ctx, query, args...)
if err != nil {
return nil, err
}
storage := &store.Storage{}
query, args, err = squirrel.Select("id", "name", "type", "config").
From("storage").
Where(squirrel.Eq{"id": update.ID}).
PlaceholderFormat(squirrel.Dollar).
ToSql()
if err != nil {
return nil, err
}
if err := d.db.QueryRowContext(ctx, query, args...).Scan(&storage.ID, &storage.Name, &storage.Type, &storage.Config); err != nil {
return nil, err
}
return storage, nil
}
func (d *DB) DeleteStorage(ctx context.Context, delete *store.DeleteStorage) error {
qb := squirrel.Delete("storage").Where(squirrel.Eq{"id": delete.ID})
query, args, err := qb.PlaceholderFormat(squirrel.Dollar).ToSql()
if err != nil {
return err
}
result, err := d.db.ExecContext(ctx, query, args...)
if err != nil {
return err
}
if _, err := result.RowsAffected(); err != nil {
return err
}
return nil
}