0
Fork 0
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:
Wang Sijie 2022-01-11 16:26:54 +08:00 committed by GitHub
parent b9f9847ec0
commit c37354d42d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 73 additions and 0 deletions

View 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是款简单高效的电子邮件群发服务构建在阿里云基础之上帮您快速、精准地实现事务邮件、通知邮件和批量邮件的发送。',
};

View 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,
});
})
);
};

View 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;
}

View file

@ -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) {

View 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,
});