Designing your persistence components

How Pip.Services facilitates code consistency.

Key takeaways

By designing your code in the correct way you can:

  1. Create a common set of instructions that are database independent.
  2. Easily transfer data from one database to another.

Introduction

In this tutorial, you will understand how to design your persistence in such a way that your code benefits from one of the main features of Pip.Services, which is symmetric code implementation.

In order to see this, we will create an example using two different databases (MySQL and PostgreSQL). Then, we will create a common set of instructions to manage CRUD operations and transfer data from one database into another.

Designing your persistence components

Pip.services allows you to reduce the amount of code through its symmetric implementation. In this tutorial, we will see how to perform CRUD operations using one set of common code for two different databases. In order to understand this, we will use an example that considers PostgreSQL and MySQL.

Pre-conditions

With a view to create our example, we need to import the following components. Among them, the two most important ones are IdentifiableMySqlPersistence and IdentifiablePostgrePersistence from Pip.Services, which will be used to define our persistence components.

import { 
    AnyValueMap, ConfigParams, DataPage, 
    FilterParams, IStringIdentifiable, 
    PagingParams, SortParams 
} from "pip-services3-commons-nodex";

import { IdentifiableMySqlPersistence } from "pip-services3-mysql-nodex";
import { IdentifiablePostgresPersistence } from "pip-services3-postgres-nodex";
using PipServices3.Commons.Config;
using PipServices3.Commons.Data;
using PipServices3.MySql.Persistence;
using PipServices3.Postgres.Persistence
Not available
Not available
from abc import ABC
from typing import Optional, Any, List
from typing import TypeVar

from pip_services3_commons.data import IStringIdentifiable
from pip_services3_commons.config import ConfigParams
from pip_services3_commons.data import SortParams, PagingParams, DataPage, AnyValueMap, FilterParams
from pip_services3_mysql.persistence import IdentifiableMySqlPersistence
from pip_services3_postgres.persistence import IdentifiablePostgresPersistence
Not available

Data object

We define the following data object, which corresponds to the tables that we will be using in both databases.

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

using System.Runtime.Serialization;

[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; }
}

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

Thus, our tables will have three columns, namely id, key and content.

Common interface

Now, we create an interface that will be used to create persistence objects for both databases and, which states a set of CRUD operations for identifiable persistence objects:

export interface IMyDataPersistence {
    set(correlationId: string, item: MyData): Promise<MyData>;

    create(correlationId: string, item: MyData): Promise<MyData>;

    getPageByFilter(correlationId: string, filter: FilterParams, paging: PagingParams, sort: SortParams): Promise<DataPage<MyData>>;

    getCountByFilter(correlationId: string, filter: FilterParams): Promise<number>;

    getListByFilter(correlationId: string, filter: FilterParams, sort: SortParams): Promise<MyData[]>;

    getOneById(correlationId: string, id: MyData): Promise<MyData>;

    getListByIds(correlationId: string, ids: MyData[]): Promise<MyData[]>;

    update(correlationId: string, item: MyData): Promise<MyData>;

    updatePartially(correlationId: string, id: string, data: AnyValueMap): Promise<MyData>;

    deleteById(correlationId: string, id: string): Promise<MyData>;

    deleteByIds(correlationId: string, ids: string[]): Promise<void>;

    deleteByFilter(correlationId: string, filter: FilterParams): Promise<void>;
}
public interface IMyDataPersistence
{
    Task<MyData> SetAsync(string correlationId, MyData item);

    Task<MyData> CreateAsync(string correlationId, MyData item);

    Task<DataPage<MyData>> GetPageByFilterAsync(string correlationId, FilterParams filter, PagingParams paging = null, SortParams sort = null);

    Task<long> GetCountByFilterAsync(string correlationId, FilterParams filter);

    Task<List<MyData>> GetListByFilterAsync(string correlationId, FilterParams filter);

    Task<MyData> GetOneByIdAsync(string correlationId, string id);

    Task<List<MyData>> GetListByIdsAsync(string correlationId, string[] ids);

    Task<MyData> UpdateAsync(string correlationId, MyData item);

    Task<MyData> UpdatePartially(string correlationId, string id, AnyValueMap data);

    Task<MyData> DeleteByIdAsync(string correlationId, string id);

    Task DeleteByIdsAsync(string correlationId, string[] ids);

    Task DeleteByFilterAsync(string correlationId, FilterParams filter);
}
Not available
Not available
class IMyDataPersistence(ABC):
    
    # CRUD operations
    
    # Create
    def set(self, correlation_id: Optional[str], item: MyData) -> MyData:
        raise NotImplemented()
        
    def create(self, correlation_id: Optional[str], item: MyData) -> MyData:
        raise NotImplemented()
    
    # Retrive
    def get_page_by_filter(self, correlation_id: Optional[str], filter: FilterParams,
                           paging: Optional[PagingParams], sort: Optional[SortParams]) -> DataPage:
        raise NotImplemented()

    def get_count_by_filter(self, correlation_id: Optional[str], filter: FilterParams) -> int:
        raise NotImplemented()

    def get_list_by_filter(self, correlation_id: Optional[str], filter: FilterParams) -> List[MyData]:
        raise NotImplemented()

    def get_list_by_ids(self, correlation_id: Optional[str], ids: List[str]) -> List[MyData]:
        raise NotImplemented()

    def get_one_by_id(self, correlation_id: Optional[str], id: str) -> MyData:
        raise NotImplemented()

    # Update
    def update(self, correlation_id: Optional[str], item: MyData) -> MyData:
        raise NotImplemented()

    def update_partially(self, correlation_id: Optional[str], id: str, data: AnyValueMap) -> MyData:
        raise NotImplemented()
    
    # Delete
    def delete_by_id(self, correlation_id: Optional[str], id: str) -> MyData:
        raise NotImplemented()

    def delete_by_ids(self, correlation_id: Optional[str], ids: List[str]):
        raise NotImplemented()

    def delete_by_filter(self, correlation_id: Optional[str], filter: FilterParams):
        raise NotImplemented()
Not available

MySQL persistence

After that, we define a component that inherits from the IdentifiableMySqlPersistence class and our previously defined interface, and implements several CRUD methods.

export class MyIdentifiableMySqlPersistence extends IdentifiableMySqlPersistence<MyData, string> implements IMyDataPersistence {
    public constructor() {
        super('mydata');
    }

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

    private composeFilter(filter: FilterParams): string {
        filter ??= new FilterParams();
        let key = filter.getAsNullableString("key");
        let content = filter.getAsNullableString("content");

        let filterCondition = "";
        if (key != null)
            filterCondition += "`key`='" + key + "'";
        if (content != null)
            filterCondition += "`content`='" + content + "'";

        return filterCondition;
    }

    private composeSort(sort: SortParams): string {
        sort ??= new SortParams();
        var composeSort = "";

        for (var i = 0; i < sort.length; i++)
            composeSort += sort[i].name + (sort[i].ascending ? " ASC" : " DESC");

        return composeSort != '' ? composeSort : null;
    }

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

    public getCountByFilter(correlationId: string, filter: FilterParams): Promise<number> {
        return super.getCountByFilter(correlationId, this.composeFilter(filter));
    }

    public getListByFilter(correlationId: string, filter: FilterParams, sort: SortParams): Promise<MyData[]> {
        return super.getListByFilter(correlationId, this.composeFilter(filter), this.composeSort(sort), null);
    }

    public deleteByFilterParams(correlationId: string, filter: FilterParams): Promise<void> {
        return super.deleteByFilter(correlationId, this.composeFilter(filter));
    }
}
public class MyIdentifiableMySqlPersistence : IdentifiableMySqlPersistence<MyData, string>, IMyDataPersistence
{
    public MyIdentifiableMySqlPersistence(): base("mydata") {}

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

    private static string ComposeFilter(FilterParams filter)
    {
        filter ??= new FilterParams();
        var key = filter.GetAsNullableString("key");
        var content = filter.GetAsNullableString("content");

        var filterCondition = "";
        if (key != null)
            filterCondition += "`key`='" + key + "'";
        if (content != null)
            filterCondition += "`content`='" + content + "'";

        return filterCondition;
    }

    private static string ComposeSort(SortParams sort)
    {
        sort ??= new SortParams();
        var composeSort = "";

        for (var i = 0; i < sort.Count; i++)
            composeSort += sort[i].Name + (sort[i].Ascending ? " ASC" : " DESC");

        return composeSort;
    }

    public Task<DataPage<MyData>> GetPageByFilterAsync(string correlationId, FilterParams filter, PagingParams paging = null, SortParams sort = null)
    {
        return base.GetPageByFilterAsync(correlationId, ComposeFilter(filter), paging, ComposeSort(sort));
    }

    public Task<long> GetCountByFilterAsync(string correlationId, FilterParams filter)
    {
        return base.GetCountByFilterAsync(correlationId, ComposeFilter(filter));
    }

    public Task<List<MyData>> GetListByFilterAsync(string correlationId, FilterParams filter)
    {
        return base.GetListByFilterAsync(correlationId, ComposeFilter(filter));
    }

    public Task DeleteByFilterAsync(string correlationId, FilterParams filter)
    {
        return base.DeleteByFilterAsync(correlationId, ComposeFilter(filter));
    }
}
Not available
Not available
class MyIdentifiableMySqlPersistence(IdentifiableMySqlPersistence, IMyDataPersistence):

    def __init__(self):
        super().__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` TEXT)')
        self._ensure_index(self._table_name + '_key', {'key': 1}, {'unique': True})

    def _compose_filter(self, filter: FilterParams):
        filter = filter or FilterParams()
        key = filter.get_as_nullable_string('key')
        content = filter.get_as_nullable_string('content')

        filter_condition = ''
        if key is not None:
            filter_condition += "`key`='" + key + "'"
        if content is not None:
            filter_condition += "`content`='" + content + "'"

        return filter_condition

    def _compose_sort(self, sort: SortParams):
        sort = sort or SortParams()
        compose_sort = ''

        for i, filed in enumerate(sort):
            compose_sort += filed.name + (' ASC' if filed.ascending else ' DESC')

        return compose_sort

    def get_page_by_filter(self, correlation_id: Optional[str], filter: FilterParams, paging: PagingParams,
                           sort: SortParams) -> DataPage:
        return super().get_page_by_filter(correlation_id, self._compose_filter(filter), paging,
                                          self._compose_sort(sort), None)

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

    def get_list_by_filter(self, correlation_id: Optional[str], filter: FilterParams, sort: SortParams) -> List[MyData]:
        return super().get_list_by_filter(correlation_id, self._compose_filter(filter), self._compose_sort(sort), None)

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

    def delete_by_filter(self, correlation_id: Optional[str], filter: FilterParams):
        return super().delete_by_filter(correlation_id, self._compose_filter(filter))

Not available

PostgreSQL persistence

Similar to what we did in the previous step, we now define a component that inherits from the IdentifiablePostgrePersistence component and the interface previously defined, and, which implements a set of CRUD operations.

export class MyIdentifiablePostgresPersistence extends IdentifiablePostgresPersistence<MyData, string> implements IMyDataPersistence {
    public constructor() {
        super('mydata');
    }

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

    private composeFilter(filter: FilterParams): string {
        filter ??= new FilterParams();
        let key = filter.getAsNullableString("key");
        let content = filter.getAsNullableString("content");

        let filterCondition = "";
        if (key != null)
            filterCondition += "key='" + key + "'";
        if (content != null)
            filterCondition += "content='" + content + "'";

        return filterCondition;
    }

    private composeSort(sort: SortParams): string {
        sort ??= new SortParams();
        var composeSort = "";

        for (var i = 0; i < sort.length; i++)
            composeSort += sort[i].name + (sort[i].ascending ? " ASC" : " DESC");

        return composeSort != '' ? composeSort : null;
    }

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

    public getCountByFilter(correlationId: string, filter: FilterParams): Promise<number> {
        return super.getCountByFilter(correlationId, this.composeFilter(filter));
    }

    public getListByFilter(correlationId: string, filter: FilterParams, sort: SortParams): Promise<MyData[]> {
        return super.getListByFilter(correlationId, this.composeFilter(filter), this.composeSort(sort), null);
    }

    public deleteByFilterParams(correlationId: string, filter: FilterParams): Promise<void> {
        return super.deleteByFilter(correlationId, this.composeFilter(filter));
    }
}
public class MyIdentifiablePostgresPersistence : IdentifiablePostgresPersistence<MyData, string>, IMyDataPersistence
{
    public MyIdentifiablePostgresPersistence() : base("mydata") { }

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

    private static string ComposeFilter(FilterParams filter)
    {
        filter ??= new FilterParams();
        var key = filter.GetAsNullableString("key");
        var content = filter.GetAsNullableString("content");

        var filterCondition = "";
        if (key != null)
            filterCondition += "key='" + key + "'";
        if (content != null)
            filterCondition += "content='" + content + "'";

        return filterCondition;
    }

    private static string ComposeSort(SortParams sort)
    {
        sort ??= new SortParams();
        var composeSort = "";

        for (var i = 0; i < sort.Count; i++)
            composeSort += sort[i].Name + (sort[i].Ascending ? " ASC" : " DESC");

        return composeSort;
    }

    public Task<DataPage<MyData>> GetPageByFilterAsync(string correlationId, FilterParams filter, PagingParams paging = null, SortParams sort = null)
    {
        return base.GetPageByFilterAsync(correlationId, ComposeFilter(filter), paging, ComposeSort(sort));
    }

    public Task<long> GetCountByFilterAsync(string correlationId, FilterParams filter)
    {
        return base.GetCountByFilterAsync(correlationId, ComposeFilter(filter));
    }

    public Task<List<MyData>> GetListByFilterAsync(string correlationId, FilterParams filter)
    {
        return base.GetListByFilterAsync(correlationId, ComposeFilter(filter));
    }

    public Task DeleteByFilterAsync(string correlationId, FilterParams filter)
    {
        return base.DeleteByFilterAsync(correlationId, ComposeFilter(filter));
    }
}
Not available
Not available
class MyIdentifiablePostgresPersistence(IdentifiablePostgresPersistence, IMyDataPersistence):

    def __init__(self):
        super().__init__('mydata')

    def _define_schema(self):
        self._clear_schema()
        self._ensure_schema('CREATE TABLE ' + self._table_name + ' (id TEXT PRIMARY KEY, key TEXT, content TEXT)')
        self._ensure_index(self._table_name + '_key', {'key': 1}, {'unique': True})

    def _compose_filter(self, filter: FilterParams):
        filter = filter or FilterParams()
        key = filter.get_as_nullable_string('key')
        content = filter.get_as_nullable_string('content')

        filter_condition = ''
        if key is not None:
            filter_condition += "key='" + key + "'"
        if content is not None:
            filter_condition += "content='" + content + "'"

        return filter_condition

    def _compose_sort(self, sort: SortParams):
        sort = sort or SortParams()
        compose_sort = ''

        for i, filed in enumerate(sort):
            compose_sort += filed.name + (' ASC' if filed.ascending else ' DESC')

        return compose_sort

    def get_page_by_filter(self, correlation_id: Optional[str], filter: FilterParams, paging: PagingParams,
                           sort: SortParams) -> DataPage:
        return super().get_page_by_filter(correlation_id, self._compose_filter(filter), paging,
                                          self._compose_sort(sort), None)

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

    def get_list_by_filter(self, correlation_id: Optional[str], filter: FilterParams, sort: SortParams) -> List[MyData]:
        return super().get_list_by_filter(correlation_id, self._compose_filter(filter), self._compose_sort(sort), None)

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

    def delete_by_filter(self, correlation_id: Optional[str], filter: FilterParams):
        return super().delete_by_filter(correlation_id, self._compose_filter(filter))

Not available

Defining the working database

In order to connect to our databases, we need to define our connection parameters. For our MySQL database, they will look like this:

let host = "localhost";
let port = "3306";
let db_name = "pip";
let user = "root";
let password = "";
var host = "localhost";
var port = "3306";
var db_name = "pip";
var user = "root";
var password = "";
Not available
Not available
host = 'localhost'
port = 3306
db_name = 'pip'
user = 'root'
password = ''
Not available

Next, we create an instance of our component and configure it.

let database1 = new MyIdentifiableMySqlPersistence();
database1.configure(ConfigParams.fromTuples(
    "connection.host", host,
    "connection.port", port,
    "connection.database", db_name,
    "credential.username", user,
    "credential.password", password
));
var database1 = new MyIdentifiableMySqlPersistence();
database1.Configure(ConfigParams.FromTuples(
    "connection.host", host,
    "connection.port", port,
    "connection.database", db_name,
    "credential.username", user,
    "credential.password", password
));
Not available
Not available
database1 = MyIdentifiableMySqlPersistence()
database1.configure(ConfigParams.from_tuples(
    "connection.host", host,
    "connection.port", port,
    "connection.database", db_name,
    "credential.username", user,
    "credential.password", password
))
Not available

And, finally, we connect it to our database.

await database1.open(null);
await database1.OpenAsync("123");
Not available
Not available
database1.open("123")
Not available

If instead, we want to work with our PostgreSQL database, we could define our configuration as

let host = "localhost";
let port = "5432";
let db_name = "pip";
let user = "postgres";
let password = "admin";
var host = "localhost";
var port = "5432";
var db_name = "pip";
var user = "postgres";
var password = "admin";
Not available
Not available
host = 'localhost'
port = 5432
db_name = 'pip'
user = 'postgres'
password = 'admin'
Not available

And we create an instance of and configure our PostgreSQL component

let database2 = new MyIdentifiablePostgresPersistence();
database2.configure(ConfigParams.fromTuples(
    "connection.host", host,
    "connection.port", port,
    "connection.database", db_name,
    "credential.username", user,
    "credential.password", password
));
var database2 = new MyIdentifiablePostgresPersistence();
database2.Configure(ConfigParams.FromTuples(
    "connection.host", host,
    "connection.port", port,
    "connection.database", db_name,
    "credential.username", user,
    "credential.password", password
));
Not available
Not available
database2 = MyIdentifiablePostgresPersistence()
database2.configure(ConfigParams.from_tuples(
    "connection.host", host,
    "connection.port", port,
    "connection.database", db_name,
    "credential.user", user,
    "credential.password", password
))
Not available

Then, we connect it to our PostgreSQL database.

await database2.open(null);
await database2.OpenAsync("123");
Not available
Not available
database2.open(None)
Not available

Once we have connected to the database that we want to work with, we define a new variable called persistence, which is of type

let persistence: IMyDataPersistence;
IMyDataPersistence persistence;
Not available
Not available
persistence: IMyDataPersistence
Not available

Following this, we equate it to the connector we want to use. If we want to use our MySQL database, we write

persistence = database1;
persistence = database1;
Not available
Not available
persistence = database1
Not available

Or, if we prefer to use our PostgreSQL database, we use

persistence = database2;
persistence = database2;
Not available
Not available
persistence = database2
Not available

CRUD operations

Whatever database we decide to use, we can now perform different CRUD operations by using the persistence object and any of the methods defined in our interface or inherited from the base classes. As our implementations were symmetric, both databases call methods with the same names.

For example

Create

Here we use the create() method and we insert ten records in our database.

for (var i = 0; i < 20; i++) {
    let data: MyData = { id: i.toString(), key: `key ${i}`, content: `content ${i}`};
    let res = await persistence.create(null, data);
}
for (var i=0; i < 20; i++)
{
    var data = new MyData { Id=i.ToString(), Key=$"key {i}", Content=$"content {i}"};
    var res = await persistence.CreateAsync("123", data);
}
Not available
Not available
for i in range(0, 20):
    data = MyData(str(i), f'key {i}', f'content {i}')
    result = persistence.create("123", data)
Not available
Retrieve

Once we have some records in our database, we can retrieve them by using one of the retrieving methods. In our example below, we use the get_list_by_ids() because we are working with identifiable records.

let ids = [ "3", "4", "5"  ];
let myDataList = await persistence.getListByIds(null, ids);
string[] ids = { "3", "4", "5"  };
var myDataList = await persistence.GetListByIdsAsync("123", ids);
Not available
Not available
ids = [str(i) for i in range(3,7)]
my_data_list = persistence.get_list_by_ids('123', ids)
Not available

However, we could have also used any of the filter-based methods defined in our interface. For example:

let result = await persistence.getPageByFilter(null, FilterParams.fromTuples("key", "key 8"), null, null);
var result = await persistence.GetPageByFilterAsync("123", FilterParams.FromTuples("key", "key 8"), null, null);
Not available
Not available
result = persistence.get_page_by_filter(None, FilterParams.from_tuples('key', 'key 8'), None, None)
Not available

Which, in our example, returns a DataPage object with the following values

result.data[0].id;       // Returns '8'
result.data[0].key;      // Returns 'key 8'
result.data[0].content;  // Returns 'content 8'
result.Data[0].Id;       // Returns '8'
result.Data[0].Key;      // Returns 'key 8'
result.Data[0].Content;  // Returns 'content 8'
Not available
Not available
result.data[0].id       # Returns '8'
result.data[0].key     # Returns 'key 8'
result.data[0].content  # Returns 'content 8'
Not available
Update

We can update a record by using the update() method. In the following example, we define a new instance for our record with id equal to ‘1’ and we update the content to ‘Updated content 1’.

let newValue: MyData = { id: "1", key: "key 1", content: "Updated content 1" };

await persistence.update(null, newValue);
var newValue = new MyData { Id = "1", Key = "key 1", Content = "Updated content 1" };

await persistence.UpdateAsync("123", newValue);
Not available
Not available
new_values = MyData('1', 'key 1', 'Updated content 1')

persistence.update(None, new_values)
Not available
Delete

Finally, we can delete some of our records with the deleteByIds method

let ids = [ "0", "1" ];
await persistence.deleteByIds(null, ids);
string[] ids = { "0", "1" };
await persistence.DeleteByIdsAsync("123", ids);
Not available
Not available
ids = ['0','1']
persistence.delete_by_ids(None, ids)
Not available

Or with the deleteByFilter() method

await persistence.deleteByFilterParams(null, FilterParams.fromTuples("key", "key 7"));
await persistence.DeleteByFilterAsync("123", FilterParams.FromTuples("key", "key 7"));
Not available
Not available
persistence.delete_by_filter(None, FilterParams.from_tuples('key', 'key 7'))
Not available

Data transfer

This approach defines an easy and practical way to migrate tables from one database to another. Let’s suppose that we want to transfer the data existing in our MySQL database to a table in our PostgreSQL database.

To achieve this, first, we retrieve the data from the table in MySQL and we obtain a list with elements of type MyData, which we call my_data_list. As both databases use the same data structure, we just need to insert those rows via the create() method, which accepts a correlationID and a list of MyData elements as inputs. The following code shows this:

// Step 1:  we extract the data from the MySQL database
persistence = database1;
let ids3: string[] = [ "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" ];
myDataList = await persistence.getListByIds(null, ids3);

// Step 2: we insert the data into the mydata table in the PostgreSQL database.
persistence = database2;
for (let item of myDataList) {
    let result: MyData = await persistence.create(null, item);
}
// Step 1:  we extract the data from the MySQL database
persistence = database1;
string[] ids = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
myDataList = await persistence.GetListByIdsAsync("123", ids);

// Step 2: we insert the data into the mydata table in the PostgreSQL database.
persistence = database2;
foreach (var item in myDataList)
    var result = await persistence.CreateAsync("123", item);
Not available
Not available
# Step 1:  we extract the data from the MySQL database
persistence = database1
ids = [str(i) for i in range(0,10)]
my_data_list = persistence.get_list_by_ids('123', ids)

# Step 2: we insert the data into the mydata table in the PostgreSQL database.
persistence = database2
for i in my_data_list:
    result = persistence.create(None, i)
Not available

Wrapping up

In this tutorial, we have seen how to design our code so we can benefit from the symmetric implementation feature that defines the Pip.Services toolkit.

We did this through an example that uses a MySQL and a PostgreSQL database. First, we created a common data object, an interface that provides a common structure to our database components, and a component per database.

Then, we showed how to connect those database components to their respective databases and we equated their instances to a variable that we named persistence, which was used to define our CRUD operations.

In this manner, we saw how we can define a common set of instructions that are used by any of the databases that we want to work with.

We also saw how to benefit from this design by transferring a set of records from our MySQL database to our PostgreSQL database.

In conclusion, Pip.Services provides a way to simplify our code and reduce the amount of work needed to work with persistence components.