# File location convention
{app,lib}/
└── */lib/__scalar/
└── <scalarName>/ # camelCase directory
└── <scalarName>.constant.ts # scalar definition file
Element | Convention | Example |
---|---|---|
Directory |
|
|
File |
|
|
Class |
|
|
Enum Values |
|
|
import { Field, Model } from "@akanjs/constant";
@Model.Scalar("ScalarName") // Must match class name exactly
export class ScalarName {
@Field.Prop(() => FieldType, { ...options })
fieldName: FieldType;
}
// Basic field types
@Field.Prop(() => String)
name: string;
@Field.Prop(() => Int)
quantity: number;
@Field.Prop(() => Float)
percentage: number;
@Field.Prop(() => Boolean)
isActive: boolean;
@Field.Prop(() => Date)
timestamp: Dayjs; // Always use Dayjs for dates
// Special field types
@Field.Hidden(() => String)
internalCode: string;
@Field.Secret(() => String)
apiKey: string;
@Field.Resolve(() => Int)
get total(): number {
return this.items.length;
}
Option | Type | Default | Description | Example |
---|---|---|---|---|
default | Any | undefined | Default field value |
|
nullable | Boolean | false | Allows null values |
|
enum | Enum | - | Restricts to enum values |
|
min | Number | - | Minimum numeric value |
|
max | Number | - | Maximum numeric value |
|
minlength | Number | - | Minimum string length |
|
maxlength | Number | - | Maximum string length |
|
example | Any | - | Example value for documentation |
|
validate | Function | - | Custom validation function |
|
immutable | Boolean | false | Prevents modification after creation |
|
select | Boolean | true | Includes in query results by default |
|
text | String | - | Enables text search capabilities |
|
Default field value
{ default: 0 }
Allows null values
{ nullable: true }
Restricts to enum values
{ enum: Status }
Minimum numeric value
{ min: 0 }
Maximum numeric value
{ max: 100 }
Minimum string length
{ minlength: 3 }
Maximum string length
{ maxlength: 255 }
Example value for documentation
{ example: [0,0] }
Custom validation function
{ validate: (v) => v > 0 }
Prevents modification after creation
{ immutable: true }
Includes in query results by default
{ select: false }
Enables text search capabilities
{ text: 'search' }
// Array fields
@Field.Prop(() => [String])
tags: string[];
@Field.Prop(() => [[Int]])
matrix: number[][];
@Field.Prop(() => [OtherScalar])
items: OtherScalar[];
// Map fields
@Field.Prop(() => Map, {
of: String, // Must specify value type
default: new Map()
})
metadata: Map<string, string>;
// Enum implementation (camelCase values)
export const Status = enumOf(["active", "inactive"] as const);
export type Status = enumOf<typeof Status>;
@Field.Prop(() => String, {
enum: Status,
default: "active"
})
status: Status;
@Model.Scalar("Coordinate")
export class Coordinate {
@Field.Prop(() => [Float], { default: [0, 0] })
coordinates: number[];
// Calculate distance between coordinates
static getDistanceKm(loc1: Coordinate, loc2: Coordinate) {
const [lon1, lat1] = loc1.coordinates;
const [lon2, lat2] = loc2.coordinates;
// Distance calculation logic
return distance;
}
}
Issue | Wrong | Correct |
---|---|---|
Incorrect Enum Case |
|
|
Incorrect Array Syntax |
|
|
Missing Nullable Type |
|
|
Improper Enum Implementation |
|
|
Multiple Models |
|
|
Incorrect Date Handling |
|
|
// Basic scalar example
@Model.Scalar("Amount")
export class Amount {
@Field.Prop(() => Float, { min: 0, default: 0 })
value: number;
@Field.Prop(() => String, { default: "USD" })
currency: string;
}
// Complex scalar with enums
@Model.Scalar("GeoLocation")
export class GeoLocation {
@Field.Prop(() => Float, { min: -90, max: 90 })
latitude: number;
@Field.Prop(() => Float, { min: -180, max: 180 })
longitude: number;
@Field.Prop(() => String, {
enum: AccuracyLevel,
default: "medium"
})
accuracy: AccuracyLevel;
}
// Scalar with nested objects
@Model.Scalar("ProductSpec")
export class ProductSpec {
@Field.Prop(() => String)
sku: string;
@Field.Prop(() => [String], { default: [] })
colors: string[];
@Field.Prop(() => Dimension)
size: Dimension;
}