Implements: IReferenceable, IUnreferenceable, IConfigurable, IOpenable, ICleanable
Description
The SqlServerPersistence class allows you to create persistence components that store data in a SQLServer database using the official driver.
Important points
- This is the most basic persistence component that is able to store data items of any type. Specific CRUD operations over the data items must be implemented in child classes by accessing this._db or this._collection properties.
Configuration parameters
- table: (optional) SQLServer table name
- schema: (optional) SQLServer table name connection(s):
- discovery_key: (optional) key to retrieve the connection from IDiscovery
- host: host name or IP address
- port: port number (default: 27017)
- uri: resource URI or connection string with all parameters in it
credential(s):
- store_key: (optional) key to retrieve the credentials from ICredentialStore
- username: (optional) username
- password: (optional) user’s password
options:
- connect_timeout: (optional) number of milliseconds to wait before timing out when connecting a new client (default: 0)
- idle_timeout: (optional) number of milliseconds a client must sit idle in the pool and not be checked out (default: 10000)
- max_pool_size: (optional) maximum number of clients the pool can contain (default: 10)
References
- *:logger:*:*:1.0 - (optional) ILogger components to pass log messages
- *:discovery:*:*:1.0 - (optional) IDiscovery services
- *:credential-store:*:*:1.0 - (optional) ICredentialStore to resolve credentials
Constructors
Creates a new instance of the persistence component.
publicconstructor(tableName?: string, _schemaName? string)
- tableName: string - (optional) table name.
- _schemaName: string - (optional) a schema name.
Fields
Instance methods
clear
Clears component state.
publicclear(context: IContext) :Promise<void>
- context: IContext- a context to trace execution through a call chain.
clearSchema
Clears all auto-created objects
protectedclearSchema(): void
close
Closes a component and frees used resources.
publicclose(context: IContext): Promise<void>
- context: IContext- a context to trace execution through a call chain.
configure
Configures component by passing configuration parameters.
publicconfigure(config: ConfigParams): void
- config: ConfigParams - configuration parameters to be set.
convertFromPublic
Converts an object value from public to internal format.
protectedconvertFromPublic(value: any): any
- value: any - object in public format to convert.
- returns: any - converted object in internal format.
convertToPublic
Converts an object value from internal to public format.
protectedconvertToPublic(value: any): any
- value: any - object in internal format to convert.
- returns: any - converted object in public format.
create
Creates a data item.
publiccreate(context: IContext, item: T): Promise<T>
- context: IContext - (optional) a context to trace execution through a call chain.
- item: T - item to be created.
- returns: Promise<T> - created item
createRequest
Creates request to the database.
protectedcreateRequest(values: any[] = null): any
- values: any[] - optional list of query parameters
- returns: any - a created request
createSchema
Creates a schema.
protectedcreateSchema(context: IContext): Promise<void>
- context: IContext - (optional) a context to trace execution through a call chain.
defineSchema
Defines a database schema via auto create objects or convenience methods.
protecteddefineSchema(): void
deleteByFilter
Deletes data items that match to a given filter. This method shall be called by a public deleteByFilter method from a child class that receives FilterParams and converts them into a filter function.
publicdeleteByFilter(context: IContext, filter: string): Promise<void>
- context: IContext - (optional) a context to trace execution through a call chain.
- filter: string - (optional) a filter JSON object.
ensureIndex
Adds an index definition to be created on opening.
protectedensureIndex(name: string, keys: any, options?: any): void
- name: string - the index name
- keys: any - index keys (fields)
- options: any - index options
ensureSchema
Adds a statement to a schema definition
protectedensureSchema(schemaStatement: string): void
- schemaStatement: string - statement to be added to the schema
generateColumns
Generates a list of column names to use in SQL statements like: “column1,column2,column3”
protectedgenerateColumns(values: any): string
- values: any - array with column values or a key-value map
- returns: string - generated list of column names
generateParameters
Generates a list of value parameters to use in SQL statements like: "$1,$2,$3".
protectedgenerateParameters(values: any): string
- values: any - array with values or a key-value map
- returns: string - generated list of value parameters
generateSetParameters
Generates a list of column sets to use in UPDATE statements like: "@1,@2,@3".
protectedgenerateSetParameters(values: any): string
- values: any - key-value map with columns and values
- returns: string - generated list of column sets
generateValues
Generates a list of column parameters
protectedgenerateValues(values: any): any[]
- values: any - key-value map with columns and values
- returns: any[] - generated list of column values
getCountByFilter
Gets a number of data items retrieved by a given filter.
This method shall be called by a public getCountByFilter method from a child class that receives FilterParams and converts them into a filter function.
protectedgetCountByFilter(context: IContext, filter: any): Promise<number>
- context: IContext - (optional) a context to trace execution through a call chain.
- filter: any - (optional) filter for JSON objects
- returns: Promise<number> - number of filtered items
getListByFilter
Gets a list of data items retrieved by a given filter and sorted according to sorting parameters.
This method shall be called by a public getListByFilter method from a child class that receives FilterParams and converts them into a filter function.
protectedgetListByFilter(context: IContext, filter: any, sort: any, select: any): Promise<T[]>
- context: IContext - (optional) a context to trace execution through a call chain.
- filter: any - (optional) filter function used to filter items
- sort: any - (optional) sorting parameters
- select: any - (optional) projection parameters (not used yet)
- returns: Promise<T[]> - data list of filtered results
getOneRandom
Gets a random item from items that match to a given filter.
This method shall be called by a public getOneRandom method from a child class that receives FilterParams and converts them into a filter function.
getOneRandom(context: IContext, filter: any): Promise<T>
- context: IContext - (optional) a context to trace execution through a call chain.
- filter: any - (optional) a filter JSON object
- returns: Promise<T> - a random item.
getPageByFilter
Gets a page of data items retrieved by a given filter and sorted according to sorting parameters.
This method shall be called by a public getPageByFilter method from a child class that receives FilterParams and converts them into a filter function.
protectedgetPageByFilter(context: IContext, filter: any, paging: PagingParams, sort: any, select: any): Promise<DataPage>
- context: IContext - (optional) a context to trace execution through a call chain.
- filter: any - (optional) filter for JSON objects.
- paging: PagingParams - (optional) paging parameters
- sort: any - (optional) sorting JSON object
- select: any - (optional) projection JSON object
- returns: Promise<DataPage
> - a data page of result by filter
isOpen
Checks if the component is opened.
publicisOpen(): boolean
- returns: boolean - True if the component has been opened and False otherwise.
open
Opens the component.
publicopen(context: IContext): Promise<void>
- context: IContext - (optional) a context to trace execution through a call chain.
quoteIdentifier
Adds single quotes to a string.
protectedquoteIdentifier(value: string): string
- value: string - string where quotes need to be added
- returns: string - string with added quotes
quotedTableName
Joins schema and database name in dot notation
protectedquotedTableName(): string
- returns: string - string with added quotes
setReferences
Sets references to dependent components.
publicsetReferences(references: IReferences): void
- references: IReferences - references to locate the component dependencies.
unsetReferences
Unsets (clears) previously set references to dependent components.
publicunsetReferences(): void
Examples
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, [name] VARCHAR(50), [content] VARCHAR(MAX))');
this.ensureIndex(this._tableName + '_name', { name: 1 }, { unique: true });
}
public async set(context: IContext, item: MyData): Promise<MyData> {
if (item == null)
return null;
let row = this.convertFromPublic(item);
let columns = this.generateColumns(row);
let params = this.generateParameters(row);
let setParams = this.generateSetParameters(row);
let values = this.generateValues(row);
values.push(item.id);
let query = "INSERT INTO " + this.quotedTableName() + " (" + columns + ") OUTPUT INSERTED.* VALUES (" + params + ")";
let request = this.createRequest(values);
let newItem = await new Promise<any>((resolve, reject) => {
request.query(query, (err, result) => {
// Suppress duplicated error entry
if (err != null && (err.number == 2601 || err.number == 2627)) {
err = null;
result == null;
}
if (err != null) {
reject(err);
return;
}
let item = result && result.recordset && result.recordset.length == 1
? result.recordset[0] : null;
resolve(item);
});
});
if (newItem != null) {
newItem = this.convertToPublic(newItem);
return newItem;
}
values.push(item.id);
query = "UPDATE " + this.quotedTableName() + " SET " + setParams + " OUTPUT INSERTED.* WHERE [id]=@" + values.length;
request = this.createRequest(values);
newItem = await new Promise<any>((resolve, reject) => {
request.query(query, (err, result) => {
if (err != null) {
reject(err);
return;
}
let item = result && result.recordset && result.recordset.length == 1
? result.recordset[0] : null;
resolve(item);
});
});
newItem = this.convertToPublic(newItem);
return newItem;
}
public async getOneByName(context: IContext, name: string): Promise<MyData> {
let query = "SELECT * FROM " + this.quotedTableName() + " WHERE [name]=@1";
let params = [name];
let request = this.createRequest(params);
let item = await new Promise<any>((resolve, reject) => {
request.query(query, (err, result) => {
if (err != null) {
reject(err);
return;
}
let item = result && result.recordset ? result.recordset[0] || null : null;
resolve(item);
});
});
item = this.convertToPublic(item);
return item;
}
}
let persistence = new MySqlServerPersistence();
persistence.configure(ConfigParams.fromTuples(
"connection.host", "localhost",
"connection.port", 1433,
"credential.username", "sa",
"credential.password", "sqlserver_123",
"connection.database", "master"
));
await persitence.open("123");
let item = await persistence.set("123", { name: "ABC" });
item = await persistence.getByName("123", "ABC");
console.log(item); // Result: { name: "ABC" }