Skip to content

Commit

Permalink
store: init ent dbv2 support
Browse files Browse the repository at this point in the history
Signed-off-by: Wei Zhang <[email protected]>
  • Loading branch information
zwpaper committed Feb 8, 2024
1 parent 2d4ae64 commit ef5b193
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 23 deletions.
29 changes: 17 additions & 12 deletions api/v2/memo_relation_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/usememos/memos/ent"
"github.com/usememos/memos/ent/memorelation"
apiv2pb "github.com/usememos/memos/proto/gen/api/v2"
"github.com/usememos/memos/store"
)
Expand Down Expand Up @@ -44,23 +46,26 @@ func (s *APIV2Service) SetMemoRelations(ctx context.Context, request *apiv2pb.Se

func (s *APIV2Service) ListMemoRelations(ctx context.Context, request *apiv2pb.ListMemoRelationsRequest) (*apiv2pb.ListMemoRelationsResponse, error) {
relationList := []*apiv2pb.MemoRelation{}
tempList, err := s.Store.ListMemoRelations(ctx, &store.FindMemoRelation{
MemoID: &request.Id,
})
tempList, err := s.Store.V2.MemoRelation.
Query().
Where(memorelation.MemoID(int(request.Id))).
All(ctx)
if err != nil {
return nil, err
}

for _, relation := range tempList {
relationList = append(relationList, convertMemoRelationFromStore(relation))
relationList = append(relationList, convertMemoRelationFromStoreV2(relation))
}
tempList, err = s.Store.ListMemoRelations(ctx, &store.FindMemoRelation{
RelatedMemoID: &request.Id,
})
tempList, err = s.Store.V2.MemoRelation.
Query().
Where(memorelation.RelatedMemoID(int(request.Id))).
All(ctx)
if err != nil {
return nil, err
}
for _, relation := range tempList {
relationList = append(relationList, convertMemoRelationFromStore(relation))
relationList = append(relationList, convertMemoRelationFromStoreV2(relation))
}

response := &apiv2pb.ListMemoRelationsResponse{
Expand All @@ -69,11 +74,11 @@ func (s *APIV2Service) ListMemoRelations(ctx context.Context, request *apiv2pb.L
return response, nil
}

func convertMemoRelationFromStore(memoRelation *store.MemoRelation) *apiv2pb.MemoRelation {
func convertMemoRelationFromStoreV2(memoRelation *ent.MemoRelation) *apiv2pb.MemoRelation {
return &apiv2pb.MemoRelation{
MemoId: memoRelation.MemoID,
RelatedMemoId: memoRelation.RelatedMemoID,
Type: convertMemoRelationTypeFromStore(memoRelation.Type),
MemoId: int32(memoRelation.MemoID),
RelatedMemoId: int32(memoRelation.RelatedMemoID),
Type: convertMemoRelationTypeFromStore(store.MemoRelationType(memoRelation.Type)),
}
}

Expand Down
65 changes: 56 additions & 9 deletions api/v2/memo_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
storepb "github.com/usememos/memos/proto/gen/store"
"github.com/usememos/memos/server/service/metric"
"github.com/usememos/memos/store"
"github.com/usememos/memos/ent"
memotype "github.com/usememos/memos/ent/memo"
)

const (
Expand Down Expand Up @@ -137,29 +139,32 @@ func (s *APIV2Service) ListMemos(ctx context.Context, request *apiv2pb.ListMemos
}

func (s *APIV2Service) GetMemo(ctx context.Context, request *apiv2pb.GetMemoRequest) (*apiv2pb.GetMemoResponse, error) {
memo, err := s.Store.GetMemo(ctx, &store.FindMemo{
ID: &request.Id,
})
memo, err := s.Store.V2.Memo.
Query().
Where(memotype.ID(int(request.Id))).
Only(ctx)
if err != nil {
if ent.IsNotFound(err) {
return nil, status.Errorf(codes.NotFound, "memo not found")
}
return nil, err
}
if memo == nil {
return nil, status.Errorf(codes.NotFound, "memo not found")
}
if memo.Visibility != store.Public {

if store.Visibility(memo.Visibility) != store.Public {
user, err := getCurrentUser(ctx, s.Store)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get user")
}
if user == nil {
return nil, status.Errorf(codes.PermissionDenied, "permission denied")
}
if memo.Visibility == store.Private && memo.CreatorID != user.ID {
if store.Visibility(memo.Visibility) == store.Private &&
int32(memo.CreatorID) != user.ID {
return nil, status.Errorf(codes.PermissionDenied, "permission denied")
}
}

memoMessage, err := s.convertMemoFromStore(ctx, memo)
memoMessage, err := s.convertMemoFromStoreV2(ctx, memo)
if err != nil {
return nil, errors.Wrap(err, "failed to convert memo")
}
Expand Down Expand Up @@ -509,6 +514,48 @@ func (s *APIV2Service) ExportMemos(ctx context.Context, request *apiv2pb.ExportM
}, nil
}

func (s *APIV2Service) convertMemoFromStoreV2(ctx context.Context, memo *ent.Memo) (*apiv2pb.Memo, error) {
displayTs := memo.CreatedTs
if displayWithUpdatedTs, err := s.getMemoDisplayWithUpdatedTsSettingValue(ctx); err == nil && displayWithUpdatedTs {
displayTs = memo.UpdatedTs
}

creatorID := int32(memo.CreatorID)
creator, err := s.Store.GetUser(ctx, &store.FindUser{ID: &creatorID})
if err != nil {
return nil, errors.Wrap(err, "failed to get creator")
}

memoID := int32(memo.ID)
listMemoRelationsResponse, err := s.ListMemoRelations(ctx, &apiv2pb.ListMemoRelationsRequest{Id: memoID})
if err != nil {
return nil, errors.Wrap(err, "failed to list memo relations")
}

listMemoResourcesResponse, err := s.ListMemoResources(ctx, &apiv2pb.ListMemoResourcesRequest{Id: memoID})
if err != nil {
return nil, errors.Wrap(err, "failed to list memo resources")
}

return &apiv2pb.Memo{
Id: int32(memo.ID),
Name: memo.ResourceName,
RowStatus: convertRowStatusFromStore(store.RowStatus(memo.RowStatus)),
Creator: fmt.Sprintf("%s%s", UserNamePrefix, creator.Username),
CreatorId: int32(memo.CreatorID),
CreateTime: timestamppb.New(memo.CreatedTs),
UpdateTime: timestamppb.New(memo.UpdatedTs),
DisplayTime: timestamppb.New(displayTs),
Content: memo.Content,
Visibility: convertVisibilityFromStore(store.Visibility(memo.Visibility)),
// TODO(kw): implement pinned
// Pinned: memo.Pinned,
// ParentId: memo.ParentID,
Relations: listMemoRelationsResponse.Relations,
Resources: listMemoResourcesResponse.Resources,
}, nil
}

func (s *APIV2Service) convertMemoFromStore(ctx context.Context, memo *store.Memo) (*apiv2pb.Memo, error) {
displayTs := memo.CreatedTs
if displayWithUpdatedTs, err := s.getMemoDisplayWithUpdatedTsSettingValue(ctx); err == nil && displayWithUpdatedTs {
Expand Down
2 changes: 2 additions & 0 deletions api/v2/v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/reflection"

"github.com/usememos/memos/ent"
"github.com/usememos/memos/internal/log"
apiv2pb "github.com/usememos/memos/proto/gen/api/v2"
"github.com/usememos/memos/server/profile"
Expand All @@ -34,6 +35,7 @@ type APIV2Service struct {
Secret string
Profile *profile.Profile
Store *store.Store
StoreV2 *ent.Client

grpcServer *grpc.Server
grpcServerPort int
Expand Down
10 changes: 9 additions & 1 deletion bin/memos/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/usememos/memos/server/service/metric"
"github.com/usememos/memos/store"
"github.com/usememos/memos/store/db"
"github.com/usememos/memos/store/dbv2"
)

const (
Expand Down Expand Up @@ -60,7 +61,14 @@ var (
return
}

storeInstance := store.New(dbDriver, profile)
dbv2, err := dbv2.NewDriver(profile)
if err != nil {
cancel()
log.Error("failed to create dbv2 driver", zap.Error(err))
return
}

storeInstance := store.New(dbDriver, dbv2, profile)
s, err := server.NewServer(ctx, profile, storeInstance)
if err != nil {
cancel()
Expand Down
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/usememos/memos
go 1.22

require (
entgo.io/ent v0.12.5
github.com/aws/aws-sdk-go-v2 v1.24.1
github.com/aws/aws-sdk-go-v2/config v1.26.6
github.com/aws/aws-sdk-go-v2/credentials v1.16.16
Expand Down Expand Up @@ -38,27 +39,35 @@ require (
)

require (
ariga.io/atlas v0.14.1-0.20230918065911-83ad451a4935 // indirect
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/agext/levenshtein v1.2.1 // indirect
github.com/antlr4-go/antlr/v4 v4.13.0 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/desertbit/timer v0.0.0-20180107155436-c41aec40b27f // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/go-openapi/inflect v0.19.0 // indirect
github.com/go-openapi/jsonpointer v0.20.2 // indirect
github.com/go-openapi/jsonreference v0.20.4 // indirect
github.com/go-openapi/spec v0.20.14 // indirect
github.com/go-openapi/swag v0.22.9 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/gorilla/css v1.0.1 // indirect
github.com/hashicorp/hcl/v2 v2.13.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 // indirect
github.com/ncruces/go-strftime v0.1.9 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/rs/cors v1.10.1 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/stoewer/go-strcase v1.3.0 // indirect
github.com/zclconf/go-cty v1.8.0 // indirect
golang.org/x/image v0.15.0 // indirect
golang.org/x/tools v0.17.0 // indirect
google.golang.org/genproto v0.0.0-20240205150955-31a09d347014 // indirect
Expand Down
Loading

0 comments on commit ef5b193

Please sign in to comment.