SQL Server persistence

How to persist data using a SQLServer database.

Key takeaways

SqlServerPersistence Persistence component that stores data in a SQLServer database using the official driver.
IdentifiableSqlServerPersistence Persistence component that stores data in a SQLServer database and implements several CRUD operations over data items with unique ids.
IdentifialeJsonSqlServerPersistence Persistence component that stores data in a SQLServer database in JSON or JSONB fields and implements several CRUD operations over data items with unique ids.

Introduction

This tutorial will help you understand how to create SQL Server persistence components using Pip.Services. It begins by explaining how to install the sqlserver module and create the data structure used in the tutorial’s examples. Then, it describes each of the three persistence classes available in the module, namely SqlServerPersistence, IdentifiableSqlServerPersistence and IdentifiableJsonSqlServerPersistence. It ends with a summary of the main learned concepts.

SQLServer persistence

The Pip.Services toolkit contains the sqlserver module, which has three persistence components. These classes are SqlServerPersistence, IdentifiableSqlServerPersistence and IdentifiableJsonSqlServerPersistence. The sections below show how to use each of these components via the use of examples.

Pre-requisites

In order to use this module, we need to install it first. This can be done with the following command:

npm install pip-services3-sqlserver-nodex --save
dotnet add package PipServices3.Sqlserver
go get -u github.com/pip-services3-gox/pip-services3-sqlserver-gox@latest
Not available
pip install pip-services3-sqlserver
Not available

Data object

Throughout this tutorial, we will work with examples based on the following data structure

import { IStringIdentifiable } from 'pip-services3-commons-nodex';

export class MyData implements IStringIdentifiable {
    public id: string;
    public key: string;
    public content: string;
}

using System.Runtime.Serialization;
using PipServices3.Commons.Data;

[DataContract]
public class MyData : IStringIdentifiable
{
    [DataMember(Name = "id")]
    public string Id { get; set; }

    [DataMember(Name = "key")]
    public string Key { get; set; }

    [DataMember(Name = "content")]
    public string Content { get; set; }
}

type MyData struct {
	Id      string `bson:"_id" json:"id"`
	Key     string `bson:"key" json:"key"`
	Content string `bson:"content" json:"content"`
}

func (d *MyData) SetId(id string) {
	d.Id = id
}

func (d MyData) GetId() string {
	return d.Id
}

func (d MyData) Clone() MyData {
	return MyData{
		Id:      d.Id,
		Key:     d.Key,
		Content: d.Content,
	}
}

Not available
from pip_services3_commons.data import IStringIdentifiable

class MyData(IStringIdentifiable): 
    def __init__(self, id: str = None, key: str = None, content: str = None): 
        self.id = id 
        self.key = key 
        self.content = content  
Not available

and the following implementations of it

let data1: MyData = { id: "1", key: "key 1", content:"content 1" };
let data2: MyData ={ id: "2", key: "key 2", content: "content 2" };
let data3: MyData = { id: "3", key: "key 3", content: "content 3" };

var data1 = new MyData { Id = "1", Key = "key 1", Content = "content 1" };
var data2 = new MyData { Id = "2", Key = "key 2", Content = "content 2" };
var data3 = new MyData { Id = "3", Key = "key 3", Content = "content 3" };

data1 := MyData{Id: "1", Key: "key 1", Content: "content 1"}
data2 := MyData{Id: "2", Key: "key 2", Content: "content 2"}
data3 := MyData{Id: "3", Key: "key 3", Content: "content 3"}

Not available
data1 = MyData('1', 'key 1', 'content 1') 
data2 = MyData('2', 'key 2', 'content 2') 
data3 = MyData('3', 'key 3', 'content 3') 
Not available

SqlServerPersistence

This is the most basic persistence component for SQLServer databases and is used as a base class for the other two components. The following sections explain its main methods and their usage.

Pre-requisites

In order to use this component, we must import it with the following command:

import { SqlServerPersistence } from 'pip-services3-sqlserver-nodex';
using PipServices3.SqlServer.Persistence;
import (
	sqlsrvpersist "github.com/pip-services3-gox/pip-services3-sqlserver-gox/persistence"
)
Not available
from pip_services3_sqlserver.persistence import SqlServerPersistence
Not available
Component implementation

Once the component has been imported, we create a persistence class that inherits it. In our example, we will work with a table named mydata. Our code will look something like this

export class MySqlServerPersistence
    extends SqlServerPersistence<MyData> {
    public constructor() {
        super('mydata');
    }

    protected defineSchema(): void {
        this.clearSchema();
        this.ensureSchema('CREATE TABLE [' + this._tableName + '] ([id] VARCHAR(32) PRIMARY KEY, [key] VARCHAR(50), [content] VARCHAR(MAX))');
        this.ensureIndex(this._tableName + '_key', { key: 1 }, { unique: true });
    }

    public getOneRandom(correlationId: string, filter: any): Promise<MyData> {
        return super.getOneRandom(correlationId, filter);
    }

    public getPageByFilter(correlationId: string, filter: any, paging: PagingParams, sort: any): Promise<DataPage<MyData>> {
        return super.getPageByFilter(correlationId, filter, paging, null, null);
    }

    public getListByFilter(correlationId: string, filter: any, sort: any, select: any): Promise<MyData[]> {
        return super.getListByFilter(correlationId, filter, sort, select);
    }

    public getCountByFilter(correlationId: string, filter: any): Promise<number> {
        return super.getCountByFilter(correlationId, filter);
    }
}
public class MySqlServerPersistence : SqlServerPersistence<MyData>
{
    public MySqlServerPersistence() : base("mydata") { }

    protected override void DefineSchema()
    {
        ClearSchema();
        EnsureSchema($"CREATE TABLE [{_tableName}] ([id] VARCHAR(32) PRIMARY KEY, [key] VARCHAR(50), [content] VARCHAR(MAX))");
        EnsureIndex($"{_tableName}_key", new Dictionary<string, bool> { { "key", true } }, new PipServices3.SqlServer.Persistence.IndexOptions { Unique = true });
    }

    public async new Task<MyData> GetOneRandomAsync(string correlationId, string filter)
    {
        return await base.GetOneRandomAsync(correlationId, filter);
    }

    public async Task<DataPage<MyData>> GetPageByFilterAsync(string correlationId, string filter, PagingParams paging)
    {
        return await base.GetPageByFilterAsync(correlationId, filter, paging, null, null);
    }

    public async new Task<List<MyData>> GetListByFilterAsync(string correlationId, string filter, string sort = null, string select = null)
    {
        return await base.GetListByFilterAsync(correlationId, filter, sort, select);
    }

    public async new Task<long> GetCountByFilterAsync(string correlationId, string filter)
    {
        return await base.GetCountByFilterAsync(correlationId, filter);
    }

}
type MySqlServerPersistence struct {
	*sqlsrvpersist.SqlServerPersistence[MyData]
}

func NewMySqlServerPersistence() *MySqlServerPersistence {
	c := &MySqlServerPersistence{}
	c.SqlServerPersistence = sqlsrvpersist.InheritSqlServerPersistence[MyData](c, "mydata")
	return c
}

func (c *MySqlServerPersistence) DefineSchema() {
	c.ClearSchema()
	c.EnsureSchema("CREATE TABLE [" + c.TableName + "] ([id] VARCHAR(32) PRIMARY KEY, [key] VARCHAR(50), [content] VARCHAR(MAX))")
	c.EnsureIndex(c.SqlServerPersistence.TableName+"_key", map[string]string{"key": "1"}, map[string]string{"unique": "true"})
}

func (c *MySqlServerPersistence) GetPageByFilter(ctx context.Context, correlationId string,
	filter string, paging cdata.PagingParams, sort string, selection string) (page cdata.DataPage[MyData], err error) {

	return c.SqlServerPersistence.GetPageByFilter(ctx, correlationId, filter, paging, sort, selection)
}

func (c *MySqlServerPersistence) GetListByFilter(ctx context.Context, correlationId string,
	filter string, sort string, selection string) (items []MyData, err error) {

	return c.SqlServerPersistence.GetListByFilter(ctx, correlationId, filter, sort, selection)
}

func (c *MySqlServerPersistence) GetCountByFilter(ctx context.Context, correlationId string, filter string) (int64, error) {
	return c.SqlServerPersistence.GetCountByFilter(ctx, correlationId, filter)
}

func (c *MySqlServerPersistence) GetOneRandom(ctx context.Context, correlationId string, filter string) (item MyData, err error) {
	return c.SqlServerPersistence.GetOneRandom(ctx, correlationId, filter)
}
Not available
class MySqlServerPersistence(SqlServerPersistence):
    def __init__(self):
        super(MySqlServerPersistence, self).__init__('mydata')

    def _define_schema(self):
        self._clear_schema()
        self._ensure_schema(
            'CREATE TABLE [' + self._table_name + \
            '] ([id] VARCHAR(32) PRIMARY KEY, [key] VARCHAR(50), [content] VARCHAR(MAX))')
        self._ensure_index(self._table_name + '_key', {'key': 1}, {'unique': True})

    def get_one_random(self, correlation_id: Optional[str], filter: Any) -> MyData:
        return super().get_one_random(correlation_id, filter)

    def get_list_by_filter(self, correlation_id: Optional[str], filter: Any, sort: Any, select: Any) -> List[MyData]:
        return super().get_list_by_filter(correlation_id, filter, sort, select)

    def get_count_by_filter(self, correlation_id: Optional[str], filter: Any) -> int:
        return super().get_count_by_filter(correlation_id, filter)

    def get_page_by_filter(self, correlation_id: Optional[str], filter: Any, paging: PagingParams, sort: Any, select: Any) -> DataPage: 
        return super().get_page_by_filter(correlation_id, filter, paging, sort, select)
Not available

Now, we create an instance of this class and configure it according to our database’s configuration parameters via the configure() method.

import { ConfigParams } from 'pip-services3-commons-nodex';

let persistence = new MySqlServerPersistence();
persistence.configure(ConfigParams.fromTuples(
    "connection.host", "localhost",
    "connection.port", 1433,
    "connection.database", "pip",
    "credential.username", "user",
    "credential.password", "password"
));

var persistence = new MySqlServerPersistence();
persistence.Configure(ConfigParams.FromTuples(
    "connection.host", "localhost",
    "connection.port", 1433,
    "connection.database", "pip",
    "credential.username", "user",
    "credential.password", "password"
));

import (
    cconf "github.com/pip-services3-gox/pip-services3-commons-gox/config"
)

persistence := NewMySqlServerPersistence()
persistence.Configure(context.Background(), cconf.NewConfigParamsFromTuples(
	"connection.host", "localhost",
	"connection.port", 1433,
	"connection.database", "pip",
	"credential.username", "user",
	"credential.password", "password",
))

Not available
from pip_services3_commons.config import ConfigParams

persistence = MySqlServerPersistence()
persistence.configure(ConfigParams.from_tuples(
    "connection.host", "localhost",
    "connection.port", 1433,
    "connection.database", "pip",
    "credential.username", "user",
    "credential.password", "password"
))
Not available
Connection

Once that our component has been configured, we can connect it to our database.

await persistence.open(null);
await persistence.OpenAsync(null);
err := persistence.Open(context.Background(), "123")
Not available
persistence.open(None)
Not available
CRUD operations

This component presents several methods for CRUD operations. The following sections explain the main ones.

Create

To add a new record to our table, we use the create() method. This method accepts the correlationId and the data object containing the record to be created as input parameters, and returns a SqlServerPersistence object containing the added record. The example below illustrates its usage.

let result = await persistence.create(null, data1);
var result = await persistence.CreateAsync(null, data1);
res, err := persistence.Create(context.Background(), "123", data1)
Not available
result = persistence.create(None, data1)
Not available

Where

result.id;       // Returns '1'
result.key;      // Returns 'key 1'
result.content;  // Returns 'content 1'
result.Id;       // Returns '1'
result.Key;      // Returns 'key 1'
result.Content;  // Returns 'content 1'
result.Id;       // Returns '1'
result.Key;      // Returns 'key 1'
result.Content;  // Returns 'content 1'
Not available
result.id       # Returns '1'
result.key      # Returns 'key 1'
result.content  # Returns 'content 1'
Not available
Retrieve

In order to retrieve records from our table, we can use three different methods, namely getOneRandom(), getListByFilter(), and getPageByFilter(). Additionally, we can use the getCountByFilter() method to obtain the number of records that comply with a given filter’s condition.

getOneRandom()

This method retrieves a random record according to a given filter. The next example shows how to use it.

let result = await persistence.getOneRandom(null, "[key]='key 3'");
var result = await persistence.GetOneRandomAsync(null, "[key]='key 3'");
result, err := persistence.GetOneRandom(context.Background(), "123", "[key]='key 3'")
Not available
result = persistence.get_one_random(None, "[key]='key 3'")
Not available

Where

result.id;       // Returns '3'
result.key;      // Returns 'key 3'
result.content;  // Returns 'content 3'
result.Id;       // Returns '3'
result.Key;      // Returns 'key 3'
result.Content;  // Returns 'content 3'
result.Id;       // Returns '3'
result.Key;      // Returns 'key 3'
result.Content;  // Returns 'content 3'
Not available
result.id       # Returns '3'
result.key      # Returns 'key 3'
result.content  # Returns 'content 3'
Not available
getListByFilter()

This method returns a set of records in accordance with a given filter. It accepts the correlationId, a filter, and sorting and projection parameters as inputs. It returns a SqlServerPersistence object with the returned records. The following example illustrates how to use it.

let result = await persistence.getListByFilter(null, "[key]='key 3'", null, null);
var result = await persistence.GetListByFilterAsync(null, "[key]='key 3'", null, null);
result, err := persistence.GetListByFilter(context.Background(), "123", "[key]='key 3'", "", "")
Not available
result = persistence.get_list_by_filter(None, "[key]='key 3'", None, None)
Not available

Where

result[0].id;       // Returns '3'
result[0].key;      // Returns 'key 3'
result[0].content;  // Returns 'content 3'
result[0].Id;       // Returns '3'
result[0].Key;      // Returns 'key 3'
result[0].Content;  // Returns 'content 3'
result[0].Id;       // Returns '3'
result[0].Key;      // Returns 'key 3'
result[0].Content;  // Returns 'content 3'
Not available
result[0].id       # Returns '3'
result[0].key      # Returns 'key 3'
result[0].content  # Returns 'content 3'
Not available
getPageByFilter()

This method retrieves a set of records that comply with a given filter’s conditions. It takes the correlationId, paging parameters, and JSON strings for sorting and projecting as input values. It returns a DataPage object with the retrieved records in its data field. The example below explains its usage.

let page = await persistence.getPageByFilter(null, "[key]='key 3'", null, null);
var result = await persistence.GetPageByFilterAsync(null, "[key]='key 3'", null, null);
page, err := persistence.GetPageByFilter(context.Background(), "123", "[key]='key 3'", *cdata.NewEmptyPagingParams(), "", "")
Not available
result = persistence.get_page_by_filter(None, "[key]='key 3'", None, None, None)
Not available

Where

result.data[0].id;       // Returns '1'
result.data[0].key;      // Returns 'key 1'
result.data[0].content;  // Returns 'content 5'
result.Data[0].Id;       // Returns '1'
result.Data[0].Key;      // Returns 'key 1'
result.Data[0].Content;  // Returns 'content 5'
result.Data[0].Id;       // Returns '1'
result.Data[0].Key;      // Returns 'key 1'
result.Data[0].Content;  // Returns 'content 5'
Not available
result.data[0].id       # Returns '1'
result.data[0].key      # Returns 'key 1'
result.data[0].content  # Returns 'content 5'
Not available
getCountByFilter()

This method returns an integer representing the number of records that comply with some given conditions. The example below shows how to use it.

let result = await persistence.getCountByFilter(null, "[key]='key 3'");  // Returns 1
var result = await persistence.GetCountByFilterAsync(null, "[key]='key 3'");  // Returns 1
count, err := persistence.GetCountByFilter(context.Background(), "123", "[key]='key 3'") // Returns 1
Not available
result = persistence.get_count_by_filter (None, "[key]='key 3'")  # Returns 1
Not available
Update

This class doesn’t present any method to update records in a table.

Delete

This component has the deleteByFilter() method, which is used to delete one or more records in a table. It accepts the correlationId and a filter as input parameters, and once the execution has been successfully completed, it returns None.

await persistence.deleteByFilter(null, "[key]='key 3'");
await persistence.DeleteByFilterAsync(null, "[key]='key 3'");
err = persistence.DeleteByFilter(context.Background(), "123", "[key]='key 3'")
Not available
persistence.delete_by_filter(None, "[key]='key 3'")
Not available

IdentifiableSqlServerPersistence

This persistence component stores data in SQL Server databases and implements several CRUD operations over data items with unique ids. It has several methods for CRUD operations, which are described in the following sections.

Pre-requisites

In order to use this component, we need to import it first. We use the following command to do this.

import { IdentifiableSqlServerPersistence } from 'pip-services3-sqlserver-nodex';
using PipServices3.SqlServer.Persistence;
import sqlsrvpersist "github.com/pip-services3-gox/pip-services3-sqlserver-gox/persistence"
Not available
from pip_services3_sqlserver.persistence import IdentifiableSqlServerPersistence
Not available
Component implementation

Once the component has been imported, we can create a persistence component by creating a child class of IdentifiableSqlServerPersistence.

export class MySqlServerPersistence
    extends IdentifiableSqlServerPersistence<MyData, string> {
    public constructor() {
        super('mydata2');
    }

    protected defineSchema(): void {
        this.clearSchema();
        this.ensureSchema('CREATE TABLE [' + this._tableName + '] ([id] VARCHAR(32) PRIMARY KEY, [key] VARCHAR(50), [content] VARCHAR(MAX))');
        this.ensureIndex(this._tableName + '_key', { key: 1 }, { unique: true });
    }

    public getOneRandom(correlationId: string, filter: any): Promise<MyData> {
        return super.getOneRandom(correlationId, filter);
    }

    public getPageByFilter(correlationId: string, filter: any, paging: PagingParams, sort: any): Promise<DataPage<MyData>> {
        return super.getPageByFilter(correlationId, filter, paging, null, null);
    }

    public getListByFilter(correlationId: string, filter: any, sort: any, select: any): Promise<MyData[]> {
        return super.getListByFilter(correlationId, filter, sort, select);
    }

    public getCountByFilter(correlationId: string, filter: any): Promise<number> {
        return super.getCountByFilter(correlationId, filter);
    }
}
public class MySqlServerPersistence : IdentifiableSqlServerPersistence<MyData, string>
{
    public MySqlServerPersistence() : base("mydata2") { }

    protected override void DefineSchema()
    {
        ClearSchema();
        EnsureSchema($"CREATE TABLE [{_tableName}] ([id] VARCHAR(32) PRIMARY KEY, [key] VARCHAR(50), [content] VARCHAR(MAX))");
        EnsureIndex($"{_tableName}_key", new Dictionary<string, bool> { { "key", true } }, new PipServices3.SqlServer.Persistence.IndexOptions { Unique = true });
    }

    public async new Task<List<MyData>> GetListByFilterAsync(string correlationId, string filter, string sort = null, string select = null)
    {
        return await base.GetListByFilterAsync(correlationId, filter, sort, select);
    }

    public async new Task<MyData> GetOneRandomAsync(string correlationId, string filter)
    {
        return await base.GetOneRandomAsync(correlationId, filter);
    }

    public async Task<DataPage<MyData>> GetPageByFilterAsync(string correlationId, string filter, PagingParams paging)
    {
        return await base.GetPageByFilterAsync(correlationId, filter, paging, null, null);
    }

    public async new Task<long> GetCountByFilterAsync(string correlationId, string filter)
    {
        return await base.GetCountByFilterAsync(correlationId, filter);
    }

}
type MySqlServerPersistence struct {
	*sqlsrvpersist.IdentifiableSqlServerPersistence[MyData, string]
}

func NewMySqlServerPersistence() *MySqlServerPersistence {
	c := &MySqlServerPersistence{}
	c.IdentifiableSqlServerPersistence = sqlsrvpersist.InheritIdentifiableSqlServerPersistence[MyData, string](c, "mydata2")
	return c
}

func (c *MySqlServerPersistence) DefineSchema() {
	c.ClearSchema()
	c.EnsureSchema("CREATE TABLE [" + c.TableName + "] ([id] VARCHAR(32) PRIMARY KEY, [key] VARCHAR(50), [content] VARCHAR(MAX))")
	c.EnsureIndex(c.SqlServerPersistence.TableName+"_key", map[string]string{"key": "1"}, map[string]string{"unique": "true"})
}

func (c *MySqlServerPersistence) GetPageByFilter(ctx context.Context, correlationId string,
	filter string, paging cdata.PagingParams, sort string, selection string) (page cdata.DataPage[MyData], err error) {

	return c.SqlServerPersistence.GetPageByFilter(ctx, correlationId, filter, paging, sort, selection)
}

func (c *MySqlServerPersistence) GetListByFilter(ctx context.Context, correlationId string,
	filter string, sort string, selection string) (items []MyData, err error) {

	return c.SqlServerPersistence.GetListByFilter(ctx, correlationId, filter, sort, selection)
}

func (c *MySqlServerPersistence) GetCountByFilter(ctx context.Context, correlationId string, filter string) (int64, error) {
	return c.SqlServerPersistence.GetCountByFilter(ctx, correlationId, filter)
}

func (c *MySqlServerPersistence) GetOneRandom(ctx context.Context, correlationId string, filter string) (item MyData, err error) {
	return c.SqlServerPersistence.GetOneRandom(ctx, correlationId, filter)
}
Not available
class MySqlServerPersistence(IdentifiableSqlServerPersistence):
    def __init__(self):
        super(MySqlServerPersistence, self).__init__('mydata2')

    def _define_schema(self):
        self._clear_schema()
        self._ensure_schema(
            'CREATE TABLE [' + self._table_name + \
            '] ([id] VARCHAR(32) PRIMARY KEY, [key] VARCHAR(50), [content] VARCHAR(MAX))')
        self._ensure_index(self._table_name + '_key', {'key': 1}, {'unique': True})

    def get_one_random(self, correlation_id: Optional[str], filter: Any) -> MyData:
        return super().get_one_random(correlation_id, filter)

    def get_list_by_filter(self, correlation_id: Optional[str], filter: Any, sort: Any, select: Any) -> List[MyData]:
        return super().get_list_by_filter(correlation_id, filter, sort, select)

    def get_count_by_filter(self, correlation_id: Optional[str], filter: Any) -> int:
        return super().get_count_by_filter(correlation_id, filter)

    def get_page_by_filter(self, correlation_id: Optional[str], filter: Any, paging: PagingParams, sort: Any, select: Any) -> DataPage: 
        return super().get_page_by_filter(correlation_id, filter, paging, sort, select)
Not available

After defining our component, we create an instance of it. Then, we configure this instance according to our database details with the configure() method.

let persistence = new MySqlServerPersistence();
persistence.configure(ConfigParams.fromTuples(
    "connection.host", "localhost",
    "connection.port", 1433,
    "connection.database", "pip",
    "credential.username", "user",
    "credential.password", "password"
));

var persistence = new MySqlServerPersistence();
persistence.Configure(ConfigParams.FromTuples(
    "connection.host", "localhost",
    "connection.port", 1433,
    "connection.database", "pip",
    "credential.username", "user",
    "credential.password", "password"
));

persistence := NewMySqlServerPersistence()
persistence.Configure(context.Background(), cconf.NewConfigParamsFromTuples(
	"connection.host", "localhost",
	"connection.port", 1433,
	"connection.database", "pip",
	"credential.username", "user",
	"credential.password", "password"
))

Not available
from pip_services3_commons.config import ConfigParams

persistence = MySqlServerPersistence()
persistence.configure(ConfigParams.from_tuples(
    "connection.host", "localhost",
    "connection.port", 1433,
    "connection.database", "pip",
    "credential.username", "user",
    "credential.password", "password"
))
Not available
Connection

Now that we have our component ready for use, we can connect it to our database by using the open() method.

await persistence.open(null);
await persistence.OpenAsync(null)
err := persistence.Open(context.Background(), "123")
Not available
persistence.open(None)
Not available
CRUD operations

This component presents several methods that can be used to perform CRUD operations. The main ones are explained in the following sections.

Create

To insert a new record into a table, we use the create() method. It accepts the correlationId parameter and a data object as inputs and returns a SqlServerPersistence object containing the inserted record. The following example explains how to use it.

let result = await persistence.create(null, data1);
result = await persistence.CreateAsync(null, data1);
res, err := persistence.Create(context.Background(), "123", data1)
Not available
result = persistence.create(None, data1)
Not available

Where

result.id;       // Returns '1'
result.key;      // Returns 'key 1'
result.content;  // Returns 'content 1'
result.Id;       // Returns '1'
result.Key;      // Returns 'key 1'
result.Content;  // Returns 'content 1'
result.Id       // Returns '1'
result.Key      // Returns 'key 1'
result.Content  // Returns 'content 1'
Not available
result.id       # Returns '1'
result.key      # Returns 'key 1'
result.content  # Returns 'content 1'
Not available
Retrieve

This class contains several methods to retrieve records from a database. They are:

getOneById()

This method retrieves a record according to a given id. It accepts the correlationId and the record’s id as input parameters and returns a SqlServerPersistence object with the retrieved record. The following example explains its usage.

let result = await persistence.getOneById(null, "3");
var result = await persistence.GetOneByIdAsync(null, "3");

{

result, err := persistence.GetOneById(context.Background(), "123", "3")

Not available
result = persistence.get_one_by_id(None,'3')
Not available

Where

result.id;       // Returns '3'
result.key;      // Returns 'key 3'
result.content;  // Returns 'content 3'
result.Id;       // Returns '3'
result.Key;      // Returns 'key 3'
result.Content;  // Returns 'content 3'
result.Id       // Returns '3'
result.Key      // Returns 'key 3'
result.Content  // Returns 'content 3'
Not available
result.id       # Returns '3'
result.key      # Returns 'key 3'
result.content  # Returns 'content 3'
Not available
getListByIds()

This method retrieves a set of records according to a set of given ids. It takes the correlationId and a list with the ids of the records to be retrieved as input parameters. It returns a list of SqlServerPersistence objects, each containing a retrieved record. The example below explains how to use it.

let ids = ['1', '2', '3'];
let result = await persistence.getListByIds(null, ids);
string[] ids = { "1", "2", "3" };
var result = await persistence.GetListByIdsAsync(null, ids);
ids := []string{"1", "2", "3"}
items, err := persistence.GetListByIds(context.Background(), "123", ids)
Not available
ids_list = ['1','2', '3']
result = persistence.get_list_by_ids(None,ids_list)
Not available

Where

for (let item of result) {
    console.log(`${item.id} ${item.key} ${item.content}`);
}

// Prints
// 1 new key 1 content 1
// 3 key 3 content 3
// 2 key 2 content 2
foreach(var item in result)
    Console.WriteLine($"{item.Id} {item.Key} {item.Content}");

// Prints
// 1 new key 1 content 1
// 3 key 3 content 3
// 2 key 2 content 2
for _, item := range items {
	fmt.Println(item.Id, item.Key, item.Content)
}

// Prints
// 1 new key 1 content 1
// 3 key 3 content 3
// 2 key 2 content 2
Not available
for item in result:
    print(item.id, item.key, item.content)

# Prints
# 1 new key 1 content 1
# 3 key 3 content 3
# 2 key 2 content 2
Not available
Update

This class has two methods that can be used to update records. These methods are:

update()

This method updates a complete record. It takes the correlationId and a data object as input parameters. It returns a SqlServerPersistence object with the updated record. The following example illustrates its usage.

let result = await persistence.update(null, { id: "2", key: "key 2.1", content: "new content 2" });
var result = await persistence.UpdateAsync(null, new MyData { Id = "2", Key = "key 2.1", Content = "new content 2" });
result, err := persistence.Update(context.Background(), "123", MyData{Id: "2", Key: "key 2.1", Content: "new content 2"})
Not available
result = persistence.update(None, MyData('2','key 2.1', 'new content 2') )
Not available

Where

result.id;       // Returns '2'
result.key;      // Returns 'key 2.1'
result.content;  // Returns 'new content 2'
result.Id;       // Returns '2'
result.Key;      // Returns 'key 2.1'
result.Content;  // Returns 'new content 2'
result.Id       // Returns '2'
result.Key      // Returns 'key 2.1'
result.Content  // Returns 'new content 2'
Not available
result.id       # Returns '2'
result.key      # Returns 'key 2.1'
result.content  # Returns 'new content 2'
Not available
updatePartially()

This method updates one or more given fields in a record. It accepts the correlationId, the record’s id, and a dictionary containing the fields to be updated as input parameters. It returns a SqlServerPersistence object containing the updated record. The following example explains how to use it.

let result = await persistence.updatePartially(null, "1", AnyValueMap.fromTuples("key", "new key 1.1"));
var result = await persistence.UpdatePartially(null, "1", AnyValueMap.FromTuples("key", "new key 1.1"));
result, err := persistence.UpdatePartially(context.Background(), "123", "1", *cdata.NewAnyValueMapFromTuples("key", "new key 1.1"))
Not available
result = persistence.update_partially(None, '1', AnyValueMap.from_tuples("key", "new key 1.1"))
Not available

Where

result.id;       // Returns '1'
result.key;      // Returns 'new key 1.1'
result.content;  // Returns 'content 1'
result.Id;       // Returns '1'
result.Key;      // Returns 'new key 1.1'
result.Content;  // Returns 'content 1'
result.Id       // Returns '1'
result.Key      // Returns 'new key 1.1'
result.Content  // Returns 'content 1'
Not available
result.id       # Returns '1'
result.key      # Returns 'new key 1.1'
result.content  # Returns 'content 1'
Not available
Delete

This class contains two methods that can be used to delete records. These are:

deleteById()

This method deletes a record according to a given id. It accepts the correlationId and the id of the record to be deleted as input parameters, and returns a SqlServerPersistence object with the deleted record. The following example shows how to use it.

let result = await persistence.deleteById(null, "1");

var result = await persistence.DeleteByIdAsync(null, "1");
res, err = persistence.DeleteById(context.Background(), "123", "1")
Not available
result = persistence.delete_by_id(None, '1')
Not available

Where

result.id;       // Returns '1'
result.key;      // Returns 'key 1'
result.content;  // Returns 'content 1'

result.Id;       // Returns '1'
result.Key;      // Returns 'key 1'
result.Content;  // Returns 'content 1'
result.Id       // Returns '1'
result.Key      // Returns 'key 1'
result.Content  // Returns 'content 1'
Not available
result.id       # Returns '1'
result.key      # Returns 'key 1'
result.content  # Returns 'content 1'
Not available
deleteByIds()

This method deletes a set of records from a table according to a given list of ids. It accepts the correlationId and a list containing the ids of the records to be deleted as input parameters. Once it has executed the query, it returns None.

let idsList = ['2', '3'];
await persistence.deleteByIds(null, idsList);
string[] idsList = { "2", "3" };
await persistence.DeleteByIdsAsync(null, idsList);
ids := []string{"1", "2", "3"}
err := persistence.DeleteByIds(context.Background(), "123", ids)
Not available
ids_list = ['2','3']
persistence.delete_by_ids(None, ids_list)
Not available

IdentifiableJsonSqlServerPersistence

This component is similar to the previous one, but considers identifiable JSON objects. It stores data in SQL Server databases and implements several CRUD operations over data items with unique ids.

Pre-requisites

In order to use this component, we need to import it first. This can be done with the following command

import { IdentifiableJsonSqlServerPersistence } from 'pip-services3-sqlserver-nodex';
using PipServices3.SqlServer.Persistence;
import (
    sqlsrvpersist "github.com/pip-services3-gox/pip-services3-sqlserver-gox/persistence"
)
Not available
from pip_services3_sqlserver.persistence import IdentifiableJsonSqlServerPersistence
Not available
Component implementation

In order to implement this component, we create a class that inherits it. In addition, we need to define the method defineSchema(), which will allow us to use a table with two fields, namely id and data, where the data field will store the JSON values. Our class will look something like this

export class MySqlServerPersistence
    extends IdentifiableJsonSqlServerPersistence<MyData, string> {
    public constructor() {
        super('mydata_json');
    }

    protected defineSchema(): void {
        this.clearSchema();
        this.ensureTable();
        this.ensureSchema("ALTER TABLE [" + this._tableName + "] ADD [data_key] AS JSON_VALUE([data],'$.key')")
        this.ensureIndex(this._tableName + '_key', { data_key: 1 }, { unique: true });
    }

    public getOneRandom(correlationId: string, filter: any): Promise<MyData> {
        return super.getOneRandom(correlationId, filter);
    }

    public getPageByFilter(correlationId: string, filter: any, paging: PagingParams, sort: any): Promise<DataPage<MyData>> {
        return super.getPageByFilter(correlationId, filter, paging, null, null);
    }

    public getListByFilter(correlationId: string, filter: any, sort: any, select: any): Promise<MyData[]> {
        return super.getListByFilter(correlationId, filter, sort, select);
    }

    public getCountByFilter(correlationId: string, filter: any): Promise<number> {
        return super.getCountByFilter(correlationId, filter);
    }
}
public class MySqlServerPersistence : IdentifiableJsonSqlServerPersistence<MyData, string>
{
    public MySqlServerPersistence() : base("mydata_json") { }

    protected override void DefineSchema()
    {
        ClearSchema();
        EnsureTable();
        EnsureSchema("ALTER TABLE [mydata_json] ADD [data_key] AS JSON_VALUE([data],'$.key')");
        EnsureIndex($"{_tableName}_key", new Dictionary<string, bool> { { "data_key", true } }, new PipServices3.SqlServer.Persistence.IndexOptions { Unique = true });
    }

    public async new Task<List<MyData>> GetListByFilterAsync(string correlationId, string filter, string sort = null, string select = null)
    {
        return await base.GetListByFilterAsync(correlationId, filter, sort, select);
    }

    public async new Task<MyData> GetOneRandomAsync(string correlationId, string filter)
    {
        return await base.GetOneRandomAsync(correlationId, filter);
    }

    public async Task<DataPage<MyData>> GetPageByFilterAsync(string correlationId, string filter, PagingParams paging)
    {
        return await base.GetPageByFilterAsync(correlationId, filter, paging, null, null);
    }

    public async new Task<long> GetCountByFilterAsync(string correlationId, string filter)
    {
        return await base.GetCountByFilterAsync(correlationId, filter);
    }
}
        
type MySqlServerPersistence struct {
	*sqlsrvpersist.IdentifiableJsonSqlServerPersistence[MyData, string]
}

func NewMySqlServerPersistence() *MySqlServerPersistence {
	c := &MySqlServerPersistence{}
	c.IdentifiableJsonSqlServerPersistence = sqlsrvpersist.InheritIdentifiableJsonSqlServerPersistence[MyData, string](c, "mydata_json")
	return c
}

func (c *MySqlServerPersistence) DefineSchema() {
	c.ClearSchema()
	c.EnsureTable("", "")
	c.EnsureSchema("ALTER TABLE [" + c.TableName + "] ADD [data_key] AS JSON_VALUE([data],'$.key')")
	c.EnsureIndex(c.SqlServerPersistence.TableName+"_key", map[string]string{"key": "1"}, map[string]string{"unique": "true"})
}

func (c *MySqlServerPersistence) GetPageByFilter(ctx context.Context, correlationId string,
	filter string, paging cdata.PagingParams, sort string, selection string) (page cdata.DataPage[MyData], err error) {

	return c.SqlServerPersistence.GetPageByFilter(ctx, correlationId, filter, paging, sort, selection)
}

func (c *MySqlServerPersistence) GetListByFilter(ctx context.Context, correlationId string,
	filter string, sort string, selection string) (items []MyData, err error) {

	return c.SqlServerPersistence.GetListByFilter(ctx, correlationId, filter, sort, selection)
}

func (c *MySqlServerPersistence) GetCountByFilter(ctx context.Context, correlationId string, filter string) (int64, error) {
	return c.SqlServerPersistence.GetCountByFilter(ctx, correlationId, filter)
}

func (c *MySqlServerPersistence) GetOneRandom(ctx context.Context, correlationId string, filter string) (item MyData, err error) {
	return c.SqlServerPersistence.GetOneRandom(ctx, correlationId, filter)
}
Not available
class MySqlServerPersistence(IdentifiableJsonSqlServerPersistence):
    def __init__(self):
        super(MySqlServerPersistence, self).__init__('mydata_json')

    def _define_schema(self):
        # clear all previously autogenerated schemas
        self._clear_schema()
        # create a table 
        self._ensure_table()
        self._ensure_schema(
            'ALTER TABLE `' + self._table_name + '` ADD `data_key` VARCHAR(50) AS (JSON_UNQUOTE(`data`->"$.key"))')
        # create an index
        self._ensure_index(self._table_name + '_json_key', {"data_key": 1}, {'unique': True})

    def get_one_random(self, correlation_id: Optional[str], filter: Any) -> MyData:
        return super().get_one_random(correlation_id, filter)

    def get_list_by_filter(self, correlation_id: Optional[str], filter: Any, sort: Any, select: Any) -> List[MyData]:
        return super().get_list_by_filter(correlation_id, filter, sort, select)

    def get_count_by_filter(self, correlation_id: Optional[str], filter: Any) -> int:
        return super().get_count_by_filter(correlation_id, filter)

    def get_page_by_filter(self, correlation_id: Optional[str], filter: Any, paging: PagingParams, sort: Any, select: Any) -> DataPage: 
        return super().get_page_by_filter(correlation_id, filter, paging, sort, select)
        
Not available

Once that this class has been defined, we can create an instance of it, configure its connection parameters and connect it to our database in the same manner as we did with the IdentifiableSqlServerPersistence component.

CRUD operations

This class inherits most of its methods from the IdentifiableSqlServerPersistece class. As a result, these operations are implemented in the same manner as explained for the parent class.

Returned objects

In general, CRUD operations return an object with the same fields that were passed to the persistence component and the fields can be accessed in the same way as in the original object.

For example, if we use the getOneRandom() method,

let result = await persistence.getOneRandom(null, "[key]='key 3'");
var result = await persistence.GetOneRandomAsync(null, "[key]='key 3'");
result, err := persistence.GetOneRandom(context.Background(), "123", "[key]='key 3'")
Not available
result = persistence.get_one_random(None, "[key]='key 3'")
Not available

we can obtain the record values as

result.id;      // Returns '3'
result.key;     // Returns 'key 3'
result.content; // Returns 'content 3'
result.Id;      // Returns '3'
result.Key;     // Returns 'key 3'
result.Content; // Returns 'content 3'
result.Id;      // Returns '3'
result.Key;     // Returns 'key 3'
result.Content; // Returns 'content 3'
Not available
result.id      # Returns '3'
result.key     # Returns 'key 3'
result.content # Returns 'content 3'
Not available

Wrapping up

In this tutorial, we have seen how to create and use three different components for SQL Server persistence. The first was SqlServerPersistence, which is a basic persistence component that is used as a parent to the other two components. The second was IdentifiableSqlPersistence, which is a persistence component for objects that can be uniquely identifiable via an id field. The last one was IdentifiableJsonSqlServerPersistence, which is similar to the previous one but considers identifiable JSON objects. For each of them, we saw how to perform CRUD operations using the methods available in each class.