0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-01-27 21:39:16 -05:00

refactor(core): connector query factory (#2854)

This commit is contained in:
Gao Sun 2023-01-09 15:48:57 +08:00 committed by GitHub
parent cab1fd646f
commit c5729e7a2d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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<Connector>(sql`
select ${sql.join(Object.values(fields), sql`, `)}
export const createConnectorQueries = (pool: CommonQueryMethods) => {
const findAllConnectors = async () =>
manyRows(
pool.query<Connector>(sql`
select ${sql.join(Object.values(fields), sql`, `)}
from ${table}
order by ${fields.id} asc
`)
);
const findConnectorById = async (id: string) =>
pool.one<Connector>(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)<CreateConnector, Connector>(Connectors, {
returning: true,
});
const updateConnector = buildUpdateWhereWithPool(pool)<CreateConnector, Connector>(
Connectors,
true
);
export const findConnectorById = async (id: string) =>
envSet.pool.one<Connector>(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<CreateConnector, Connector>(Connectors, {
returning: true,
});
export const updateConnector = buildUpdateWhere<CreateConnector, Connector>(Connectors, true);
/** @deprecated Will be removed soon. Use createConnectorQueries() factory instead. */
export const {
findAllConnectors,
findConnectorById,
countConnectorByConnectorId,
deleteConnectorById,
deleteConnectorByIds,
insertConnector,
updateConnector,
} = createConnectorQueries(envSet.pool);