The SQL Server connector enables LoopBack applications to connect to Microsoft SQL Server data sources.Note: This page was generated from the loopback-connector-mssql/README.md.

Note:The SQL Server connector requires SQL Server 2005+.

loopback-connector-mssql

Microsoft SQL Server is a relational database management system developed by Microsoft.The loopback-connector-mssql module is the Microsoft SQL Server connector for the LoopBack framework.

For more information, see LoopBack documentation.

Installation

In your application root directory, enter:

  1. $ npm install loopback-connector-mssql --save

This will install the module from npm and add it as a dependency to the application’s package.json file.

If you create a SQL Server data source using the data source generator as described below, you don’t have to do this, since the generator will run npm install for you.

Creating a SQL Server data source

Use the Data source generator to add a SQL Server data source to your application.The generator will prompt for the database server hostname, port, and other settingsrequired to connect to a SQL Server database. It will also run the npm install command above for you.

The entry in the application’s /server/datasources.json will look like this (for example):

/server/datasources.json

  1. "sqlserverdb": {
  2. "name": "sqlserverdb",
  3. "connector": "mssql",
  4. "host": "myhost",
  5. "port": 1234,
  6. "url": "mssql://username:password@dbhost/dbname",
  7. "database": "mydb",
  8. "password": "admin",
  9. "user": "admin",
  10. }

Edit datasources.json to add other properties that enable you to connect the data source to a SQL Server database.

To connect to a SQL Server instance running in Azure, you must specify a qualified user name with hostname, and add the following to the data source declaration:

  1. "options": {
  2. "encrypt": true
  3. ...
  4. }

Connector settings

To configure the data source to use your MS SQL Server database, edit datasources.json and add the following settings as appropriate.The MSSQL connector uses node-mssql as the driver. For more information about configuration parameters,see node-mssql documentation.

PropertyTypeDefaultDescription
connectorString Either "loopback-connector-mssql" or "mssql"
databaseStringDatabase name
debugBooleanIf true, turn on verbose mode to debug database queries and lifecycle.
hostStringlocalhostDatabase host name
passwordStringPassword to connect to database
portNumber1433Database TCP port
schemaStringdboDatabase schema
urlStringUse instead of the host, port, user, password, and database properties. For example: 'mssql://test:mypassword@localhost:1433/dev'.
userStringQualified username with host name, for example "user@your.sqlserver.dns.host".

Instead of specifying individual connection properties, you can use a single url property that combines them into a single string, for example:

  1. "accountDB": {
  2. "url": "mssql://test:mypassword@localhost:1433/demo?schema=dbo"
  3. }

The application will automatically load the data source when it starts. You can then refer to it in code, for example:

/server/boot/script.js

  1. var app = require('./app');
  2. var dataSource = app.dataSources.accountDB;

Alternatively, you can create the data source in application code; for example:

/server/script.js

  1. var DataSource = require('loopback-datasource-juggler').DataSource;
  2. var dataSource = new DataSource('mssql', config);
  3. config = { ... }; // JSON object as specified above in "Connector settings"

Model discovery

The SQL Server connector supports model discovery that enables you to create LoopBack modelsbased on an existing database schema using the unified database discovery API. For more information on discovery, see Discovering models from relational databases.

Auto-migratiion

The SQL Server connector also supports auto-migration that enables you to create a database schemafrom LoopBack models using the LoopBack automigrate method.For each model, the LoopBack SQL Server connector creates a table in the ‘dbo’ schema in the database.

For more information on auto-migration, see Creating a database schema from models for more information.

Destroying models may result in errors due to foreign key integrity. First delete any related models by calling delete on models with relationships.

Defining models

The model definition consists of the following properties:

  • name: Name of the model, by default, the table name in camel-case.
  • options: Model-level operations and mapping to Microsoft SQL Server schema/table. Use the mssql model property to specify additional SQL Server-specific properties for a LoopBack model.
  • properties: Property definitions, including mapping to Microsoft SQL Server columns.
    • For each property, use the mssql key to specify additional settings for that property/field.For example:

/common/models/inventory.json

  1. {"name": "Inventory",
  2. "options": {
  3. "idInjection": false,
  4. "mssql": {
  5. "schema": "strongloop",
  6. "table": "inventory"
  7. }
  8. }, "properties": {
  9. "id": {
  10. "type": "String",
  11. "required": false,
  12. "length": 64,
  13. "precision": null,
  14. "scale": null,
  15. "mssql": {
  16. "columnName": "id",
  17. "dataType": "varchar",
  18. "dataLength": 64,
  19. "dataPrecision": null,
  20. "dataScale": null,
  21. "nullable": "NO"
  22. }
  23. },
  24. "productId": {
  25. "type": "String",
  26. "required": false,
  27. "length": 64,
  28. "precision": null,
  29. "scale": null,
  30. "id": 1,
  31. "mssql": {
  32. "columnName": "product_id",
  33. "dataType": "varchar",
  34. "dataLength": 64,
  35. "dataPrecision": null,
  36. "dataScale": null,
  37. "nullable": "YES"
  38. }
  39. },
  40. "locationId": {
  41. "type": "String",
  42. "required": false,
  43. "length": 64,
  44. "precision": null,
  45. "scale": null,
  46. "id": 1,
  47. "mssql": {
  48. "columnName": "location_id",
  49. "dataType": "varchar",
  50. "dataLength": 64,
  51. "dataPrecision": null,
  52. "dataScale": null,
  53. "nullable": "YES"
  54. }
  55. },
  56. "available": {
  57. "type": "Number",
  58. "required": false,
  59. "length": null,
  60. "precision": 10,
  61. "scale": 0,
  62. "mssql": {
  63. "columnName": "available",
  64. "dataType": "int",
  65. "dataLength": null,
  66. "dataPrecision": 10,
  67. "dataScale": 0,
  68. "nullable": "YES"
  69. }
  70. },
  71. "total": {
  72. "type": "Number",
  73. "required": false,
  74. "length": null,
  75. "precision": 10,
  76. "scale": 0,
  77. "mssql": {
  78. "columnName": "total",
  79. "dataType": "int",
  80. "dataLength": null,
  81. "dataPrecision": 10,
  82. "dataScale": 0,
  83. "nullable": "YES"
  84. }
  85. }
  86. }}

Type mapping

See LoopBack types for details on LoopBack’s data types.

LoopBack to SQL Server types

LoopBack TypeSQL Server Type
BooleanBIT
DateDATETIME
GeoPointFLOAT
NumberINT
String JSON NVARCHAR

SQL Server to LoopBack types

SQL Server TypeLoopBack Type
BITBoolean
BINARYVARBINARYIMAGE Node.js Buffer object
DATEDATETIMEOFFSETDATETIME2SMALLDATETIMEDATETIMETIME Date
POINTGeoPoint
BIGINTNUMERICSMALLINTDECIMALSMALLMONEYINTTINYINTMONEYFLOATREAL Number
CHARVARCHARTEXTNCHARNVARCHARNTEXTCHARACTER VARYINGCHARACTER String

Running tests

Own instance

If you have a local or remote MSSQL instance and would like to use that to run the test suite, use the following command:

  • Linux
  1. MSSQL_HOST=<HOST> MSSQL_PORT=<PORT> MSSQL_USER=<USER> MSSQL_PASSWORD=<PASSWORD> MSSQL_DATABASE=<DATABASE> CI=true npm test
  • Windows
  1. SET MSSQL_HOST=<HOST> SET MSSQL_PORT=<PORT> SET MSSQL_USER=<USER> SET MSSQL_PASSWORD=<PASSWORD> SET MSSQL_DATABASE=<DATABASE> SET CI=true npm test

Docker

If you do not have a local MSSQL instance, you can also run the test suite with very minimal requirements.

  • Assuming you have Docker installed, run the following script which would spawn a MSSQL instance on your local:
  1. source setup.sh <HOST> <PORT> <USER> <PASSWORD> <DATABASE>

where <HOST>, <PORT>, <USER>, <PASSWORD> and <DATABASE> are optional parameters. The default values are localhost, 1433, sa, M55sqlT35t and master respectively.

  • Run the test:
  1. npm test

Tags: connectors