Skip to main content

Defining MongoDB Collections

Collections represent MongoDB collections. Define them using defineCollection and the prop namespace for property types.

Example: User Collection

import { defineCollection, prop } from "hysteria-orm";

const User = defineCollection("users", {
properties: {
name: prop.string(),
email: prop.string(),
age: prop.number(),
isActive: prop.boolean(),
createdAt: prop.date(),
},
});

Property Types

The prop namespace provides type-safe property definitions:

import { defineCollection, prop } from "hysteria-orm";

const Product = defineCollection("products", {
properties: {
name: prop.string(), // string values
price: prop.number(), // numeric values
inStock: prop.boolean(), // boolean values
releaseDate: prop.date(), // Date values
metadata: prop.object<{
// typed nested objects
tags: string[];
category: string;
}>(),
extra: prop.any(), // any type (untyped)
},
});
HelperTypeScript TypeDescription
prop.string()stringString values
prop.number()numberNumeric values
prop.boolean()booleanBoolean values
prop.date()DateDate values
prop.object<T>()TTyped nested objects
prop.any()anyAny type (untyped)

Notes

  • The id property is handled automatically and maps to MongoDB _id.
  • You can use nested objects and arrays via prop.object<T>().
  • The first argument to defineCollection is the MongoDB collection name.

Next: Collection Methods