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