0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-30 20:33:54 -05:00

feat: adopt schemas v1.0.6

This commit is contained in:
Gao Sun 2021-07-04 21:19:29 +08:00
parent 916130b5fc
commit 3231be014a
No known key found for this signature in database
GPG key ID: 0F0EFA2E36639F31
3 changed files with 33 additions and 29 deletions

View file

@ -13,7 +13,7 @@
},
"dependencies": {
"@logto/essentials": "^1.0.5",
"@logto/schemas": "^1.0.5",
"@logto/schemas": "^1.0.6",
"dayjs": "^1.10.5",
"dotenv": "^10.0.0",
"got": "^11.8.2",

View file

@ -2,7 +2,11 @@ import dayjs from 'dayjs';
import { AdapterFactory } from 'oidc-provider';
import { IdentifierSqlTokenType, sql, ValueExpressionType } from 'slonik';
import { conditional } from '@logto/essentials';
import { OidcModelInstances, OidcModelInstanceDBEntry } from '@logto/schemas';
import {
OidcModelInstances,
OidcModelInstanceDBEntry,
OidcModelInstancePayload,
} from '@logto/schemas';
import pool from '@/database/pool';
import { convertToIdentifiers } from '@/database/utils';
@ -37,43 +41,43 @@ export default function postgresAdapter(modelName: string) {
return convertResult(result);
};
const findByPayloadField = async <
T extends ValueExpressionType,
Field extends keyof OidcModelInstancePayload
>(
field: Field,
value: T
) => {
const result = await pool.maybeOne<QueryResult>(sql`
select ${fields.payload}, ${fields.consumedAt}
from ${table}
where ${fields.modelName}=${modelName}
and ${fields.payload}->>${field}=${value}
`);
return convertResult(result);
};
const adapter: ReturnType<AdapterFactory> = {
upsert: async (id, payload, expiresIn) => {
await pool.query(sql`
insert into ${table} (${sql.join(
[
fields.modelName,
fields.id,
fields.payload,
fields.expiresAt,
fields.userCode,
fields.uid,
fields.grantId,
],
[fields.modelName, fields.id, fields.payload, fields.expiresAt],
sql`, `
)})
values (
${modelName},
${id},
${JSON.stringify(payload)},
${dayjs().add(expiresIn, 'second').unix()},
${payload.userCode ?? null},
${payload.uid ?? null},
${payload.grantId ?? null}
${dayjs().add(expiresIn, 'second').unix()}
)
on conflict (${fields.modelName}, ${fields.id}) do update
set ${setExcluded(
fields.payload,
fields.expiresAt,
fields.userCode,
fields.uid,
fields.grantId
)}
set ${setExcluded(fields.payload, fields.expiresAt)}
`);
},
find: async (id) => findByField(fields.id, id),
findByUserCode: async (userCode) => findByField(fields.userCode, userCode),
findByUid: async (uid) => findByField(fields.uid, uid),
findByUserCode: async (userCode) => findByPayloadField('userCode', userCode),
findByUid: async (uid) => findByPayloadField('uid', uid),
consume: async (id) => {
await pool.query(sql`
update ${table}
@ -93,7 +97,7 @@ export default function postgresAdapter(modelName: string) {
await pool.query(sql`
delete from ${table}
where ${fields.modelName}=${modelName}
and ${fields.grantId}=${grantId}
and ${fields.payload}->>'grantId'=${grantId}
`);
},
};

View file

@ -396,10 +396,10 @@
lodash.orderby "^4.6.0"
lodash.pick "^4.4.0"
"@logto/schemas@^1.0.5":
version "1.0.5"
resolved "https://registry.yarnpkg.com/@logto/schemas/-/schemas-1.0.5.tgz#7784ca8f58569bfe1feafb4ad5ca6f098b1a557e"
integrity sha512-JxA9uXNz2tt0NqAp8Gl0hp0L/dLhZakG5sm41JrJKfhtse2Yy2K1X7gBYkH1C9P8P+NcHhv/K4pEgY66yXMOGQ==
"@logto/schemas@^1.0.6":
version "1.0.6"
resolved "https://registry.yarnpkg.com/@logto/schemas/-/schemas-1.0.6.tgz#ef72c883b29d698a0d5b1075eee0a0de8ab80191"
integrity sha512-rQncFIl4cs3kZfXUTC6nrvDA1sdTlSn7ibQoZ0sjuXalqw6iGhpkQ/iQUYibkJMASc2KOtndMd47FwnQGISNUQ==
"@mrmlnc/readdir-enhanced@^2.2.1":
version "2.2.1"