Skip to content

feat: adding default values#26

Merged
mvhenten merged 1 commit intomainfrom
feat-default-values
Dec 15, 2025
Merged

feat: adding default values#26
mvhenten merged 1 commit intomainfrom
feat-default-values

Conversation

@mvhenten
Copy link
Owner

@mvhenten mvhenten commented Dec 15, 2025

Example Usage
In TypeSpec:

model Task {
    description?: string = "No description provided";
    priority: Priority = Priority.MEDIUM;
    count: int32 = 0;
    active: boolean = true;
}

Generates ElectroDB schema:

{
    description: { type: "string", required: false, default: "No description provided" },
    priority: { type: ["LOW", "MEDIUM", "HIGH"], required: true, default: "MEDIUM" },
    count: { type: "number", required: true, default: 0 },
    active: { type: "boolean", required: true, default: true }
}

Copilot AI review requested due to automatic review settings December 15, 2025 13:42
@mvhenten mvhenten enabled auto-merge (squash) December 15, 2025 13:43
@mvhenten mvhenten merged commit d61facb into main Dec 15, 2025
7 checks passed
@mvhenten mvhenten deleted the feat-default-values branch December 15, 2025 13:43
@github-actions
Copy link

🎉 This PR is included in version 3.1.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds support for default values in TypeSpec model properties, enabling developers to specify default values for string, number, boolean, and enum types in their entity definitions.

Key Changes:

  • Added extractDefaultValue function to convert TypeSpec Value types to primitive JavaScript values
  • Modified emitModelProperty and emitAttribute to include default values in the generated ElectroDB entity schemas
  • Added comprehensive test coverage for default values across different data types (strings, numbers, booleans, enums) and nested models

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
test/main.tsp Defines new Task entity model and Priority enum with various default values to test the feature, including optional and required properties with defaults, nested models with defaults, and different data types
test/entities.test.js Adds comprehensive test suite for Task entity covering default values for all supported types (string, number, boolean, enum) and nested model properties. Also includes minor formatting improvements to existing tests
src/emitter.ts Implements core functionality: adds extractDefaultValue helper function to extract default values from TypeSpec Value types, and updates both emitModelProperty and emitAttribute functions to include default values in generated attributes

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +312 to +319
// Add default value if present
if (prop.defaultValue) {
const defaultValue = extractDefaultValue(prop.defaultValue);
if (defaultValue !== undefined) {
// @ts-expect-error - default is a valid ElectroDB attribute property
attr.default = defaultValue;
}
}
Copy link

Copilot AI Dec 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic for adding default values is duplicated between emitModelProperty (lines 257-264) and emitAttribute (lines 312-319). Consider refactoring to avoid duplication. Since emitAttribute calls emitType which may eventually call emitModelProperty for nested models, you could rely on emitModelProperty to handle default values for nested properties and only handle them once in emitAttribute for top-level properties.

Suggested change
// Add default value if present
if (prop.defaultValue) {
const defaultValue = extractDefaultValue(prop.defaultValue);
if (defaultValue !== undefined) {
// @ts-expect-error - default is a valid ElectroDB attribute property
attr.default = defaultValue;
}
}

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants