Abstract
Private
_contextThe underlying adapter.
Gets the underlying context.
The context.
Abstract
countCounts a list of items.
The class / type.
Optional
options: Nilable<IFindOptions>The custom options.
The promise with the items.
import { IDataRepository } from '@egomobile/orm'
class User {
id: number = -1;
}
async function countActiveUsers(repo: IDataRepository): Promise<number> {
// in SQL context
return await repo.count(User, {
where: 'is_active=$1 AND (is_deleted=$2 OR is_deleted IS NULL)',
params: [ true, false ], // $1, $2
})
}
Abstract
findFinds a list of items.
The class / type.
Optional
options: Nilable<IFindOptions>The custom options.
The promise with the items.
import { IDataRepository } from '@egomobile/orm'
// keep sure to initialize your props
// with a value, which is not (undefined)
class User {
id: number = -1;
first_name: string = '';
last_name: string = '';
is_active: boolean | null = null;
is_deleted: boolean = false;
}
async function load10ActiveUsersAndSkipFirst(repo: IDataRepository): Promise<User[]> {
// in SQL context
return await repo.find(User, {
where: 'is_active=$1 AND (is_deleted=$2 OR is_deleted IS NULL)',
params: [ true, false ], // $1, $2
offset: 1,
limit: 10
})
}
Abstract
findTries to find a simple item.
The class / type.
Optional
options: Nilable<IFindOneOptions>The custom options.
The promise with the item or (null) if not found.
import { IDataRepository } from '@egomobile/orm'
// keep sure to initialize your props
// with a value, which is not (undefined)
class User {
id: number = -1;
first_name: string = '';
last_name: string = '';
is_active: boolean | null = null;
is_deleted: boolean = false;
}
async function loadLastActiveUser(repo: IDataRepository): Promise<User> {
// in SQL context
return await repo.findOne(User, {
where: 'is_active=$1 AND (is_deleted=$2 OR is_deleted IS NULL)',
params: [ true, false ], // $1, $2
sort: {
'created': 'DESC', // first sort by 'created' (descending)
'id': 'DESC', // then by 'id' (descending)
'last_name': 'ASC', // then by 'last_name' (ascending)
'first_name': 'ASC' // then by 'first_name' (ascending)
}
})
}
Returns an entity by its type.
The type of the entity.
The object or (null), if not found.
Returns an entity by its type or throws an exception if not found.
The type of the entity.
The object.
Abstract
insertInsert one or more entities.
The promise with the inserted entity/entities.
import { IDataRepository } from '@egomobile/orm'
// keep sure to initialize your props
// with a value, which is not (undefined)
class User {
id: number | null = null;
first_name: string = '';
last_name: string = '';
is_active: boolean | null = null;
is_deleted: boolean = false;
}
async function createUser(repo: IDataRepository, firstName: string, lastName: string): Promise<User> {
const newUser = new User()
newUser.last_name = lastName
newUser.first_name = firstName
newUser.is_active = true
await repo.insert(newUser)
}
Abstract
queryDoes a raw query.
The object / value, which represents the query.
Rest
...paramsOrArgs: any[]A list of optional parameters or arguments for the query.
The promise with the raw result.
import { IDataRepository } from '@egomobile/orm'
async function deleteInactiveUsers(repo: IDataRepository) {
// in SQL context
//
// result is an object or value
// from the underlying data adapter itself
const result: any = await repo.query(
"UPDATE users SET is_deleted=$1 WHERE is_active=$2;",
false, false // $1, $2
)
}
Does a raw query and maps the result(s) to entity objects.
The classes do not need to be configured in context, so it is
possbile to implement and work with "joins".
The difference to queryAndMap()
is, that is makes use of cursor pattern
instead of returning final data.
The target type.
The object / value, which represents the query.
Rest
...paramsOrArgs: any[]A list of optional parameters or arguments for the query.
The async generator.
import { IDataRepository } from '@egomobile/orm'
class UserAndRole {
role_id!: string;
user_id!: string;
}
async function loadUserRoles(repo: IDataRepository): AsyncGenerator<UserAndRole> {
// PostgreSQL example
// s. https://github.com/egomobile/node-orm-pg
return repo.queryAndIterate(
UserAndRole, // type of the target entity
// does not need to be configured
// in data context
// build query
"SELECT DISTINCT ur.id AS role_id, u.id AS user_id " +
"FROM user_roles ur " +
"INNER JOIN users u ON u.id = ur.user_id " +
"WHERE u.is_active = $1 AND u.is_deleted = $2;",
// additional parameters
true, false // $1, $2
)
}
const iterator = loadUserRoles(
// ... your repo instance ...
)
for await (const userAndRole of iterator) {
// your code ...
}
Abstract
queryDoes a raw query and maps the result(s) to entity objects. The classes do not need to be configured in context, so it is possbile to implement and work with "joins".
The target type.
The object / value, which represents the query.
Rest
...paramsOrArgs: any[]A list of optional parameters or arguments for the query.
The promise with the mapped entities.
import { IDataRepository } from '@egomobile/orm'
class UserAndRole {
role_id!: string;
user_id!: string;
}
async function loadUserRoles(repo: IDataRepository): Promise<UserAndRole[]> {
// PostgreSQL example
// s. https://github.com/egomobile/node-orm-pg
return await repo.queryAndMap(
UserAndRole, // type of the target entity
// does not need to be configured
// in data context
// build query
"SELECT DISTINCT ur.id AS role_id, u.id AS user_id " +
"FROM user_roles ur " +
"INNER JOIN users u ON u.id = ur.user_id " +
"WHERE u.is_active = $1 AND u.is_deleted = $2;",
// additional parameters
true, false // $1, $2
)
}
Abstract
removeRemoves one or more entities.
The promise with the removed entity/entities.
import { IDataRepository } from '@egomobile/orm'
// keep sure to initialize your props
// with a value, which is not (undefined)
class User {
id: number = -1;
is_active: boolean | null = null;
}
async function removeInactiveUsers(repo: IDataRepository, users: User[]) {
const inactiveUsers = users.find(u => u.is_active === false)
await repo.remove(inactiveUsers)
}
Sets the data context.
The context.
Abstract
updateUpdates one or more entities.
The promise with the updated entity/entities.
import { IDataRepository } from '@egomobile/orm'
// keep sure to initialize your props
// with a value, which is not (undefined)
class User {
id: number = -1;
is_deleted: boolean = false;
}
async function deleteUsers(repo: IDataRepository, users: User[]) {
users.forEach((user) => {
user.is_deleted = true
})
await repo.update(inactiveUsers)
}
Generated using TypeDoc
A basic data adapter.