mirror of
https://github.com/logto-io/logto.git
synced 2025-02-24 22:05:56 -05:00
refactor(core): add manyRows()
for queries (#665)
This commit is contained in:
parent
2715383ca2
commit
0fbca8be4b
4 changed files with 44 additions and 22 deletions
|
@ -1,7 +1,7 @@
|
|||
import { SchemaValuePrimitive, SchemaValue } from '@logto/schemas';
|
||||
import { Falsy, notFalsy } from '@silverhand/essentials';
|
||||
import dayjs from 'dayjs';
|
||||
import { sql, SqlSqlToken, SqlToken } from 'slonik';
|
||||
import { sql, SqlSqlToken, SqlToken, QueryResult } from 'slonik';
|
||||
|
||||
import { FieldIdentifiers, Table } from './types';
|
||||
|
||||
|
@ -68,3 +68,9 @@ export const convertToIdentifiers = <T extends Table>(
|
|||
});
|
||||
|
||||
export const convertToTimestamp = (time = dayjs()) => sql`to_timestamp(${time.valueOf() / 1000})`;
|
||||
|
||||
export const manyRows = async <T>(query: Promise<QueryResult<T>>): Promise<readonly T[]> => {
|
||||
const { rows } = await query;
|
||||
|
||||
return rows;
|
||||
};
|
||||
|
|
|
@ -4,7 +4,12 @@ import { sql } from 'slonik';
|
|||
import { buildInsertInto } from '@/database/insert-into';
|
||||
import { getTotalRowCount } from '@/database/row-count';
|
||||
import { buildUpdateWhere } from '@/database/update-where';
|
||||
import { convertToIdentifiers, OmitAutoSetFields, conditionalSql } from '@/database/utils';
|
||||
import {
|
||||
convertToIdentifiers,
|
||||
OmitAutoSetFields,
|
||||
conditionalSql,
|
||||
manyRows,
|
||||
} from '@/database/utils';
|
||||
import envSet from '@/env-set';
|
||||
import { DeletionError } from '@/errors/SlonikError';
|
||||
|
||||
|
@ -13,13 +18,15 @@ const { table, fields } = convertToIdentifiers(Applications);
|
|||
export const findTotalNumberOfApplications = async () => getTotalRowCount(table);
|
||||
|
||||
export const findAllApplications = async (limit: number, offset: number) =>
|
||||
envSet.pool.many<Application>(sql`
|
||||
manyRows(
|
||||
envSet.pool.query<Application>(sql`
|
||||
select ${sql.join(Object.values(fields), sql`, `)}
|
||||
from ${table}
|
||||
order by ${fields.createdAt} desc
|
||||
${conditionalSql(limit, (limit) => sql`limit ${limit}`)}
|
||||
${conditionalSql(offset, (offset) => sql`offset ${offset}`)}
|
||||
`);
|
||||
`)
|
||||
);
|
||||
|
||||
export const findApplicationById = async (id: string) =>
|
||||
envSet.pool.one<Application>(sql`
|
||||
|
|
|
@ -3,17 +3,19 @@ import { sql } from 'slonik';
|
|||
|
||||
import { buildInsertInto } from '@/database/insert-into';
|
||||
import { buildUpdateWhere } from '@/database/update-where';
|
||||
import { convertToIdentifiers } from '@/database/utils';
|
||||
import { convertToIdentifiers, manyRows } from '@/database/utils';
|
||||
import envSet from '@/env-set';
|
||||
|
||||
const { table, fields } = convertToIdentifiers(Connectors);
|
||||
|
||||
export const findAllConnectors = async () =>
|
||||
envSet.pool.many<Connector>(sql`
|
||||
manyRows(
|
||||
envSet.pool.query<Connector>(sql`
|
||||
select ${sql.join(Object.values(fields), sql`, `)}
|
||||
from ${table}
|
||||
order by ${fields.enabled} desc, ${fields.id} asc
|
||||
`);
|
||||
`)
|
||||
);
|
||||
|
||||
export const findConnectorById = async (id: string) =>
|
||||
envSet.pool.one<Connector>(sql`
|
||||
|
|
|
@ -4,7 +4,12 @@ import { sql } from 'slonik';
|
|||
import { buildInsertInto } from '@/database/insert-into';
|
||||
import { getTotalRowCount } from '@/database/row-count';
|
||||
import { buildUpdateWhere } from '@/database/update-where';
|
||||
import { convertToIdentifiers, OmitAutoSetFields, conditionalSql } from '@/database/utils';
|
||||
import {
|
||||
convertToIdentifiers,
|
||||
OmitAutoSetFields,
|
||||
conditionalSql,
|
||||
manyRows,
|
||||
} from '@/database/utils';
|
||||
import envSet from '@/env-set';
|
||||
import { DeletionError } from '@/errors/SlonikError';
|
||||
|
||||
|
@ -13,12 +18,14 @@ const { table, fields } = convertToIdentifiers(Resources);
|
|||
export const findTotalNumberOfResources = async () => getTotalRowCount(table);
|
||||
|
||||
export const findAllResources = async (limit: number, offset: number) =>
|
||||
envSet.pool.many<Resource>(sql`
|
||||
manyRows(
|
||||
envSet.pool.query<Resource>(sql`
|
||||
select ${sql.join(Object.values(fields), sql`, `)}
|
||||
from ${table}
|
||||
${conditionalSql(limit, (limit) => sql`limit ${limit}`)}
|
||||
${conditionalSql(offset, (offset) => sql`offset ${offset}`)}
|
||||
`);
|
||||
`)
|
||||
);
|
||||
|
||||
export const findResourceByIndicator = async (indicator: string) =>
|
||||
envSet.pool.maybeOne<Resource>(sql`
|
||||
|
|
Loading…
Add table
Reference in a new issue