From c000277e5a3b054a2fdaf53d4c93f33e58b4bc1e Mon Sep 17 00:00:00 2001 From: "Matthew D. Groves" Date: Wed, 12 May 2021 15:32:53 -0400 Subject: [PATCH] upgrading couchbase SDK, some other minor tweaks/improvements --- SqlServerToCouchbase/SqlServerToCouchbase.csproj | 2 +- .../Controllers/PersonController.cs | 12 +++++++----- WebApiExample.Couchbase/Models/Db/Indexes.n1ql | 5 +++++ WebApiExample.Couchbase/Startup.cs | 7 ++++++- 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/SqlServerToCouchbase/SqlServerToCouchbase.csproj b/SqlServerToCouchbase/SqlServerToCouchbase.csproj index 115d645..48a6103 100644 --- a/SqlServerToCouchbase/SqlServerToCouchbase.csproj +++ b/SqlServerToCouchbase/SqlServerToCouchbase.csproj @@ -10,7 +10,7 @@ - + diff --git a/WebApiExample.Couchbase/Controllers/PersonController.cs b/WebApiExample.Couchbase/Controllers/PersonController.cs index 33e5049..3eb6c32 100644 --- a/WebApiExample.Couchbase/Controllers/PersonController.cs +++ b/WebApiExample.Couchbase/Controllers/PersonController.cs @@ -56,8 +56,9 @@ public async Task GetPersonByIdRawAsync(int personId) { var bucket = await _bucketProvider.GetBucketAsync(); var cluster = bucket.Cluster; - var personResult = await cluster.QueryAsync(@" - SELECT p.* FROM AdventureWorks2016.Person.Person p WHERE p.BusinessEntityID = $personId", + var bucketName = bucket.Name; + var personResult = await cluster.QueryAsync($@" + SELECT p.* FROM `{bucketName}`.Person.Person p WHERE p.BusinessEntityID = $personId", new QueryOptions().Parameter("personId", personId)); return Ok(await personResult.Rows.SingleOrDefaultAsync()); } @@ -67,10 +68,11 @@ public async Task GetPersonByIdExpandedAsync(int personId) { var bucket = await _bucketProvider.GetBucketAsync(); var cluster = bucket.Cluster; - var personResult = await cluster.QueryAsync(@" + var bucketName = bucket.Name; + var personResult = await cluster.QueryAsync($@" SELECT p.*, EmailAddresses - FROM AdventureWorks2016.Person.Person p - NEST AdventureWorks2016.Person.EmailAddress EmailAddresses ON EmailAddresses.BusinessEntityID = p.BusinessEntityID + FROM `{bucketName}`.Person.Person p + LEFT NEST `{bucketName}`.Person.EmailAddress EmailAddresses ON EmailAddresses.BusinessEntityID = p.BusinessEntityID WHERE p.BusinessEntityID = $personId", new QueryOptions().Parameter("personId", personId)); return Ok(await personResult.Rows.SingleOrDefaultAsync()); diff --git a/WebApiExample.Couchbase/Models/Db/Indexes.n1ql b/WebApiExample.Couchbase/Models/Db/Indexes.n1ql index fc733a8..93cff17 100644 --- a/WebApiExample.Couchbase/Models/Db/Indexes.n1ql +++ b/WebApiExample.Couchbase/Models/Db/Indexes.n1ql @@ -11,3 +11,8 @@ CREATE INDEX sql_DiscontinuedDate_SellEndDate_ListPrice CREATE INDEX sql_ComponentID ON `default`:`AdventureWorks2016`.`Production`.`BillOfMaterials`(`ComponentID`); + +-- use these indexes when demoing, in case you don't want to wait for ALL adventureworks indexes to get copied over +-- but note that when using sampleData, the corresponding EmailAddress might not have been copied over +CREATE INDEX ix_Person_BusinessEntityID ON `AdventureWorks2016`.`Person`.`Person`(`BusinessEntityID`); +CREATE INDEX ix_EmailAddress_BusinessEntityID ON `AdventureWorks2016`.`Person`.`EmailAddress`(`BusinessEntityID`); \ No newline at end of file diff --git a/WebApiExample.Couchbase/Startup.cs b/WebApiExample.Couchbase/Startup.cs index 0f010e6..8166fa8 100644 --- a/WebApiExample.Couchbase/Startup.cs +++ b/WebApiExample.Couchbase/Startup.cs @@ -31,7 +31,7 @@ public void ConfigureServices(IServiceCollection services) } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IWebHostEnvironment env) + public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime appLifetime) { if (env.IsDevelopment()) { @@ -50,6 +50,11 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { endpoints.MapControllers(); }); + + appLifetime.ApplicationStopped.Register(() => + { + app.ApplicationServices.GetRequiredService().Close(); + }); } } }