Skip to content

Instantly share code, notes, and snippets.

@kiliman
Last active August 29, 2015 13:56
Show Gist options
  • Save kiliman/9009010 to your computer and use it in GitHub Desktop.
Save kiliman/9009010 to your computer and use it in GitHub Desktop.
Simple RavenDB clone using SQLite for use in mobile clients.
Documents
3 result(s)
{"Id":"fca1cac5-77c7-4bc0-9962-60a3cafc7ec8","FirstName":"Lisa","LastName":"Carter","Age":51,"Home":{"City":"Chesapeake","State":"VA"}}
{"Id":"b399e2a5-a390-4cb0-b443-b973cb2c8b7c","FirstName":"Michael","LastName":"Carter","Age":43,"Home":{"City":"Chesapeake","State":"VA"}}
{"Id":"33a5c416-051a-4a1a-92ab-0c8b5bd3343e","FirstName":"Matthew","LastName":"Carter","Age":16,"Home":{"City":"Chesapeake","State":"VA"}}
People
3 result(s)
Lisa Carter Age: 51 City: Chesapeake
Michael Carter Age: 43 City: Chesapeake
Matthew Carter Age: 16 City: Chesapeake
public class IndexResult
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
[Indexed]
public string DocumentId { get; set; }
}
public class PersonIndex : IndexResult
{
[Indexed, Collation("nocase")]
public string LastName { get; set; }
[Indexed]
public int Age { get; set; }
[Indexed, Collation("nocase")]
public string Home_City { get; set; }
}
public class PersonIndexMap : IndexMap<Person, PersonIndex>
{
public PersonIndexMap()
{
Map = persons => from person in persons
select new PersonIndex
{
DocumentId = person.Id,
LastName = person.LastName,
Age = person.Age,
Home_City = person.Home.City
};
}
}
public class Person
{
public Person()
{
Id = Guid.NewGuid().ToString();
}
public string Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public Address Home { get; set; }
}
public class Address
{
public string City { get; set; }
public string State { get; set; }
}
class Program
{
static void Main(string[] args)
{
var fileName = @"C:\temp\test.db";
if (File.Exists(fileName))
{
File.Delete(fileName);
}
var store = new DocumentStore(fileName);
var people = new List<Person>()
{
new Person
{
FirstName = "Michael",
LastName = "Carter",
Age = 43,
Home = new Address
{
City = "Chesapeake",
State = "VA"
}
},
new Person
{
FirstName = "Lisa",
LastName = "Carter",
Age = 51,
Home = new Address
{
City = "Chesapeake",
State = "VA"
}
},
new Person
{
FirstName = "Matthew",
LastName = "Carter",
Age = 16,
Home = new Address
{
City = "Chesapeake",
State = "VA"
}
},
new Person
{
FirstName = "Sarah",
LastName = "Carter",
Age = 19,
Home = new Address
{
City = "Richmond",
State = "VA"
}
}
};
using (var session = store.OpenSession())
{
foreach (var person in people)
{
session.Store(person, person.Id);
}
session.SaveChanges();
}
using (var session = store.OpenSession())
{
var docs = session.Query<PersonIndex>()
.Where(x => x.LastName == "carter" && x.Home_City == "chesapeake")
.OrderByDescending(x => x.Age)
.AsDocument()
.ToList();
System.Console.WriteLine("Documents");
System.Console.WriteLine("{0} result(s)", docs.Count);
foreach (var doc in docs)
{
System.Console.WriteLine(doc.Json);
}
var persons = docs.AsType<Person>().ToList();
System.Console.WriteLine();
System.Console.WriteLine("People");
System.Console.WriteLine("{0} result(s)", persons.Count);
foreach (var person in persons)
{
System.Console.WriteLine("{0} {1} Age: {2} City: {3}",
person.FirstName, person.LastName, person.Age, person.Home.City );
}
}
System.Console.ReadKey();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment