Skip to content

Commit

Permalink
feat: add ephemeral attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
npaton committed Mar 31, 2024
1 parent ca85faf commit ca23f2d
Show file tree
Hide file tree
Showing 12 changed files with 221 additions and 25 deletions.
74 changes: 73 additions & 1 deletion internal/graph/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions internal/graph/mgen/models_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions internal/graph/schema/attribute.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ type Attribute implements Node {
"""
immutable: Boolean!

"""
ephemeral indicates the Attribute should not be persisted. Ephemeral
Attributes are not stored in the database and are only synced to the
connected clients. An ephemeral Attribute cannot become non-ephemeral and vice
versa.
"""
ephemeral: Boolean!

"""
deletedAt is the time when the Attribute was deleted. If null, the Attribute
was not deleted.
Expand Down Expand Up @@ -123,6 +131,15 @@ input SetAttributeInput {
"""
immutable: Boolean

"""
ephemeral indicates the Attribute should not be persisted. Ephemeral
Attributes are not stored in the database and are only synced to the
connected clients.
Defaults to false and does need to be sent on subsequent updates. An ephemeral
Attribute cannot become non-ephemeral and vice versa.
"""
ephemeral: Boolean

"""
ID of object on which to update the value. NodeID is required if attribute is
not created with addScope().
Expand Down
4 changes: 4 additions & 0 deletions internal/models/attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ type Attribute struct {
Versions []*Attribute `json:"-"`
// version is the version number of this Attribute, starting at 1.
Version int `json:"-"`
// ephemeral indicates the Attribute should not be persisted. Ephemeral
// Attributes are not stored in the database and are only synced to the
// connected clients.
Ephemeral bool `json:"-"`
// private indicates whether the Attribute shouldn't be visible to Participants
// in the scope.
// private must be set on the Attribute at creation.
Expand Down
26 changes: 21 additions & 5 deletions internal/runtime/attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (r *Runtime) prepAttributes(

index := input.Index

var vector, private, protected, immutable bool
var vector, private, protected, immutable, ephemeral bool

if input.Append != nil && *input.Append {
vector = true
Expand Down Expand Up @@ -129,6 +129,10 @@ func (r *Runtime) prepAttributes(
immutable = true
}

if input.Ephemeral != nil && *input.Ephemeral {
ephemeral = true
}

a := &models.Attribute{
ID: ids.ID(ctx),
CreatedAt: now,
Expand All @@ -144,6 +148,7 @@ func (r *Runtime) prepAttributes(
Private: private,
Protected: protected,
Immutable: immutable,
Ephemeral: ephemeral,
}

last := scope.AttributesMap[a.LookupKey()]
Expand All @@ -158,6 +163,14 @@ func (r *Runtime) prepAttributes(
return nil, ErrImmutable
}

if last.Ephemeral && !a.Ephemeral {
return nil, ErrEphemeral
}

if !last.Ephemeral && a.Ephemeral {
return nil, ErrNotEphemeral
}

if (a.Vector && !last.Vector) || !a.Vector && last.Vector {
return nil, errors.New("cannot mutate vector")
}
Expand Down Expand Up @@ -194,12 +207,15 @@ func (r *Runtime) setAttributes(
attrs []*models.Attribute,
) (attributes []*models.Attribute, err error) {
conn := store.ForContext(ctx)

for _, attr := range attrs {
err = conn.Save(attr)
if err != nil {
log.Ctx(r.ctx).Error().Err(err).Msg("runtime: failed to save attribute")
if !attr.Ephemeral {
err = conn.Save(attr)
if err != nil {
log.Ctx(r.ctx).Error().Err(err).Msg("runtime: failed to save attribute")

continue
continue
}
}

scope, _ := attr.Node.(*models.Scope)
Expand Down
2 changes: 2 additions & 0 deletions internal/runtime/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ var (
ErrCursorNotFound = errors.New("cursor not found")
ErrLengthInvalid = errors.New("invalid pagination length")
ErrImmutable = errors.New("record is immutable")
ErrEphemeral = errors.New("record is ephemeral")
ErrNotEphemeral = errors.New("record is not ephemeral")
ErrInvalidNode = errors.New("invalid node")
ErrNotAuthorized = errors.New("not authorized")
ErrAuthenticationFailed = errors.New("authentication failed")
Expand Down
12 changes: 6 additions & 6 deletions lib/tajriba/modd.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ src/documents/**/*.graphql codegen.yml {
prep: npm run generate
}

src/**/*.ts {
src/**/*.ts !src/generated/**/*.ts {
prep: npm run build
}

src/**/*.ts {
src/**/*.ts !src/generated/**/*.ts {
prep: npm run check
}

# Requires Tajriba to be running on the default port.
dist/module.js src/test.mjs {
prep: npm run test
}
# # Requires Tajriba to be running on the default port.
# dist/module.js src/test.mjs {
# prep: npm run test
# }
6 changes: 3 additions & 3 deletions lib/tajriba/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@
"access": "public"
},
"volta": {
"node": "20.10.0",
"npm": "10.2.3"
"node": "20.12.0",
"npm": "10.5.0"
},
"engines": {
"node": ">= 20.10.0"
"node": ">= 20.12.0"
},
"scripts": {
"generate": "graphql-codegen",
Expand Down
1 change: 1 addition & 0 deletions lib/tajriba/src/documents/attributes.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ fragment attributeFields on Attribute {
private
protected
immutable
ephemeral
deletedAt
key
val
Expand Down

0 comments on commit ca23f2d

Please sign in to comment.