mirror of
https://github.com/logto-io/logto.git
synced 2024-12-16 20:26:19 -05:00
Optmize adapter upsert query
This commit is contained in:
parent
b892c882be
commit
65cd21377c
1 changed files with 12 additions and 6 deletions
|
@ -17,6 +17,11 @@ export default function postgresAdapter(modelName: string) {
|
|||
type QueryResult = Pick<OidcModelInstanceDBEntry, 'payload' | 'consumedAt'>;
|
||||
const convertResult = (result: QueryResult | null) =>
|
||||
conditional(result && withConsumed(result.payload, result.consumedAt));
|
||||
const setExcluded = (...fields: IdentifierSqlTokenType[]) =>
|
||||
sql.join(
|
||||
fields.map((field) => sql`${field}=excluded.${field}`),
|
||||
sql`, `
|
||||
);
|
||||
|
||||
const findByField = async <T extends ValueExpressionType>(
|
||||
field: IdentifierSqlTokenType,
|
||||
|
@ -57,12 +62,13 @@ export default function postgresAdapter(modelName: string) {
|
|||
${payload.grantId ?? null}
|
||||
)
|
||||
on conflict (${fields.modelName}, ${fields.id}) do update
|
||||
set
|
||||
${fields.payload}=excluded.${fields.payload},
|
||||
${fields.expiresAt}=excluded.${fields.expiresAt},
|
||||
${fields.userCode}=excluded.${fields.userCode},
|
||||
${fields.uid}=excluded.${fields.uid},
|
||||
${fields.grantId}=excluded.${fields.grantId}
|
||||
set ${setExcluded(
|
||||
fields.payload,
|
||||
fields.expiresAt,
|
||||
fields.userCode,
|
||||
fields.uid,
|
||||
fields.grantId
|
||||
)}
|
||||
`);
|
||||
},
|
||||
find: async (id) => findByField(fields.id, id),
|
||||
|
|
Loading…
Reference in a new issue