Skip to content

Instantly share code, notes, and snippets.

View JudahGabriel's full-sized avatar

Judah Gabriel Himango JudahGabriel

View GitHub Profile
@JudahGabriel
JudahGabriel / Startup.cs
Created August 1, 2019 20:47
Configuring a connection to a RavenDB Cloud cluster
// Connect to our RavenDB Cloud cluster.
// Notice the only thing that has changed is that we're passing 3 URLs in, one for each node in our cluster.
var raven = new DocumentStore
{
Database = "OldWestHeroes",
Urls = new[]
{
"https://a.cluster.clistctrl.ravendb.cloud",
"https://b.cluster.clistctrl.ravendb.cloud",
"https://c.cluster.clistctrl.ravendb.cloud",
@JudahGabriel
JudahGabriel / OutlawsController.cs
Created August 1, 2019 20:26
Querying with RavenDB
using (var session = raven.OpenSession())
{
var gunslingers = session.Query<Outlaw>().Where(o => o.IsGunslinger);
}
@JudahGabriel
JudahGabriel / HomeController.cs
Created August 1, 2019 20:23
Using RavenDB to store an object
using (var session = raven.OpenSession())
{
var outlaw = new Outlaw
{
Name = "Simmons, J.",
IsGunslinger = true
};
session.Store(outlaw);
session.SaveChanges();
}
var raven = new DocumentStore
{
Urls = new[] { "https://a.free.clistctrl.ravendb.cloud" },
Database = "Outlaws",
Certificate = new X509Certificate2("\path\to\free.clistctrl.client.certificate.pfx", "")
};
raven.Initialize();
public class AccountController : Controller
{
private UserManager<AppUser> userManager;
private SignInManager<AppUser> signInManager;
// RavenDB.Identity provides a UserManager and SignInManager for dependency injection.
public AccountController(UserManager<AppUser> userManager, SignInManager<AppUser> signInManager)
{
this.userManager = userManager;
this.signInManager = signInManager;
public class HomeController : Controller
{
[Authorize] // Authorize: the user must be signed in
public IActionResult Index()
{
}
[Authorize(Roles = "Admin"] // Authorize: the signed in user must be an admin
public IActionResult Index()
{
public void ConfigureServices(IServiceCollection services)
{
// Add RavenDB and identity.
services
.AddRavenDb(Configuration.GetConnectionString("RavenDbConnection")) // Create a RavenDB DocumentStore singleton.
.AddRavenDbAsyncSession() // Create a RavenDB IAsyncDocumentSession for each request.
.AddRavenDbIdentity<AppUser>(); // Use Raven for users and roles. AppUser is your class, a simple DTO to hold user data. See https://github.com/JudahGabriel/RavenDB.Identity/blob/master/Sample/Models/AppUser.cs
...
}
UsersController.prototype.fetchUsers = function () {
return __awaiter(this, void 0, void 0, function () {
var results;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.usersApi.getUsers(0, 100)];
case 1:
results = _a.sent();
this.users = results;
return [2 /*return*/];
{
"compilerOptions": {
"noImplicitAny": false,
"noEmitOnError": true,
"removeComments": false,
"sourceMap": false,
"target": "es5",
"noEmitHelpers": true,
"strictNullChecks": true
},
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t;