# ORM

ORM is an acronym for object-relational mapping which is a way in which data types in one system can be represented in another using object oriented programming.  Common ORM solutions in modern server-side programming include NHibernate and Entity Framework.

WireBootstrap has an [ORM solution](https://docs.wirebootstrap.com/orm/) built on top of it's base query features that creates Typescript classes, interfaces, and enums from tables, views, and stored procedures inside a database, data service, or other data connector end-point.  The resulting TypeScript classes allow CRUD operations such as reading, writing, deleting, and updating these objects directly from JavaScript.

The WireBootstrap Query Service uses the WireBootstrap ORM solution for Microsoft SQL Server.  It contains a TSQL script that can be run against a database that creates an ORM file.  The ORM file can then be added to TypeScript projects in order to program against the SQL objects directly from code.

The files needed for this solution are installed in the `\orm` directly off of the WireBootstrap Query Service installation root.

This pages uses the [Northwind sample database](https://github.com/Microsoft/sql-server-samples/tree/master/samples/databases/northwind-pubs) for illustration purposes.

## DataSource

Before creating an ORM file, be sure a [WireBootstrap DataSource](https://docs.wirebootstrap.com/wirebootstrap/reference/wire.data/wire.data.datasource) object is available to be used by the objects in the ORM to make calls to the query service.  See [Service Provider](/query-service/service-provider.md) for setting the data source configuration `Provider` attributes when working with the query service.

```javascript
export const Northwind = new wire.data.DataSource("sql", {
    ServiceRoot: "http://localhost:1895",
    Provider: {
        ServiceId: "qs_ebi-appdev2",
        SecretKey: "WireBootstrap$",
        ConnectionId: "northwind_ebi-appdev2"
    }
});
```

## SQL Script

In order to generate the TypeScript objects needed for a database ORM, a SQL script needs to be run from SQL Server Management Studio (SSMS) or other SQL Server IDE.   The script is located at `\orm\wire-mssql-orm.sql` inside the installation root.

## Script Variables

Once the script is opened in SSMS, three inputs are needed at the top of the SQL Script.  These are described below.

| Input                | Description                                                                                                                                                                       |
| -------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Database Name        | Replace **%***DatabaseName%*  with the name of the database in SQL Server.  This is used once in the `use` statement to set the database current for the execution of the script. |
| DataSource Name      | Replace %*DataSourceName%* with the name of the WireBootstrap `wire.data.DataSource` object that will be used to connect to the database in the target project.                   |
| DataSource Full Path | Replace %*DataSourceFullPath%* with the full path to the module file containing the object in %*DataSourceName%*.                                                                 |

```sql
/****************************************************************
	Variables and Replacements
*****************************************************************/

/*
	The database name in SQL Server
*/
use [%DatabaseName%]

/*
	The name of the WireBootstrap wire.data.DataSource object	
	Example: Northwind
*/
declare @datasourceName varchar(max) = '%DataSourceName%'

/*
	The full path to the module file containing %DataSourceName%
	Example: ./datasources/northwind.source.js
*/
declare @datasourceFullPath varchar(max) = '%DataSourceFullPath%'
```

## Running the Script

Once the replacements are complete, the script can be executed.  The script will create the contents of the ORM file by printing the output to the *Messages* tab as seen below.&#x20;

![](/files/-MU6GLuiMtmMwPB_0K94)

## ORM File

Once the script has finished running, the contents of the *Message* tab can be copy and pasted into an empty TypeScript file.

```javascript
/* ./northwind.orm.ts */

import { Northwind } from "./northwind.source.js";
import { ProcedureBase } from "./_procedure-base.js";
import { EntityBase } from "./_entity-base.js";
 
enum Orders_Fields {
  CustomerID = "CustomerID",
  EmployeeID = "EmployeeID",
  Freight = "Freight",
  OrderDate = "OrderDate",
  OrderID = "OrderID",
  RequiredDate = "RequiredDate",
  ShipAddress = "ShipAddress",
  ShipCity = "ShipCity",
  ShipCountry = "ShipCountry",
  ShipName = "ShipName",
  ShippedDate = "ShippedDate",
  ShipPostalCode = "ShipPostalCode",
  ShipRegion = "ShipRegion",
  ShipVia = "ShipVia"
}
 
...
```

For details on how to use the ORM classes in projects, visit [WireBootstrap ORM](https://docs.wirebootstrap.com/orm/).


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.wirebootstrap.com/query-service/orm.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
