Create a Model
After configuring the database connection, the next step is to define your data models using Noblex’s Model
helper.
Here’s how you can create a model for a blogPosts
collection.
import { Model } from "noblex";
interface IModel {
name?: string;
deleted?: boolean;
createdAt: Date;
updatedAt: Date;
}
export interface IBlogPost extends IModel {
address: string;
lastSyncedBlockEth: number;
lastSyncedBlockToken: number;
lastSyncedBlockEthInternal: number;
}
export const schema = {
address: { type: String, required: true, unique: true, lowercase: true },
lastSyncedBlockEth: { type: Number, default: -1 },
lastSyncedBlockEthInternal: { type: Number, default: -1 },
lastSyncedBlockToken: { type: Number, default: -1 },
};
const BlogPost = Model<IBlogPost>("BlogPost", schema);
export default BlogPost;
What’s happening here?
-
You define an interface
IWallet
extending a baseIModel
with common fields. -
Define the Mongoose schema with field types, defaults, and validations.
-
Use Noblex’s
Model
function to create a typed model linked to the"Wallet"
collection. -
Export the model to use it elsewhere in your app.
This pattern keeps your code clean, strongly typed, and integrated with Noblex’s features like request-aware hooks and RBAC.
Next: Learn how to add powerful hooks to your models for request-aware data access and transformation.