问题
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