mirror of
https://github.com/Microsoft/sql-server-samples.git
synced 2025-12-08 14:58:54 +00:00
@@ -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
|
||||
<a name=disclaimers></a>
|
||||
|
||||
## 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.
|
||||
|
||||
<a name=related-links></a>
|
||||
@@ -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.
|
||||
Email questions to: [sqlserversamples@microsoft.com](mailto: sqlserversamples@microsoft.com).
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
node_modules/*
|
||||
bin/*
|
||||
bin/*.dll
|
||||
obj/*
|
||||
*.sln
|
||||
*.log
|
||||
@@ -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.
|
||||
|
||||
<a name=sample-details></a>
|
||||
|
||||
@@ -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.
|
||||
|
||||
<a name=disclaimers></a>
|
||||
|
||||
## 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).
|
||||
@@ -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);
|
||||
});
|
||||
@@ -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.
|
||||
@@ -1,6 +1,6 @@
|
||||
# mssql server 2014 express image listening on static port 1433
|
||||
#
|
||||
# Note: This dockerfile is based on Buc Rogers' work here:
|
||||
#
|
||||
# Note: This dockerfile is based on Buc Rogers' work here:
|
||||
# https://github.com/brogersyh/Dockerfiles-for-windows/tree/master/sqlexpress
|
||||
#
|
||||
# .NET 3.5 required for SQL Server
|
||||
@@ -12,12 +12,13 @@ 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 . /
|
||||
WORKDIR /
|
||||
|
||||
# download and install Microsoft SQL 2014 Express Edition in one step
|
||||
# download and install Microsoft SQL 2014 Express Edition in one step
|
||||
RUN powershell -Command (New-Object System.Net.WebClient).DownloadFile('%sql_express_download_url%', 'sqlexpress.exe') && /sqlexpress.exe /qs /x:setup && /setup/setup.exe /q /ACTION=Install /INSTANCENAME=SQLEXPRESS /FEATURES=SQLEngine /UPDATEENABLED=0 /SQLSVCACCOUNT="NT AUTHORITY\System" /SQLSYSADMINACCOUNTS="BUILTIN\ADMINISTRATORS" /TCPENABLED=1 /NPENABLED=0 /IACCEPTSQLSERVERLICENSETERMS && del /F /Q sqlexpress.exe && rd /q /s setup
|
||||
|
||||
RUN powershell -Command \
|
||||
@@ -25,6 +26,6 @@ RUN powershell -Command \
|
||||
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 ;
|
||||
set-itemproperty -path 'HKLM:\software\microsoft\microsoft sql server\mssql12.SQLEXPRESS\mssqlserver\' -name LoginMode -value 2 ;
|
||||
|
||||
CMD powershell ./start %sa_password%
|
||||
CMD powershell ./start -sa_password %sa_password% -attach_dbs \"%attach_dbs%\" -Verbose
|
||||
|
||||
@@ -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]
|
||||
|
||||
<a name=before-you-begin></a>
|
||||
|
||||
@@ -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=<YOUR 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=<YOUR SA PASSWORD> --env attach_dbs="<DB-JSON-CONFIG>" -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.
|
||||
|
||||
<a name=run-this-sample></a>
|
||||
|
||||
## Run this sample
|
||||
|
||||
The image provides one environment variable to set the sa password: </br>
|
||||
The image provides two environment variables to optionally set: </br>
|
||||
- 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']}]" microsoft/mssql-server-2014-express-windows
|
||||
```
|
||||
|
||||
<a name=sample-details></a>
|
||||
|
||||
## Sample details
|
||||
|
||||
@@ -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 }
|
||||
Reference in New Issue
Block a user