Skip to content

Commit bce7d37

Browse files
initial documentation pass (#11)
Co-authored-by: jeffrey-elliott <[email protected]> Co-authored-by: Steve Hewitt <[email protected]>
1 parent 90cf471 commit bce7d37

File tree

86 files changed

+3751
-408
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+3751
-408
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ test/DataStax.AstraDB.DataApi.UnitTests/bin
88
test/DataStax.AstraDB.DataApi.UnitTests/obj
99
appsettings.json
1010
*.log
11+
_site
12+
api
13+
api-docs

csharp-client.adoc

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
= C# client usage
2+
:description: Guidance for using the {data-api} C# client.
3+
4+
This page provides language-specific guidance for using the data-api C# client.
5+
6+
For information about installing and getting started with the C# client, see ???.
7+
8+
== Terminology
9+
10+
The Data API stores structured data as `Documents` which are essentially key-value pairs. In the C# client these documents will be represented by C# classes (POCOs) or, at the lowest level, by dictionaries. When we refer to "fields" or "properties" we are referring to these key-value pairs and/or their representative members in the C# classes, not specifying the C# implementation.
11+
12+
== Client hierarchy
13+
14+
All interactions with the data begin with a DataApiClient object.
15+
16+
Once you have an instance of a `DataAPIClient`, you can use it to get an instance of a `Database` object, which can be used to manage and access `Collection` objects which are themselves used to interact with the actual documents.
17+
18+
[source,c#]
19+
----
20+
//instantiate a client
21+
var client = new DataApiClient("YourTokenHere");
22+
23+
//connect to a database
24+
var database = client.GetDatabase("YourDatabaseUrlHere");
25+
26+
//create a new collection
27+
var collection = await database.CreateCollectionAsync<SimpleObject>("YourCollectionNameHere");
28+
29+
//insert a document into the collection
30+
var newObject = new SimpleObject()
31+
{
32+
Name = "Test Object 1",
33+
};
34+
var insertResult = await collection.InsertOneAsync(newObject);
35+
var insertedId = insertResult.InsertedId;
36+
----
37+
38+
=== Command Options
39+
40+
The `CommandOptions` class provides a set of low-level options to control the interactions with the underlying data store.
41+
42+
These options can be provided at any level of the SDK hierarchy:
43+
44+
* `DataApiClient`
45+
** `Database`
46+
*** `Collection`
47+
48+
as well as directly to each of the methods. You can provide different options objects at each level. The options specified at the most granular level will take precedence.
49+
50+
== Serialization and Deserialization
51+
52+
=== Default Behavior
53+
54+
The C# client uses System.Text.Json to handle serializing the documents to JSON to send to the underlying datastore and to deserialize resultant documents back into C# objects. As such, you can use standard System.Text.Json attributes to affect the serialization and deserialization behavior. For example, you can use the `JsonIgnore` attribute to exclude a property from serialization and deserialization.
55+
56+
[source,c#]
57+
----
58+
public class MyDocument
59+
{
60+
public int? _id { get; set; }
61+
public string Name { get; set; }
62+
//This property will not be serialized or deserialized
63+
[JsonIgnore]
64+
public InternalProperty { get; set; }
65+
}
66+
----
67+
68+
=== Special Fields
69+
70+
While most of the properties passed to the data store are simply serialized to JSON and stored, there are several reserved fields that have special handling. These special fields are identified on your documents by using the `DocumentMappingAttribute`.
71+
72+
The most commonly used special field is `_id`, which is the primary key for the document and is required -- if not provided it will be added by the Data API when storing the document. The Data API can handle multiple types for the `_id` field ([Include link here to documentation on the supported types]). The C# class defining your document can either specifically name a property `_id` or use `DocumentMappingAttribute(DocumentMappingField.Id)` to specify which property should be used as the id.
73+
74+
[source,c#]
75+
----
76+
//This document will use the default id property name
77+
public class DocumentWithDefaultId
78+
{
79+
public int _id { get; set; }
80+
}
81+
82+
//This document will use a custom id property name
83+
public class CustomIdProperty
84+
{
85+
[DocumentMapping(DocumentMappingField.Id)]
86+
public Guid ThisIsTheId { get; set; }
87+
}
88+
----
89+
90+
The other field mappings are:
91+
92+
* `DocumentMappingField.Vectorize` - When you want a document to have embeddings automatically generated (based on the vectorize settings for the collection), use this field mapping to specify the text to be vectorized for this document.
93+
94+
* `DocumentMappingField.Vector` - When you generate your own embeddings for a document, these are passed to the Data API through a float array annotated with this field mapping.
95+
96+
* `DocumentMappingField.Similarity` - When performing a vector search on the data, you can specify to include a similarity score for the results. Include this field mapping on a property of the class receiving the results to store the similarity score.
97+
98+
== Handling List Results
99+
100+
For the operations that return a list of documents (i.e. the Find variants), the underlying data is returned in batches by the Data API. The most straightforward way to interact with the data is to simply iterate over the results as an `IAsyncEnumerable<T>` or `IEnumerable<T>`. The enumerable implementations will handle the batch iteration internally.
101+
102+
You can also at this point use all of the standard IEnumerable extensions (e.g. `Skip`, `Take`, `Where`, etc.) to further filter and process the results.
103+
104+
[source,c#]
105+
----
106+
// find all documents
107+
var results = collection.Find();
108+
109+
// synchronous example
110+
foreach (var doc in results) {
111+
// do something
112+
}
113+
114+
// asynchronous example
115+
await foreach (var doc in results)
116+
{
117+
// do something
118+
}
119+
----
120+
121+
If you need to manually control the batch iteration, you can use the `Cursor` class by calling ToCursor() on the results.
122+
123+
[source,c#]
124+
----
125+
// find all documents
126+
var cursor = collection.Find().ToCursor();
127+
128+
// synchronous example
129+
while (cursor.MoveNext())
130+
{
131+
var batch = cursor.Current;
132+
foreach (var doc in batch) {
133+
// do something
134+
}
135+
}
136+
137+
// asynchronous example
138+
while (await cursor.MoveNextAsync())
139+
{
140+
var batch = cursor.Current;
141+
// do something with batch
142+
}
143+
----

docfx.json

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/dotnet/docfx/main/schemas/docfx.schema.json",
3+
"metadata": [
4+
{
5+
"src": [
6+
{
7+
"files": [
8+
"**/*.csproj"
9+
],
10+
"exclude": [
11+
"**/bin/**",
12+
"**/obj/**"
13+
],
14+
"src": "./src"
15+
}
16+
],
17+
"dest": "api-docs"
18+
}
19+
],
20+
"build": {
21+
"content": [
22+
{
23+
"files": [
24+
"**/*.{md|yml}"
25+
],
26+
"exclude": [
27+
"_site/**"
28+
]
29+
}
30+
],
31+
"resource": [
32+
{
33+
"files": [
34+
"images/**"
35+
]
36+
}
37+
],
38+
"output": "_site",
39+
"template": [
40+
"default",
41+
"modern"
42+
],
43+
"globalMetadata": {
44+
"_appName": "astra-db-csharp",
45+
"_appTitle": "astra-db-csharp",
46+
"_appLogoPath": "images/logo.svg",
47+
"_enableSearch": true,
48+
"pdf": true
49+
}
50+
}
51+
}

images/logo.svg

Lines changed: 2 additions & 0 deletions
Loading

index.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
_layout: landing
3+
---
4+
5+
# Overview
6+
7+
This C# Client Library simplifies using the DataStax Data API to manage and interact with AstraDB instances as well as other DataStax databases.
8+
9+
# Installation
10+
11+
// TODO: NuGet instructions
12+
13+
# Quickstart
14+
15+
```
16+
//instantiate a client
17+
var client = new DataApiClient("YourTokenHere");
18+
19+
//connect to a database
20+
var database = client.GetDatabase("YourDatabaseUrlHere");
21+
22+
//create a new collection
23+
var collection = await database.CreateCollectionAsync<SimpleObject>("YourCollectionNameHere");
24+
25+
//insert a document into the collection
26+
var documents = new List<SimpleObject>
27+
{
28+
new SimpleObject()
29+
{
30+
Name = "Test Object 1",
31+
},
32+
new SimpleObject()
33+
{
34+
Name = "Test Object 2",
35+
}
36+
};
37+
var insertResult = await collection.InsertManyAsync(documents);
38+
39+
//find a document
40+
var filter = Builders<SimpleObject>.Filter.Eq(so => so.Name, "Test Object 1");
41+
var results = await collection.Find(filter);
42+
```

0 commit comments

Comments
 (0)