import mongoose, { Model, Schema, Document, Types } from 'mongoose'; /** * FACTORY FUNCTION FOR CREATING CUSTOM VIEW MODELS * * This allows you to create a custom Mongoose model for views with: * - Custom model name * - Custom collection name * - Additional schema fields * - Custom indexes * * @example * ```typescript * import { createViewModel } from '@peakify/polaris-data-table-views/models/createViewModel'; * import { Schema } from 'mongoose'; * * // Create custom model with additional fields * const CustomViewModel = createViewModel({ * modelName: 'CustomView', * collectionName: 'custom_views', * schemaOptions: { * // Add custom fields * customField: { type: String }, * metadata: { type: Schema.Types.Mixed } * } * }); * ``` */ /** * Mongoose schema field definition * Supports all Mongoose schema types and options * This is a flexible type that accepts any valid Mongoose schema field definition */ type SchemaFieldDefinition = Record; /** * Options for creating a custom View model */ interface CreateViewModelOptions { /** * Custom model name (default: 'View') */ modelName?: string; /** * Custom collection name (default: undefined, uses modelName) */ collectionName?: string; /** * Additional schema fields to add to the base schema * You can add as many custom fields as needed * * @example * ```typescript * schemaOptions: { * customField1: { type: String }, * customField2: { type: Number }, * tags: [{ type: String }], * metadata: { type: Schema.Types.Mixed }, * userId: { type: Schema.Types.ObjectId, ref: 'User' }, * isActive: { type: Boolean, default: true }, * // ... add as many fields as you need * } * ``` */ schemaOptions?: Record; /** * Custom schema options (timestamps, etc.) */ mongooseSchemaOptions?: { timestamps?: boolean; [key: string]: any; }; /** * Additional indexes to add * You can add multiple indexes for your custom fields * * @example * ```typescript * additionalIndexes: [ * { fields: { customField1: 1 }, options: { sparse: true } }, * { fields: { tags: 1 }, options: {} }, * { fields: { userId: 1, isActive: 1 }, options: {} }, * ] * ``` */ additionalIndexes?: Array<{ fields: Record; options?: any; }>; } /** * Base View schema definition * You can use this to extend your own schema */ declare const baseViewSchemaDefinition: { readonly path: { readonly type: StringConstructor; readonly required: true; readonly index: true; }; readonly name: { readonly type: StringConstructor; readonly required: true; }; readonly filters: { readonly type: typeof Schema.Types.Mixed; readonly default: {}; }; readonly ownerId: { readonly type: StringConstructor; readonly required: false; readonly index: true; }; }; /** * Create base indexes for View schema */ declare function createBaseViewIndexes(schema: Schema): void; /** * Factory function to create a custom View model * * @param options - Configuration options for the custom model * @returns Mongoose Model instance * * @example * ```typescript * import { createViewModel } from '@peakify/polaris-data-table-views/server'; * import { Schema } from 'mongoose'; * * // Basic usage with default settings * const ViewModel = createViewModel(); * * // Custom model name and collection * const CustomViewModel = createViewModel({ * modelName: 'CustomView', * collectionName: 'custom_views' * }); * * // With multiple custom fields * const ExtendedViewModel = createViewModel({ * modelName: 'ExtendedView', * schemaOptions: { * // String fields * description: { type: String }, * category: { type: String, index: true }, * * // Number fields * priority: { type: Number, default: 0 }, * viewCount: { type: Number, default: 0 }, * * // Boolean fields * isPublic: { type: Boolean, default: false }, * isFavorite: { type: Boolean, default: false }, * * // Array fields * tags: [{ type: String }], * permissions: [{ type: String }], * * // Object/Mixed fields * metadata: { type: Schema.Types.Mixed, default: {} }, * settings: { type: Schema.Types.Mixed }, * * // Reference fields * createdBy: { type: Schema.Types.ObjectId, ref: 'User', index: true }, * teamId: { type: Schema.Types.ObjectId, ref: 'Team' }, * * // Date fields * lastAccessed: { type: Date }, * expiresAt: { type: Date, index: true }, * * // Add as many fields as you need! * }, * additionalIndexes: [ * { fields: { category: 1, isPublic: 1 } }, * { fields: { tags: 1 } }, * { fields: { createdBy: 1, priority: -1 } }, * ] * }); * ``` */ declare function createViewModel(options?: CreateViewModelOptions): Model; /** * SERVER-SIDE MONGOOSE MODEL FOR VIEW MANAGEMENT * * ⚠️ IMPORTANT: This is a SERVER-SIDE Mongoose model that should only be used * in Node.js backend code (API routes, Express handlers, Next.js API routes, etc.). * * DO NOT import this model in client-side React components or browser code. * * This model defines the database schema for storing saved table views (filters, sorting, etc.) * that users can create, update, and delete. * * Usage: * ```typescript * // ✅ Server-side only (API route, Express handler, etc.) * import { ViewModel } from '@peakify/polaris-data-table-views/models/View'; * * // In your server-side code * const views = await ViewModel.find({ path: '/admin/users' }); * ``` * * For custom models, use the factory function: * ```typescript * import { createViewModel } from '@peakify/polaris-data-table-views/models/createViewModel'; * * const CustomViewModel = createViewModel({ * modelName: 'CustomView', * schemaOptions: { customField: { type: String } } * }); * ``` * * Schema Fields: * - path: The URL path where the view is used (e.g., '/admin/users') * - name: The name of the saved view * - filters: Object containing filter values (queryValue, custom filters, etc.) * - ownerId: Optional user ID for user-specific views (null for shared views) * - createdAt: Auto-generated timestamp * - updatedAt: Auto-generated timestamp */ /** * Interface for the View document in MongoDB * This extends Mongoose Document and includes all view-related fields */ interface IView extends Document { _id: Types.ObjectId; path: string; name: string; filters: Record; ownerId?: string; createdAt: Date; updatedAt: Date; } declare const ViewModel: mongoose.Model | mongoose.Model & IView & Required<{ _id: Types.ObjectId; }> & { __v: number; }, any>; export { type CreateViewModelOptions as C, type IView as I, type SchemaFieldDefinition as S, ViewModel as V, createBaseViewIndexes as a, baseViewSchemaDefinition as b, createViewModel as c };