API: store
Source:
db/store.ts
Defines the available comparison operators for query conditions.
Usage
// ---------
// Entity.ts
// ---------
@Entity("Customer")
@Table("CUSTOMER")
export class Customer {
@Id()
@Generated("sequence")
@Column({ name: "CUSTOMER_ID", type: "long" })
public id: number;
@Column({ name: "CUSTOMER_NAME", type: "string" })
public name: string;
@Column({ name: "CUSTOMER_ADDRESS", type: "string" })
public address: string;
}
// ---------
// Service.ts
// ---------
import { store } from "sdk/db";
import { response } from "sdk/http";
// Basic
let entry = { 'name': 'John', 'address': 'Sofia, Bulgaria' };
store.save('Customer', entry);
let list = store.list('Customer');
response.println(JSON.stringify(list));
// Advanced
let entry1 = { 'name': 'John', 'address': 'Sofia, Bulgaria' };
let entry2 = { 'name': 'Jane', 'address': 'Varna, Bulgaria' };
let entry3 = { 'name': 'Matthias', 'address': 'Berlin, Germany' };
store.save('Customer', entry1);
store.save('Customer', entry2);
store.save('Customer', entry3);
let list = store.list('Customer');
response.println("List all customers:");
response.println(JSON.stringify(list, null, 2));
response.println("");
response.println("Select customers with first name John:");
let select = store.query("from Customer c where c.name = 'John'");
response.println(JSON.stringify(select, null, 2));
response.println("");
response.println("Select native customers with first name John:");
let selectNative = store.queryNative("select * from Customer c where c.name = 'John'");
response.println(JSON.stringify(selectNative, null, 2));
response.println("");
response.println("Find customers by Example:");
let findByExample = store.find('Customer', {"name":"John"});
response.println(JSON.stringify(findByExample, null, 2));
response.println("");
response.println("List customers with filter options:");
let listWithOptions = store.list('Customer', {"conditions":[{"propertyName":"name","operator":"LIKE","value":"J%"}],"sorts":[{"propertyName":"name","direction":"ASC"}],"limit":"100"});
response.println(JSON.stringify(listWithOptions, null, 2));
response.flush();
response.close();Classes
Store
Facade class for interacting with the underlying Dirigible Data Store.
All methods serialize/deserialize JavaScript objects to/from JSON strings
before interacting with the native Java facade.
Methods
save
save (name:string, entry:any):string|numberSaves a new entry to the data store.
@param name The entity/table name.
@param entry The JavaScript object to save.
@returns The ID of the newly created entry (string or number).
upsert
upsert (name:string, entry:any):voidInserts a new entry or updates an existing one if the ID is present.
@param name The entity/table name.
@param entry The JavaScript object to insert/update.
update
update (name:string, entry:any):voidUpdates an existing entry.
@param name The entity/table name.
@param entry The JavaScript object with the ID and updated data.
list
list (name:string, options?:Options):any[]Lists entries based on optional filtering, sorting, and pagination options.
@param name The entity/table name.
@param options Optional {@link Options} for query execution.
@returns An array of JavaScript objects.
count
count (name:string, options?:Options):numberCounts the number of entries based on optional filtering options.
@param name The entity/table name.
@param options Optional {@link Options} for query execution.
@returns The count of matching entries.
get
get (name:string, id:any):any|undefinedRetrieves a single entry by its ID.
@param name The entity/table name.
@param id The ID of the entry.
@returns The entry object, or undefined if not found.
remove
remove (name:string, id:any):voidDeletes an entry by its ID.
@param name The entity/table name.
@param id The ID of the entry to remove.
find
find (name:string, example:any, limit:number=100, offset:number=0):any[]Finds entries matching an example object (query-by-example).
@param name The entity/table name.
@param example An object containing properties to match.
@param limit Maximum number of results to return.
@param offset Number of results to skip.
@returns An array of matching JavaScript objects.
query
query (name:string, limit:number=100, offset:number=0):any[]Queries all entries for a given entity name with pagination.
@param name The entity/table name.
@param limit Maximum number of results to return.
@param offset Number of results to skip.
@returns An array of JavaScript objects.
queryNative
queryNative (name:string):any[]Queries all entries for a given entity name without pagination.
@param name The entity/table name.
@returns An array of all JavaScript objects.
getEntityName
getEntityName (name:string):stringGets the name of the entity associated with the store name.
getTableName
getTableName (name:string):stringGets the underlying database table name for the entity.
getIdName
getIdName (name:string):stringGets the property name used as the ID field in the entity object.
getIdColumn
getIdColumn (name:string):stringGets the underlying database column name used for the ID field.