add get table columns

This commit is contained in:
2025-12-03 11:38:54 +08:00
parent 5b79068534
commit f0b75f91e5
6 changed files with 54 additions and 27 deletions

View File

@@ -1,6 +1,6 @@
{ {
"name": "@kevisual/noco", "name": "@kevisual/noco",
"version": "0.0.7", "version": "0.0.8",
"description": "", "description": "",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {

View File

@@ -53,7 +53,6 @@ export class Query {
fetchOptions.body = JSON.stringify(options.data); fetchOptions.body = JSON.stringify(options.data);
} }
return fetch(url.href, fetchOptions).then(async (response) => { return fetch(url.href, fetchOptions).then(async (response) => {
console.log('Response Status:', response.status, response);
if (!response.ok) { if (!response.ok) {
return { code: response.status, message: response.statusText }; return { code: response.status, message: response.statusText };
} }
@@ -63,8 +62,8 @@ export class Query {
if (isArray) { if (isArray) {
return { return {
code: 200, code: 200,
list: result, data: { list: result },
} as ResponseList; };
} else if (result && typeof result === 'object' && !('code' in result)) { } else if (result && typeof result === 'object' && !('code' in result)) {
return { return {
code: 200, code: 200,
@@ -81,12 +80,14 @@ export class Query {
export type ResponseList<T = any> = { export type ResponseList<T = any> = {
code: number; code: number;
list: T[]; data: {
pageInfo?: { list: T[];
totalRows?: number; pageInfo?: {
page?: number; totalRows?: number;
pageSize?: number; page?: number;
isFirstPage?: boolean; pageSize?: number;
isLastPage?: boolean; isFirstPage?: boolean;
}; isLastPage?: boolean;
};
}
}; };

View File

@@ -19,9 +19,9 @@ export class NocoApi {
const token = options?.token; const token = options?.token;
const baseURL = options?.baseURL; const baseURL = options?.baseURL;
this.query = new Query({ baseURL, token }); this.query = new Query({ baseURL, token });
this.record = new Record(this.query, table);
this.meta = new Meta({ query: this.query }); this.meta = new Meta({ query: this.query });
this.upload = new Upload(this.query); this.upload = new Upload(this.query);
this.record = new Record({ query: this.query, table, meta: this.meta });
} }
/** /**
* *

View File

@@ -1,5 +1,6 @@
import { Query } from './api.ts'; import { Query } from './api.ts';
import { Meta } from './meta/index.ts';
type QueryParams = { type QueryParams = {
/** /**
* fields=field1,field2 * fields=field1,field2
@@ -20,14 +21,16 @@ type QueryParams = {
}; };
type ResultList<T = any> = { type ResultList<T = any> = {
code: number; code: number;
list: T[]; data: {
pageInfo?: { list: T[];
totalRows?: number; pageInfo?: {
page?: number; totalRows?: number;
pageSize?: number; page?: number;
isFirstPage?: boolean; pageSize?: number;
isLastPage?: boolean; isFirstPage?: boolean;
}; isLastPage?: boolean;
};
}
}; };
type Id = string | number; type Id = string | number;
/** /**
@@ -36,9 +39,11 @@ type Id = string | number;
export class Record { export class Record {
query: Query; query: Query;
table: string; table: string;
constructor(query: Query, table: string) { meta: Meta
constructor({ query, table, meta }: { query: Query, table: string, meta?: Meta }) {
this.query = query; this.query = query;
this.table = table; this.table = table;
this.meta = meta;
} }
list<T = any>(params: QueryParams = {}): Promise<ResultList<T>> { list<T = any>(params: QueryParams = {}): Promise<ResultList<T>> {
return this.query.makeRequest(`/api/v2/tables/${this.table}/records`, { return this.query.makeRequest(`/api/v2/tables/${this.table}/records`, {
@@ -57,7 +62,7 @@ export class Record {
method: 'GET', method: 'GET',
}); });
} }
update(data: { Id?: Id; [key: string]: any }) { update(data: { Id?: Id;[key: string]: any }) {
return this.query.makeRequest(`/api/v2/tables/${this.table}/records`, { return this.query.makeRequest(`/api/v2/tables/${this.table}/records`, {
method: 'PATCH', method: 'PATCH',
data, data,
@@ -90,6 +95,10 @@ export class Record {
method: 'DELETE', method: 'DELETE',
}); });
} }
getTableMeta() {
return this.meta.tables.getTableMeta(this.table);
}
} }

View File

@@ -1,12 +1,13 @@
import { NocoApi } from './../src/main.ts'; import { NocoApi } from './../src/main.ts';
import { useConfig } from '@kevisual/use-config' import { useConfig } from '@kevisual/use-config'
import { CreateColumnData } from '../src/meta/index.ts'; import { CreateBaseData } from '../src/meta/index.ts';
export const config = useConfig() export const config = useConfig()
import util from 'node:util'; import util from 'node:util';
import { writeFile, writeFileSync } from 'node:fs';
// # 签到表 // # 签到表
// const table = 'mcby44q8zrayvn9' // const table = 'mcby44q8zrayvn9'
// 本地 // 本地
const table = 'mecdgojq151iwk9' const table = 'mecdgojq151iwk9' // gitstarred_repos
export const nocoApi = new NocoApi({ export const nocoApi = new NocoApi({
baseURL: config.NOCODB_URL || 'http://localhost:8080', baseURL: config.NOCODB_URL || 'http://localhost:8080',
token: config.NOCODB_API_KEY || '', token: config.NOCODB_API_KEY || '',
@@ -14,5 +15,11 @@ export const nocoApi = new NocoApi({
}); });
// const list = await nocoApi.record.list() // const res = await nocoApi.record.list()
// console.log(util.inspect(list, { depth: null, colors: true })) // console.log(util.inspect(res.data, { depth: null, colors: true }))
const res =await nocoApi.record.getTableMeta()
// console.log(util.inspect(res, { depth: null, colors: true }))
const keys = Object.keys(res.data || {})
console.log('表字段列表:', keys);
writeFileSync('table-metadata.json', JSON.stringify(res, null, 2));

10
test/list-table.ts Normal file
View File

@@ -0,0 +1,10 @@
import { nocoApi } from './common.ts';
import util from 'node:util';
import { writeFileSync } from 'node:fs';
const res =await nocoApi.record.getTableMeta()
// console.log(util.inspect(res, { depth: null, colors: true }))
const keys = Object.keys(res.data || {})
console.log('表字段列表:', keys);
writeFileSync('table-metadata.json', JSON.stringify(res, null, 2));