Skip to content

Commit

Permalink
upgrading couchbase SDK, some other minor tweaks/improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
mgroves committed May 12, 2021
1 parent c2bd069 commit c000277
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
2 changes: 1 addition & 1 deletion SqlServerToCouchbase/SqlServerToCouchbase.csproj
Expand Up @@ -10,7 +10,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CouchbaseNetClient" Version="3.1.3" />
<PackageReference Include="CouchbaseNetClient" Version="3.1.4" />
<PackageReference Include="Dapper" Version="2.0.78" />
<PackageReference Include="Dynamitey" Version="2.0.10.189" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.1.1" />
Expand Down
12 changes: 7 additions & 5 deletions WebApiExample.Couchbase/Controllers/PersonController.cs
Expand Up @@ -56,8 +56,9 @@ public async Task<IActionResult> GetPersonByIdRawAsync(int personId)
{
var bucket = await _bucketProvider.GetBucketAsync();
var cluster = bucket.Cluster;
var personResult = await cluster.QueryAsync<Person>(@"
SELECT p.* FROM AdventureWorks2016.Person.Person p WHERE p.BusinessEntityID = $personId",
var bucketName = bucket.Name;
var personResult = await cluster.QueryAsync<Person>($@"
SELECT p.* FROM `{bucketName}`.Person.Person p WHERE p.BusinessEntityID = $personId",
new QueryOptions().Parameter("personId", personId));
return Ok(await personResult.Rows.SingleOrDefaultAsync());
}
Expand All @@ -67,10 +68,11 @@ public async Task<IActionResult> GetPersonByIdExpandedAsync(int personId)
{
var bucket = await _bucketProvider.GetBucketAsync();
var cluster = bucket.Cluster;
var personResult = await cluster.QueryAsync<Person>(@"
var bucketName = bucket.Name;
var personResult = await cluster.QueryAsync<Person>($@"
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());
Expand Down
5 changes: 5 additions & 0 deletions WebApiExample.Couchbase/Models/Db/Indexes.n1ql
Expand Up @@ -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`);
7 changes: 6 additions & 1 deletion WebApiExample.Couchbase/Startup.cs
Expand Up @@ -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())
{
Expand All @@ -50,6 +50,11 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
endpoints.MapControllers();
});

appLifetime.ApplicationStopped.Register(() =>
{
app.ApplicationServices.GetRequiredService<ICouchbaseLifetimeService>().Close();
});
}
}
}

0 comments on commit c000277

Please sign in to comment.