如何覆盖与复数表和蛇形列有关的rails配置

因此,我正在构建一个应用程序,其中有一个用Rails编写的后端和一个用Vue编写的带有Amplify的客户端。我的数据库是MySQL,我正在使用带有GraphQL作为数据源(指向我的数据库)的AWS AppSync。

AWS Amplify有一个框架,使我可以使用一个简单的命令amplify api add-graphql-datasource根据表名和列来生成模式。但是因为我使用的是Rails迁移,所以我的数据库使用的是Rails约定:带有蛇形列的复数表。

现在,问题在于GraphQL模式非常丑陋,并且没有使用正确的约定(类型和输入的单数名称,带有驼峰式道具)。示例:

我的后端进行了以下迁移:

class CreatePosts < activeRecord::Migration[6.0]
  def change
    create_table :posts do |t|
      t.belongs_to :site,null: false
      t.string :title
      t.string :url
      t.text :body

      t.timestamps
    end
  end
end

为此生成的模式是:

type posts {
  id: Int!
  site_id: Int!
  title: String
  url: String
  body: String
  created_at: AWSDateTime!
  updated_at: AWSDateTime!
}

type Query {
  getPosts(id: Int!): posts
  listPostss: [posts]
  // ...
}

schema {
  query: Query
  // ...
}

更何况:

input CreatepostsInput {
  id: Int!
  site_id: Int!
  title: String
  url: String
  body: String
  created_at: AWSDateTime!
  updated_at: AWSDateTime!
}

因此,AWS Amplify是新功能,还不如Rails成熟,最重要的是,我没有找到任何适配器或转换器来处理客户端中的问题……我希望找到一种处理它的方法。在Rails上。

我需要能够完全更改Rails约定而不破坏任何内容:迁移,关联,如何管理关联(create_xxx,build_xxx)。

此应用真的很新,因此我可以从头开始重新创建所有迁移。

谢谢

pengsijing 回答:如何覆盖与复数表和蛇形列有关的rails配置

我可以看到您可以做的一些事情:

表格:

在迁移中,您可以更改表名,但是现在您需要使用self.table_name让模型知道表名是什么。

# migration
class CreatePosts < ActiveRecord::Migration[6.0]
  def change
    create_table :post do |t| # singular 'post'
      ...
    end
  end
end

#model
class Post
  self.table_name = "post" # i think you can also use a symbol :post
end

属性:

您需要避免使用遵循t.belongs_tot.referencest.timestamps之类的Rails约定的Rails迁移方法。

# migration
class CreatePosts < ActiveRecord::Migration[6.0]
  def change
    create_table :post do |t|
      # do not use this below
      # t.belongs_to :site,null: false
      t.bigint :siteId,null: false
      t.string :title
      t.string :url
      t.text :body

      # do not use this below
      # t.timestamps
      t.datetime :createdAt,default: ->{'CURRENT_TIMESTAMP'}
      t.datetime :updatedAt,default: ->{'CURRENT_TIMESTAMP'}
    end
  end
end

关系:

您还需要更新模型中的关系

class Post
  belongs_to :site,foreign_key: 'siteId'
end

更多信息可以在Rails API中找到。确保检查documenation for other relationship methods

时间戳:

由于时间戳列(created_atupdated_at)不再是ActiveRecord期望的列,因此您可能需要override the ActiveRecord::Timestamp module才能使它们继续按预期运行。最简单的选择之一是使用以下内容更新您的ApplicationRecord或单个模型类:

class ApplicationRecord # or class Post
  before_create :set_timestamps
  before_save :set_timestamps

  private
  def set_timestamps
    self.createdAt = DateTime.current if self.new_record?
    self.updatedAt = DateTime.now
  end
end

或者该其他选项取自https://stackoverflow.com/a/52276865/1845602

class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true

  class << self
    private

    def timestamp_attributes_for_create
      super << 'my_created_at_column'
    end

    def timestamp_attributes_for_update
      super << 'my_updated_at_column'
    end
  end
end

自动生成的输入

我在这里可能是错的,但是似乎表名正像Create<table>Input一样被注入到输入名中,因此,可以的话,您可以将表命名为Post而不是{{1 }}。

post
本文链接:https://www.f2er.com/3164330.html

大家都在问