mirror of
https://github.com/nocodb/nocodb.git
synced 2026-04-30 03:56:51 +00:00
refactor(gui): linting
Signed-off-by: Pranav C <61551451+pranavxc@users.noreply.github.com>
This commit is contained in:
@@ -1,15 +1,15 @@
|
||||
import RestApi from "@/components/project/spreadsheet/apis/restApi";
|
||||
import GqlApi from "@/components/project/spreadsheet/apis/gqlApi";
|
||||
import GrpcApi from "@/components/project/spreadsheet/apis/grpcApi";
|
||||
import RestApi from '@/components/project/spreadsheet/apis/restApi'
|
||||
import GqlApi from '@/components/project/spreadsheet/apis/gqlApi'
|
||||
import GrpcApi from '@/components/project/spreadsheet/apis/grpcApi'
|
||||
|
||||
export default class ApiFactory {
|
||||
static create(type, table, columns, ctx, meta) {
|
||||
static create (type, table, columns, ctx, meta) {
|
||||
if (type === 'graphql') {
|
||||
return new GqlApi(table, columns, meta, ctx,);
|
||||
return new GqlApi(table, columns, meta, ctx)
|
||||
} else if (type === 'grpc') {
|
||||
return new GrpcApi(table, ctx)
|
||||
} else if (type === 'rest') {
|
||||
return new RestApi(table, ctx);
|
||||
return new RestApi(table, ctx)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,45 +1,41 @@
|
||||
import inflection from 'inflection';
|
||||
|
||||
import inflection from 'inflection'
|
||||
|
||||
export default class GqlApi {
|
||||
|
||||
constructor(table, columns, meta, $ctx) {
|
||||
constructor (table, columns, meta, $ctx) {
|
||||
// this.table = table;
|
||||
this.columns = columns;
|
||||
this.meta = meta;
|
||||
this.$ctx = $ctx;
|
||||
this.columns = columns
|
||||
this.meta = meta
|
||||
this.$ctx = $ctx
|
||||
}
|
||||
|
||||
// todo: - get version letter and use table alias
|
||||
async list(params) {
|
||||
async list (params) {
|
||||
const data = await this.post(`/nc/${this.$ctx.$route.params.project_id}/v1/graphql`, {
|
||||
query: await this.gqlQuery(params),
|
||||
variables: null
|
||||
});
|
||||
return data.data.data[this.gqlQueryListName];
|
||||
})
|
||||
return data.data.data[this.gqlQueryListName]
|
||||
}
|
||||
|
||||
async count(params) {
|
||||
async count (params) {
|
||||
const data = await this.post(`/nc/${this.$ctx.$route.params.project_id}/v1/graphql`, {
|
||||
query: this.gqlCountQuery(params),
|
||||
variables: null
|
||||
});
|
||||
return data.data.data[this.gqlQueryCountName];
|
||||
})
|
||||
return data.data.data[this.gqlQueryCountName]
|
||||
}
|
||||
|
||||
|
||||
post(url, params) {
|
||||
post (url, params) {
|
||||
return this.$axios({
|
||||
url: `${this.$axios.defaults.baseURL}${url}`,
|
||||
method: 'post',
|
||||
data: params,
|
||||
data: params
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
generateQueryParams(params) {
|
||||
if (!params) return '(where:"")';
|
||||
const res = [];
|
||||
generateQueryParams (params) {
|
||||
if (!params) { return '(where:"")' }
|
||||
const res = []
|
||||
if ('limit' in params) {
|
||||
res.push(`limit: ${params.limit}`)
|
||||
}
|
||||
@@ -58,39 +54,39 @@ export default class GqlApi {
|
||||
if (params.conditionGraph) {
|
||||
res.push(`conditionGraph: ${JSON.stringify(JSON.stringify(params.conditionGraph))}`)
|
||||
}
|
||||
return `(${res.join(',')})`;
|
||||
return `(${res.join(',')})`
|
||||
}
|
||||
|
||||
async gqlQuery(params) {
|
||||
async gqlQuery (params) {
|
||||
return `{${this.gqlQueryListName}${this.generateQueryParams(params)}{${this.gqlReqBody}${await this.gqlRelationReqBody(params)}}}`
|
||||
}
|
||||
|
||||
gqlReadQuery(id) {
|
||||
gqlReadQuery (id) {
|
||||
return `{${this.gqlQueryReadName}(id:"${id}"){${this.gqlReqBody}}}`
|
||||
}
|
||||
|
||||
gqlCountQuery(params) {
|
||||
gqlCountQuery (params) {
|
||||
return `{${this.gqlQueryCountName}${this.generateQueryParams(params)}}`
|
||||
}
|
||||
|
||||
get gqlQueryListName() {
|
||||
return `${this.meta._tn}List`;
|
||||
get gqlQueryListName () {
|
||||
return `${this.meta._tn}List`
|
||||
}
|
||||
|
||||
get gqlQueryReadName() {
|
||||
return `${this.meta._tn}Read`;
|
||||
get gqlQueryReadName () {
|
||||
return `${this.meta._tn}Read`
|
||||
}
|
||||
|
||||
get tableCamelized() {
|
||||
return `${this.meta._tn}`;
|
||||
get tableCamelized () {
|
||||
return `${this.meta._tn}`
|
||||
}
|
||||
|
||||
get gqlReqBody() {
|
||||
get gqlReqBody () {
|
||||
return `\n${this.columns.map(c => c._cn).join('\n')}\n`
|
||||
}
|
||||
|
||||
async gqlRelationReqBody(params) {
|
||||
let str = '';
|
||||
async gqlRelationReqBody (params) {
|
||||
let str = ''
|
||||
if (params.hm) {
|
||||
for (const child of params.hm.split(',')) {
|
||||
await this.$ctx.$store.dispatch('meta/ActLoadMeta', {
|
||||
@@ -98,7 +94,7 @@ export default class GqlApi {
|
||||
env: this.$ctx.nodes.env,
|
||||
tn: child
|
||||
})
|
||||
const meta = this.$ctx.$store.state.meta.metas[child];
|
||||
const meta = this.$ctx.$store.state.meta.metas[child]
|
||||
if (meta) {
|
||||
str += `\n${meta._tn}List{\n${meta.columns.map(c => c._cn).join('\n')}\n}`
|
||||
}
|
||||
@@ -111,7 +107,7 @@ export default class GqlApi {
|
||||
env: this.$ctx.nodes.env,
|
||||
tn: parent
|
||||
})
|
||||
const meta = this.$ctx.$store.state.meta.metas[parent];
|
||||
const meta = this.$ctx.$store.state.meta.metas[parent]
|
||||
if (meta) {
|
||||
str += `\n${meta._tn}Read{\n${meta.columns.map(c => c._cn).join('\n')}\n}`
|
||||
}
|
||||
@@ -124,33 +120,32 @@ export default class GqlApi {
|
||||
env: this.$ctx.nodes.env,
|
||||
tn: mm
|
||||
})
|
||||
const meta = this.$ctx.$store.state.meta.metas[mm];
|
||||
const meta = this.$ctx.$store.state.meta.metas[mm]
|
||||
if (meta) {
|
||||
str += `\n${meta._tn}MMList{\n${meta.columns.map(c => c._cn).join('\n')}\n}`
|
||||
}
|
||||
}
|
||||
}
|
||||
return str;
|
||||
return str
|
||||
}
|
||||
|
||||
get gqlQueryCountName() {
|
||||
return `${this.tableCamelized}Count`;
|
||||
get gqlQueryCountName () {
|
||||
return `${this.tableCamelized}Count`
|
||||
}
|
||||
|
||||
get gqlMutationCreateName() {
|
||||
return `${this.tableCamelized}Create`;
|
||||
get gqlMutationCreateName () {
|
||||
return `${this.tableCamelized}Create`
|
||||
}
|
||||
|
||||
get gqlMutationUpdateName() {
|
||||
return `${this.tableCamelized}Update`;
|
||||
get gqlMutationUpdateName () {
|
||||
return `${this.tableCamelized}Update`
|
||||
}
|
||||
|
||||
get gqlMutationDeleteName() {
|
||||
return `${this.tableCamelized}Delete`;
|
||||
get gqlMutationDeleteName () {
|
||||
return `${this.tableCamelized}Delete`
|
||||
}
|
||||
|
||||
|
||||
async paginatedList(params) {
|
||||
async paginatedList (params) {
|
||||
// const list = await this.list(params);
|
||||
// const count = (await this.count({where: params.where || ''}));
|
||||
const [list, count] = await Promise.all([
|
||||
@@ -159,23 +154,22 @@ export default class GqlApi {
|
||||
conditionGraph: params.conditionGraph,
|
||||
condition: params.condition
|
||||
})
|
||||
]);
|
||||
return {list, count};
|
||||
])
|
||||
return { list, count }
|
||||
}
|
||||
|
||||
|
||||
async update(id, data, oldData) {
|
||||
async update (id, data, oldData) {
|
||||
const data1 = await this.post(`/nc/${this.$ctx.$route.params.project_id}/v1/graphql`, {
|
||||
query: `mutation update($id:String!, $data:${this.tableCamelized}Input){
|
||||
${this.gqlMutationUpdateName}(id: $id, data: $data)
|
||||
}`,
|
||||
variables: {
|
||||
id: id, data
|
||||
id, data
|
||||
}
|
||||
});
|
||||
})
|
||||
|
||||
const colName = Object.keys(data)[0];
|
||||
this.$ctx.$store.dispatch('sqlMgr/ActSqlOp', [{dbAlias: this.$ctx.nodes.dbAlias}, 'xcAuditCreate', {
|
||||
const colName = Object.keys(data)[0]
|
||||
this.$ctx.$store.dispatch('sqlMgr/ActSqlOp', [{ dbAlias: this.$ctx.nodes.dbAlias }, 'xcAuditCreate', {
|
||||
tn: this.table,
|
||||
cn: colName,
|
||||
pk: id,
|
||||
@@ -183,10 +177,10 @@ export default class GqlApi {
|
||||
prevValue: oldData[colName]
|
||||
}])
|
||||
|
||||
return data1.data.data[this.gqlMutationUpdateName];
|
||||
return data1.data.data[this.gqlMutationUpdateName]
|
||||
}
|
||||
|
||||
async insert(data) {
|
||||
async insert (data) {
|
||||
const data1 = await this.post(`/nc/${this.$ctx.$route.params.project_id}/v1/graphql`, {
|
||||
query: `mutation create($data:${this.tableCamelized}Input){
|
||||
${this.gqlMutationCreateName}(data: $data){${this.gqlReqBody}}
|
||||
@@ -194,44 +188,38 @@ export default class GqlApi {
|
||||
variables: {
|
||||
data
|
||||
}
|
||||
});
|
||||
return data1.data.data[this.gqlMutationCreateName];
|
||||
})
|
||||
return data1.data.data[this.gqlMutationCreateName]
|
||||
}
|
||||
|
||||
async delete(id) {
|
||||
async delete (id) {
|
||||
const data1 = await this.post(`/nc/${this.$ctx.$route.params.project_id}/v1/graphql`, {
|
||||
query: `mutation delete($id:String!){
|
||||
${this.gqlMutationDeleteName}(id: $id)
|
||||
}`,
|
||||
variables: {id}
|
||||
});
|
||||
variables: { id }
|
||||
})
|
||||
|
||||
return data1.data.data[this.gqlMutationDeleteName];
|
||||
return data1.data.data[this.gqlMutationDeleteName]
|
||||
}
|
||||
|
||||
|
||||
async read(id) {
|
||||
|
||||
async read (id) {
|
||||
const data = await this.post(`/nc/${this.$ctx.$route.params.project_id}/v1/graphql`, {
|
||||
query: this.gqlReadQuery(id),
|
||||
variables: null
|
||||
});
|
||||
return data.data.data[this.gqlQueryReadName];
|
||||
|
||||
})
|
||||
return data.data.data[this.gqlQueryReadName]
|
||||
}
|
||||
|
||||
|
||||
get $axios() {
|
||||
return this.$ctx.$axios;
|
||||
get $axios () {
|
||||
return this.$ctx.$axios
|
||||
}
|
||||
|
||||
get table() {
|
||||
return this.meta && this.meta._tn && inflection.camelize(this.meta._tn);
|
||||
get table () {
|
||||
return this.meta && this.meta._tn && inflection.camelize(this.meta._tn)
|
||||
}
|
||||
|
||||
|
||||
async paginatedM2mNotChildrenList(params, assoc, pid) {
|
||||
|
||||
async paginatedM2mNotChildrenList (params, assoc, pid) {
|
||||
const list = await this.post(`/nc/${this.$ctx.$route.params.project_id}/v1/graphql`, {
|
||||
query: `query m2mNotChildren($pid: String!,$assoc:String!,$parent:String!, $limit:Int, $offset:Int){
|
||||
m2mNotChildren(pid: $pid,assoc:$assoc,parent:$parent,limit:$limit, offset:$offset)
|
||||
@@ -239,7 +227,7 @@ export default class GqlApi {
|
||||
variables: {
|
||||
parent: this.meta.tn, assoc, pid: pid + '', ...params
|
||||
}
|
||||
});
|
||||
})
|
||||
const count = await this.post(`/nc/${this.$ctx.$route.params.project_id}/v1/graphql`, {
|
||||
query: `query m2mNotChildrenCount($pid: String!,$assoc:String!,$parent:String!){
|
||||
m2mNotChildrenCount(pid: $pid,assoc:$assoc,parent:$parent)
|
||||
@@ -247,11 +235,9 @@ export default class GqlApi {
|
||||
variables: {
|
||||
parent: this.meta.tn, assoc, pid: pid + ''
|
||||
}
|
||||
});
|
||||
return {list: list.data.data.m2mNotChildren, count: count.data.data.m2mNotChildrenCount.count};
|
||||
})
|
||||
return { list: list.data.data.m2mNotChildren, count: count.data.data.m2mNotChildrenCount.count }
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
/**
|
||||
* @copyright Copyright (c) 2021, Xgene Cloud Ltd
|
||||
|
||||
@@ -1,26 +1,23 @@
|
||||
export default class GrpcApi {
|
||||
|
||||
constructor(table, ctx) {
|
||||
this.table = table;
|
||||
this.ctx = ctx;
|
||||
constructor (table, ctx) {
|
||||
this.table = table
|
||||
this.ctx = ctx
|
||||
}
|
||||
|
||||
// todo: - get version letter and use table alias
|
||||
|
||||
async paginatedList(params) {
|
||||
|
||||
async paginatedList (params) {
|
||||
const result = await this.ctx.$store.dispatch('sqlMgr/ActSqlOp', [{
|
||||
env: this.ctx.nodes.env,
|
||||
dbAlias: this.ctx.nodes.dbAlias
|
||||
}, 'list', {
|
||||
tn: this.table,
|
||||
size: params.limit,
|
||||
page: ((params.offset || 0) / (params.limit || 20)) + 1,
|
||||
page: ((params.offset || 0) / (params.limit || 20)) + 1
|
||||
// orderBy:
|
||||
}])
|
||||
return result.data;
|
||||
return result.data
|
||||
}
|
||||
|
||||
}
|
||||
/**
|
||||
* @copyright Copyright (c) 2021, Xgene Cloud Ltd
|
||||
|
||||
@@ -1,42 +1,41 @@
|
||||
export default class RestApi {
|
||||
|
||||
constructor(table, $ctx) {
|
||||
this.table = table;
|
||||
this.$ctx = $ctx;
|
||||
constructor (table, $ctx) {
|
||||
this.table = table
|
||||
this.$ctx = $ctx
|
||||
}
|
||||
|
||||
// todo: - get version letter and use table alias
|
||||
async list(params) {
|
||||
async list (params) {
|
||||
// const data = await this.get(`/nc/${this.$ctx.$route.params.project_id}/api/v1/${this.table}`, params)
|
||||
const data = await this.get(`/nc/${this.$ctx.$route.params.project_id}/api/v1/${this.table}`, params)
|
||||
return data.data;
|
||||
return data.data
|
||||
}
|
||||
|
||||
async read(id) {
|
||||
const data = await this.get(`/nc/${this.$ctx.$route.params.project_id}/api/v1/${this.table}/${id}`);
|
||||
return data.data;
|
||||
async read (id) {
|
||||
const data = await this.get(`/nc/${this.$ctx.$route.params.project_id}/api/v1/${this.table}/${id}`)
|
||||
return data.data
|
||||
}
|
||||
|
||||
async count(params) {
|
||||
async count (params) {
|
||||
if (this.timeout) {
|
||||
return this.timeout;
|
||||
return this.timeout
|
||||
}
|
||||
try {
|
||||
const data = await this.get(`/nc/${this.$ctx.$route.params.project_id}/api/v1/${this.table}/count`, params, {
|
||||
timeout: 10000,
|
||||
});
|
||||
return data && data.data;
|
||||
timeout: 10000
|
||||
})
|
||||
return data && data.data
|
||||
} catch (e) {
|
||||
if (e.code === "ECONNABORTED") {
|
||||
return this.timeout = {count: Infinity};
|
||||
if (e.code === 'ECONNABORTED') {
|
||||
// eslint-disable-next-line no-return-assign
|
||||
return this.timeout = { count: Infinity }
|
||||
} else {
|
||||
throw e;
|
||||
throw e
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
get(url, params, extras = {}) {
|
||||
get (url, params, extras = {}) {
|
||||
return this.$axios({
|
||||
url,
|
||||
params,
|
||||
@@ -44,33 +43,32 @@ export default class RestApi {
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
async paginatedList(params) {
|
||||
async paginatedList (params) {
|
||||
// const list = await this.list(params);
|
||||
// const count = (await this.count({where: params.where || ''})).count;
|
||||
const [list, {count}] = await Promise.all([this.list(params), this.count({
|
||||
const [list, { count }] = await Promise.all([this.list(params), this.count({
|
||||
where: params.where || '',
|
||||
conditionGraph: params.conditionGraph
|
||||
})]);
|
||||
return {list, count};
|
||||
})])
|
||||
return { list, count }
|
||||
}
|
||||
|
||||
async paginatedM2mNotChildrenList(params, assoc, pid) {
|
||||
///api/v1/Film/m2mNotChildren/film_actor/44
|
||||
async paginatedM2mNotChildrenList (params, assoc, pid) {
|
||||
/// api/v1/Film/m2mNotChildren/film_actor/44
|
||||
// const list = await this.list(params);
|
||||
// const count = (await this.count({where: params.where || ''})).count;
|
||||
const {list, info: {count}} = (await this.get(`/nc/${this.$ctx.$route.params.project_id}/api/v1/${this.table}/m2mNotChildren/${assoc}/${pid}`, params)).data
|
||||
return {list, count};
|
||||
const { list, info: { count } } = (await this.get(`/nc/${this.$ctx.$route.params.project_id}/api/v1/${this.table}/m2mNotChildren/${assoc}/${pid}`, params)).data
|
||||
return { list, count }
|
||||
}
|
||||
|
||||
async update(id, data, oldData) {
|
||||
async update (id, data, oldData) {
|
||||
const res = await this.$axios({
|
||||
method: 'put',
|
||||
url: `/nc/${this.$ctx.$route.params.project_id}/api/v1/${this.table}/${id}`,
|
||||
data,
|
||||
});
|
||||
const colName = Object.keys(data)[0];
|
||||
this.$ctx.$store.dispatch('sqlMgr/ActSqlOp', [{dbAlias: this.$ctx.nodes.dbAlias}, 'xcAuditCreate', {
|
||||
data
|
||||
})
|
||||
const colName = Object.keys(data)[0]
|
||||
this.$ctx.$store.dispatch('sqlMgr/ActSqlOp', [{ dbAlias: this.$ctx.nodes.dbAlias }, 'xcAuditCreate', {
|
||||
tn: this.table,
|
||||
cn: colName,
|
||||
pk: id,
|
||||
@@ -78,35 +76,33 @@ export default class RestApi {
|
||||
prevValue: oldData[colName]
|
||||
}])
|
||||
|
||||
return res;
|
||||
return res
|
||||
}
|
||||
|
||||
async insert(data) {
|
||||
async insert (data) {
|
||||
return (await this.$axios({
|
||||
method: 'post',
|
||||
url: `/nc/${this.$ctx.$route.params.project_id}/api/v1/${this.table}`,
|
||||
data,
|
||||
})).data;
|
||||
data
|
||||
})).data
|
||||
}
|
||||
|
||||
async delete(id) {
|
||||
async delete (id) {
|
||||
return this.$axios({
|
||||
method: 'delete',
|
||||
url: `/nc/${this.$ctx.$route.params.project_id}/api/v1/${this.table}/${id}`,
|
||||
url: `/nc/${this.$ctx.$route.params.project_id}/api/v1/${this.table}/${id}`
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
get $axios() {
|
||||
return this.$ctx.$axios;
|
||||
get $axios () {
|
||||
return this.$ctx.$axios
|
||||
}
|
||||
|
||||
get apiUrl() {
|
||||
return `${process.env.NODE_ENV === 'production' ?
|
||||
`${window.location.protocol}//${window.location.hostname}${window.location.port ? `:${window.location.port}` : ''}`
|
||||
get apiUrl () {
|
||||
return `${process.env.NODE_ENV === 'production'
|
||||
? `${window.location.protocol}//${window.location.hostname}${window.location.port ? `:${window.location.port}` : ''}`
|
||||
: 'http://localhost:8080'}/nc/${this.$ctx.$route.params.project_id}/api/v1/${this.table}`
|
||||
}
|
||||
|
||||
}
|
||||
/**
|
||||
* @copyright Copyright (c) 2021, Xgene Cloud Ltd
|
||||
|
||||
Reference in New Issue
Block a user