Raven.Database.DocumentDatabase.GetDocumentsWithIdStartingWith C# (CSharp) Method

GetDocumentsWithIdStartingWith() public method

public GetDocumentsWithIdStartingWith ( string idPrefix, int start, int pageSize ) : RavenJArray
idPrefix string
start int
pageSize int
return RavenJArray
		public RavenJArray GetDocumentsWithIdStartingWith(string idPrefix, int start, int pageSize)
		{
			var list = new RavenJArray();
			TransactionalStorage.Batch(actions =>
			{
				var documents = actions.Documents.GetDocumentsWithIdStartingWith(idPrefix, start)
					.Take(pageSize);
				var documentRetriever = new DocumentRetriever(actions, ReadTriggers);
				foreach (var doc in documents)
				{
					DocumentRetriever.EnsureIdInMetadata(doc);
					var document = documentRetriever
						.ExecuteReadTriggers(doc, null, ReadOperation.Load);
					if (document == null)
						continue;

					list.Add(document.ToJson());
				}
			});
			return list;
		}

Usage Example

Esempio n. 1
0
		public void Execute(DocumentDatabase database)
		{
			if (string.IsNullOrEmpty(database.Name) == false)
				return;// we don't care about tenant databases

			if (string.Equals(database.Configuration.AuthenticationMode, "OAuth", StringComparison.InvariantCultureIgnoreCase) == false)
				return; // we don't care if we aren't using oauth

			var array = database.GetDocumentsWithIdStartingWith("Raven/Users/", 0, 1);
			if (array.Length > 0)
				return; // there is already at least one user in there

			var pwd = Guid.NewGuid().ToString();

			if (database.Configuration.RunInMemory == false)
			{
				var authConfigPath = Path.Combine(Path.GetDirectoryName(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile), "authentication.config");

				File.WriteAllText(authConfigPath,
@"Since no users were found in the database, and the database authentication mode was set to OAuth, the following user was automatically created.

Username: Admin
Password: "******"

You can use those credentials to login to RavenDB.");

				logger.Info(@"Since no users were found, and the database authentication mode was set to OAuth, a default user was generated name 'Admin'.
Credentials for this user can be found in the following file: {0}", authConfigPath);
			}


			var ravenJTokenWriter = new RavenJTokenWriter();
			JsonExtensions.CreateDefaultJsonSerializer().Serialize(ravenJTokenWriter, new AuthenticationUser
			{
				AllowedDatabases = new[] { "*" },
				Name = "Admin",
				Admin = true
			}.SetPassword(pwd));


			var userDoc = (RavenJObject)ravenJTokenWriter.Token;
			userDoc.Remove("Id");
			database.Put("Raven/Users/Admin", null,
						 userDoc, 
						 new RavenJObject
						{
							{Constants.RavenEntityName, "AuthenticationUsers"},
							{
								Constants.RavenClrType,
								typeof (AuthenticationUser).FullName + ", " + typeof (AuthenticationUser).Assembly.GetName().Name
								}
						}, null);


		}