0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-03-24 22:41:28 -05:00
logto/packages/core/src/queries/scope.ts
simeng-li fb6a1dc236
test(core): add ut for queires (#287)
* test(core): add ut for queires

add ut for queries

* test(core): add user query ut

add user query ut

* fix(core): remove test code

remove console log
2022-02-28 14:30:27 +08:00

35 lines
1,000 B
TypeScript

import { ResourceScope, CreateResourceScope, ResourceScopes } from '@logto/schemas';
import { sql } from 'slonik';
import { buildInsertInto } from '@/database/insert-into';
import pool from '@/database/pool';
import { convertToIdentifiers } from '@/database/utils';
import { DeletionError } from '@/errors/SlonikError';
const { table, fields } = convertToIdentifiers(ResourceScopes);
export const findAllScopesWithResourceId = async (resourceId: string) =>
pool.any<ResourceScope>(sql`
select ${sql.join(Object.values(fields), sql`, `)}
from ${table}
where ${fields.resourceId}=${resourceId}
`);
export const insertScope = buildInsertInto<CreateResourceScope, ResourceScope>(
pool,
ResourceScopes,
{
returning: true,
}
);
export const deleteScopeById = async (id: string) => {
const { rowCount } = await pool.query(sql`
delete from ${table}
where ${fields.id}=${id}
`);
if (rowCount < 1) {
throw new DeletionError(ResourceScopes.table, id);
}
};