Skip to content

Commit

Permalink
Merge pull request #5513 from logto-io/gao-use-svhd-slonik
Browse files Browse the repository at this point in the history
refactor: use silverhand slonik
  • Loading branch information
gao-sun authored Mar 19, 2024
2 parents 2f388c0 + feaca53 commit b40a347
Show file tree
Hide file tree
Showing 212 changed files with 431 additions and 723 deletions.
4 changes: 1 addition & 3 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@
"pg-protocol": "^1.6.0",
"roarr": "^7.11.0",
"semver": "^7.3.8",
"slonik": "30.4.0",
"slonik-interceptor-preset": "^1.2.10",
"slonik-sql-tag-raw": "^1.1.4",
"@silverhand/slonik": "31.0.0-beta.2",
"tar": "^6.1.11",
"typescript": "^5.3.3",
"yargs": "^17.6.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createMockUtils } from '@logto/shared/esm';
import { createMockPool } from '@silverhand/slonik';
import Sinon from 'sinon';
import { createMockPool } from 'slonik';

import { chooseAlterationsByVersion } from './version.js';

Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/database/alteration/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { AlterationScript } from '@logto/schemas/lib/types/alteration.js';
import { conditionalString } from '@silverhand/essentials';
import type { DatabasePool } from '@silverhand/slonik';
import chalk from 'chalk';
import type { DatabasePool } from 'slonik';
import type { CommandModule } from 'yargs';

import { createPoolFromConfig } from '../../../database.js';
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/commands/database/seed/cloud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import {
} from '@logto/schemas';
import { GlobalValues } from '@logto/shared';
import { appendPath } from '@silverhand/essentials';
import type { CommonQueryMethods } from 'slonik';
import { sql } from 'slonik';
import type { CommonQueryMethods } from '@silverhand/slonik';
import { sql } from '@silverhand/slonik';

import { insertInto } from '../../../database.js';
import { consoleLog } from '../../../utils.js';
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/database/seed/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { DatabasePool } from 'slonik';
import type { DatabasePool } from '@silverhand/slonik';
import type { CommandModule } from 'yargs';

import { createPoolAndDatabaseIfNeeded } from '../../../database.js';
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/database/seed/oidc-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import type { LogtoOidcConfigType } from '@logto/schemas';
import { LogtoOidcConfigKey, logtoConfigGuards } from '@logto/schemas';
import { generateStandardId } from '@logto/shared';
import { getEnvAsStringArray } from '@silverhand/essentials';
import type { DatabaseTransactionConnection } from '@silverhand/slonik';
import chalk from 'chalk';
import type { DatabaseTransactionConnection } from 'slonik';
import { z } from 'zod';

import { getRowsByKeys, updateValueByKey } from '../../../queries/logto-config.js';
Expand Down
9 changes: 4 additions & 5 deletions packages/cli/src/commands/database/seed/tables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ import {
import { getTenantRole } from '@logto/schemas';
import { Tenants } from '@logto/schemas/models';
import { generateStandardId } from '@logto/shared';
import type { DatabaseTransactionConnection } from 'slonik';
import { sql } from 'slonik';
import { raw } from 'slonik-sql-tag-raw';
import type { DatabaseTransactionConnection } from '@silverhand/slonik';
import { sql } from '@silverhand/slonik';

import { insertInto } from '../../../database.js';
import { getDatabaseName } from '../../../queries/database.js';
Expand Down Expand Up @@ -105,7 +104,7 @@ export const createTables = async (

if (query) {
await connection.query(
sql`${raw(
sql`${sql.raw(
/* eslint-disable no-template-curly-in-string */
query
.replaceAll('${name}', parameters.name ?? '')
Expand All @@ -129,7 +128,7 @@ export const createTables = async (

/* eslint-disable no-await-in-loop */
for (const [file, query] of sorted) {
await connection.query(sql`${raw(query)}`);
await connection.query(sql`${sql.raw(query)}`);

if (!query.includes('/* no_after_each */')) {
await runLifecycleQuery('after_each', { name: file.split('.')[0], database });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
getTenantOrganizationCreateData,
Organizations,
} from '@logto/schemas';
import type { DatabaseTransactionConnection } from 'slonik';
import type { DatabaseTransactionConnection } from '@silverhand/slonik';

import { insertInto } from '../../../database.js';
import { consoleLog } from '../../../utils.js';
Expand Down
7 changes: 3 additions & 4 deletions packages/cli/src/commands/database/seed/tenant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ import {
} from '@logto/schemas';
import { generateStandardId } from '@logto/shared';
import { assert } from '@silverhand/essentials';
import type { CommonQueryMethods, DatabaseTransactionConnection } from 'slonik';
import { sql } from 'slonik';
import { raw } from 'slonik-sql-tag-raw';
import type { CommonQueryMethods, DatabaseTransactionConnection } from '@silverhand/slonik';
import { sql } from '@silverhand/slonik';

import { insertInto } from '../../../database.js';
import { getDatabaseName } from '../../../queries/database.js';
Expand All @@ -37,7 +36,7 @@ export const createTenant = async (pool: CommonQueryMethods, tenantId: string) =
await pool.query(insertInto(createTenant, 'tenants'));
await pool.query(sql`
create role ${sql.identifier([role])} with inherit login
password '${raw(password)}'
password '${sql.raw(password)}'
in role ${sql.identifier([parentRole])};
`);
};
Expand Down
13 changes: 9 additions & 4 deletions packages/cli/src/database.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import type { SchemaLike } from '@logto/schemas';
import { assert } from '@silverhand/essentials';
import {
createPool,
parseDsn,
sql,
stringifyDsn,
createInterceptorsPreset,
} from '@silverhand/slonik';
import decamelize from 'decamelize';
import { DatabaseError } from 'pg-protocol';
import { createPool, parseDsn, sql, stringifyDsn } from 'slonik';
import { createInterceptors } from 'slonik-interceptor-preset';

import { convertToPrimitiveOrSql } from './sql.js';
import { ConfigKey, consoleLog, getCliConfigWithPrompt } from './utils.js';
Expand All @@ -22,7 +27,7 @@ export const createPoolFromConfig = async () => {
assert(parseDsn(databaseUrl).databaseName, new Error('Database name is required in URL'));

return createPool(databaseUrl, {
interceptors: createInterceptors(),
interceptors: createInterceptorsPreset(),
});
};

Expand All @@ -49,7 +54,7 @@ export const createPoolAndDatabaseIfNeeded = async () => {
// - It will throw error when creating database using '?'
const databaseName = dsn.databaseName ?? '?';
const maintenancePool = await createPool(stringifyDsn({ ...dsn, databaseName: 'postgres' }), {
interceptors: createInterceptors(),
interceptors: createInterceptorsPreset(),
});
await maintenancePool.query(sql`
create database ${sql.identifier([databaseName])}
Expand Down
10 changes: 0 additions & 10 deletions packages/cli/src/include.d/slonik-interceptor-preset.d.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/cli/src/queries/database.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { CommonQueryMethods } from 'slonik';
import { sql } from 'slonik';
import type { CommonQueryMethods } from '@silverhand/slonik';
import { sql } from '@silverhand/slonik';

export const getDatabaseName = async (pool: CommonQueryMethods, normalized = false) => {
const { currentDatabase } = await pool.one<{ currentDatabase: string }>(sql`
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/queries/logto-config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { LogtoConfig, LogtoConfigKey, logtoConfigGuards } from '@logto/schemas';
import { LogtoConfigs } from '@logto/schemas';
import type { Nullable } from '@silverhand/essentials';
import type { CommonQueryMethods } from 'slonik';
import { sql } from 'slonik';
import type { CommonQueryMethods } from '@silverhand/slonik';
import { sql } from '@silverhand/slonik';
import type { z } from 'zod';

import { convertToIdentifiers } from '../sql.js';
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/queries/system.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AlterationStateKey, Systems } from '@logto/schemas';
import { createMockPool, createMockQueryResult, sql } from '@silverhand/slonik';
import { DatabaseError } from 'pg-protocol';
import { createMockPool, createMockQueryResult, sql } from 'slonik';

import { convertToIdentifiers } from '../sql.js';
import type { QueryType } from '../test-utils.js';
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/queries/system.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { AlterationState, System, SystemKey } from '@logto/schemas';
import { systemGuards, Systems, AlterationStateKey } from '@logto/schemas';
import type { Nullable } from '@silverhand/essentials';
import type { CommonQueryMethods, DatabaseTransactionConnection } from '@silverhand/slonik';
import { sql } from '@silverhand/slonik';
import { DatabaseError } from 'pg-protocol';
import type { CommonQueryMethods, DatabaseTransactionConnection } from 'slonik';
import { sql } from 'slonik';
import type { z } from 'zod';

import { convertToIdentifiers } from '../sql.js';
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

import { type SchemaValue, type SchemaValuePrimitive, type Table } from '@logto/shared';
import { type IdentifierSqlToken, type SqlToken, sql } from 'slonik';
import { type IdentifierSqlToken, type SqlToken, sql } from '@silverhand/slonik';

/**
* Note `undefined` is removed from the acceptable list,
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/test-utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copied from core

import type { QueryResult, QueryResultRow } from 'slonik';
import type { PrimitiveValueExpression } from 'slonik/dist/src/types.js';
import type { QueryResult, QueryResultRow } from '@silverhand/slonik';
import type { PrimitiveValueExpression } from '@silverhand/slonik/dist/src/types.js';

export type QueryType = (
sql: string,
Expand Down
4 changes: 1 addition & 3 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,7 @@
"roarr": "^7.11.0",
"samlify": "2.8.10",
"semver": "^7.3.8",
"slonik": "30.4.0",
"slonik-interceptor-preset": "^1.2.10",
"slonik-sql-tag-raw": "^1.1.4",
"@silverhand/slonik": "31.0.0-beta.2",
"snake-case": "^3.0.4",
"snakecase-keys": "^6.0.0",
"zod": "^3.22.4"
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/database/delete-by-id.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type CommonQueryMethods, sql } from 'slonik';
import { type CommonQueryMethods, sql } from '@silverhand/slonik';

import { DeletionError } from '#src/errors/SlonikError/index.js';

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/database/find-all-entities.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type GeneratedSchema, type SchemaLike } from '@logto/schemas';
import { sql, type CommonQueryMethods } from 'slonik';
import { sql, type CommonQueryMethods } from '@silverhand/slonik';

import { conditionalSql, convertToIdentifiers, manyRows } from '#src/utils/sql.js';

Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/database/find-entity-by-id.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { SchemaLike, GeneratedSchema } from '@logto/schemas';
import type { CommonQueryMethods } from 'slonik';
import { sql, NotFoundError } from 'slonik';
import type { CommonQueryMethods } from '@silverhand/slonik';
import { sql, NotFoundError } from '@silverhand/slonik';

import RequestError from '#src/errors/RequestError/index.js';
import assertThat from '#src/utils/assert-that.js';
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/database/insert-into.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { GeneratedSchema, SchemaLike } from '@logto/schemas';
import { has } from '@silverhand/essentials';
import type { CommonQueryMethods, IdentifierSqlToken } from 'slonik';
import { sql } from 'slonik';
import type { CommonQueryMethods, IdentifierSqlToken } from '@silverhand/slonik';
import { sql } from '@silverhand/slonik';

import { InsertionError } from '#src/errors/SlonikError/index.js';
import assertThat from '#src/utils/assert-that.js';
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/database/row-count.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type GeneratedSchema } from '@logto/schemas';
import { type SchemaLike } from '@logto/shared';
import type { CommonQueryMethods, IdentifierSqlToken } from 'slonik';
import { sql } from 'slonik';
import type { CommonQueryMethods, IdentifierSqlToken } from '@silverhand/slonik';
import { sql } from '@silverhand/slonik';

import { type SearchOptions, buildSearchSql } from './utils.js';

Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/database/update-where.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import type { SchemaLike, GeneratedSchema, SchemaValue } from '@logto/schemas';
import type { UpdateWhereData } from '@logto/shared';
import type { Truthy } from '@silverhand/essentials';
import { notFalsy } from '@silverhand/essentials';
import type { CommonQueryMethods } from 'slonik';
import { sql } from 'slonik';
import type { CommonQueryMethods } from '@silverhand/slonik';
import { sql } from '@silverhand/slonik';

import { UpdateError } from '#src/errors/SlonikError/index.js';
import assertThat from '#src/utils/assert-that.js';
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/database/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type GeneratedSchema } from '@logto/schemas';
import { type SchemaLike, type Table } from '@logto/shared';
import { type SqlSqlToken, sql } from 'slonik';
import { type SqlSqlToken, sql } from '@silverhand/slonik';

import { conditionalSql, convertToIdentifiers } from '#src/utils/sql.js';

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/env-set/check-alteration-state.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getAvailableAlterations } from '@logto/cli/lib/commands/database/alteration/index.js';
import type { DatabasePool } from '@silverhand/slonik';
import chalk from 'chalk';
import type { DatabasePool } from 'slonik';

import { consoleLog } from '#src/utils/console.js';

Expand Down
16 changes: 11 additions & 5 deletions packages/core/src/env-set/create-pool.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { assert } from '@silverhand/essentials';
import { createMockPool, createMockQueryResult, createPool, parseDsn } from 'slonik';
import { createInterceptors } from 'slonik-interceptor-preset';
import {
createMockPool,
createMockQueryResult,
createPool,
parseDsn,
createInterceptorsPreset,
} from '@silverhand/slonik';

const createPoolByEnv = async (
databaseDsn: string,
Expand All @@ -12,11 +17,12 @@ const createPoolByEnv = async (
return createMockPool({ query: async () => createMockQueryResult([]) });
}

const interceptors = [...createInterceptors()];

assert(parseDsn(databaseDsn).databaseName, new Error('Database name is required'));

return createPool(databaseDsn, { interceptors, maximumPoolSize: poolSize });
return createPool(databaseDsn, {
interceptors: createInterceptorsPreset(),
maximumPoolSize: poolSize,
});
};

export default createPoolByEnv;
2 changes: 1 addition & 1 deletion packages/core/src/env-set/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { GlobalValues } from '@logto/shared';
import type { Optional } from '@silverhand/essentials';
import { appendPath } from '@silverhand/essentials';
import type { DatabasePool } from 'slonik';
import type { DatabasePool } from '@silverhand/slonik';

import { createLogtoConfigLibrary } from '#src/libraries/logto-config.js';
import { createLogtoConfigQueries } from '#src/queries/logto-config.js';
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/errors/SlonikError/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { SchemaLike, GeneratedSchema } from '@logto/schemas';
import type { UpdateWhereData } from '@logto/shared';
import { SlonikError } from 'slonik';
import { SlonikError } from '@silverhand/slonik';

import { type OmitAutoSetFields } from '#src/utils/sql.js';

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/errors/SlonikError/slonik-error.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SlonikError } from 'slonik';
import { SlonikError } from '@silverhand/slonik';

import { DeletionError } from './index.js';

Expand Down
10 changes: 0 additions & 10 deletions packages/core/src/include.d/slonik-interceptor-preset.d.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/core/src/middleware/koa-auth/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {
LogtoConfigs,
} from '@logto/schemas';
import { appendPath } from '@silverhand/essentials';
import { sql } from '@silverhand/slonik';
import type { JWK } from 'jose';
import { sql } from 'slonik';

import { EnvSet, getTenantEndpoint } from '#src/env-set/index.js';
import { exportJWK } from '#src/utils/jwks.js';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { Users } from '@logto/schemas';
import { NotFoundError, SlonikError, UniqueIntegrityConstraintViolationError } from 'slonik';
import {
NotFoundError,
SlonikError,
UniqueIntegrityConstraintViolationError,
} from '@silverhand/slonik';

import RequestError from '#src/errors/RequestError/index.js';
import { DeletionError, InsertionError, UpdateError } from '#src/errors/SlonikError/index.js';
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/middleware/koa-slonik-error-handler.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { SchemaLike } from '@logto/schemas';
import type { Middleware } from 'koa';
import {
SlonikError,
NotFoundError,
InvalidInputError,
CheckIntegrityConstraintViolationError,
UniqueIntegrityConstraintViolationError,
} from 'slonik';
} from '@silverhand/slonik';
import type { Middleware } from 'koa';

import RequestError from '#src/errors/RequestError/index.js';
import { DeletionError, InsertionError, UpdateError } from '#src/errors/SlonikError/index.js';
Expand Down
Loading

0 comments on commit b40a347

Please sign in to comment.