Exemple #1
0
        public List <PropertyData> GetPropertiesPaths(MongoDatabaseInfo databaseInfo, string collectionName)
        {
            MongoCollectionModelInfo model = GetModel(databaseInfo, collectionName);

            if (string.IsNullOrWhiteSpace(model.ModelCode))
            {
                throw new Exception("Model does not exist");
            }

            var types    = new MongoDynamicCodeRunner().CompileModelCode(model.ModelCode);
            var rootType = types.Find(t => t.Name == model.RootClassName);

            if (rootType == null)
            {
                throw new Exception($"{model.RootClassName} class not found in Model.");
            }

            List <PropertyData> properties = ObjectHelper.ToPropertyPaths(rootType);

            return(properties);
        }
Exemple #2
0
        private MongoCollectionModelInfo GetModel(MongoDatabaseInfo databaseInfo, string collectionName)
        {
            MongoCollectionInfo collectionInfo = databaseInfo.GetCollectionInfo(collectionName);

            if (collectionInfo != null && collectionInfo.HasModel)
            {   // Model already exists for this collection.
                return(collectionInfo.Models[0]);
            }

            // Generate and save model code.
            var collection = databaseInfo.GetCollection(collectionName);
            var doc        = collection.FindOne();

            if (doc == null)
            {
                throw new Exception(
                          $"Collection '{collectionName}' is empty. Unable to determine schema from first document");
            }

            var schemaInfo = MongoCollectionSchemaStore.GetSchemaDocument(databaseInfo, collectionName);
            var classes    = new BsonDocumentConverter().ToCSharpClassDeclarations(schemaInfo.SchemaDocument, collectionName);

            var sb = new StringBuilder();

            foreach (var classSyntax in classes)
            {
                sb.AppendLine(classSyntax);
            }

            string modelCode = sb.ToString();

            MongoCollectionModelInfo newModel = databaseInfo.SetCollectionModel(collectionName, modelCode, "Doc0", true, (int)schemaInfo.SampleSize, schemaInfo.SamplePercent);

            Settings.Instance.Save();

            return(newModel);
        }