Created
December 23, 2020 21:24
-
-
Save cmaneu/245591933521f67177f267c7b2b1556f to your computer and use it in GitHub Desktop.
SQL Serverless Data Generation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE [dbo].[Customers] ( | |
[CustomerID] UNIQUEIDENTIFIER NOT NULL, | |
[FirstName] NVARCHAR (MAX) NOT NULL, | |
[LastName] NVARCHAR (MAX) NOT NULL, | |
[Email] NVARCHAR (MAX) NOT NULL, | |
[RegisteredAt] DATETIME NOT NULL, | |
CONSTRAINT [PK_dbo.Customers] PRIMARY KEY CLUSTERED ([CustomerID]) | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Data; | |
using System.Data.SqlClient; | |
using System.IO; | |
using System.Linq; | |
using System.Threading.Tasks; | |
using Bogus; | |
using Dapper; | |
using Microsoft.AspNetCore.Mvc; | |
using Microsoft.Azure.WebJobs; | |
using Microsoft.Azure.WebJobs.Extensions.Http; | |
using Microsoft.AspNetCore.Http; | |
using Microsoft.Extensions.Logging; | |
using Newtonsoft.Json; | |
namespace DemoResilient | |
{ | |
public static class Function1 | |
{ | |
[FunctionName("Home")] | |
public static async Task<IActionResult> Home( | |
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "/")] HttpRequest req, | |
ILogger log) | |
{ | |
log.LogInformation("C# HTTP trigger function processed a request."); | |
return new OkObjectResult( | |
new | |
{ | |
location= Environment.GetEnvironmentVariable("Location"), | |
fabricRepId= Environment.GetEnvironmentVariable("Fabric_ReplicaId"), | |
lastCustomers = await GetLastCustomers() | |
} | |
); | |
} | |
[FunctionName("Generate")] | |
public static async Task<IActionResult> Generate( | |
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "gen")] HttpRequest req, | |
ILogger log) | |
{ | |
log.LogInformation("Generate"); | |
var parameters = req.GetQueryParameterDictionary(); | |
var numberofNewCustomers = parameters.ContainsKey("count") ? Int32.Parse(parameters["count"]) : 10; | |
using (IDbConnection db = new SqlConnection(Environment.GetEnvironmentVariable("CUSTOMERDB_CONNECTIONSTRING"))) | |
{ | |
var testUsers = new Faker<Customer>() | |
.RuleFor(u => u.Id, f => Guid.NewGuid()) | |
.RuleFor(u => u.FirstName, (f, u) => f.Name.FirstName()) | |
.RuleFor(u => u.LastName, (f, u) => f.Name.LastName()) | |
.RuleFor(u => u.Email, (f, u) => f.Internet.Email(u.FirstName, u.LastName)) | |
.RuleFor(u => u.RegisteredAt, f => DateTime.UtcNow); | |
List<Customer> customers = new List<Customer>(numberofNewCustomers); | |
for (int i = 0; i < numberofNewCustomers; i++) | |
{ | |
var customer = testUsers.Generate(); | |
customers.Add(customer); | |
} | |
await db.ExecuteAsync("INSERT INTO [dbo].[Customers] VALUES (@Id, @FirstName, @LastName, @Email, @RegisteredAt)", customers); | |
} | |
return new OkObjectResult("ok"); | |
} | |
public static async Task<IEnumerable<Customer>> GetLastCustomers() | |
{ | |
using (IDbConnection db = new SqlConnection(Environment.GetEnvironmentVariable("CUSTOMERDB_CONNECTIONSTRING"))) | |
{ | |
return await db.QueryAsync<Customer>("Select TOP 5 [CustomerID], FirstName, LastName, Email, RegisteredAt From Customers ORDER BY RegisteredAt DESC"); | |
} | |
} | |
} | |
public class Customer | |
{ | |
public Guid Id { get; set; } | |
public string FirstName { get; set; } | |
public string LastName { get; set; } | |
public string Email { get; set; } | |
public DateTime RegisteredAt { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment