Skip to content

Commit d659208

Browse files
committed
添加 Knex 驱动和 PostgreSQL 依赖,更新元数据获取端点的 OpenAPI 规范
1 parent b9b1677 commit d659208

File tree

5 files changed

+78
-26
lines changed

5 files changed

+78
-26
lines changed

examples/apps/nestjs/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
"@nestjs/platform-express": "^10.0.0",
1515
"@objectql/core": "*",
1616
"@objectql/driver-mongo": "*",
17+
"@objectql/driver-knex": "*",
18+
"pg": "^8.11.3",
1719
"@objectql/api": "*",
1820
"@example/project-management": "*",
1921
"reflect-metadata": "^0.1.13",

examples/apps/nestjs/src/objectql/objectql.instance.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { ObjectQL } from '@objectql/core';
2-
import { MongoDriver } from '@objectql/driver-mongo';
2+
import { KnexDriver } from '@objectql/driver-knex';
33

44
export const objectql = new ObjectQL({
55
datasources: {
6-
default: new MongoDriver({
7-
url: process.env.MONGO_URL || 'mongodb://127.0.0.1:27017/objectql_example',
8-
dbName: 'objectql_example'
6+
default: new KnexDriver({
7+
client: 'pg',
8+
connection: process.env.DATABASE_URL || 'postgres://postgres:postgres@localhost:5432/objectql_example'
99
})
1010
},
1111
packages: [

packages/api/src/index.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,19 @@ export function createObjectQLRouter(options: ObjectQLServerOptions): Router {
143143
}
144144
});
145145

146+
router.get('/_schema/:type/:id', async (req: Request, res: Response) => {
147+
try {
148+
const { type, id } = req.params;
149+
const item = objectql.metadata.get(type, id);
150+
if (!item) {
151+
return res.status(404).json({ error: "Metadata not found" });
152+
}
153+
res.json(item);
154+
} catch (e: any) {
155+
res.status(500).json({ error: e.message });
156+
}
157+
});
158+
146159
// NONE for now, as :objectName is the root.
147160
// However, we might want /:objectName/count or /:objectName/aggregate.
148161

packages/api/src/swagger/generator.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,42 @@ export function generateOpenApiSpec(objectql: IObjectQL) {
6363
}
6464
};
6565

66+
paths['/api/_schema/{type}/{id}'] = {
67+
get: {
68+
summary: "Get Single Metadata",
69+
tags: ["System"],
70+
parameters: [
71+
{
72+
name: "type",
73+
in: "path",
74+
required: true,
75+
schema: { type: "string" },
76+
description: "Metadata type"
77+
},
78+
{
79+
name: "id",
80+
in: "path",
81+
required: true,
82+
schema: { type: "string" },
83+
description: "Metadata ID"
84+
}
85+
],
86+
responses: {
87+
200: {
88+
description: "Metadata content",
89+
content: {
90+
'application/json': {
91+
schema: { type: 'object' }
92+
}
93+
}
94+
},
95+
404: {
96+
description: "Metadata not found"
97+
}
98+
}
99+
}
100+
};
101+
66102
Object.values(configs).forEach((config) => {
67103
const objectName = config.name;
68104
const schemaName = objectName;

packages/metadata/src/plugins/objectql.ts

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -142,30 +142,31 @@ export function registerObjectQLPlugins(loader: MetadataLoader) {
142142
});
143143

144144
// Data
145-
loader.use({
146-
name: 'data',
147-
glob: ['**/*.data.yml', '**/*.data.yaml'],
148-
handler: (ctx) => {
149-
try {
150-
const content = ctx.content;
151-
const data = yaml.load(content);
152-
if (!Array.isArray(data)) return;
145+
// loader.use({
146+
// name: 'data',
147+
// glob: ['**/*.data.yml', '**/*.data.yaml'],
148+
// handler: (ctx) => {
149+
// try {
150+
// const content = ctx.content;
151+
// const data = yaml.load(content);
152+
// if (!Array.isArray(data)) return;
153153

154-
const basename = path.basename(ctx.file);
155-
const objectName = basename.replace(/\.data\.ya?ml$/, '');
154+
// const basename = path.basename(ctx.file);
155+
// const objectName = basename.replace(/\.data\.ya?ml$/, '');
156156

157-
const entry = ctx.registry.getEntry('object', objectName);
158-
if (entry) {
159-
const config = entry.content as ObjectConfig;
160-
config.data = data;
161-
} else {
162-
console.warn(`Found data for unknown object '${objectName}' in ${ctx.file}`);
163-
}
164-
} catch (e) {
165-
console.error(`Error loading data from ${ctx.file}:`, e);
166-
}
167-
}
168-
});
157+
// const entry = ctx.registry.getEntry('object', objectName);
158+
// if (entry) {
159+
// const config = entry.content as ObjectConfig;
160+
// // We don't want to load data into metadata
161+
// // config.data = data;
162+
// } else {
163+
// // console.warn(`Found data for unknown object '${objectName}' in ${ctx.file}`);
164+
// }
165+
// } catch (e) {
166+
// console.error(`Error loading data from ${ctx.file}:`, e);
167+
// }
168+
// }
169+
// });
169170
}
170171

171172
function registerObject(registry: any, obj: any, file: string, packageName?: string) {

0 commit comments

Comments
 (0)