mirror of
https://github.com/logto-io/logto.git
synced 2025-03-31 22:51:25 -05:00
feat: init connectors (#166)
* feat: init connectors * chore: optional in getConnectorById Co-authored-by: Gao Sun <gao@silverhand.io> * chore: pr fix * chore: pr fix * chore: fix PR Co-authored-by: Gao Sun <gao@silverhand.io>
This commit is contained in:
parent
b9f9847ec0
commit
c37354d42d
5 changed files with 73 additions and 0 deletions
12
packages/core/src/connectors/aliyun-dm/index.ts
Normal file
12
packages/core/src/connectors/aliyun-dm/index.ts
Normal file
|
@ -0,0 +1,12 @@
|
|||
import { ConnectorType } from '@logto/schemas';
|
||||
|
||||
import { ConnectorMetadata } from '../types';
|
||||
|
||||
export const metadata: ConnectorMetadata = {
|
||||
id: 'aliyun-dm',
|
||||
type: ConnectorType.Email,
|
||||
name: '阿里云邮件推送',
|
||||
logo: './logo.png',
|
||||
description:
|
||||
'邮件推送(DirectMail)是款简单高效的电子邮件群发服务,构建在阿里云基础之上,帮您快速、精准地实现事务邮件、通知邮件和批量邮件的发送。',
|
||||
};
|
26
packages/core/src/connectors/index.ts
Normal file
26
packages/core/src/connectors/index.ts
Normal file
|
@ -0,0 +1,26 @@
|
|||
import { findConnectorByIdAndType, insertConnector } from '@/queries/connector';
|
||||
|
||||
import * as AliyunDM from './aliyun-dm';
|
||||
import { ConnectorInstance } from './types';
|
||||
|
||||
const connectors: ConnectorInstance[] = [AliyunDM];
|
||||
|
||||
export const getConnectorById = (id: string): ConnectorInstance | null => {
|
||||
return connectors.find((connector) => connector.metadata.id === id) ?? null;
|
||||
};
|
||||
|
||||
export const initConnectors = async () => {
|
||||
await Promise.all(
|
||||
connectors.map(async ({ metadata: { id, type } }) => {
|
||||
const record = await findConnectorByIdAndType(id, type);
|
||||
if (record) {
|
||||
return;
|
||||
}
|
||||
|
||||
await insertConnector({
|
||||
id,
|
||||
type,
|
||||
});
|
||||
})
|
||||
);
|
||||
};
|
14
packages/core/src/connectors/types.ts
Normal file
14
packages/core/src/connectors/types.ts
Normal file
|
@ -0,0 +1,14 @@
|
|||
import { ConnectorType } from '@logto/schemas';
|
||||
|
||||
export interface ConnectorMetadata {
|
||||
id: string;
|
||||
type: ConnectorType;
|
||||
name: string;
|
||||
logo: string;
|
||||
description: string;
|
||||
}
|
||||
|
||||
// The name `Connector` is used for database, use `ConnectorInstance` to avoid confusing.
|
||||
export interface ConnectorInstance {
|
||||
metadata: ConnectorMetadata;
|
||||
}
|
|
@ -7,6 +7,7 @@ import Koa from 'koa';
|
|||
dotenv.config();
|
||||
|
||||
import initApp from './app/init';
|
||||
import { initConnectors } from './connectors';
|
||||
import { trustingTlsOffloadingProxies } from './env/consts';
|
||||
import initI18n from './i18n/init';
|
||||
|
||||
|
@ -16,6 +17,7 @@ const app = new Koa({
|
|||
|
||||
(async () => {
|
||||
try {
|
||||
await initConnectors();
|
||||
await initI18n();
|
||||
await initApp(app);
|
||||
} catch (error: unknown) {
|
||||
|
|
19
packages/core/src/queries/connector.ts
Normal file
19
packages/core/src/queries/connector.ts
Normal file
|
@ -0,0 +1,19 @@
|
|||
import { Connector, ConnectorDBEntry, Connectors, ConnectorType } from '@logto/schemas';
|
||||
import { sql } from 'slonik';
|
||||
|
||||
import { buildInsertInto } from '@/database/insert-into';
|
||||
import pool from '@/database/pool';
|
||||
import { convertToIdentifiers } from '@/database/utils';
|
||||
|
||||
const { table, fields } = convertToIdentifiers(Connectors);
|
||||
|
||||
export const findConnectorByIdAndType = async (id: string, type: ConnectorType) =>
|
||||
pool.maybeOne<Connector>(sql`
|
||||
select ${sql.join(Object.values(fields), sql`, `)}
|
||||
from ${table}
|
||||
where ${fields.id}=${id} and ${fields.type}=${type}
|
||||
`);
|
||||
|
||||
export const insertConnector = buildInsertInto<ConnectorDBEntry, Connector>(pool, Connectors, {
|
||||
returning: true,
|
||||
});
|
Loading…
Add table
Reference in a new issue