Counts 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
})
}
Finds 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
})
}
Tries 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)
}
})
}
Insert one or more entities.
The entity to insert.
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)
}
Does a raw query.
The object / value, which represents the query.
Optional
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.
Optional
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 ...
}
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 target type.
The object / value, which represents the query.
Optional
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
)
}
Removes one or more entities.
The entity to remove.
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.
Updates one or more entities.
The entity to update.
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 data adapter, like a Mongo or PostgreSQL connection.