|
|
@ -25,6 +25,8 @@ export default class Base implements BaseType { |
|
|
|
updated_at?: any; |
|
|
|
updated_at?: any; |
|
|
|
inflection_column?: string; |
|
|
|
inflection_column?: string; |
|
|
|
inflection_table?: string; |
|
|
|
inflection_table?: string; |
|
|
|
|
|
|
|
order?: number; |
|
|
|
|
|
|
|
enabled?: boolean; |
|
|
|
|
|
|
|
|
|
|
|
constructor(base: Partial<Base>) { |
|
|
|
constructor(base: Partial<Base>) { |
|
|
|
Object.assign(this, base); |
|
|
|
Object.assign(this, base); |
|
|
@ -44,6 +46,8 @@ export default class Base implements BaseType { |
|
|
|
'updated_at', |
|
|
|
'updated_at', |
|
|
|
'inflection_column', |
|
|
|
'inflection_column', |
|
|
|
'inflection_table', |
|
|
|
'inflection_table', |
|
|
|
|
|
|
|
'order', |
|
|
|
|
|
|
|
'enabled', |
|
|
|
]); |
|
|
|
]); |
|
|
|
insertObj.config = CryptoJS.AES.encrypt( |
|
|
|
insertObj.config = CryptoJS.AES.encrypt( |
|
|
|
JSON.stringify(base.config), |
|
|
|
JSON.stringify(base.config), |
|
|
@ -56,13 +60,19 @@ export default class Base implements BaseType { |
|
|
|
MetaTable.BASES, |
|
|
|
MetaTable.BASES, |
|
|
|
insertObj |
|
|
|
insertObj |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
await NocoCache.appendToList( |
|
|
|
await NocoCache.appendToList( |
|
|
|
CacheScope.BASE, |
|
|
|
CacheScope.BASE, |
|
|
|
[base.projectId], |
|
|
|
[base.projectId], |
|
|
|
`${CacheScope.BASE}:${id}` |
|
|
|
`${CacheScope.BASE}:${id}` |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
return this.get(id, ncMeta); |
|
|
|
// call before reorder to update cache
|
|
|
|
|
|
|
|
const returnBase = await this.get(id, ncMeta); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await this.reorderBases(base.projectId); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return returnBase; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static async updateBase( |
|
|
|
public static async updateBase( |
|
|
@ -81,7 +91,7 @@ export default class Base implements BaseType { |
|
|
|
await NocoCache.deepDel( |
|
|
|
await NocoCache.deepDel( |
|
|
|
CacheScope.BASE, |
|
|
|
CacheScope.BASE, |
|
|
|
`${CacheScope.BASE}:${baseId}`, |
|
|
|
`${CacheScope.BASE}:${baseId}`, |
|
|
|
CacheDelDirection.PARENT_TO_CHILD |
|
|
|
CacheDelDirection.CHILD_TO_PARENT |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
const insertObj = extractProps(base, [ |
|
|
|
const insertObj = extractProps(base, [ |
|
|
@ -94,37 +104,48 @@ export default class Base implements BaseType { |
|
|
|
'updated_at', |
|
|
|
'updated_at', |
|
|
|
'inflection_column', |
|
|
|
'inflection_column', |
|
|
|
'inflection_table', |
|
|
|
'inflection_table', |
|
|
|
|
|
|
|
'order', |
|
|
|
|
|
|
|
'enabled', |
|
|
|
]); |
|
|
|
]); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (insertObj.config) { |
|
|
|
insertObj.config = CryptoJS.AES.encrypt( |
|
|
|
insertObj.config = CryptoJS.AES.encrypt( |
|
|
|
JSON.stringify(base.config), |
|
|
|
JSON.stringify(base.config), |
|
|
|
Noco.getConfig()?.auth?.jwt?.secret |
|
|
|
Noco.getConfig()?.auth?.jwt?.secret |
|
|
|
).toString(); |
|
|
|
).toString(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// type property is undefined even if not provided
|
|
|
|
|
|
|
|
if (!insertObj.type) { |
|
|
|
|
|
|
|
insertObj.type = oldBase.type; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// add missing (not updated) fields
|
|
|
|
|
|
|
|
const finalInsertObj = { |
|
|
|
|
|
|
|
...oldBase, |
|
|
|
|
|
|
|
...insertObj, |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
const { id } = await ncMeta.metaInsert2( |
|
|
|
const { id } = await ncMeta.metaInsert2( |
|
|
|
base.projectId, |
|
|
|
base.projectId, |
|
|
|
null, |
|
|
|
null, |
|
|
|
MetaTable.BASES, |
|
|
|
MetaTable.BASES, |
|
|
|
insertObj |
|
|
|
finalInsertObj |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
await NocoCache.appendToList( |
|
|
|
await NocoCache.appendToList( |
|
|
|
CacheScope.BASE, |
|
|
|
CacheScope.BASE, |
|
|
|
[base.projectId], |
|
|
|
[base.projectId], |
|
|
|
`${CacheScope.BASE}:${id}` |
|
|
|
`${CacheScope.BASE}:${id}` |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
return this.get(id, ncMeta); |
|
|
|
// call before reorder to update cache
|
|
|
|
} |
|
|
|
const returnBase = await this.get(id, ncMeta); |
|
|
|
|
|
|
|
|
|
|
|
/* |
|
|
|
await this.reorderBases(base.projectId, id, ncMeta); |
|
|
|
await ncMeta.metaDelete(null, null, MetaTable.COL_LOOKUP, { |
|
|
|
|
|
|
|
fk_column_id: colId, |
|
|
|
return returnBase; |
|
|
|
}); |
|
|
|
} |
|
|
|
await NocoCache.deepDel( |
|
|
|
|
|
|
|
CacheScope.COL_LOOKUP, |
|
|
|
|
|
|
|
`${CacheScope.COL_LOOKUP}:${colId}`, |
|
|
|
|
|
|
|
CacheDelDirection.CHILD_TO_PARENT |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static async list( |
|
|
|
static async list( |
|
|
|
args: { projectId: string }, |
|
|
|
args: { projectId: string }, |
|
|
@ -137,10 +158,22 @@ export default class Base implements BaseType { |
|
|
|
baseDataList = await ncMeta.metaList2( |
|
|
|
baseDataList = await ncMeta.metaList2( |
|
|
|
args.projectId, |
|
|
|
args.projectId, |
|
|
|
null, |
|
|
|
null, |
|
|
|
MetaTable.BASES |
|
|
|
MetaTable.BASES, |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
orderBy: { |
|
|
|
|
|
|
|
order: 'asc', |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
} |
|
|
|
); |
|
|
|
); |
|
|
|
await NocoCache.setList(CacheScope.BASE, [args.projectId], baseDataList); |
|
|
|
await NocoCache.setList(CacheScope.BASE, [args.projectId], baseDataList); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
baseDataList.sort( |
|
|
|
|
|
|
|
(a, b) => |
|
|
|
|
|
|
|
(a.order != null ? a.order : Infinity) - |
|
|
|
|
|
|
|
(b.order != null ? b.order : Infinity) |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
return baseDataList?.map((baseData) => { |
|
|
|
return baseDataList?.map((baseData) => { |
|
|
|
return new Base(baseData); |
|
|
|
return new Base(baseData); |
|
|
|
}); |
|
|
|
}); |
|
|
@ -160,6 +193,70 @@ export default class Base implements BaseType { |
|
|
|
return baseData && new Base(baseData); |
|
|
|
return baseData && new Base(baseData); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static async reorderBases(projectId: string, keepBase?: string, ncMeta = Noco.ncMeta) { |
|
|
|
|
|
|
|
const bases = await this.list({ projectId: projectId }, ncMeta); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// order list for bases
|
|
|
|
|
|
|
|
const orders = []; |
|
|
|
|
|
|
|
const takenOrders = bases.map((base) => base.order); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (keepBase) { |
|
|
|
|
|
|
|
bases.find((base) => { |
|
|
|
|
|
|
|
if (base.id === keepBase) { |
|
|
|
|
|
|
|
orders.push({ id: base.id, order: base.order }); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (const b of bases) { |
|
|
|
|
|
|
|
if (b.id === keepBase) continue; |
|
|
|
|
|
|
|
let tempIndex = b.order; |
|
|
|
|
|
|
|
if (!b.order || orders.find((o) => o.order === tempIndex)) { |
|
|
|
|
|
|
|
tempIndex = 1; |
|
|
|
|
|
|
|
while (takenOrders.includes(tempIndex)) { |
|
|
|
|
|
|
|
tempIndex++; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// use index as order if order is not set
|
|
|
|
|
|
|
|
orders.push({ id: b.id, order: tempIndex }); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
orders.sort((a, b) => a.order - b.order); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// update order for bases
|
|
|
|
|
|
|
|
for (const [i, o] of Object.entries(orders)) { |
|
|
|
|
|
|
|
const fnd = bases.find((b) => b.id === o.id); |
|
|
|
|
|
|
|
if (fnd && (!fnd.order || fnd.order != parseInt(i) + 1)) { |
|
|
|
|
|
|
|
await ncMeta.metaDelete(null, null, MetaTable.BASES, { |
|
|
|
|
|
|
|
id: fnd.id, |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await NocoCache.deepDel( |
|
|
|
|
|
|
|
CacheScope.BASE, |
|
|
|
|
|
|
|
`${CacheScope.BASE}:${fnd.id}`, |
|
|
|
|
|
|
|
CacheDelDirection.CHILD_TO_PARENT |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fnd.order = parseInt(i) + 1; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const { id } = await ncMeta.metaInsert2( |
|
|
|
|
|
|
|
fnd.project_id, |
|
|
|
|
|
|
|
null, |
|
|
|
|
|
|
|
MetaTable.BASES, |
|
|
|
|
|
|
|
fnd |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await NocoCache.appendToList( |
|
|
|
|
|
|
|
CacheScope.BASE, |
|
|
|
|
|
|
|
[fnd.project_id], |
|
|
|
|
|
|
|
`${CacheScope.BASE}:${id}` |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await NocoCache.set(`${CacheScope.BASE}:${id}`, fnd); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public getConnectionConfig(): any { |
|
|
|
public getConnectionConfig(): any { |
|
|
|
if (this.is_meta) { |
|
|
|
if (this.is_meta) { |
|
|
|
const metaConfig = Noco.getConfig()?.meta?.db; |
|
|
|
const metaConfig = Noco.getConfig()?.meta?.db; |
|
|
|