diff --git a/packages/core/src/queries/connector.ts b/packages/core/src/queries/connector.ts index 32ab5b3f2..2f92056a6 100644 --- a/packages/core/src/queries/connector.ts +++ b/packages/core/src/queries/connector.ts @@ -1,62 +1,85 @@ import type { Connector, CreateConnector } from '@logto/schemas'; import { Connectors } from '@logto/schemas'; import { manyRows, convertToIdentifiers } from '@logto/shared'; +import type { CommonQueryMethods } from 'slonik'; import { sql } from 'slonik'; -import { buildInsertInto } from '#src/database/insert-into.js'; -import { buildUpdateWhere } from '#src/database/update-where.js'; +import { buildInsertIntoWithPool } from '#src/database/insert-into.js'; +import { buildUpdateWhereWithPool } from '#src/database/update-where.js'; import envSet from '#src/env-set/index.js'; import { DeletionError } from '#src/errors/SlonikError/index.js'; const { table, fields } = convertToIdentifiers(Connectors); -export const findAllConnectors = async () => - manyRows( - envSet.pool.query(sql` - select ${sql.join(Object.values(fields), sql`, `)} +export const createConnectorQueries = (pool: CommonQueryMethods) => { + const findAllConnectors = async () => + manyRows( + pool.query(sql` + select ${sql.join(Object.values(fields), sql`, `)} + from ${table} + order by ${fields.id} asc + `) + ); + const findConnectorById = async (id: string) => + pool.one(sql` + select ${sql.join(Object.values(fields), sql`,`)} from ${table} - order by ${fields.id} asc - `) + where ${fields.id}=${id} + `); + const countConnectorByConnectorId = async (connectorId: string) => + pool.one<{ count: number }>(sql` + select count(*) + from ${table} + where ${fields.connectorId}=${connectorId} + `); + + const deleteConnectorById = async (id: string) => { + const { rowCount } = await pool.query(sql` + delete from ${table} + where ${fields.id}=${id} + `); + + if (rowCount < 1) { + throw new DeletionError(Connectors.table, id); + } + }; + + const deleteConnectorByIds = async (ids: string[]) => { + const { rowCount } = await pool.query(sql` + delete from ${table} + where ${fields.id} in (${sql.join(ids, sql`, `)}) + `); + + if (rowCount !== ids.length) { + throw new DeletionError(Connectors.table, JSON.stringify({ ids })); + } + }; + const insertConnector = buildInsertIntoWithPool(pool)(Connectors, { + returning: true, + }); + const updateConnector = buildUpdateWhereWithPool(pool)( + Connectors, + true ); -export const findConnectorById = async (id: string) => - envSet.pool.one(sql` - select ${sql.join(Object.values(fields), sql`,`)} - from ${table} - where ${fields.id}=${id} - `); - -export const countConnectorByConnectorId = async (connectorId: string) => - envSet.pool.one<{ count: number }>(sql` - select count(*) - from ${table} - where ${fields.connectorId}=${connectorId} - `); - -export const deleteConnectorById = async (id: string) => { - const { rowCount } = await envSet.pool.query(sql` - delete from ${table} - where ${fields.id}=${id} - `); - - if (rowCount < 1) { - throw new DeletionError(Connectors.table, id); - } + return { + findAllConnectors, + findConnectorById, + countConnectorByConnectorId, + deleteConnectorById, + deleteConnectorByIds, + insertConnector, + updateConnector, + }; }; -export const deleteConnectorByIds = async (ids: string[]) => { - const { rowCount } = await envSet.pool.query(sql` - delete from ${table} - where ${fields.id} in (${sql.join(ids, sql`, `)}) - `); - - if (rowCount !== ids.length) { - throw new DeletionError(Connectors.table, JSON.stringify({ ids })); - } -}; - -export const insertConnector = buildInsertInto(Connectors, { - returning: true, -}); - -export const updateConnector = buildUpdateWhere(Connectors, true); +/** @deprecated Will be removed soon. Use createConnectorQueries() factory instead. */ +export const { + findAllConnectors, + findConnectorById, + countConnectorByConnectorId, + deleteConnectorById, + deleteConnectorByIds, + insertConnector, + updateConnector, +} = createConnectorQueries(envSet.pool);