更新类型定义,优化获取表架构的返回值
This commit is contained in:
@@ -92,4 +92,9 @@ export type ResponseList<T = any> = {
|
|||||||
isLastPage?: boolean;
|
isLastPage?: boolean;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export type Result<T = any> = {
|
||||||
|
code: number;
|
||||||
|
data: T;
|
||||||
};
|
};
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
|
|
||||||
import { Query } from './api.ts';
|
import { Query, Result } from './api.ts';
|
||||||
import { Meta } from './meta/index.ts';
|
import { Meta } from './meta/index.ts';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* https://nocodb.com/docs/product-docs/developer-resources/rest-apis#v3-where-query-parameter
|
* https://nocodb.com/docs/product-docs/developer-resources/rest-apis#v3-where-query-parameter
|
||||||
*/
|
*/
|
||||||
@@ -36,6 +37,7 @@ type ResultList<T = any> = {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
type Id = string | number;
|
type Id = string | number;
|
||||||
|
const stystemFields = ['nc_order', 'nc_created_by', 'nc_updated_by']
|
||||||
/**
|
/**
|
||||||
* @url https://nocodb.com/apis/v2/data#tag/Table-Records
|
* @url https://nocodb.com/apis/v2/data#tag/Table-Records
|
||||||
*/
|
*/
|
||||||
@@ -102,11 +104,11 @@ export class Record {
|
|||||||
getTableMeta() {
|
getTableMeta() {
|
||||||
return this.meta.tables.getTableMeta(this.table);
|
return this.meta.tables.getTableMeta(this.table);
|
||||||
}
|
}
|
||||||
async getTableSchema() {
|
async getTableSchema(): Promise<Result<TableSchema>> {
|
||||||
const res = await this.getTableMeta();
|
const res = await this.getTableMeta();
|
||||||
if (res.code === 200) {
|
if (res.code === 200) {
|
||||||
const columns = res.data.columns;
|
const columns = res.data.columns;
|
||||||
return columns.map(col => ({
|
const _columns = columns.filter(col => !stystemFields.includes(col.title)).map(col => ({
|
||||||
id: col.id,
|
id: col.id,
|
||||||
name: col.title,
|
name: col.title,
|
||||||
type: col.uidt,
|
type: col.uidt,
|
||||||
@@ -114,9 +116,43 @@ export class Record {
|
|||||||
primary: col.pv,
|
primary: col.pv,
|
||||||
options: col.colOptions,
|
options: col.colOptions,
|
||||||
}));
|
}));
|
||||||
|
const schema: Schema = {};
|
||||||
|
for (const col of _columns) {
|
||||||
|
schema[col.name] = {
|
||||||
|
type: col.type,
|
||||||
|
required: col.required,
|
||||||
|
options: col.options,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
code: 200,
|
||||||
|
data: {
|
||||||
|
columns: _columns,
|
||||||
|
schema,
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
return [];
|
return res as {
|
||||||
|
code: number; data: {
|
||||||
|
columns: any[]; schema: Schema
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type Schema<T = {}> = {
|
||||||
|
[key: string]: any;
|
||||||
|
} & T;
|
||||||
|
export type TableSchema<T = {}> = {
|
||||||
|
columns: {
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
type: string;
|
||||||
|
required: boolean;
|
||||||
|
primary: boolean;
|
||||||
|
options: any;
|
||||||
|
}[];
|
||||||
|
schema: Schema<T>;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -10,4 +10,4 @@ import { writeFileSync } from 'node:fs';
|
|||||||
// writeFileSync('table-metadata.json', JSON.stringify(res, null, 2));
|
// writeFileSync('table-metadata.json', JSON.stringify(res, null, 2));
|
||||||
|
|
||||||
const res2 = await nocoApi.record.getTableSchema()
|
const res2 = await nocoApi.record.getTableSchema()
|
||||||
console.log(util.inspect(res2, { depth: null, colors: true }))
|
console.log(util.inspect(res2.data.schema, { depth: null, colors: true }))
|
||||||
Reference in New Issue
Block a user