From 2188003b5f25c0b4b19278497830fe62652a1355 Mon Sep 17 00:00:00 2001 From: Max Knor Date: Tue, 21 Jun 2016 15:12:52 +0200 Subject: [PATCH 1/7] Added custom DB ATTACH support to the start.ps1 as well as dockerfile. Changes to be committed: modified: dockerfile modified: start.ps1 --- .../dockerfile | 5 +-- .../start.ps1 | 32 +++++++++++++++++-- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/samples/manage/windows-containers/mssql-server-2014-express-windows/dockerfile b/samples/manage/windows-containers/mssql-server-2014-express-windows/dockerfile index 8bd3e52b..e76236a8 100644 --- a/samples/manage/windows-containers/mssql-server-2014-express-windows/dockerfile +++ b/samples/manage/windows-containers/mssql-server-2014-express-windows/dockerfile @@ -12,6 +12,7 @@ MAINTAINER Perry Skountrianos # set environment variables ENV sql_express_download_url "https://download.microsoft.com/download/1/5/6/156992E6-F7C7-4E55-833D-249BD2348138/ENU/x64/SQLEXPR_x64_ENU.exe" ENV sa_password _ +ENV attach_dbs "[]" # make install files accessible COPY . / @@ -22,9 +23,9 @@ RUN powershell -Command (New-Object System.Net.WebClient).DownloadFile('%sql_exp RUN powershell -Command \ set-strictmode -version latest ; \ - stop-service MSSQL`$%sqlinstance% ; \ + stop-service MSSQL`$SQLEXPRESS ; \ set-itemproperty -path 'HKLM:\software\microsoft\microsoft sql server\mssql12.SQLEXPRESS\mssqlserver\supersocketnetlib\tcp\ipall' -name tcpdynamicports -value '' ; \ set-itemproperty -path 'HKLM:\software\microsoft\microsoft sql server\mssql12.SQLEXPRESS\mssqlserver\supersocketnetlib\tcp\ipall' -name tcpport -value 1433 ; \ set-itemproperty -path 'HKLM:\software\microsoft\microsoft sql server\mssql12.SQLEXPRESS\mssqlserver\' -name LoginMode -value 2 ; -CMD powershell ./start %sa_password% \ No newline at end of file +CMD powershell ./start -sa_password %sa_password% -attach_dbs \"%attach_dbs%\" \ No newline at end of file diff --git a/samples/manage/windows-containers/mssql-server-2014-express-windows/start.ps1 b/samples/manage/windows-containers/mssql-server-2014-express-windows/start.ps1 index 1fa234ab..f28672f1 100644 --- a/samples/manage/windows-containers/mssql-server-2014-express-windows/start.ps1 +++ b/samples/manage/windows-containers/mssql-server-2014-express-windows/start.ps1 @@ -1,17 +1,43 @@ -# The script sets the sa password and start the SQl Service +# The script sets the sa password and start the SQL Service +# Also it attaches additional database from the disk +# The format for attach_dbs param( [Parameter(Mandatory=$false)] -[string]$sa_password +[string]$sa_password, + +[Parameter(Mandatory=$false)] +[string]$attach_dbs ) # start the service +Write-Verbose "Starting SQL Server" start-service MSSQL`$SQLEXPRESS - if($sa_password -ne "_"){ + Write-Verbose "Changing SA login credentials" $sqlcmd = "ALTER LOGIN sa with password=" +"'" + $sa_password + "'" + ";ALTER LOGIN sa ENABLE;" Invoke-Sqlcmd -Query $sqlcmd -ServerInstance ".\SQLEXPRESS" } +$attach_dbs = $attach_dbs | ConvertFrom-Json + +if ($null -ne $attach_dbs){ + Write-Verbose "Attaching database(s)" + Foreach($db in $attach_dbs) + { + $files = @(); + Foreach($file in $db.dbFiles) + { + $files += "(FILENAME = 'N$($file)')"; + } + + $files = $files -join "," + $sqlcmd = "sp_detach_db $($db.dbName);GO;CREATE DATABASE $($db.dbName) ON $($files) FOR ATTACH ;GO;" + + Write-Host Invoke-Sqlcmd -Query $($sqlcmd) -ServerInstance ".\SQLEXPRESS" + Invoke-Sqlcmd -Query $sqlcmd -ServerInstance ".\SQLEXPRESS" + } +} + while ($true) { Start-Sleep -Seconds 3600 } \ No newline at end of file From 919362cf60235cb43a1be6a1b767437933a155af Mon Sep 17 00:00:00 2001 From: Max Knor Date: Tue, 21 Jun 2016 15:30:12 +0200 Subject: [PATCH 2/7] Added additional comments to readme.md --- .../readme.md | 36 ++++++++++++++++--- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/samples/manage/windows-containers/mssql-server-2014-express-windows/readme.md b/samples/manage/windows-containers/mssql-server-2014-express-windows/readme.md index 24c76d71..16478eac 100644 --- a/samples/manage/windows-containers/mssql-server-2014-express-windows/readme.md +++ b/samples/manage/windows-containers/mssql-server-2014-express-windows/readme.md @@ -17,7 +17,7 @@ Note: This dockerfile is based on Buc Rogers' work that can be found [here] (htt ## About this sample 1. **Applies to:** SQL Server 2014 Express, Windows Server Technical Preview 4 or later -5. **Authors:** Perry Skountrianos [perrysk-msft] +5. **Authors:** Perry Skountrianos [perrysk-msft], Max Knor [knom] @@ -27,16 +27,44 @@ To run this sample, you need the following prerequisites. **Software prerequisites:** -You can run the container with the following command. Note the you'll need Windows Server Core TP5 v10.0.14300.1000. -docker run -p 1433:1433 --env sa_password= microsoft/mssql-server-2014-express-windows +You can run the container with the following command. +(Note the you'll need Windows Server Core TP5 v10.0.14300.1000.) + +```` +docker run -p 1433:1433 -v C:/temp/:C:/temp/ --env sa_password= --env attach_dbs="" -v microsoft/mssql-server-2014-express-windows +```` + +- **-p HostPort:containerPort** is for port-mapping a container network port to a host port. +- **-v HostPath:containerPath** is for mounting a folder from the host inside the container. + + This can be used for saving database outside of the container. ## Run this sample -The image provides one environment variable to set the sa password:
+The image provides two environment variables to optionally set:
- sa_password: Sets the sa password and enables the sa login +- attach_dbs: The configuration for attaching custom DBs (.mdf, .ldf files). + This should be a JSON string, formed like the following (note the SINGLE quotes and everything in one line): + ``` +[{'dbName':'MaxDb','dbFiles':['C:\\temp\\maxtest.mdf','C:\\temp\\maxtest_log. +ldf']},{'dbName':'PerryDb','dbFiles':['C:\\temp\\perrytest.mdf','C:\\temp\\perrytest_log. +ldf']}] + ``` + There can be zero to many databases in the array. + - dbName: The name of the database + - dbFiles: An array of absolute paths to the .MDF and .LDF files. + + Can be one or many, note that the path has double backslashes for escaping! + +This example shows all parameters in action: +``` +docker run -p 1433:1433 -v C:/temp/:C:/temp/ --env attach_dbs="[{'dbName':'MaxTest','dbFiles':['C:\\temp\\maxtest.mdf','C:\\temp\\maxtest_log. +ldf']}]" sqlexpr +``` + ## Sample details From 7acd16b67cbf21825e999507bb2d8e43498f9526 Mon Sep 17 00:00:00 2001 From: Max K Date: Tue, 21 Jun 2016 15:32:17 +0200 Subject: [PATCH 3/7] Updated readme.md --- .../mssql-server-2014-express-windows/readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/manage/windows-containers/mssql-server-2014-express-windows/readme.md b/samples/manage/windows-containers/mssql-server-2014-express-windows/readme.md index 16478eac..6b194926 100644 --- a/samples/manage/windows-containers/mssql-server-2014-express-windows/readme.md +++ b/samples/manage/windows-containers/mssql-server-2014-express-windows/readme.md @@ -62,7 +62,7 @@ ldf']}] This example shows all parameters in action: ``` docker run -p 1433:1433 -v C:/temp/:C:/temp/ --env attach_dbs="[{'dbName':'MaxTest','dbFiles':['C:\\temp\\maxtest.mdf','C:\\temp\\maxtest_log. -ldf']}]" sqlexpr +ldf']}]" microsoft/mssql-server-2014-express-windows ``` From 390b476f40718455d85d73ad74013c0025aaaa7d Mon Sep 17 00:00:00 2001 From: Max Knor Date: Tue, 21 Jun 2016 16:48:52 +0200 Subject: [PATCH 4/7] Update start.ps1 --- .../mssql-server-2014-express-windows/dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/manage/windows-containers/mssql-server-2014-express-windows/dockerfile b/samples/manage/windows-containers/mssql-server-2014-express-windows/dockerfile index e76236a8..2c3d1655 100644 --- a/samples/manage/windows-containers/mssql-server-2014-express-windows/dockerfile +++ b/samples/manage/windows-containers/mssql-server-2014-express-windows/dockerfile @@ -28,4 +28,4 @@ RUN powershell -Command \ set-itemproperty -path 'HKLM:\software\microsoft\microsoft sql server\mssql12.SQLEXPRESS\mssqlserver\supersocketnetlib\tcp\ipall' -name tcpport -value 1433 ; \ set-itemproperty -path 'HKLM:\software\microsoft\microsoft sql server\mssql12.SQLEXPRESS\mssqlserver\' -name LoginMode -value 2 ; -CMD powershell ./start -sa_password %sa_password% -attach_dbs \"%attach_dbs%\" \ No newline at end of file +CMD powershell ./start -sa_password %sa_password% -attach_dbs \"%attach_dbs%\" -Verbose \ No newline at end of file From 338cf90f52918ac255c32495c8758e996b620a41 Mon Sep 17 00:00:00 2001 From: Jos de Bruijn Date: Tue, 21 Jun 2016 12:47:36 -0700 Subject: [PATCH 5/7] Added Code of Conduct --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 7671a409..eb66a452 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,9 @@ To work in GitHub, go to https://github.com/microsoft/sql-server-samples and for Each sample should be in its own folder with a README.md file that follows the [template](README_samples_template.md). Generated files (e.g., .exe or .bacpac) and user configuration settings (e.g., .user) should not be committed to GitHub. +## Code of Conduct +This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments. + ## License These samples and templates are all licensed under the MIT license. See the license.txt file in the root. From 09786217eeb9ab6540fd226a726f5d3adc0ac1d9 Mon Sep 17 00:00:00 2001 From: Jovan Popovic Date: Wed, 22 Jun 2016 13:31:31 +0200 Subject: [PATCH 6/7] Added missing www file bin/www file was ignored in .gitignore. Added missing file. --- .../json/todo-app/nodejs-express4-rest-api/.gitignore | 2 +- .../json/todo-app/nodejs-express4-rest-api/bin/www | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 samples/features/json/todo-app/nodejs-express4-rest-api/bin/www diff --git a/samples/features/json/todo-app/nodejs-express4-rest-api/.gitignore b/samples/features/json/todo-app/nodejs-express4-rest-api/.gitignore index ac5caeaf..f9d85749 100644 --- a/samples/features/json/todo-app/nodejs-express4-rest-api/.gitignore +++ b/samples/features/json/todo-app/nodejs-express4-rest-api/.gitignore @@ -1,5 +1,5 @@ node_modules/* -bin/* +bin/*.dll obj/* *.sln *.log \ No newline at end of file diff --git a/samples/features/json/todo-app/nodejs-express4-rest-api/bin/www b/samples/features/json/todo-app/nodejs-express4-rest-api/bin/www new file mode 100644 index 00000000..0f63533b --- /dev/null +++ b/samples/features/json/todo-app/nodejs-express4-rest-api/bin/www @@ -0,0 +1,9 @@ +#!/usr/bin/env node +var debug = require('debug')('nodejs_express4_rest_api'); +var app = require('../app'); + +app.set('port', process.env.PORT || 3000); + +var server = app.listen(app.get('port'), function() { + debug('Express server listening on port ' + server.address().port); +}); From 0e46c20b248eb5ae8f318635809eb02a815bd656 Mon Sep 17 00:00:00 2001 From: Jovan Popovic Date: Wed, 22 Jun 2016 14:51:09 +0200 Subject: [PATCH 7/7] Minor fixes in readme.md Added minor corrections in readme.md files --- .../json/todo-app/dotnet-rest-api/README.md | 9 +++-- .../nodejs-express4-rest-api/README.md | 33 ++++++++++++------- .../nodejs-express4-rest-api/setup.bat | 1 + samples/features/readme.md | 4 +++ 4 files changed, 32 insertions(+), 15 deletions(-) create mode 100644 samples/features/json/todo-app/nodejs-express4-rest-api/setup.bat diff --git a/samples/features/json/todo-app/dotnet-rest-api/README.md b/samples/features/json/todo-app/dotnet-rest-api/README.md index 4807bcc9..181938b7 100644 --- a/samples/features/json/todo-app/dotnet-rest-api/README.md +++ b/samples/features/json/todo-app/dotnet-rest-api/README.md @@ -1,4 +1,4 @@ -# ASP.NET Core REST Web API that uses SQL/JSON functionalites +# ASP.NET Core REST Web API that uses SQL/JSON functionalities This project contains an example implementation of ASP.NET Core REST API with CRUD operations on a simple Todo table. You can learn how to build REST API on the existing database schema using new JSON functionalities that are available in SQL Server 2016 (or higher) and Azure SQL Database. @@ -61,7 +61,7 @@ Service uses built-in JSON functionalities that are available in SQL Server 2016 ## Disclaimers -The code included in this sample is not intended demonstrate some general guidances and arhitectural patterns for web development. It contains minimal code required to create REST API, and it does not use some patterns such as Repository. Sample uses built-in ASP.NET Core Dependency Injection mechanism; however, this is not prerequisite. +The code included in this sample is not intended demonstrate some general guidance and architectural patterns for web development. It contains minimal code required to create REST API, and it does not use some patterns such as Repository. Sample uses built-in ASP.NET Core Dependency Injection mechanism; however, this is not prerequisite. You can easily modify this code to fit the architecture of your application. @@ -70,8 +70,11 @@ You can easily modify this code to fit the architecture of your application. For more information, see this [article](http://www.codeproject.com/Articles/1106622/Building-Web-API-REST-services-on-Azure-SQL-Databa). +## Code of Conduct +This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments. + ## License These samples and templates are all licensed under the MIT license. See the license.txt file in the root. ## Questions -Email questions to: sqlserversamples@microsoft.com. \ No newline at end of file +Email questions to: [sqlserversamples@microsoft.com](mailto: sqlserversamples@microsoft.com). diff --git a/samples/features/json/todo-app/nodejs-express4-rest-api/README.md b/samples/features/json/todo-app/nodejs-express4-rest-api/README.md index 71e16865..46de88c1 100644 --- a/samples/features/json/todo-app/nodejs-express4-rest-api/README.md +++ b/samples/features/json/todo-app/nodejs-express4-rest-api/README.md @@ -1,6 +1,6 @@ -# NodeJS Express4 REST API that uses SQL/JSON functionalites +# NodeJS Express4 REST API that uses SQL/JSON functionalities -This project contains an example implementation of NodeJS REST API with CRUD operations on a simple Todo table. You can learn how to build REST API on the existing database schema using new JSON functionalities that are available in SQL Server 2016 (or higher) and Azure SQL Database. +This project contains an example implementation of NodeJS REST API with CRUD operations on a simple Todo table. You can learn how to build REST API on the existing database schema using NodeJS, Express4, and new JSON functionalities that are available in SQL Server 2016 (or higher) and Azure SQL Database. ### Contents @@ -39,18 +39,23 @@ To run this sample, you need the following prerequisites. ## Run this sample -1. Navigate to the folder where you have downloaded sample and run **npm install** in command window. This command will install necessary npm packages defined in project.json. +1. Navigate to the folder where you have downloaded sample and run **npm install** in command window, or run setup.bat if you are on Windows operating system. This command will install necessary npm packages defined in project.json. -2. From SQL Server Management Studio or Sql Server Data Tools connect to your SQL Server 2016 or Azure SQL database and execute setup.sql script that will create and populate Todo table in the database. +2. From SQL Server Management Studio or SQL Server Data Tools connect to your SQL Server 2016 or Azure SQL database and execute setup.sql script that will create and populate Todo table in the database. 3. From Visual Studio, open the **TodoApp.xproj** file from the root directory, -4. Locate db.js file in the project, change database connection info in createConnection() method to reference your database, and build solution using Ctrl+Shift+B, right-click on project + Build, or Build/Build Solution from menu. +4. Locate db.js file in the project, change database connection info in createConnection() method to reference your database. the following tokens should be replaced: +4.1. SERVERNAME - name of the database server. +4.2. DATABASE - Name of database where Todo table is stored. +4.3. USERNAME - SQL Server login that can access table data and execute stored procedures. +4.4. PASSWORD - Password associated to SQL Server login. -5. Run sample app using F5 or Ctrl+F5, -4.1. Open /api/Todo Url to get all Todo items as a JSON array, -4.2. Open /api/Todo/1 Url to get details about a single Todo item with id 1, -4.3. Send POST, PUT, PATCH, or DELETE Http requests to update content of Todo table. +5. Build project using Ctrl+Shift+B, right-click on project + Build, or Build/Build Solution from menu. + +6. Run sample app using F5 or Ctrl+F5. /todo Url will be opened with a list of all Todo items as a JSON array, +6.1. Open /api/Todo/1 Url to get details about a single Todo item with id 1, +6.2. Send POST, PUT, or DELETE Http requests to update content of Todo table. @@ -58,12 +63,16 @@ To run this sample, you need the following prerequisites. This sample application shows how to create simple REST API service that performs CRUD operations on a simple Todo table. NodeJS REST API is used to implement REST Service in the example. -Service uses built-in JSON functionalities that are available in SQL Server 2016 and Azure SQL Database. +1. app.js file that contains startup code. +2. db.js file that contains functions that wrap Tedious library +3. todo.js file that contains action that will be called on GET, POST, PUT, and DELETE Http requests. + +Service uses Tedious library for data access and built-in JSON functionalities that are available in SQL Server 2016 and Azure SQL Database. ## Disclaimers -The code included in this sample is not intended demonstrate some general guidances and arhitectural patterns for web development. +The code included in this sample is not intended demonstrate some general guidance and architectural patterns for web development. It contains minimal code required to create REST API. You can easily modify this code to fit the architecture of your application. @@ -80,4 +89,4 @@ This project has adopted the [Microsoft Open Source Code of Conduct](https://ope These samples and templates are all licensed under the MIT license. See the license.txt file in the root. ## Questions -Email questions to: sqlserversamples@microsoft.com. +Email questions to: [sqlserversamples@microsoft.com](mailto: sqlserversamples@microsoft.com). \ No newline at end of file diff --git a/samples/features/json/todo-app/nodejs-express4-rest-api/setup.bat b/samples/features/json/todo-app/nodejs-express4-rest-api/setup.bat new file mode 100644 index 00000000..b66c116a --- /dev/null +++ b/samples/features/json/todo-app/nodejs-express4-rest-api/setup.bat @@ -0,0 +1 @@ +npm install \ No newline at end of file diff --git a/samples/features/readme.md b/samples/features/readme.md index ce6c230d..ed91354f 100644 --- a/samples/features/readme.md +++ b/samples/features/readme.md @@ -11,3 +11,7 @@ Master Data Services (MDS) is the SQL Server solution for master data management [R Services](features/r-services) SQL Server R Services brings R processing close to the data, allowing more scalable and more efficient predictive analytics. + +[JSON Support](features/json) + +Built-in JSON functions enable you to easily parse and query JSON data stored in database, transform relational data to JSON text, and vice versa. \ No newline at end of file