Class: Polyn::SchemaStore

Inherits:
Object
  • Object
show all
Defined in:
lib/polyn/schema_store.rb

Overview

Persisting and interacting with persisted schemas

Constant Summary collapse

STORE_NAME =
"POLYN_SCHEMAS"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nats, **opts) ⇒ SchemaStore

Returns a new instance of SchemaStore.



9
10
11
12
13
14
# File 'lib/polyn/schema_store.rb', line 9

def initialize(nats, **opts)
  @nats       = nats
  @store_name = opts[:name] || STORE_NAME
  @key_prefix = "$KV.#{@store_name}"
  @schemas    = opts[:schemas] || fetch_schemas
end

Instance Attribute Details

#schemasObject (readonly)

Returns the value of attribute schemas.



16
17
18
# File 'lib/polyn/schema_store.rb', line 16

def schemas
  @schemas
end

Instance Method Details

#get(type) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/polyn/schema_store.rb', line 37

def get(type)
  schema = schemas[type]

  if schema.nil?
    return Polyn::Errors::SchemaError.new(
        "Schema for #{type} does not exist. Make sure it's "\
        "been added to your `events` codebase and has been loaded "\
        "into the schema store on your NATS server",
      )
  end

  schema
end

#get!(type) ⇒ Object



30
31
32
33
34
35
# File 'lib/polyn/schema_store.rb', line 30

def get!(type)
  result = get(type)
  raise result if result.is_a?(Polyn::Errors::SchemaError)

  result
end

#json_schema?(schema) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/polyn/schema_store.rb', line 26

def json_schema?(schema)
  JSONSchemer.schema(schema)
end

#save(type, schema) ⇒ Object

Persist a schema. In prod/dev schemas should have already been persisted via the Polyn CLI.



21
22
23
24
# File 'lib/polyn/schema_store.rb', line 21

def save(type, schema)
  json_schema?(schema)
  schemas[type] = JSON.generate(schema)
end