Scalar Types

Scalar types are primitive data types used in GraphQL and MongoDB schemas. Akan.js provides a set of predefined scalar types that map to GraphQL scalars and are used throughout the framework for type-safe model definitions.
Supported Scalar Types
📦Import from @akanjs/base
import { ID, Int, Float, Upload, JSON } from "@akanjs/base";

Scalar Type Reference Table

Complete reference of all scalar types with their GraphQL mapping, MongoDB type, default values, and example usage.
TypeGraphQLMongoDBDefaultExample Value
StringStringString"""Hello World"
BooleanBooleanBooleanfalsetrue
DateDate (custom)Datedayjs(new Date(-1))"2024-01-15T09:30:00Z"
IDIDObjectIdnull"1234567890abcdef12345678"
IntIntNumber042
FloatFloatNumber03.14159
UploadUpload (GraphQL Upload)--FileUpload stream
JSONJSON (custom)Mixed{}{ "key": "value" }
MapJSONMap{}{ "a": 1, "b": 2 }

constant.ts Scalars

Scalar types are used in constant.ts files to define model field types. Here are practical examples of how each scalar type is used:
String - Text Data
String Usage
Boolean - True/False
Boolean Usage
Date - Date and Time
Date Usage
Int & Float - Numbers
Int & Float Usage
ID - MongoDB ObjectId
ID Usage
JSON - Arbitrary Data
JSON Usage

Complete Model Example

Here's a complete example showing various scalar types used together in a model definition:
article.constant.ts
💡Scalar Types Used
  • String: title, slug
  • Boolean: isPublic, isFeatured
  • Int: viewCount, likeCount, commentCount
  • Float: rating
  • Date: publishedAt, lastEditedAt
  • ID: authorId, tagIds
  • JSON: content, seoMeta

Scalar Best Practices

1️⃣Use Int for Counts and Quantities
Use Int instead of Float for whole numbers like counts, quantities, and IDs. It provides better performance and prevents floating-point issues.
2️⃣Use dayjs() for Date Defaults
Always use a function for Date defaults: { default: () => dayjs() }. Static defaults would capture the build time, not creation time.
3️⃣Use ID for Document References
Use ID type when you need to store a reference to another MongoDB document. It automatically converts to ObjectId in the database.
4️⃣Use JSON for Flexible Content
Use JSON type for rich text content (TipTap editor), flexible metadata, or configuration objects. Avoid using it for structured data - define proper fields instead.
🎉 What You've Learned:
  • 9 scalar types: String, Boolean, Date, ID, Int, Float, Upload, JSON, Map
  • Custom scalar classes (ID, Int, Float, Upload, JSON) from @akanjs/base
  • GraphQL and MongoDB type mappings for each scalar
  • Practical usage patterns in constant.ts model definitions
Released under the MIT License
Official Akan.js Consulting onAkansoft
Copyright © 2025 Akan.js. All rights reserved.
System managed bybassman