id which must be of type string.
You can view our example app
Polybase Social to see it working in
action.
Create a collection
Create collection using the Explorer (recommended)
Navigate to the Explorer, and click on “Create Collection”. You will then be prompted to sign in. If you create a collection in the explorer, the collection will be owned by the account you used to sign in.Create collection programmatically
To create a schema programatically, you can use theapplySchema method on your Polybase client instance.
In this example, we create a collection called City that has name and country fields, and a setCountry() function. You can define multiple collections in a single applySchema call.
Define a Collection
Collections are defined using Polylang (the language for Polybase), that is very similar to Javascript/TypeScript. Collections allow you define the rules and indexes for your collection records. The following is a valid collection definition.name of type string and optional age property of type
number.
Constructor
The collectionconstructor function lets you create new records in your collection, and is called when you create a record.
The constructor function must assign this.id and any additional required fields defined in your collection. An error will be returned if a
record already exists with the assigned id is provided, as each record in a collection must have a unique id.
Polybase functions have a 5 second timeout period.
Fields
You can specify the fields that are allowed in your collection. These should be at the top most part of your collection. A collection should always have aid
field (id is unique). By default, all fields are required.
id Field
Theid fields is required on every Polybase collection. It must be assigned in the constructor. Each record must have a unique id across the entire collection. id must be of type string.
You will soon be able to use other types as the
id field.Nested Fields
You can also define nested fields:Optional Fields
All fields are required by default, if you want to make a field optional simply append? after the field name and before the :. For example:
Field Types
The following types are supported:stringnumberbooleanbytesPublicKeyCollectionstring[],number[],boolean[],PublicKey[]andCollection*[]map<string | number, T>
Collection* is the name of a specific collection in the same namespace.
Here are some examples of fields being defined:
Additional types such as dates will be added soon.
Primitives
The following primitive types are supported:stringnumberboolean
Bytes
The bytes type allows you to store arbitary bytes in Polybase, without having to encode the data as a string. If you use our client library@polybase/client, the bytes type will be returned as a Uint8Array. If this field is returned
from the Gateway API, then it will be a base64 encoded string.
bytes data is currently not indexedCollection
Collection fields reference another collection in the same namespace. This allows you to create relationships between records in the same or different collections.
Similar to how you have relationships in SQL.
You can define the field on the collection, and then also pass a parameter as a Collection type.
create() passing in the reference to a specific record of that collection:
Public Key
ThePublicKey type is a special type that is used to represent a public key. Currently, we only support Ethereum (secp256k1) public keys.
.toHex()
If you need a string hex representation of the public key, you can use this.publicKey.toHex(). This will return a string starting with 0x,
and the hexidecimal representation of the 64 byte public key.
Public keys on Polybase use non-prefixed Public Keys, so they are 64 bytes long.
We will be adding support for Falcon, this is a zk optimized PublicKey
Arrays
You can create arrays ofstring, number, boolean, PublicKey and Collection type. For example:
Array fields are not currently indexed
Maps
You can create map, these allow you to create key value pairs. For example:Map fields are not currently indexed
Functions
Field values can only be modified using functions.Polybase functions have a 5 second timeout period.
.call(functionName, args):
Context
The global variablectx is available inside any Polybase function.
Public Key
ctx.publicKey is populated when you provide a signer function to the Polybase client.
publicKey of the wallet used to sign the request
in your collection code, for example see how in setName we check the ctx.publicKey and compare
it against the records own publicKey:
.toHex()
If you need a string hex representation of the public key, you can use ctx.publicKey.toHex(). This will return a string starting with 0x,
and the hexidecimal representation of the 64 byte public key.
Public keys on Polybase use non-prefixed Public Keys, so they are 64 bytes long.
Indexes
Indexes are a list of fields in addition to the record’sid field that should
be indexed. You need to ensure that all fields that are included in a where or
sort clause are included in the indexes.
All collection fields are automatically indexed, which means
you only need to specify multi-field indexes.
Fields of type Collection must be indexed explicitly
@index(collectionField).Indexing array/map fields is currently not possible.
Permissions
By default, collections in Polybase are private and their records cannot be accessed. To make a collection public, add the@public directive.
To allow users to access and manipulate their records, you can use the following directives:
@public on collections
Allows anyone to read all records and call functions in the collection (it's the equivalent of adding @read and @call).
You can still further restrict write permissions by adding custom code to your collections functions.
@read on collections
Allows anyone to read all records in the collection (but calls to functions are still restricted).
@read on fields
Allows anyone who can sign using the specified public key to read the record.
@call on collections
Allows anyone to call functions that do not have a @call directive.
@call on functions
Allows anyone who can sign using the specified public key to call a given function.
@delegate
Delegation allows you to create rules across multiple records, allowing for complex permissions to be defined.
Delegation rules must always end in a PublicKey field. You must
authenticate the user with using a
signer function in order to use delegation.@read, @call and @delegate directives to control who can read, call and delegate data.
Example of delegating read access to Response to a user with given publicKey:
Response → Form → User → publicKey:
Reference a collection
To reference a collection, you can call.collection("collection-name") on your
Polybase instance. The returned collection instance relates to a specific
collection of data and allows you .write() and .read() data
from Polybase.
Relative Path (with default namespace)
The following would return a collection with path:your-namespace/cities:
Relative Path (without default namespace)
The following would return a collection with path:your-namespace/cities:
Absolute Path
To override the default namespace, you can use an absolute path by specifying a/ at the start of the path.
The following would return a collection with path: alt-namespace/cities: