2022-02-14 16:20:25 +00:00
|
|
|
import { merge, uid } from '@nocobase/utils';
|
|
|
|
import { resolve } from 'path';
|
|
|
|
import { Database, IDatabaseOptions } from './database';
|
2023-10-17 14:22:45 +00:00
|
|
|
import fetch from 'node-fetch';
|
|
|
|
import path from 'path';
|
|
|
|
import { customAlphabet } from 'nanoid';
|
2022-02-14 16:20:25 +00:00
|
|
|
export class MockDatabase extends Database {
|
|
|
|
constructor(options: IDatabaseOptions) {
|
|
|
|
super({
|
|
|
|
storage: ':memory:',
|
2022-03-28 14:01:10 +00:00
|
|
|
dialect: 'sqlite',
|
2022-02-14 16:20:25 +00:00
|
|
|
...options,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getConfigByEnv() {
|
feat: database view collection (#1587)
* test: create view collection
* feat: view collection class
* feat: list view
* chore: skip sync view collection
* test: should create view collection in difference schema
* test: create view collection in collection manager
* feat: create view collection by user sql
* test: view resourcer
* feat: view collection
* feat: view collection cannot be added, deleted, or modified
* feat: view collection cannot be added, deleted, or modified
* feat: view collection cannot be added, deleted, or modified
* feat: view collection cannot be added, deleted, or modified
* refactor: connect to database view
* refactor: sync from database
* chore: rename list view sql
* chore: list view fields api
* chore: create collection without viewName
* feat: bring out fields when selecting a view
* chore: bring out fields when selecting a view
* feat: view field inference class
* chore: bring out fields when selecting a view
* chore: sync form database view
* chore: sync form database view
* refactor: view collection local
* feat: view get api
* feat: database type infer
* feat: integer map
* chore: remove from in view list
* chore: build error
* chore: uniq collection
* fix: typo
* chore: replace collection list source field
* fix: destroy view collection
* chore: timestamp field map
* refactor: interface avalableTypes
* refactor: interface avalableTypes
* chore: list fields test
* refactor: interface avalableTypes
* chore: uiSchema response in field source
* fix: view query
* chore: collection snippet
* refactor: view collection support preview
* fix: handle field source
* fix: typo
* fix: configure fileds title
* fix: configure fileds title
* fix: configure fileds title
* fix: sync from databse interface
* fix: sync from databse interface
* feat: set fields api
* fix: sync from databse fix
* feat: possibleTypes
* chore: fields get
* fix: sync from databse
* fix: list view test
* fix: view test in difference schema
* chore: comment
* feat: when there is only one source collection, the view is a subset of a Collection
* feat: view collection add field
* fix: inherit query with schema
* fix: test
* fix: ci test
* fix: test with schema
* chore: set pg default search path
* chore: mysql test
* fix: test with schema
* chore: test
* chore: action test
* chore: view column usage return type
* feat: mysql field inference
* fix: tableName
* chore: node sql parser
* fix: sql build
* fix: database build
* fix: mysql test
* feat: view collection uiSchema title
* fix: incorrect field source display when switching views
* refactor: view collection not allow modify
* fix: view collection is allow add, delete, and modify
* fix: mysql test
* fix: sqlite test
* fix: sqlite test
* fix: sqlite test
* fix: sqlite test
* chore: add id field as default target key
* style: style improve
* feat: load source field options
* style: style improve
* chore: disable remove column in view collection
* chore: support creating view collection with different schemas with the same name
* chore: support creating view collection with different schemas with the same name
* fix: query view in difference schema
* refactor: view collection viewname
* fix: query view collection in difference schema
* fix: field load
* chore: field options
* fix: mysql test
* fix: uiSchema component error when using a view field in a block
* fix: sqlite test
* chore: test
* fix: dump user views
* fix: view collection can be updated and edited in table block
* chore: sync from database display last field configuration
* chore: loadCollections
* chore: sync from database display last field configuration
* fix: field options merge issues
* style: preview table
* fix: view collection is allow using in kanban blocks
* refactor: code improve
* fix: view collection can be updated an edited in calendar block
* chore: disable infer field without interface
* feat: preview only shows source or interface fields
* fix: test
* refactor: locale
* feat: sql parser
* chore: remove node-sql-parser
* fix: yarn.lock
* test: view repository
* fix: view repository test
* chore: console.log
* chore: console.log
* fix: mysql without schema
* fix: mysql without schema
* chore: preview with field schema
* chore: tableActionInitializers
* style: preview style improve
* chore: parameter is filter when there is no filterByTk
* fix: preview pagination
* fix: preview pagination
* style: preview table style improve
* fix: sync from database loading
* chore: preview performance optimization
* chore: preview performance optimization
* feat: limit & offset
* chore: preview performance optimization
* test: field with dot column
* fix: datetime interface display
* fix: missing boolean type
* fix: sync
* fix: sync from database
* style: style improve
* style: style improve
* style: style improve
* chore: preview table
* chore: preview table
* chore: preview table
* fix: styling
---------
Co-authored-by: katherinehhh <katherine_15995@163.com>
Co-authored-by: chenos <chenlinxh@gmail.com>
2023-04-01 13:56:01 +00:00
|
|
|
const options = {
|
2022-02-14 16:20:25 +00:00
|
|
|
username: process.env.DB_USER,
|
|
|
|
password: process.env.DB_PASSWORD,
|
|
|
|
database: process.env.DB_DATABASE,
|
|
|
|
host: process.env.DB_HOST,
|
|
|
|
port: process.env.DB_PORT,
|
2022-10-06 02:29:53 +00:00
|
|
|
dialect: process.env.DB_DIALECT || 'sqlite',
|
2023-04-01 01:06:20 +00:00
|
|
|
logging: process.env.DB_LOGGING === 'on' ? customLogger : false,
|
2022-03-28 14:01:10 +00:00
|
|
|
storage:
|
|
|
|
process.env.DB_STORAGE && process.env.DB_STORAGE !== ':memory:'
|
|
|
|
? resolve(process.cwd(), process.env.DB_STORAGE)
|
|
|
|
: ':memory:',
|
2022-02-15 14:32:02 +00:00
|
|
|
define: {
|
2022-02-14 16:20:25 +00:00
|
|
|
charset: 'utf8mb4',
|
|
|
|
collate: 'utf8mb4_unicode_ci',
|
|
|
|
},
|
2022-06-01 03:41:46 +00:00
|
|
|
timezone: process.env.DB_TIMEZONE,
|
2023-02-13 13:38:47 +00:00
|
|
|
underscored: process.env.DB_UNDERSCORED === 'true',
|
2023-02-16 02:53:04 +00:00
|
|
|
schema: process.env.DB_SCHEMA !== 'public' ? process.env.DB_SCHEMA : undefined,
|
feat: database view collection (#1587)
* test: create view collection
* feat: view collection class
* feat: list view
* chore: skip sync view collection
* test: should create view collection in difference schema
* test: create view collection in collection manager
* feat: create view collection by user sql
* test: view resourcer
* feat: view collection
* feat: view collection cannot be added, deleted, or modified
* feat: view collection cannot be added, deleted, or modified
* feat: view collection cannot be added, deleted, or modified
* feat: view collection cannot be added, deleted, or modified
* refactor: connect to database view
* refactor: sync from database
* chore: rename list view sql
* chore: list view fields api
* chore: create collection without viewName
* feat: bring out fields when selecting a view
* chore: bring out fields when selecting a view
* feat: view field inference class
* chore: bring out fields when selecting a view
* chore: sync form database view
* chore: sync form database view
* refactor: view collection local
* feat: view get api
* feat: database type infer
* feat: integer map
* chore: remove from in view list
* chore: build error
* chore: uniq collection
* fix: typo
* chore: replace collection list source field
* fix: destroy view collection
* chore: timestamp field map
* refactor: interface avalableTypes
* refactor: interface avalableTypes
* chore: list fields test
* refactor: interface avalableTypes
* chore: uiSchema response in field source
* fix: view query
* chore: collection snippet
* refactor: view collection support preview
* fix: handle field source
* fix: typo
* fix: configure fileds title
* fix: configure fileds title
* fix: configure fileds title
* fix: sync from databse interface
* fix: sync from databse interface
* feat: set fields api
* fix: sync from databse fix
* feat: possibleTypes
* chore: fields get
* fix: sync from databse
* fix: list view test
* fix: view test in difference schema
* chore: comment
* feat: when there is only one source collection, the view is a subset of a Collection
* feat: view collection add field
* fix: inherit query with schema
* fix: test
* fix: ci test
* fix: test with schema
* chore: set pg default search path
* chore: mysql test
* fix: test with schema
* chore: test
* chore: action test
* chore: view column usage return type
* feat: mysql field inference
* fix: tableName
* chore: node sql parser
* fix: sql build
* fix: database build
* fix: mysql test
* feat: view collection uiSchema title
* fix: incorrect field source display when switching views
* refactor: view collection not allow modify
* fix: view collection is allow add, delete, and modify
* fix: mysql test
* fix: sqlite test
* fix: sqlite test
* fix: sqlite test
* fix: sqlite test
* chore: add id field as default target key
* style: style improve
* feat: load source field options
* style: style improve
* chore: disable remove column in view collection
* chore: support creating view collection with different schemas with the same name
* chore: support creating view collection with different schemas with the same name
* fix: query view in difference schema
* refactor: view collection viewname
* fix: query view collection in difference schema
* fix: field load
* chore: field options
* fix: mysql test
* fix: uiSchema component error when using a view field in a block
* fix: sqlite test
* chore: test
* fix: dump user views
* fix: view collection can be updated and edited in table block
* chore: sync from database display last field configuration
* chore: loadCollections
* chore: sync from database display last field configuration
* fix: field options merge issues
* style: preview table
* fix: view collection is allow using in kanban blocks
* refactor: code improve
* fix: view collection can be updated an edited in calendar block
* chore: disable infer field without interface
* feat: preview only shows source or interface fields
* fix: test
* refactor: locale
* feat: sql parser
* chore: remove node-sql-parser
* fix: yarn.lock
* test: view repository
* fix: view repository test
* chore: console.log
* chore: console.log
* fix: mysql without schema
* fix: mysql without schema
* chore: preview with field schema
* chore: tableActionInitializers
* style: preview style improve
* chore: parameter is filter when there is no filterByTk
* fix: preview pagination
* fix: preview pagination
* style: preview table style improve
* fix: sync from database loading
* chore: preview performance optimization
* chore: preview performance optimization
* feat: limit & offset
* chore: preview performance optimization
* test: field with dot column
* fix: datetime interface display
* fix: missing boolean type
* fix: sync
* fix: sync from database
* style: style improve
* style: style improve
* style: style improve
* chore: preview table
* chore: preview table
* chore: preview table
* fix: styling
---------
Co-authored-by: katherinehhh <katherine_15995@163.com>
Co-authored-by: chenos <chenlinxh@gmail.com>
2023-04-01 13:56:01 +00:00
|
|
|
dialectOptions: {},
|
2022-02-14 16:20:25 +00:00
|
|
|
};
|
feat: database view collection (#1587)
* test: create view collection
* feat: view collection class
* feat: list view
* chore: skip sync view collection
* test: should create view collection in difference schema
* test: create view collection in collection manager
* feat: create view collection by user sql
* test: view resourcer
* feat: view collection
* feat: view collection cannot be added, deleted, or modified
* feat: view collection cannot be added, deleted, or modified
* feat: view collection cannot be added, deleted, or modified
* feat: view collection cannot be added, deleted, or modified
* refactor: connect to database view
* refactor: sync from database
* chore: rename list view sql
* chore: list view fields api
* chore: create collection without viewName
* feat: bring out fields when selecting a view
* chore: bring out fields when selecting a view
* feat: view field inference class
* chore: bring out fields when selecting a view
* chore: sync form database view
* chore: sync form database view
* refactor: view collection local
* feat: view get api
* feat: database type infer
* feat: integer map
* chore: remove from in view list
* chore: build error
* chore: uniq collection
* fix: typo
* chore: replace collection list source field
* fix: destroy view collection
* chore: timestamp field map
* refactor: interface avalableTypes
* refactor: interface avalableTypes
* chore: list fields test
* refactor: interface avalableTypes
* chore: uiSchema response in field source
* fix: view query
* chore: collection snippet
* refactor: view collection support preview
* fix: handle field source
* fix: typo
* fix: configure fileds title
* fix: configure fileds title
* fix: configure fileds title
* fix: sync from databse interface
* fix: sync from databse interface
* feat: set fields api
* fix: sync from databse fix
* feat: possibleTypes
* chore: fields get
* fix: sync from databse
* fix: list view test
* fix: view test in difference schema
* chore: comment
* feat: when there is only one source collection, the view is a subset of a Collection
* feat: view collection add field
* fix: inherit query with schema
* fix: test
* fix: ci test
* fix: test with schema
* chore: set pg default search path
* chore: mysql test
* fix: test with schema
* chore: test
* chore: action test
* chore: view column usage return type
* feat: mysql field inference
* fix: tableName
* chore: node sql parser
* fix: sql build
* fix: database build
* fix: mysql test
* feat: view collection uiSchema title
* fix: incorrect field source display when switching views
* refactor: view collection not allow modify
* fix: view collection is allow add, delete, and modify
* fix: mysql test
* fix: sqlite test
* fix: sqlite test
* fix: sqlite test
* fix: sqlite test
* chore: add id field as default target key
* style: style improve
* feat: load source field options
* style: style improve
* chore: disable remove column in view collection
* chore: support creating view collection with different schemas with the same name
* chore: support creating view collection with different schemas with the same name
* fix: query view in difference schema
* refactor: view collection viewname
* fix: query view collection in difference schema
* fix: field load
* chore: field options
* fix: mysql test
* fix: uiSchema component error when using a view field in a block
* fix: sqlite test
* chore: test
* fix: dump user views
* fix: view collection can be updated and edited in table block
* chore: sync from database display last field configuration
* chore: loadCollections
* chore: sync from database display last field configuration
* fix: field options merge issues
* style: preview table
* fix: view collection is allow using in kanban blocks
* refactor: code improve
* fix: view collection can be updated an edited in calendar block
* chore: disable infer field without interface
* feat: preview only shows source or interface fields
* fix: test
* refactor: locale
* feat: sql parser
* chore: remove node-sql-parser
* fix: yarn.lock
* test: view repository
* fix: view repository test
* chore: console.log
* chore: console.log
* fix: mysql without schema
* fix: mysql without schema
* chore: preview with field schema
* chore: tableActionInitializers
* style: preview style improve
* chore: parameter is filter when there is no filterByTk
* fix: preview pagination
* fix: preview pagination
* style: preview table style improve
* fix: sync from database loading
* chore: preview performance optimization
* chore: preview performance optimization
* feat: limit & offset
* chore: preview performance optimization
* test: field with dot column
* fix: datetime interface display
* fix: missing boolean type
* fix: sync
* fix: sync from database
* style: style improve
* style: style improve
* style: style improve
* chore: preview table
* chore: preview table
* chore: preview table
* fix: styling
---------
Co-authored-by: katherinehhh <katherine_15995@163.com>
Co-authored-by: chenos <chenlinxh@gmail.com>
2023-04-01 13:56:01 +00:00
|
|
|
|
|
|
|
if (process.env.DB_DIALECT == 'postgres') {
|
|
|
|
options.dialectOptions['application_name'] = 'nocobase.main';
|
|
|
|
}
|
|
|
|
|
|
|
|
return options;
|
2022-02-14 16:20:25 +00:00
|
|
|
}
|
|
|
|
|
2023-04-01 01:06:20 +00:00
|
|
|
function customLogger(queryString, queryObject) {
|
|
|
|
console.log(queryString); // outputs a string
|
2023-05-11 07:16:33 +00:00
|
|
|
if (queryObject.bind) {
|
|
|
|
console.log(queryObject.bind); // outputs an array
|
|
|
|
}
|
2023-04-01 01:06:20 +00:00
|
|
|
}
|
|
|
|
|
2022-02-14 16:20:25 +00:00
|
|
|
export function mockDatabase(options: IDatabaseOptions = {}): MockDatabase {
|
2023-02-13 13:38:47 +00:00
|
|
|
const dbOptions = merge(getConfigByEnv(), options) as any;
|
2023-10-17 14:22:45 +00:00
|
|
|
|
|
|
|
if (process.env['DB_TEST_PREFIX']) {
|
|
|
|
let configKey = 'database';
|
|
|
|
if (dbOptions.dialect === 'sqlite') {
|
|
|
|
configKey = 'storage';
|
|
|
|
} else {
|
|
|
|
configKey = 'database';
|
|
|
|
}
|
|
|
|
|
|
|
|
const shouldChange = () => {
|
|
|
|
if (dbOptions.dialect === 'sqlite') {
|
|
|
|
return !dbOptions[configKey].includes(process.env['DB_TEST_PREFIX']);
|
|
|
|
}
|
|
|
|
|
|
|
|
return !dbOptions[configKey].startsWith(process.env['DB_TEST_PREFIX']);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (dbOptions[configKey] && shouldChange()) {
|
|
|
|
const nanoid = customAlphabet('1234567890abcdefghijklmnopqrstuvwxyz', 10);
|
|
|
|
|
|
|
|
const instanceId = `d_${nanoid()}`;
|
|
|
|
const databaseName = `${process.env['DB_TEST_PREFIX']}_${instanceId}`;
|
|
|
|
|
|
|
|
if (dbOptions.dialect === 'sqlite') {
|
|
|
|
dbOptions.storage = path.resolve(path.dirname(dbOptions.storage), databaseName);
|
|
|
|
} else {
|
|
|
|
dbOptions.database = databaseName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (process.env['DB_TEST_DISTRIBUTOR_PORT']) {
|
|
|
|
dbOptions.hooks = dbOptions.hooks || {};
|
|
|
|
|
|
|
|
dbOptions.hooks.beforeConnect = async (config) => {
|
|
|
|
const url = `http://127.0.0.1:${process.env['DB_TEST_DISTRIBUTOR_PORT']}/acquire?via=${db.instanceId}&name=${config.database}`;
|
|
|
|
await fetch(url);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const db = new MockDatabase(dbOptions);
|
|
|
|
|
|
|
|
return db;
|
2022-02-14 16:20:25 +00:00
|
|
|
}
|