├── .ruby-version ├── .rspec ├── lib ├── schema2type │ ├── version.rb │ ├── conversion_table.yml │ ├── cli.rb │ ├── covert_service.rb │ └── schema_converter.rb └── schema2type.rb ├── .travis.yml ├── Rakefile ├── Gemfile ├── bin ├── setup └── console ├── .gitignore ├── exe └── schema2type ├── spec ├── spec_helper.rb ├── schema_converter_spec.rb └── cli_spec.rb ├── LICENSE.txt ├── Gemfile.lock ├── schema2type.gemspec ├── README.md └── CODE_OF_CONDUCT.md /.ruby-version: -------------------------------------------------------------------------------- 1 | 2.7.0 2 | -------------------------------------------------------------------------------- /.rspec: -------------------------------------------------------------------------------- 1 | --format documentation 2 | --color 3 | --require spec_helper 4 | -------------------------------------------------------------------------------- /lib/schema2type/version.rb: -------------------------------------------------------------------------------- 1 | module Schema2type 2 | VERSION = '0.4.0' 3 | end 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | --- 2 | sudo: false 3 | language: ruby 4 | cache: bundler 5 | rvm: 6 | - 2.6.1 7 | before_install: gem install bundler -v 2.0.1 8 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require "bundler/gem_tasks" 2 | require "rspec/core/rake_task" 3 | 4 | RSpec::Core::RakeTask.new(:spec) 5 | 6 | task :default => :spec 7 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "https://rubygems.org" 2 | ruby '2.7.0' 3 | 4 | # Specify your gem's dependencies in schema2type.gemspec 5 | gemspec 6 | 7 | gem "activesupport" 8 | -------------------------------------------------------------------------------- /lib/schema2type.rb: -------------------------------------------------------------------------------- 1 | require 'schema2type/version' 2 | require 'schema2type/schema_converter' 3 | require 'schema2type/covert_service' 4 | require 'schema2type/cli' 5 | -------------------------------------------------------------------------------- /bin/setup: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -euo pipefail 3 | IFS=$'\n\t' 4 | set -vx 5 | 6 | bundle install 7 | 8 | # Do any other automated setup that you need to do here 9 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /.bundle/ 2 | /.yardoc 3 | /_yardoc/ 4 | /coverage/ 5 | /doc/ 6 | /pkg/ 7 | /spec/reports/ 8 | /tmp/ 9 | 10 | # rspec failure tracking 11 | .rspec_status 12 | -------------------------------------------------------------------------------- /exe/schema2type: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | require 'optparse' 4 | require 'schema2type' 5 | 6 | params = ARGV.getopts('s:o:n:', 'snake') 7 | 8 | Schema2type.execute(input_file: params['s'], out_file: params['o'], name_space: params['n'], is_snake_case: params['snake']) 9 | -------------------------------------------------------------------------------- /bin/console: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | require "bundler/setup" 4 | require "schema2type" 5 | 6 | # You can add fixtures and/or initialization code here to make experimenting 7 | # with your gem easier. You can also use a different console, if you like. 8 | 9 | # (If you use this, don't forget to add pry to your Gemfile!) 10 | # require "pry" 11 | # Pry.start 12 | 13 | require "irb" 14 | IRB.start(__FILE__) 15 | -------------------------------------------------------------------------------- /lib/schema2type/conversion_table.yml: -------------------------------------------------------------------------------- 1 | # This is the correspondence table of method of schema.rb and Type of TypeScript 2 | string: string 3 | inet: string 4 | text: string 5 | json: Record 6 | jsonb: Record 7 | binary: string 8 | integer: number 9 | bigint: number 10 | float: number 11 | decimal: number 12 | boolean: boolean 13 | date: string 14 | datetime: string 15 | timestamp: string 16 | datetime_with_timezone: string 17 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | require "bundler/setup" 2 | require "schema2type" 3 | require "tempfile" 4 | 5 | RSpec.configure do |config| 6 | # Enable flags like --only-failures and --next-failure 7 | config.example_status_persistence_file_path = ".rspec_status" 8 | 9 | # Disable RSpec exposing methods globally on `Module` and `main` 10 | config.disable_monkey_patching! 11 | 12 | config.expect_with :rspec do |c| 13 | c.syntax = :expect 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /lib/schema2type/cli.rb: -------------------------------------------------------------------------------- 1 | module Schema2type 2 | DEFAULT_SCHEMA_PATH = "./db/schema.rb".freeze 3 | DEFAULT_NAME_SPACE = "schema".freeze 4 | 5 | def self.execute(input_file:, out_file:, name_space:, is_snake_case:) 6 | resultHash = eval(File.read(input_file || DEFAULT_SCHEMA_PATH), CovertService.new(is_snake_case).get_binding) 7 | 8 | File.open(out_file, "w") do |f| 9 | f.puts <<~EOS 10 | /* eslint no-unused-vars: 0 */ 11 | 12 | /** 13 | * auto-generated file 14 | * schema version: #{resultHash[:version]} 15 | * This file was automatically generated by schema2type 16 | */ 17 | declare namespace #{name_space || DEFAULT_NAME_SPACE} { 18 | #{resultHash[:lines]} 19 | } 20 | EOS 21 | end 22 | end 23 | end 24 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2019 ryo 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | PATH 2 | remote: . 3 | specs: 4 | schema2type (0.4.0) 5 | activesupport 6 | 7 | GEM 8 | remote: https://rubygems.org/ 9 | specs: 10 | activesupport (6.0.3.1) 11 | concurrent-ruby (~> 1.0, >= 1.0.2) 12 | i18n (>= 0.7, < 2) 13 | minitest (~> 5.1) 14 | tzinfo (~> 1.1) 15 | zeitwerk (~> 2.2, >= 2.2.2) 16 | concurrent-ruby (1.1.6) 17 | diff-lcs (1.3) 18 | i18n (1.8.3) 19 | concurrent-ruby (~> 1.0) 20 | minitest (5.14.1) 21 | rake (13.0.1) 22 | rspec (3.9.0) 23 | rspec-core (~> 3.9.0) 24 | rspec-expectations (~> 3.9.0) 25 | rspec-mocks (~> 3.9.0) 26 | rspec-core (3.9.2) 27 | rspec-support (~> 3.9.3) 28 | rspec-expectations (3.9.2) 29 | diff-lcs (>= 1.2.0, < 2.0) 30 | rspec-support (~> 3.9.0) 31 | rspec-mocks (3.9.1) 32 | diff-lcs (>= 1.2.0, < 2.0) 33 | rspec-support (~> 3.9.0) 34 | rspec-support (3.9.3) 35 | thread_safe (0.3.6) 36 | tzinfo (1.2.7) 37 | thread_safe (~> 0.1) 38 | zeitwerk (2.3.0) 39 | 40 | PLATFORMS 41 | ruby 42 | 43 | DEPENDENCIES 44 | activesupport 45 | bundler (~> 2.0) 46 | rake (~> 13.0) 47 | rspec (~> 3.0) 48 | schema2type! 49 | 50 | RUBY VERSION 51 | ruby 2.7.0p0 52 | 53 | BUNDLED WITH 54 | 2.1.4 55 | -------------------------------------------------------------------------------- /schema2type.gemspec: -------------------------------------------------------------------------------- 1 | 2 | lib = File.expand_path("../lib", __FILE__) 3 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 4 | require "schema2type/version" 5 | 6 | Gem::Specification.new do |spec| 7 | spec.name = "schema2type" 8 | spec.version = Schema2type::VERSION 9 | spec.authors = ["ryo"] 10 | spec.email = ["ba068082@gmail.com"] 11 | 12 | spec.summary = %q{generate TypeScript type definitions from Rails schema.rb} 13 | spec.description = %q{Using schema2type, you can generate TypeScript type definitions from Rails'schema.rb automatically.} 14 | spec.homepage = "https://github.com/kawamataryo/schema2type" 15 | spec.license = "MIT" 16 | 17 | # Specify which files should be added to the gem when it is released. 18 | # The `git ls-files -z` loads the files in the RubyGem that have been added into git. 19 | spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do 20 | `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } 21 | end 22 | spec.bindir = "exe" 23 | spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } 24 | spec.require_paths = ["lib"] 25 | spec.add_runtime_dependency "activesupport" 26 | spec.add_development_dependency "bundler", "~> 2.0" 27 | spec.add_development_dependency "rake", "~> 13.0" 28 | spec.add_development_dependency "rspec", "~> 3.0" 29 | end 30 | -------------------------------------------------------------------------------- /lib/schema2type/covert_service.rb: -------------------------------------------------------------------------------- 1 | module Schema2type 2 | class CovertService 3 | def initialize(is_snake_case) 4 | @converted_types = [] 5 | @is_snake_case = is_snake_case 6 | end 7 | 8 | def get_binding 9 | binding 10 | end 11 | 12 | # mock method for create_table in schema.rb 13 | def convert_schema_to_type(table_name, *) 14 | converter = SchemaConverter.new(table_name: table_name, is_snake_case: @is_snake_case) 15 | yield converter 16 | @converted_types.concat converter.converted_type_lines 17 | end 18 | 19 | def skip_dsl(*) 20 | @converted_types 21 | end 22 | 23 | def method_missing(*) 24 | # To exclude unnecessary methods 25 | # TODO: add error handling 26 | end 27 | 28 | def self.method_missing(*) 29 | # To exclude unnecessary methods 30 | # TODO: add error handling 31 | end 32 | 33 | # mock module and method for shcema.rb 34 | module ActiveRecord 35 | module Schema 36 | def self.define(*arg) 37 | converted_types = yield 38 | converted_type_lines = converted_types.map { |t| " #{t}" }.join("\n").strip 39 | { 40 | lines: converted_type_lines, 41 | version: arg[0][:version] 42 | } 43 | end 44 | end 45 | end 46 | 47 | alias create_table convert_schema_to_type 48 | alias add_foreign_key skip_dsl 49 | end 50 | end 51 | -------------------------------------------------------------------------------- /lib/schema2type/schema_converter.rb: -------------------------------------------------------------------------------- 1 | require 'yaml' 2 | require 'active_support/inflector' 3 | 4 | module Schema2type 5 | class SchemaConverter 6 | attr_reader :property_lines, :table_name, :is_snake_case 7 | 8 | COLUMN_METHODS = YAML.load_file(File.expand_path(__dir__) + '/conversion_table.yml').to_a 9 | ID_PROPERTY_LINE_TEXT = " id: number;".freeze 10 | 11 | def self.define_convert_methods(methods) 12 | methods.each do |m| 13 | define_method(m[0]) do |name, *options| 14 | convert_property_line_and_push name: name, type: m[1], options: options 15 | end 16 | end 17 | end 18 | 19 | define_convert_methods COLUMN_METHODS 20 | 21 | def initialize(table_name:, is_snake_case: false) 22 | @property_lines = [] 23 | @table_name = table_name.singularize.camelize 24 | @is_snake_case = is_snake_case 25 | end 26 | 27 | def converted_type_lines 28 | ["type #{table_name} = {", ID_PROPERTY_LINE_TEXT, property_lines, "}\n"].flatten 29 | end 30 | 31 | def method_missing(*) 32 | # To exclude unnecessary methods 33 | # TODO: add error handling 34 | end 35 | 36 | private 37 | 38 | def convert_property_line_and_push(name:, type:, options:) 39 | is_non_nullable = options[0] && options[0].key?(:null) && !options[0][:null] 40 | formatted_name = is_snake_case ? name.underscore : name.camelcase(:lower) 41 | property_line = is_non_nullable ? "#{formatted_name}: #{type};" : "#{formatted_name}: #{type} | null;" 42 | 43 | property_lines << " #{property_line}" 44 | end 45 | end 46 | end 47 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # schema2type 2 | Using schema2type, you can generate TypeScript type definitions from Rails'schema.rb automatically. 3 | 4 | Start with a schema.rb: 5 | 6 | ```ruby 7 | ActiveRecord::Schema.define(version: xxxx) do 8 | create_table "Users", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t| 9 | t.string "name" 10 | t.integer "age" 11 | t.bigint "sales" 12 | t.boolean "paid", null: false 13 | t.datetime "created_at", null: false 14 | t.datetime "updated_at", null: false 15 | t.index ["patient_id"], name: "index_histories_on_patient_id" 16 | end 17 | end 18 | ``` 19 | 20 | Automatically have the following TypesScript type generated. 21 | 22 | ```typescript 23 | declare namespace schema { 24 | type User = { 25 | id: number; 26 | name: string | null; 27 | age: number | null; 28 | sales: number | null; 29 | paid: boolean; 30 | createdAt: string; 31 | updatedAt: string; 32 | } 33 | } 34 | ``` 35 | 36 | ## Installation 37 | 38 | Add this line to your application's Gemfile: 39 | 40 | ```ruby 41 | gem 'schema2type' 42 | ``` 43 | 44 | And then execute: 45 | 46 | $ bundle 47 | 48 | Or install it yourself as: 49 | 50 | $ gem install schema2type 51 | 52 | 53 | 54 | ## Usage 55 | 56 | ``` 57 | bundle exec schema2type -o schema.d.ts 58 | ``` 59 | 60 | ### options 61 | 62 | |command | require | default | detail | 63 | |---|---|---|---| 64 | | -o | true | - | Output file name of TypeScript | 65 | | -s | false | "./db/schema.rb" | Path of your schema.rb | 66 | | -n | false | "schema" | Name of declare namespace | 67 | | --snake | false | false | Convert property name to snake_case | 68 | 69 | ## conversion table 70 | the schema2type convert as per this conversion table. 71 | 72 | |create_table block method| converted Type| 73 | |---|---| 74 | | string | string | 75 | | text | string | 76 | | json | Record | 77 | | jsonb | Record | 78 | | binary | string | 79 | | inet | string | 80 | | integer | number | 81 | | bigint | number | 82 | | float | number | 83 | | decimal | number | 84 | | boolean | boolean | 85 | | date | string | 86 | | datetime | string | 87 | | timestamp | string | 88 | | datetime_with_timezone | string | 89 | ## License 90 | 91 | The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). 92 | 93 | 94 | -------------------------------------------------------------------------------- /spec/schema_converter_spec.rb: -------------------------------------------------------------------------------- 1 | RSpec.describe 'SchemaConverter' do 2 | let(:sc) { Schema2type::SchemaConverter.new(table_name: 'users') } 3 | 4 | it 'テーブル名を単数形のキャメルケースに変換すること' do 5 | expect(sc.table_name).to eq 'User' 6 | end 7 | 8 | describe '#result' do 9 | it 'out_textの先頭に"type table_name = {", "id: number\n"、末尾に"}\n"を加えること' do 10 | sc.send(:convert_property_line_and_push, name: 'name', type: 'strong', options: [{ null: true }]) 11 | expect(sc.converted_type_lines).to eq(['type User = {', " id: number;", ' name: strong | null;', "}\n"]) 12 | end 13 | end 14 | 15 | describe '#push_property_line' do 16 | context 'optionsにnull: falseを含まない場合' do 17 | it 'null許容型のプロパティ宣言を配列にプラスする' do 18 | sc.send(:convert_property_line_and_push, name: 'name', type: 'strong', options: [{ null: true }]) 19 | expect(sc.property_lines[0]).to eq(' name: strong | null;') 20 | end 21 | end 22 | context 'optionsにnull: falseを含む場合' do 23 | it 'optionsにnull: falseを含む場合、null非許容型のプロパティ宣言を配列にプラスする' do 24 | sc.send(:convert_property_line_and_push, name: 'name', type: 'strong', options: [{ null: false }]) 25 | expect(sc.property_lines[0]).to eq(' name: strong;') 26 | end 27 | end 28 | context 'snake_caseがfalseの場合' do 29 | let(:sc_not_snake) do 30 | Schema2type::SchemaConverter.new(table_name: 'users', is_snake_case: false) 31 | end 32 | it 'lowerCamelでプロパティ名を設定すること' do 33 | p sc_not_snake 34 | sc_not_snake.send(:convert_property_line_and_push, name: 'user_name', type: 'strong', options: []) 35 | expect(sc_not_snake.property_lines[0]).to eq(' userName: strong | null;') 36 | end 37 | end 38 | context 'snake_caseがtrueの場合' do 39 | let(:sc_snake) do 40 | Schema2type::SchemaConverter.new(table_name: 'users', is_snake_case: true) 41 | end 42 | it 'snake_caseでプロパティ名を設定すること' do 43 | sc_snake.send(:convert_property_line_and_push, name: 'userName', type: 'strong', options: []) 44 | expect(sc_snake.property_lines[0]).to eq(' user_name: strong | null;') 45 | end 46 | end 47 | end 48 | 49 | describe 'property_methods' do 50 | Schema2type::SchemaConverter::COLUMN_METHODS.each do |m| 51 | it "#{m[0]}メソッドは#{m[1]}を型に設定すること" do 52 | sc.send(m[0], 'column') 53 | expect(sc.property_lines[0]).to eq(%( column: #{m[1]} | null;)) 54 | end 55 | end 56 | end 57 | 58 | describe '#method_missing' do 59 | it '不明なメソッドが呼ばれたときでもErrorをNoMethodsErrorが発生しないこと' do 60 | expect { sc.hogehoge }.not_to raise_error 61 | expect { sc.fuggfgg }.not_to raise_error 62 | expect { sc.dfkdsfsfdsaf }.not_to raise_error 63 | expect { sc.fdsadsfsadfafsa }.not_to raise_error 64 | end 65 | end 66 | end 67 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, gender identity and expression, level of experience, 9 | nationality, personal appearance, race, religion, or sexual identity and 10 | orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at ba068082@gmail.com. All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at [http://contributor-covenant.org/version/1/4][version] 72 | 73 | [homepage]: http://contributor-covenant.org 74 | [version]: http://contributor-covenant.org/version/1/4/ 75 | -------------------------------------------------------------------------------- /spec/cli_spec.rb: -------------------------------------------------------------------------------- 1 | RSpec.describe Schema2type do 2 | describe '#execute' do 3 | INPUT_FIlE = Tempfile.open(%w(input .rb)) do |f| 4 | f.write <<-EOS 5 | ActiveRecord::Schema.define(version: 20170929125808) do 6 | create_table "histories", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t| 7 | t.string "column_string" 8 | t.inet "column_inet" 9 | t.text "column_text" 10 | t.json "column_json" 11 | t.jsonb "column_jsonb", null: false 12 | t.binary "column_binary", null: false 13 | t.integer "column_integer" 14 | end 15 | 16 | create_table "users", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t| 17 | t.bigint "column_bigint" 18 | t.float "column_float" 19 | t.decimal "column_decimal" 20 | t.boolean "column_boolean", null: true 21 | t.date "column_date", null: false 22 | t.datetime "column_datetime" 23 | t.timestamp "column_timestamp" 24 | t.datetime_with_timezone "column_datetime_with_timezone" 25 | t.index ["patient_id"], name: "index_histories_on_patient_id" 26 | end 27 | end 28 | EOS 29 | f 30 | end 31 | let(:out_file) { Tempfile.new(['out.hoge.ts']) } 32 | 33 | context 'snake_caseがtrueの場合' do 34 | it 'snake_caseで変換できる' do 35 | Schema2type::execute(input_file: INPUT_FIlE.path, out_file: out_file.path, name_space: "hoge", is_snake_case: true) 36 | expect(out_file.read).to eq <<~EOS 37 | /* eslint no-unused-vars: 0 */ 38 | 39 | /** 40 | * auto-generated file 41 | * schema version: 20170929125808 42 | * This file was automatically generated by schema2type 43 | */ 44 | declare namespace hoge { 45 | type History = { 46 | id: number; 47 | column_string: string | null; 48 | column_inet: string | null; 49 | column_text: string | null; 50 | column_json: Record | null; 51 | column_jsonb: Record; 52 | column_binary: string; 53 | column_integer: number | null; 54 | } 55 | 56 | type User = { 57 | id: number; 58 | column_bigint: number | null; 59 | column_float: number | null; 60 | column_decimal: number | null; 61 | column_boolean: boolean | null; 62 | column_date: string; 63 | column_datetime: string | null; 64 | column_timestamp: string | null; 65 | column_datetime_with_timezone: string | null; 66 | } 67 | } 68 | EOS 69 | end 70 | end 71 | 72 | context 'snake_caseがfalseの場合' do 73 | it 'lowerCamelで変換できる' do 74 | Schema2type::execute(input_file: INPUT_FIlE.path, out_file: out_file.path, name_space: "hoge", is_snake_case: false) 75 | expect(out_file.read).to eq <<~EOS 76 | /* eslint no-unused-vars: 0 */ 77 | 78 | /** 79 | * auto-generated file 80 | * schema version: 20170929125808 81 | * This file was automatically generated by schema2type 82 | */ 83 | declare namespace hoge { 84 | type History = { 85 | id: number; 86 | columnString: string | null; 87 | columnInet: string | null; 88 | columnText: string | null; 89 | columnJson: Record | null; 90 | columnJsonb: Record; 91 | columnBinary: string; 92 | columnInteger: number | null; 93 | } 94 | 95 | type User = { 96 | id: number; 97 | columnBigint: number | null; 98 | columnFloat: number | null; 99 | columnDecimal: number | null; 100 | columnBoolean: boolean | null; 101 | columnDate: string; 102 | columnDatetime: string | null; 103 | columnTimestamp: string | null; 104 | columnDatetimeWithTimezone: string | null; 105 | } 106 | } 107 | EOS 108 | end 109 | end 110 | end 111 | end 112 | --------------------------------------------------------------------------------