Browse Source

feat: accept both project id or title in data apis

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/1854/head
Pranav C 3 years ago
parent
commit
c59da67f67
  1. 49
      packages/nocodb/src/lib/noco-models/Project.ts
  2. 6
      packages/nocodb/src/lib/noco/meta/api/dataApis/helpers.ts
  3. 2
      packages/nocodb/src/lib/noco/meta/helpers/extractProjectIdAndAuthenticate.ts

49
packages/nocodb/src/lib/noco-models/Project.ts

@ -323,4 +323,53 @@ export default class Project implements ProjectType {
} }
return projectData?.id && this.get(projectData?.id, ncMeta); return projectData?.id && this.get(projectData?.id, ncMeta);
} }
static async getByTitleOrId(titleOrId: string, ncMeta = Noco.ncMeta) {
const projectId =
titleOrId &&
(await NocoCache.get(
`${CacheScope.PROJECT}:${titleOrId}`,
CacheGetType.TYPE_OBJECT
));
let projectData = null;
if (!projectId) {
projectData = await Noco.ncMeta.metaGet2(
null,
null,
MetaTable.PROJECT,
{
deleted: false
},
null,
{
_or: [
{
id: {
eq: titleOrId
}
},
{
title: {
eq: titleOrId
}
}
]
}
);
await NocoCache.set(
`${CacheScope.PROJECT}:${titleOrId}`,
projectData?.id
);
} else {
return this.get(projectId);
}
return projectData?.id && this.get(projectData?.id, ncMeta);
}
static async getWithInfoByTitleOrId(titleOrId: string, ncMeta = Noco.ncMeta) {
const project = await this.getByTitleOrId(titleOrId, ncMeta);
if (project) await project.getBases(ncMeta);
return project;
}
} }

6
packages/nocodb/src/lib/noco/meta/api/dataApis/helpers.ts

@ -18,11 +18,7 @@ export async function getViewAndModelFromRequestByAliasOrId(
| Request<{ projectName: string; tableName: string; viewName?: string }> | Request<{ projectName: string; tableName: string; viewName?: string }>
| Request | Request
) { ) {
let project = await Project.getWithInfoByTitle(req.params.projectName); const project = await Project.getWithInfoByTitleOrId(req.params.projectName);
if (!project) {
project = await Project.getWithInfo(req.params.projectName);
}
const model = await Model.getByAliasOrId({ const model = await Model.getByAliasOrId({
project_id: project.id, project_id: project.id,

2
packages/nocodb/src/lib/noco/meta/helpers/extractProjectIdAndAuthenticate.ts

@ -18,7 +18,7 @@ export default async (req, res, next) => {
// extract project id based on request path params // extract project id based on request path params
if (params.projectName) { if (params.projectName) {
const project = await Project.getByTitle(params.projectName); const project = await Project.getByTitleOrId(params.projectName);
req.ncProjectId = project.id; req.ncProjectId = project.id;
res.locals.project = project; res.locals.project = project;
} }

Loading…
Cancel
Save