#astro DB optional fields?

6 messages · Page 1 of 1 (latest)

wise axleBOT
#

Hi @quiet flower. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

In Astro DB, you can make a field optional by setting the optional property to true in the column definition. By default, Astro DB uses NOT NULL for all columns, which means they are required. When you set optional to true, it allows null values for that column.

Here's how you can modify your ProjectDetails table to make the endDate and shortDescription fields optional:

import { defineDb, defineTable, column } from 'astro:db';

const ProjectDetails = defineTable({
  columns: {
    id: column.number({ primaryKey: true }),
    name: column.text(),
    startDate: column.date(),
    endDate: column.date({ optional: true }),
    shortDescription: column.text({ optional: true }),
    logoUrl: column.text(),
    websiteUrl: column.text(),
    whitepaperLink: column.text(),
    discordLink: column.text(),
    telegramLink: column.text(),
    twitterLink: column.text(),
    facebookLink: column.text(),
    category: column.text(),
    contactName: column.text(),
    contactEmail: column.text(),
    platform: column.text(),
    symbol: column.text(),
    softCap: column.text(),
    hardCap: column.text(),
    price: column.text(),
    origin: column.text(),
    kycRequired: column.boolean(),
    restrictedCountries: column.text(),
    buyWith: column.text(),
    fullDescription: column.text(),
    slug: column.text(),
  }
});

You can apply the optional: true property to any other fields you want to make optional.

For more details, you can refer to the Astro DB documentation (https://docs.astro.build/en/guides/integrations-guide/db#columns).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@quiet flower kapa.ai is still learning and improving, please let us know how it did by reacting below

quiet flower
#

@wise axle category: column.text({ optional: true},{ default: 0 }),
How to set 2 options?

wise axleBOT
#

Let me check the knowledge base for you. One moment! :books:

#

You can set multiple options for a column in Astro DB by including them in the same object. Here's how you can make the category field optional and set a default value:

category: column.text({ optional: true, default: '0' }),

Please note that the default value should match the column type. Since category is a text column, the default value is provided as a string '0'.

For more details, you can refer to the Astro DB documentation (https://docs.astro.build/en/guides/integrations-guide/db#columns).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: