M.E.
M.E.

Reputation: 5495

Tarantool - Named fields?

Is there any way to name fields similar to columns in SQL?

The idea is that I might want to insert a customer record with: - Name - Phone - Email - Website

some fields might be present sometimes, other not, and they might be presented in different order.

Is there any other way to insert the records into a tuple referencing them by field name?

Something in pseudocode like:

s:insert(1, "name": "foo name", "phone": "foo phone")
s:insert(2, "phone": "bar phone", "name": "bar name", "email": "bar email")

Upvotes: 2

Views: 436

Answers (1)

Vladimir Lebedev
Vladimir Lebedev

Reputation: 1237

You can assign field names using not yet documented space:format() function when you define schema for a space, afterwards and you can use these names for index definitions. [available in Tarantool 1.7+]

Sample code:

box.once("testapp:schema:1", function()
    local customer = box.schema.space.create('customer')
    customer:format({
        {'customer_id', 'unsigned'},
        {'name', 'string'},
    })
    customer:create_index('customer_id', {parts = {'customer_id'}})

    local account = box.schema.space.create('account')
    account:format({
        {'account_id', 'unsigned'},
        {'customer_id', 'unsigned'},
        {'balance', 'unsigned'},
        {'name', 'string'},
    })
    account:create_index('account_id', {parts = {'account_id'}})
    account:create_index('customer_id', {parts = {'customer_id'}, unique = false})
    box.snapshot()
end)

Unfortunately, you can't use field names in space:insert() or similar functions.

Upvotes: 2

Related Questions