mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-05-08 06:59:26 +08:00
feat: support query interface generateJsonPathExpression
This commit is contained in:
parent
a71dff4d4b
commit
5be0bb9807
@ -105,6 +105,7 @@ export interface IDatabaseOptions extends Options {
|
|||||||
logger?: LoggerOptions | Logger;
|
logger?: LoggerOptions | Logger;
|
||||||
customHooks?: any;
|
customHooks?: any;
|
||||||
instanceId?: string;
|
instanceId?: string;
|
||||||
|
sqlParser?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type DatabaseOptions = IDatabaseOptions;
|
export type DatabaseOptions = IDatabaseOptions;
|
||||||
|
@ -87,6 +87,7 @@ export async function parseDatabaseOptionsFromEnv(): Promise<IDatabaseOptions> {
|
|||||||
tablePrefix: process.env.DB_TABLE_PREFIX,
|
tablePrefix: process.env.DB_TABLE_PREFIX,
|
||||||
schema: process.env.DB_SCHEMA,
|
schema: process.env.DB_SCHEMA,
|
||||||
underscored: process.env.DB_UNDERSCORED === 'true',
|
underscored: process.env.DB_UNDERSCORED === 'true',
|
||||||
|
sqlParser: process.env.DB_DIALECT === 'mssql' ? 'TransactSQL' : undefined,
|
||||||
};
|
};
|
||||||
|
|
||||||
const sslOptions = await extractSSLOptionsFromEnv();
|
const sslOptions = await extractSSLOptionsFromEnv();
|
||||||
|
@ -8,7 +8,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
/* istanbul ignore file -- @preserve */
|
/* istanbul ignore file -- @preserve */
|
||||||
import { Database, IDatabaseOptions } from '@nocobase/database';
|
import { Database, IDatabaseOptions, sqlParser } from '@nocobase/database';
|
||||||
import { merge } from '@nocobase/utils';
|
import { merge } from '@nocobase/utils';
|
||||||
import { customAlphabet } from 'nanoid';
|
import { customAlphabet } from 'nanoid';
|
||||||
import fetch from 'node-fetch';
|
import fetch from 'node-fetch';
|
||||||
@ -42,6 +42,7 @@ export function getConfigByEnv() {
|
|||||||
underscored: process.env.DB_UNDERSCORED === 'true',
|
underscored: process.env.DB_UNDERSCORED === 'true',
|
||||||
schema: process.env.DB_SCHEMA !== 'public' ? process.env.DB_SCHEMA : undefined,
|
schema: process.env.DB_SCHEMA !== 'public' ? process.env.DB_SCHEMA : undefined,
|
||||||
dialectOptions: {},
|
dialectOptions: {},
|
||||||
|
sqlParser: process.env.DB_DIALECT === 'mssql' ? 'TransactSQL' : undefined,
|
||||||
};
|
};
|
||||||
|
|
||||||
if (process.env.DB_DIALECT == 'postgres') {
|
if (process.env.DB_DIALECT == 'postgres') {
|
||||||
|
@ -166,4 +166,8 @@ export default abstract class QueryInterface {
|
|||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public generateJsonPathExpression(field: string, path: string) {
|
||||||
|
return `${field}.${path}`;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -90,7 +90,7 @@ describe('sql collection', () => {
|
|||||||
expect(res.body.data.sources).toEqual(['testSqlCollection']);
|
expect(res.body.data.sources).toEqual(['testSqlCollection']);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('sqlCollection:update', async () => {
|
it.only('sqlCollection:update', async () => {
|
||||||
await agent.resource('collections').create({
|
await agent.resource('collections').create({
|
||||||
values: {
|
values: {
|
||||||
name: 'fakeCollection',
|
name: 'fakeCollection',
|
||||||
|
@ -41,6 +41,10 @@ export class SQLModel extends Model {
|
|||||||
const schema = process.env.DB_SCHEMA || 'public';
|
const schema = process.env.DB_SCHEMA || 'public';
|
||||||
return `${schema}.${table}`;
|
return `${schema}.${table}`;
|
||||||
}
|
}
|
||||||
|
if (this.database.inDialect('mssql') && !table.includes('.')) {
|
||||||
|
const schema = process.env.DB_SCHEMA || 'dbo';
|
||||||
|
return `${schema}.${table}`;
|
||||||
|
}
|
||||||
return table;
|
return table;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -76,7 +80,7 @@ export class SQLModel extends Model {
|
|||||||
table: string;
|
table: string;
|
||||||
columns: { name: string; as?: string }[];
|
columns: { name: string; as?: string }[];
|
||||||
}[] {
|
}[] {
|
||||||
let { ast: _ast } = sqlParser.parse(this.sql);
|
let { ast: _ast } = sqlParser.parse(this.sql, { database: this.database.options.sqlParser });
|
||||||
if (Array.isArray(_ast)) {
|
if (Array.isArray(_ast)) {
|
||||||
_ast = _ast[0];
|
_ast = _ast[0];
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,12 @@
|
|||||||
|
/**
|
||||||
|
* This file is part of the NocoBase (R) project.
|
||||||
|
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
||||||
|
* Authors: NocoBase Team.
|
||||||
|
*
|
||||||
|
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
||||||
|
* For more information, please refer to: https://www.nocobase.com/agreement.
|
||||||
|
*/
|
||||||
|
|
||||||
import { Database } from '@nocobase/database';
|
import { Database } from '@nocobase/database';
|
||||||
import { FieldIsDependedOnByOtherError } from '../errors/field-is-depended-on-by-other';
|
import { FieldIsDependedOnByOtherError } from '../errors/field-is-depended-on-by-other';
|
||||||
|
|
||||||
@ -14,12 +23,12 @@ export function beforeDestoryField(db: Database) {
|
|||||||
filter: {
|
filter: {
|
||||||
$or: [
|
$or: [
|
||||||
{
|
{
|
||||||
['options.sourceKey']: name,
|
[db.queryInterface.generateJsonPathExpression('options', 'sourceKey')]: name,
|
||||||
collectionName,
|
collectionName,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
['options.targetKey']: name,
|
[db.queryInterface.generateJsonPathExpression('options', 'targetKey')]: name,
|
||||||
['options.target']: collectionName,
|
[db.queryInterface.generateJsonPathExpression('options', 'targetKey')]: collectionName,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user