Class: Nostrb::SQLite::Writer

Inherits:
Storage
  • Object
show all
Defined in:
lib/nostrb/sqlite.rb

Constant Summary

Constants inherited from Storage

Storage::CONFIG, Storage::FILENAME, Storage::GB, Storage::KB, Storage::MB, Storage::PRAGMAS, Storage::SQLITE_USAGE

Instance Attribute Summary

Attributes inherited from Storage

#db, #filename, #pragma

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Storage

#all_index_names, #all_table_names, #compile_options, #database_files, #index_names, #initialize, #pragma_scalars, #reader, #report, #set_pragmas, #setup, #table_names, #writer

Constructor Details

This class inherits a constructor from Nostrb::SQLite::Storage

Class Method Details

.serialize_tags(valid) ⇒ Object



397
398
399
# File 'lib/nostrb/sqlite.rb', line 397

def self.serialize_tags(valid)
  valid.merge("tags" => Nostrb.json(valid["tags"]))
end

Instance Method Details

#add_event(valid) ⇒ Object

a valid hash, as returned from SignedEvent.validate!



402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
# File 'lib/nostrb/sqlite.rb', line 402

def add_event(valid)
  @add_event ||= @db.prepare("INSERT INTO events
                                 VALUES (:content, :kind, :tags, :pubkey,
                                         :created_at, :id, :sig)")
  @add_tag ||= @db.prepare("INSERT INTO tags
                               VALUES (:event_id, :created_at,
                                       :tag, :value, :json)")
  @add_event.execute(Writer.serialize_tags(valid)) # insert event
  valid["tags"].each { |a|                         # insert tags
    @add_tag.execute(event_id: valid['id'],
                     created_at: valid['created_at'],
                     tag: a[0],
                     value: a[1],
                     json: Nostrb.json(a))
  }
end

#add_r_event(valid) ⇒ Object

add replaceable event



420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# File 'lib/nostrb/sqlite.rb', line 420

def add_r_event(valid)
  @add_r_event ||=
    @db.prepare("INSERT OR REPLACE INTO r_events
                            VALUES (:content, :kind, :tags, :d_tag,
                                    :pubkey, :created_at, :id, :sig)")
  @add_rtag ||= @db.prepare("INSERT INTO r_tags
                                VALUES (:r_event_id, :created_at,
                                        :tag, :value, :json)")
  tags = valid['tags']
  record = Writer.serialize_tags(valid)
  record['d_tag'] = Event.d_tag(tags)
  @add_r_event.execute(record) # upsert event
  tags.each { |a|              # insert tags
    @add_rtag.execute(r_event_id: valid['id'],
                      created_at: valid['created_at'],
                      tag: a[0],
                      value: a[1],
                      json: Nostrb.json(a))
  }
end