Description
- App(1) = Namescpace(N)
- App Accessor/virtual field (namespace)
- Namespace Accessor/virtual field (app)
1. APP
1.1. App model
import { Schema, model, Document, Types } from 'mongoose';
import { INamespace } from './namespace.model';
enum IsActiveEnum {
Inactive = 0,
Active = 1,
}
// Define the authentication interface
interface IAuthentication {
salt: string;
password: string;
}
interface IApp extends Document {
name: string;
authentication: IAuthentication;
is_active: IsActiveEnum;
created_by?: number;
created_at?: Date;
}
const AppSchema = new Schema<IApp>({
name: { type: String, required: true, unique: true },
authentication: {
password: { type: String, required: true, select: false },
salt: { type: String, required: true, select: false },
},
is_active: {
type: Number,
enum: [IsActiveEnum.Inactive, IsActiveEnum.Active],
default: IsActiveEnum.Active,
},
created_by: { type: String },
created_at: { type: Date },
});
// Add a virtual property to the AppSchema
AppSchema.virtual('namespace', {
type: 'ObjectId',
ref: 'Namespace',
localField: '_id',
foreignField: 'app_id',
});
const AppModel = model<IApp>('AppModel', AppSchema, 'app');
export { IApp, AppModel };
1.2. App Service
const getAppsPaginated = async (req: Request): Promise<any> => {
let data = await AppModel.find()
.lean()
.populate('namespace') // Use the virtual property 'namespaces'
.exec();
return data;
};
2. Namespace
2.1. Namespace model
import { Schema, model, Document, Types } from 'mongoose';
enum IsActiveEnum {
Inactive = 0,
Active = 1,
}
interface INamespace extends Document {
name: string;
path: string;
is_active: IsActiveEnum;
created_by?: number;
created_at?: Date;
app_id?: Types.ObjectId;
}
const NamespaceSchema = new Schema<INamespace>({
name: { type: String, required: true, unique: true },
path: { type: String, required: true, unique: true },
is_active: {
type: Number,
enum: [IsActiveEnum.Inactive, IsActiveEnum.Active],
default: IsActiveEnum.Active,
},
created_by: { type: String },
created_at: { type: Date },
app_id: { type: Schema.Types.ObjectId, ref: 'AppModel' }, // Reference to App model
});
// Add a virtual property to the AppSchema
NamespaceSchema.virtual('app', {
type: 'ObjectId',
ref: 'AppModel',
foreignField: '_id',
localField: 'app_id',
justOne : true
});
const Namespace = model<INamespace>('Namespace', NamespaceSchema, 'namespace');
export { INamespace, Namespace };
2.2. Namespace Service
const getNamespacePaginated = async (req: Request): Promise<any> => {
return await Namespace.find()
.lean()
.populate('app') // Assuming 'posts' is the name of the field in the User schema referencing the Post model
.exec();
};