mirror of
https://github.com/logto-io/logto.git
synced 2025-01-20 21:32:31 -05:00
fix(database): insertKeys filtering (#194)
* fix(database): insertKeys filtering * fix: tests
This commit is contained in:
parent
f85b922836
commit
67843f45db
2 changed files with 19 additions and 13 deletions
|
@ -6,22 +6,25 @@ import RequestError from '@/errors/RequestError';
|
|||
import { createTestPool } from '@/utils/test-utils';
|
||||
|
||||
import { buildInsertInto } from './insert-into';
|
||||
import { convertToIdentifiers, excludeAutoSetFields } from './utils';
|
||||
import { convertToIdentifiers } from './utils';
|
||||
|
||||
const buildExpectedInsertIntoSql = (keys: string[]) => [
|
||||
`insert into "users" (${keys.map((key) => `"${decamelize(key)}"`).join(', ')})`,
|
||||
`values (${keys.map((_, index) => `$${index + 1}`).join(', ')})`,
|
||||
];
|
||||
|
||||
describe('buildInsertInto()', () => {
|
||||
const keys = excludeAutoSetFields(Users.fieldKeys);
|
||||
const expectInsertIntoSql = [
|
||||
`insert into "users" (${keys.map((key) => `"${decamelize(key)}"`).join(', ')})`,
|
||||
`values (${keys.map((_, index) => `$${index + 1}`).join(', ')})`,
|
||||
];
|
||||
|
||||
it('resolves a promise with `undefined` when `returning` is false', async () => {
|
||||
const user: CreateUser = { id: 'foo', username: '456' };
|
||||
const expectInsertIntoSql = buildExpectedInsertIntoSql(Object.keys(user));
|
||||
const pool = createTestPool(expectInsertIntoSql.join('\n'));
|
||||
const insertInto = buildInsertInto(pool, Users);
|
||||
await expect(insertInto({ id: 'foo', username: '456' })).resolves.toBe(undefined);
|
||||
await expect(insertInto(user)).resolves.toBe(undefined);
|
||||
});
|
||||
|
||||
it('resolves a promise with `undefined` when `returning` is false and `onConflict` is enabled', async () => {
|
||||
const user: CreateUser = { id: 'foo', username: '456', primaryEmail: 'foo@bar.com' };
|
||||
const expectInsertIntoSql = buildExpectedInsertIntoSql(Object.keys(user));
|
||||
const pool = createTestPool(
|
||||
[
|
||||
...expectInsertIntoSql,
|
||||
|
@ -36,13 +39,12 @@ describe('buildInsertInto()', () => {
|
|||
setExcludedFields: [fields.primaryEmail],
|
||||
},
|
||||
});
|
||||
await expect(
|
||||
insertInto({ id: 'foo', username: '456', primaryEmail: 'foo@bar.com' })
|
||||
).resolves.toBe(undefined);
|
||||
await expect(insertInto(user)).resolves.toBe(undefined);
|
||||
});
|
||||
|
||||
it('resolves a promise with single entity when `returning` is true', async () => {
|
||||
const user: CreateUser = { id: 'foo', username: '123', primaryEmail: 'foo@bar.com' };
|
||||
const expectInsertIntoSql = buildExpectedInsertIntoSql(Object.keys(user));
|
||||
const pool = createTestPool(
|
||||
[...expectInsertIntoSql, 'returning *'].join('\n'),
|
||||
(_, [id, username, primaryEmail]) => ({
|
||||
|
@ -58,6 +60,8 @@ describe('buildInsertInto()', () => {
|
|||
});
|
||||
|
||||
it('throws `entity.create_failed` error with `undefined` when `returning` is true', async () => {
|
||||
const user: CreateUser = { id: 'foo', username: '123', primaryEmail: 'foo@bar.com' };
|
||||
const expectInsertIntoSql = buildExpectedInsertIntoSql(Object.keys(user));
|
||||
const pool = createTestPool([...expectInsertIntoSql, 'returning *'].join('\n'));
|
||||
const insertInto = buildInsertInto(pool, Users, { returning: true });
|
||||
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import { SchemaLike, GeneratedSchema } from '@logto/schemas';
|
||||
import { has } from '@silverhand/essentials';
|
||||
import { DatabasePoolType, IdentifierSqlTokenType, sql } from 'slonik';
|
||||
|
||||
import assertThat from '@/utils/assert-that';
|
||||
|
@ -59,15 +60,16 @@ export const buildInsertInto: BuildInsertInto = <
|
|||
const onConflict = config?.onConflict;
|
||||
|
||||
return async (data: OmitAutoSetFields<Schema>): Promise<ReturnType | void> => {
|
||||
const insertingKeys = keys.filter((key) => has(data, key));
|
||||
const {
|
||||
rows: [entry],
|
||||
} = await pool.query<ReturnType>(sql`
|
||||
insert into ${table} (${sql.join(
|
||||
keys.map((key) => fields[key]),
|
||||
insertingKeys.map((key) => fields[key]),
|
||||
sql`, `
|
||||
)})
|
||||
values (${sql.join(
|
||||
keys.map((key) => convertToPrimitiveOrSql(key, data[key] ?? null)),
|
||||
insertingKeys.map((key) => convertToPrimitiveOrSql(key, data[key] ?? null)),
|
||||
sql`, `
|
||||
)})
|
||||
${conditionalSql(returning, () => sql`returning *`)}
|
||||
|
|
Loading…
Add table
Reference in a new issue