0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-01-20 21:32:31 -05:00
logto/packages/core/src/queries/roles-scopes.ts

45 lines
1.5 KiB
TypeScript

import type { RolesScope } from '@logto/schemas';
import { RolesScopes } from '@logto/schemas';
import { convertToIdentifiers } from '@logto/shared';
import type { CommonQueryMethods } from 'slonik';
import { sql } from 'slonik';
import envSet from '#src/env-set/index.js';
import { DeletionError } from '#src/errors/SlonikError/index.js';
const { table, fields } = convertToIdentifiers(RolesScopes);
export const createRolesScopesQueries = (pool: CommonQueryMethods) => {
const insertRolesScopes = async (rolesScopes: RolesScope[]) =>
pool.query(sql`
insert into ${table} (${fields.scopeId}, ${fields.roleId}) values
${sql.join(
rolesScopes.map(({ scopeId, roleId }) => sql`(${scopeId}, ${roleId})`),
sql`, `
)}
`);
const findRolesScopesByRoleId = async (roleId: string) =>
pool.any<RolesScope>(sql`
select ${sql.join(Object.values(fields), sql`,`)}
from ${table}
where ${fields.roleId}=${roleId}
`);
const deleteRolesScope = async (roleId: string, scopeId: string) => {
const { rowCount } = await pool.query(sql`
delete from ${table}
where ${fields.scopeId} = ${scopeId} and ${fields.roleId} = ${roleId}
`);
if (rowCount < 1) {
throw new DeletionError(RolesScopes.table);
}
};
return { insertRolesScopes, findRolesScopesByRoleId, deleteRolesScope };
};
/** @deprecated Will be removed soon. Use createRolesScopesQueries() factory instead. */
export const { insertRolesScopes, findRolesScopesByRoleId, deleteRolesScope } =
createRolesScopesQueries(envSet.pool);