learndb/db/export_json.rb

80 wiersze
1.4 KiB
Ruby
Czysty Zwykły widok Historia

2022-12-26 00:21:39 +00:00
require 'pg'
require 'active_record'
require 'json'
class MyDB < ActiveRecord::Base
self.abstract_class = true
end
class Topic < MyDB
def as_json(options = {})
{
name: name,
2022-12-26 01:04:11 +00:00
hname: hname,
2022-12-26 00:21:39 +00:00
parent_id: parent_name,
sort_index: sort_index
}
end
end
class Creator < MyDB
end
class Item < MyDB
def as_json(options = {})
{
2022-12-26 01:04:11 +00:00
id: id,
2022-12-26 00:21:39 +00:00
name: hname,
description: description,
image: image_url,
2022-12-26 01:04:11 +00:00
links: links,
topics: topics,
creators: creators,
2022-12-26 00:21:39 +00:00
year: year,
difficulty: level,
cost: cost,
rating: rating,
2022-12-26 01:04:11 +00:00
tags: tags
2022-12-26 00:21:39 +00:00
}
end
end
class Review < MyDB
def as_json(options = {})
{
item_id: item_id,
by: by_creator,
rating: rating,
blurb: blurb,
url: url
}
end
end
ActiveRecord::Base.logger = Logger.new(STDERR)
ActiveRecord::Base.establish_connection(
{ adapter: 'postgresql',
database: 'postgres',
host: ENV['SUPABASE_HOST'],
username: 'postgres',
password: ENV['SUPABASE_PASSWORD'],
port: 6543
}
)
File.open("topics.json","w") do |f|
f.write(JSON.pretty_generate(JSON.parse(Topic.all.to_json)))
end
File.open("creators.json","w") do |f|
f.write(JSON.pretty_generate(JSON.parse(Creator.all.to_json)))
end
File.open("items.json","w") do |f|
f.write(JSON.pretty_generate(JSON.parse(Item.all.to_json)))
end
File.open("reviews.json","w") do |f|
f.write(JSON.pretty_generate(JSON.parse(Review.all.to_json)))
end