How to create custom schema with Corda Vault Extension

无人久伴 提交于 2019-12-11 02:51:35

问题


From Corda documents, it says we can have custom schema in Vault Extension.

However there is not much clarity for Vault Extension which should have ability to create/manage custom database schema pertaining to node vault database.

Are we going to publish API in feature release of Corda


回答1:


Inside flows, the node exposes a JDBC connection that allows you to write native custom SQL queries (as a vault extension). You can access this JDBC connection using serviceHub.jdbcSession().

If your question is about how to write a custom schema then please see the existing Corda Persistence API docs.

You can then query that custom schema using the new Vault Query API - please see the existing [Corda Vault Query API][3] docs.




回答2:


Just to add an example to the above, here's a custom schema for the Yo! CorDapp. See YoSchemaV1 below:

// State.
data class State(val origin: Party,
                 val target: Party,
                 val yo: String = "Yo!") : ContractState, QueryableState {
    override val participants get() = listOf(target)
    override val contract get() = Yo()
    override fun toString() = "${origin.name}: $yo"
    override fun supportedSchemas() = listOf(YoSchemaV1)
    override fun generateMappedObject(schema: MappedSchema) = YoSchemaV1.YoEntity(this)

    object YoSchemaV1 : MappedSchema(Yo.State::class.java, 1, listOf(YoEntity::class.java)) {
        @Entity @Table(name = "yos")
        class YoEntity(yo: State) : PersistentState() {
            @Column var origin: String = yo.origin.name.toString()
            @Column var target: String = yo.target.name.toString()
            @Column var yo: String = yo.yo
        }
    }
}

In short, your state object needs to implement QueryableState, as above.

The full CorDapp is available here: https://github.com/roger3cev/yo-cordapp

Cheers



来源:https://stackoverflow.com/questions/45083270/how-to-create-custom-schema-with-corda-vault-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!