From d410c5cb819d3281d6207b7fc1c008170c1bb38f Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 16 Feb 2024 08:15:33 +0100 Subject: [PATCH] RDoc-2294 Add getting-started to v4.2 --- .../start/getting-started.markdown | 7 - .../start/getting-started.markdown | 7 - .../start/getting-started.markdown | 302 ++++++++++++++++++ .../Start/GettingStarted.cs | 181 +++++++++++ .../net/ravendb/Start/GettingStarted.java | 139 ++++++++ .../Samples/nodejs/start/gettingStarted.js | 90 ++++++ 6 files changed, 712 insertions(+), 14 deletions(-) create mode 100644 Documentation/4.2/Raven.Documentation.Pages/start/getting-started.markdown create mode 100644 Documentation/4.2/Samples/csharp/Raven.Documentation.Samples/Start/GettingStarted.cs create mode 100644 Documentation/4.2/Samples/java/src/test/java/net/ravendb/Start/GettingStarted.java create mode 100644 Documentation/4.2/Samples/nodejs/start/gettingStarted.js diff --git a/Documentation/4.0/Raven.Documentation.Pages/start/getting-started.markdown b/Documentation/4.0/Raven.Documentation.Pages/start/getting-started.markdown index d581069b5b..7bd484a890 100644 --- a/Documentation/4.0/Raven.Documentation.Pages/start/getting-started.markdown +++ b/Documentation/4.0/Raven.Documentation.Pages/start/getting-started.markdown @@ -55,13 +55,6 @@ If you are interested in hosting the server in a Docker container, please read o {NOTE/} -{NOTE: Running in RavenDB Cloud} - -If you want to test RavenDB without manual setup try [RavenDB Cloud](https://cloud.ravendb.net). -We offer one free instance per customer. For more information, please read our [dedicated article](../cloud/cloud-overview). - -{NOTE/} - --- ### Configuration diff --git a/Documentation/4.1/Raven.Documentation.Pages/start/getting-started.markdown b/Documentation/4.1/Raven.Documentation.Pages/start/getting-started.markdown index b0a22ddf54..0c2278c517 100644 --- a/Documentation/4.1/Raven.Documentation.Pages/start/getting-started.markdown +++ b/Documentation/4.1/Raven.Documentation.Pages/start/getting-started.markdown @@ -65,13 +65,6 @@ If you are interested in hosting the server on a VM, please refer to {NOTE/} -{NOTE: Running in RavenDB Cloud} - -If you want to test RavenDB without manual setup try [RavenDB Cloud](https://cloud.ravendb.net). -We offer one free instance per customer. For more information, please read our [dedicated article](../cloud/cloud-overview). - -{NOTE/} - --- ### Configuration diff --git a/Documentation/4.2/Raven.Documentation.Pages/start/getting-started.markdown b/Documentation/4.2/Raven.Documentation.Pages/start/getting-started.markdown new file mode 100644 index 0000000000..b0a22ddf54 --- /dev/null +++ b/Documentation/4.2/Raven.Documentation.Pages/start/getting-started.markdown @@ -0,0 +1,302 @@ +# Getting Started + +Welcome to RavenDB! + +This article will get you started and guide you through all the parts of RavenDB needed for basic understanding and simple setup. It consists of two parts: + +- The [Server](../start/getting-started#server) part will focus on installation, setup & configuration of the RavenDB server +- The [Client](../start/getting-started#client) part will describe the general principles behind our client libraries + +{PANEL: Server} + +Let's start by installing and configuring the server. In order to do that first we need to download the server package from the [downloads](https://ravendb.net/downloads) page. + +RavenDB is cross-platform with support for the following operating systems: + +- Windows x64 / x86 +- Linux x64 +- Docker +- MacOS +- Raspberry Pi + +--- + +### Prerequisites + +RavenDB is written in .NET Core so it requires the same set of prerequisites as .NET Core. + +{NOTE: Windows} + +Please install [Visual C++ 2015 Redistributable Package](https://support.microsoft.com/en-us/help/2977003/the-latest-supported-visual-c-downloads) (or newer) before launching the RavenDB server. This package should be the sole requirement for 'Windows' platforms. If you're experiencing difficulties, please check the [Prerequisites for .NET Core on Windows](https://docs.microsoft.com/en-us/dotnet/core/windows-prerequisites) article written by Microsoft. + +{NOTE/} + +{NOTE: Linux} + +We highly recommend **updating** your **Linux OS** prior to launching the RavenDB server. Also check if .NET Core requires any other prerequisites in the [Prerequisites for .NET Core on Linux](https://docs.microsoft.com/en-us/dotnet/core/linux-prerequisites) article written by Microsoft. + +{NOTE/} + +{NOTE: MacOS} + +We highly recommend **updating** your **MacOS** and checking the [Prerequisites for .NET Core on macOS](https://docs.microsoft.com/en-us/dotnet/core/macos-prerequisites) article written by Microsoft before launching the RavenDB Server. + +{NOTE/} + +--- + +### Installation & Setup + +After extraction of the server package, you can start the [Setup Wizard](../start/installation/setup-wizard) by running the `run.ps1` (or `run.sh`) script or by [disabling the 'Setup Wizard' and configuring the server manually](../start/installation/manual). + +{NOTE: Running in a Docker container} + +If you are interested in hosting the server in a Docker container, please read our [dedicated article](../start/installation/running-in-docker-container). + +{NOTE/} + +{NOTE: Running in a VM} + +If you are interested in hosting the server on a VM, please refer to + +- [AWS Windows VM](../start/installation/setup-examples/aws-windows-vm) +- [AWS Linux VM](../start/installation/setup-examples/aws-linux-vm) +- [Docker on AWS Linux VM](../start/installation/setup-examples/aws-docker-linux-vm) + +{NOTE/} + +{NOTE: Running in RavenDB Cloud} + +If you want to test RavenDB without manual setup try [RavenDB Cloud](https://cloud.ravendb.net). +We offer one free instance per customer. For more information, please read our [dedicated article](../cloud/cloud-overview). + +{NOTE/} + +--- + +### Configuration + +The RavenDB server is using a [settings.json](../server/configuration/configuration-options#settings.json) file to store the server-wide configuration options. This file is located in the `Server` directory, but please note that after making changes to this file, a server restart is required in order for them to be applied. + +You can read more about the available configuration options in our [dedicated article](../server/configuration/configuration-options). + +{INFO:Default configuration} + +The configuration file included in each RavenDB server distribution package is as follows: + +{CODE-BLOCK:json} +{ + "ServerUrl": "http://127.0.0.1:0", + "Setup.Mode": "Initial", + "DataDir": "RavenData" +} +{CODE-BLOCK/} + +Which means that the server will run: + +- On `localhost` with a `random port` +- In `Setup Wizard` mode +- Store the data in the `RavenData` directory. + +{INFO/} + +{WARNING: Port in Use} + +In some cases the port might be in use. This will prevent the Server from starting with an "address in use" error (`EADDRINUSE`). + +The port can be changed by editing the `ServerUrl` value. + +{WARNING/} + +{NOTE: Write Permissions} + +RavenDB requires write permissions to the following locations: + +- The folder where RavenDB server is running (to update [settings.json](../server/configuration/configuration-options#settings.json) by the [Setup Wizard](../start/installation/setup-wizard)) +- The data folder ([`DataDir`](../server/configuration/core-configuration#datadir) setting) +- The logs folder ([`Logs.Path`](../server/configuration/logs-configuration#logs.path) setting) + +If you intend to run as a service, the write permissions should be granted to the user running the service (e.g. "Local Service"). + +{NOTE/} + +--- + +### Studio + +{SAFE: Free} + +Our GUI, the RavenDB Management Studio, comes **free** with **every license type**: + +- Community +- Professional +- Enterprise + +{SAFE/} + +After installation and setup, the Studio can be accessed via the browser using the `ServerUrl` or the `ServerPublicUrl` value e.g. `http://localhost:8080`. + +--- + +### Security Concerns + +To let a developer start coding an application quickly, RavenDB will run with the following default security mode: + +{WARNING: Default Security Mode} + +As long as the database is used inside the local machine and no outside connections are allowed, you can ignore security concerns +and you require no authentication. Once you set RavenDB to listen to connections outside your local machine, +your database will immediately block this now vulnerable configuration and require the administrator to properly setup the security and +access control to prevent unauthorized access to your data or to explicitly allow the unsecured configuration. + +{WARNING/} + +**We recommend using the 'Setup Wizard' to easily install RavenDB securely from the very start.** + +Read more about security and how to enable authentication [here](../server/security/overview). + +{PANEL/} + +{PANEL: Client} + +After your server is up and running, to write an application you need to acquire one of the `Client` access libraries: + +- .NET from [NuGet](https://www.nuget.org/packages/RavenDB.Client/) +- Java from [Maven](https://search.maven.org/#search%7Cga%7C1%7Cg%3A%22net.ravendb%22%20AND%20a%3A%22ravendb%22) +- Node.js from [NPM](https://www.npmjs.com/package/ravendb) +- Python from [PyPi](https://pypi.org/project/pyravendb/) +- [Ruby](https://github.com/ravendb/ravendb-ruby-client) +- [Go](https://github.com/ravendb/ravendb-go-client) + +
+ +### DocumentStore + +In order to start, you need to create an instance of the `DocumentStore` - the main entry point for your application which is responsible for establishing and managing connections between a RavenDB server (or cluster) and your application. + +{INFO: Examples} + +Before proceeding to the examples, we would like to point out that most of the articles are using the `Northwind` database. You can read more about it and how to deploy it [here](../studio/database/tasks/create-sample-data). + +{INFO/} + +{CODE-TABS} +{CODE-TAB:csharp:C# client_1@Start/GettingStarted.cs /} +{CODE-TAB:java:Java client_1@Start\GettingStarted.java /} +{CODE-TAB:nodejs:Node.js client_1@start\gettingStarted.js /} +{CODE-TAB:python:Python client_1@start\getting_started.py /} +{CODE-TABS/} + +{INFO: Singleton} + +The `DocumentStore` is capable of working with multiple databases and for proper operation we **recommend** having only one instance of it per application. + +{INFO/} + +The following articles can extend your knowledge about the `DocumentStore` and its configuration: + +- [What is a Document Store?](../client-api/what-is-a-document-store) +- [How to Create a Document Store?](../client-api/creating-document-store) +- [How to Setup a Default Database?](../client-api/setting-up-default-database) +- [How to configure the Document Store using Conventions?](../client-api/configuration/conventions) + +
+ +### Session + +The `Session` is used to manipulate the data. It implements the `Unit of Work` pattern and is capable of batching the requests to save expensive remote calls. In contrast to a `DocumentStore` it is a lightweight object and can be created more frequently. For example, in web applications, a common (and recommended) pattern is to create a session per request. + +### Example I - Storing + +RavenDB is a Document Database. All stored objects are called `documents`. Each document contains a **unique ID** that identifies it, **data** and adjacent **metadata**, both stored in JSON format. The metadata contains information describing the document, e.g. the last modification date (`@last-modified` property) or the [collection](../client-api/faq/what-is-a-collection) (`@collection` property) assignment. + +{CODE-TABS} +{CODE-TAB:csharp:C# client_2@Start/GettingStarted.cs /} +{CODE-TAB:java:Java client_2@Start\GettingStarted.java /} +{CODE-TAB:nodejs:Node.js client_2@start\gettingStarted.js /} +{CODE-TAB:python:Python client_2@start\getting_started.py /} +{CODE-TABS/} + +### Example II - Loading + +The `Session` was designed to help the user write efficient code easily. For example, when a document is being loaded (`.Load`) from the server, there is an option to retrieve additional documents in the same request (using `.Include`), keeping the number of expensive calls to minimum. + +Besides that, the session implements the `Unit of Work` pattern, meaning that all **changes** to loaded entities are **automatically tracked**. The `SaveChanges` call will synchronize (with the server) **only the documents that have changed within the session**. **All of those changes are sent in one request (saving network calls)** and **processed in one transaction** (you can read why RavenDB is an **ACID database** [here](../client-api/faq/transaction-support)). + +{CODE-TABS} +{CODE-TAB:csharp:C# client_3@Start/GettingStarted.cs /} +{CODE-TAB:java:Java client_3@Start\GettingStarted.java /} +{CODE-TAB:nodejs:Node.js client_3@start\gettingStarted.js /} +{CODE-TAB:python:Python client_3@start\getting_started.py /} +{CODE-TABS/} + +### Example III - Querying + +To satisfy queries, indexes are used. From the querying perspective, an index defines which document fields can be used to find a document. The whole indexing process is done asynchronously, which gives very quick querying response times, even when large amounts of data have been changed. However, an implication of this approach is that the index might be [stale](../indexes/stale-indexes). + +When no index is specified in the query (like in the query below), RavenDB will use its **intelligent auto-indexes** feature that will either use an already existing index or create a new one if no match is found. The other option is to write the index yourself and deploy it to the server. Those indexes are called [Static Indexes](../indexes/creating-and-deploying#static-indexes). + +Behind the scenes, queries are translated to the Raven Query Language (RQL) syntax. Read more about RQL [here](../indexes/querying/what-is-rql). + +{CODE-TABS} +{CODE-TAB:csharp:C# client_4@Start/GettingStarted.cs /} +{CODE-TAB:java:Java client_4@Start\GettingStarted.java /} +{CODE-TAB:nodejs:Node.js client_4@start\gettingStarted.js /} +{CODE-TAB:python:Python client_4@start\getting_started.py /} +{CODE-TAB-BLOCK:sql:RQL} +from Products +where UnitsInStock > 5 +select Name +{CODE-TAB-BLOCK/} +{CODE-TABS/} + +The following articles can extend your knowledge about the `Session`: + +- [What is a Session and how does it work?](../client-api/session/what-is-a-session-and-how-does-it-work) +- [Opening a Session](../client-api/session/opening-a-session) +- [Storing Entities](../client-api/session/storing-entities) +- [Deleting Entities](../client-api/session/deleting-entities) +- [Loading Entities](../client-api/session/loading-entities) +- [Saving Changes](../client-api/session/saving-changes) + +The introductory articles describing `Querying` can be found here: + +- [Basics](../indexes/querying/basics) +- [What is RQL?](../indexes/querying/what-is-rql) + +If you wish to understand `Indexes` better, we recommend reading the following articles: + +- [Indexes: What are indexes?](../indexes/what-are-indexes) +- [Indexes: Creating and deploying indexes?](../indexes/creating-and-deploying) +- [Indexes: Indexing basics](../indexes/indexing-basics) +- [Indexes: Map indexes](../indexes/map-indexes) + +{PANEL/} + +## Related Articles + +### Installation + +- [Setup Wizard](../start/installation/setup-wizard) +- [System Requirements](../start/installation/system-requirements) + +### Client API + +- [What is a Session and How Does it Work](../client-api/session/what-is-a-session-and-how-does-it-work) +- [Opening a Session](../client-api/session/opening-a-session) +- [Storing Entities](../client-api/session/storing-entities) +- [Deleting Entities](../client-api/session/deleting-entities) +- [Loading Entities](../client-api/session/loading-entities) +- [Saving Changes](../client-api/session/saving-changes) + +### Querying + +- [Basics](../indexes/querying/basics) +- [What is RQL](../indexes/querying/what-is-rql) + +### Indexes + +- [What are Indexes](../indexes/what-are-indexes) +- [Creating and Deploying Indexes](../indexes/creating-and-deploying) +- [Indexing Basics](../indexes/indexing-basics) +- [Map Indexes](../indexes/map-indexes) diff --git a/Documentation/4.2/Samples/csharp/Raven.Documentation.Samples/Start/GettingStarted.cs b/Documentation/4.2/Samples/csharp/Raven.Documentation.Samples/Start/GettingStarted.cs new file mode 100644 index 0000000000..ccc5b30008 --- /dev/null +++ b/Documentation/4.2/Samples/csharp/Raven.Documentation.Samples/Start/GettingStarted.cs @@ -0,0 +1,181 @@ +using System.Collections.Generic; +using System.Linq; +using Raven.Client.Documents; +using Raven.Client.Documents.Indexes; +using Raven.Client.Documents.Session; +using Raven.Documentation.Samples.Orders; +using Xunit; + +namespace Raven.Documentation.Samples.Start +{ + public class GettingStarted + { + public void T1() + { + #region start_1 + using (IDocumentStore store = new DocumentStore + { + Urls = new[] { "http://localhost:8080/" }, // server URL + Database = "Northwind" // default database + }) + { + store.Initialize(); // initializes document store, by connecting to server and downloading various configurations + + using (IDocumentSession session = store.OpenSession()) // opens a session that will work in context of 'DefaultDatabase' + { + Employee employee = new Employee + { + FirstName = "John", + LastName = "Doe" + }; + + session.Store(employee); // stores employee in session, assigning it to a collection `Employees` + string employeeId = employee.Id; // Session.Store will assign Id to employee, if it is not set + + session.SaveChanges(); // sends all changes to server + + // Session implements Unit of Work pattern, + // therefore employee instance would be the same and no server call will be made + Employee loadedEmployee = session.Load(employeeId); + Assert.Equal(employee, loadedEmployee); + } + } + #endregion + } + + #region start_2 + /// + /// All _ in index class names will be converted to / + /// it means that Employees_ByFirstNameAndLastName will be Employees/ByFirstNameAndLastName + /// when deployed to server + /// + /// AbstractIndexCreationTask is a helper class that gives you strongly-typed syntax + /// for creating indexes + /// + public class Employees_ByFirstNameAndLastName : AbstractIndexCreationTask + { + public Employees_ByFirstNameAndLastName() + { + // this is a simple (Map) index LINQ-flavored mapping function + // that enables searching of Employees by + // FirstName, LastName (or both) + Map = employees => from employee in employees + select new + { + FirstName = employee.FirstName, + LastName = employee.LastName + }; + } + } + #endregion + + public void T2() + { + #region start_2 + using (IDocumentStore store = new DocumentStore + { + Urls = new[] { "http://localhost:8080/" }, // server URL + Database = "Northwind" // default database + }) + { + store.Initialize(); // initializes document store, by connecting to server and downloading various configurations + + new Employees_ByFirstNameAndLastName().Execute(store); // deploying index to server + + using (IDocumentSession session = store.OpenSession()) // opens a session that will work in context of 'DefaultDatabase' + { + List employees = session.Query() // returning object of type Employee, using Employees/ByFirstNameAndLastName index + .Where(x => x.FirstName == "Robert") // predicates (can only use fields that index defines, so FirstName, LastName or both) + .ToList(); // materializing query - sending to server + } + } + #endregion + } + + public void Sample() + { + #region client_1 + using (IDocumentStore store = new DocumentStore + { + Urls = new[] // URL to the Server, + { // or list of URLs + "http://live-test.ravendb.net" // to all Cluster Servers (Nodes) + }, + Database = "Northwind", // Default database that DocumentStore will interact with + Conventions = { } // DocumentStore customizations + }) + { + store.Initialize(); // Each DocumentStore needs to be initialized before use. + // This process establishes the connection with the Server + // and downloads various configurations + // e.g. cluster topology or client configuration + } + #endregion + } + + public void Sample2() + { + using (var store = new DocumentStore()) + { + #region client_2 + using (IDocumentSession session = store.OpenSession()) // Open a session for a default 'Database' + { + Category category = new Category + { + Name = "Database Category" + }; + + session.Store(category); // Assign an 'Id' and collection (Categories) + // and start tracking an entity + + Product product = new Product + { + Name = "RavenDB Database", + Category = category.Id, + UnitsInStock = 10 + }; + + session.Store(product); // Assign an 'Id' and collection (Products) + // and start tracking an entity + + session.SaveChanges(); // Send to the Server + // one request processed in one transaction + } + #endregion + + string productId = string.Empty; + + #region client_3 + using (IDocumentSession session = store.OpenSession()) // Open a session for a default 'Database' + { + Product product = session + .Include(x => x.Category) // Include Category + .Load(productId); // Load the Product and start tracking + + Category category = session + .Load(product.Category); // No remote calls, + // Session contains this entity from .Include + + product.Name = "RavenDB"; // Apply changes + category.Name = "Database"; + + session.SaveChanges(); // Synchronize with the Server + // one request processed in one transaction + } + #endregion + + #region client_4 + using (IDocumentSession session = store.OpenSession()) // Open a session for a default 'Database' + { + List productNames = session + .Query() // Query for Products + .Where(x => x.UnitsInStock > 5) // Filter + .Skip(0).Take(10) // Page + .Select(x => x.Name) // Project + .ToList(); // Materialize query + } + #endregion + } + } + } +} diff --git a/Documentation/4.2/Samples/java/src/test/java/net/ravendb/Start/GettingStarted.java b/Documentation/4.2/Samples/java/src/test/java/net/ravendb/Start/GettingStarted.java new file mode 100644 index 0000000000..1769e63fea --- /dev/null +++ b/Documentation/4.2/Samples/java/src/test/java/net/ravendb/Start/GettingStarted.java @@ -0,0 +1,139 @@ +package net.ravendb.start; + +import net.ravendb.client.documents.DocumentStore; +import net.ravendb.client.documents.IDocumentStore; +import net.ravendb.client.documents.conventions.DocumentConventions; +import net.ravendb.client.documents.session.IDocumentSession; + +import java.util.List; + +public class GettingStarted { + public void foo() { + + //region client_1 + try (IDocumentStore store = new DocumentStore( + new String[]{ "http://live-test.ravendb.net" }, // URL to the Server, + // or list of URLs + // to all Cluster Servers (Nodes) + "Northwind") // Default database that DocumentStore will interact with + ) { + + DocumentConventions conventions = store.getConventions(); // DocumentStore customizations + + store.initialize(); // Each DocumentStore needs to be initialized before use. + // This process establishes the connection with the Server + // and downloads various configurations + // e.g. cluster topology or client configuration + } + //endregion + + { + IDocumentStore store = new DocumentStore(); + //region client_2 + try (IDocumentSession session = store.openSession()) { // Open a session for a default 'Database' + Category category = new Category(); + category.setName("Database Category"); + + session.store(category); // Assign an 'Id' and collection (Categories) + // and start tracking an entity + + Product product = new Product(); + product.setName("RavenDB Database"); + product.setCategory(category.getId()); + product.setUnitsInStock(10); + + session.store(product); // Assign an 'Id' and collection (Products) + // and start tracking an entity + + session.saveChanges(); // Send to the Server + // one request processed in one transaction + } + //endregion + } + + + IDocumentStore store = null; + String productId = null; + + //region client_3 + try (IDocumentSession session = store.openSession()) { // Open a session for a default 'Database' + Product product = session + .include("Category") // Include Category + .load(Product.class, productId); // Load the Product and start tracking + + Category category = session + .load(Category.class, // No remote calls, + product.getCategory()); // Session contains this entity from .include + + product.setName("RavenDB"); // Apply changes + category.setName("Database"); + + + session.saveChanges(); // Synchronize with the Server + // one request processed in one transaction + } + //endregion + + //region client_4 + try (IDocumentSession session = store.openSession()) { // Open a session for a default 'Database' + List productNames = session + .query(Product.class) // Query for Products + .whereGreaterThan("UnitsInStock", 5) // Filter + .skip(0).take(10) // Page + .selectFields(String.class, "Name") // Project + .toList(); // Materialize query + } + //endregion + } + + private static class Product { + private String name; + private String category; + private int unitsInStock; + + public String getCategory() { + return category; + } + + public void setCategory(String category) { + this.category = category; + } + + public int getUnitsInStock() { + return unitsInStock; + } + + public void setUnitsInStock(int unitsInStock) { + this.unitsInStock = unitsInStock; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + } + + private static class Category { + private String id; + private String name; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + } +} diff --git a/Documentation/4.2/Samples/nodejs/start/gettingStarted.js b/Documentation/4.2/Samples/nodejs/start/gettingStarted.js new file mode 100644 index 0000000000..6b0fc9df16 --- /dev/null +++ b/Documentation/4.2/Samples/nodejs/start/gettingStarted.js @@ -0,0 +1,90 @@ +//region client_1 +import { DocumentStore } from "ravendb"; + +const store = new DocumentStore( + ["http://live-test.ravendb.net"], // URL to the Server + // or list of URLs + // to all Cluster Servers (Nodes) + + "Northwind"); // Default database that DocumentStore will interact with + +const conventions = store.conventions; // DocumentStore customizations + +store.initialize(); // Each DocumentStore needs to be initialized before use. + // This process establishes the connection with the Server + // and downloads various configurations + // e.g. cluster topology or client configuration + +store.dispose(); // Dispose the resources claimed by the DocumentStore +//endregion + +class Category { + constructor(name) { + this.Id = null; + this.Name = name; + } +} + +class Product { + constructor(name, categoryId, unitsInStock) { + this.Id = null; + this.Name = name; + this.Category = categoryId; + this.UnitsInStock = unitsInStock; + } +} + +async function c2() { + //region client_2 + const session = store.openSession(); // Open a session for a default 'Database' + + const category = new Category("Database Category"); + + await session.store(category); // Assign an 'Id' and collection (Categories) + // and start tracking an entity + + const product = new Product( + "RavenDB Database", + category.Id, + 10); + + await session.store(product); // Assign an 'Id' and collection (Products) + // and start tracking an entity + + await session.saveChanges(); // Send to the Server + // one request processed in one transaction + //endregion +} + +async function c22(productId) { + //region client_3 + const session = store.openSession(); // Open a session for a default 'Database' + + const product = await session + .include("Category") // Include Category + .load(productId); // Load the Product and start tracking + + const category = await session + .load(product.Category); // No remote calls, + // Session contains this entity from .include + + product.Name = "RavenDB"; // Apply changes + category.Name = "Database"; + + await session.saveChanges(); // Synchronize with the Server + // one request processed in one transaction + //endregion +} + +async function c3() { + //region client_4 + const session = store.openSession(); // Open a session for a default 'Database' + + const productNames = await session + .query({ collection: "Products" }) // Query for Products + .whereGreaterThan("UnitsInStock", 5) // Filter + .skip(0).take(10) // Page + .selectFields("Name") // Project + .all(); // Materialize query + //endregion +}