Skip to content

Commit

Permalink
chore: upgrade slonik
Browse files Browse the repository at this point in the history
  • Loading branch information
gao-sun committed Mar 18, 2024
1 parent fbbe767 commit 0a5226c
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 460 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",
"@silverhand/slonik": "31.0.0-beta.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
5 changes: 2 additions & 3 deletions packages/cli/src/commands/database/seed/tables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import { Tenants } from '@logto/schemas/models';
import { generateStandardId } from '@logto/shared';
import type { DatabaseTransactionConnection } from '@silverhand/slonik';
import { sql } from '@silverhand/slonik';
import { raw } from 'slonik-sql-tag-raw';

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
3 changes: 1 addition & 2 deletions packages/cli/src/commands/database/seed/tenant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { generateStandardId } from '@logto/shared';
import { assert } from '@silverhand/essentials';
import type { CommonQueryMethods, DatabaseTransactionConnection } from '@silverhand/slonik';
import { sql } from '@silverhand/slonik';
import { raw } from 'slonik-sql-tag-raw';

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 } from '@silverhand/slonik';
import {
createPool,
parseDsn,
sql,
stringifyDsn,
createInterceptorsPreset,
} from '@silverhand/slonik';
import decamelize from 'decamelize';
import { DatabaseError } from 'pg-protocol';
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
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",
"@silverhand/slonik": "31.0.0-beta.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
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 '@silverhand/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;
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { sql } from '@silverhand/slonik';
import { raw } from 'slonik-sql-tag-raw';

import type { AlterationScript } from '../lib/types/alteration.js';

Expand Down Expand Up @@ -121,7 +120,7 @@ const alteration: AlterationScript = {
on ${getId(table)} (
${tenantId},
${sql.join(
columns.map((column) => raw(column)),
columns.map((column) => sql.raw(column)),
sql`, `
)}
);
Expand All @@ -147,7 +146,7 @@ const alteration: AlterationScript = {
add constraint ${indexName} unique (
${tenantId},
${sql.join(
columns.map((column) => raw(column)),
columns.map((column) => sql.raw(column)),
sql`, `
)}
);
Expand All @@ -171,7 +170,7 @@ const alteration: AlterationScript = {
create unique index ${indexName}
on ${getId(table)} (
${sql.join(
columns.map((column) => raw(column)),
columns.map((column) => sql.raw(column)),
sql`, `
)}
)
Expand All @@ -180,7 +179,7 @@ const alteration: AlterationScript = {
alter table ${getId(table)}
add constraint ${indexName} unique (
${sql.join(
columns.map((column) => raw(column)),
columns.map((column) => sql.raw(column)),
sql`, `
)}
);
Expand All @@ -203,7 +202,7 @@ const alteration: AlterationScript = {
create index ${indexName}
on ${getId(table)} (
${sql.join(
columns.map((column) => raw(column)),
columns.map((column) => sql.raw(column)),
sql`, `
)}
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { generateStandardId } from '@logto/shared/universal';
import type { CommonQueryMethods } from '@silverhand/slonik';
import { sql } from '@silverhand/slonik';
import { raw } from 'slonik-sql-tag-raw';

import type { AlterationScript } from '../lib/types/alteration.js';

Expand Down Expand Up @@ -123,7 +122,7 @@ const alteration: AlterationScript = {
`);
await pool.query(sql`
create role ${sql.identifier([role])} with inherit login
password '${raw(password)}'
password '${sql.raw(password)}'
in role ${sql.identifier([baseRole])};
`);
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { generateStandardId } from '@logto/shared/universal';
import type { CommonQueryMethods } from '@silverhand/slonik';
import { sql } from '@silverhand/slonik';
import { raw } from 'slonik-sql-tag-raw';

import type { AlterationScript } from '../lib/types/alteration.js';

Expand Down Expand Up @@ -154,7 +153,7 @@ const alteration: AlterationScript = {
`);
await pool.query(sql`
create role ${getId(role)} with inherit login
password '${raw(password)}'
password '${sql.raw(password)}'
in role ${getId(baseRole)};
`);

Expand Down
3 changes: 1 addition & 2 deletions packages/schemas/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@
"pluralize": "^8.0.0",
"prettier": "^3.0.0",
"roarr": "^7.11.0",
"@silverhand/slonik": "31.0.0-beta.0",
"slonik-sql-tag-raw": "^1.1.4",
"@silverhand/slonik": "31.0.0-beta.2",
"typescript": "^5.3.3"
},
"eslintConfig": {
Expand Down
Loading

0 comments on commit 0a5226c

Please sign in to comment.