2023-07-14 14:49:12 +08:00
|
|
|
import { Logger } from '@nocobase/logger';
|
2022-09-19 09:23:01 +08:00
|
|
|
import { applyMixins, AsyncEmitter, requireModule } from '@nocobase/utils';
|
2022-01-30 11:11:36 +08:00
|
|
|
import merge from 'deepmerge';
|
2021-09-07 00:28:16 +08:00
|
|
|
import { EventEmitter } from 'events';
|
2023-09-24 11:41:26 +08:00
|
|
|
import { backOff } from 'exponential-backoff';
|
2022-06-17 10:25:59 +08:00
|
|
|
import glob from 'glob';
|
2022-04-19 17:04:54 +08:00
|
|
|
import lodash from 'lodash';
|
2022-06-17 10:25:59 +08:00
|
|
|
import { basename, isAbsolute, resolve } from 'path';
|
2022-07-04 13:41:07 +08:00
|
|
|
import semver from 'semver';
|
2022-04-29 20:04:02 +08:00
|
|
|
import {
|
2022-11-20 14:40:41 +08:00
|
|
|
DataTypes,
|
2022-12-24 16:30:01 +08:00
|
|
|
ModelStatic,
|
2022-04-29 20:04:02 +08:00
|
|
|
Op,
|
|
|
|
Options,
|
|
|
|
QueryInterfaceDropAllTablesOptions,
|
|
|
|
QueryOptions,
|
|
|
|
Sequelize,
|
|
|
|
SyncOptions,
|
2022-06-18 00:18:12 +08:00
|
|
|
Transactionable,
|
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 21:56:01 +08:00
|
|
|
Utils,
|
2022-04-29 20:04:02 +08:00
|
|
|
} from 'sequelize';
|
2022-05-30 23:10:32 +08:00
|
|
|
import { SequelizeStorage, Umzug } from 'umzug';
|
2021-12-06 21:23:34 +08:00
|
|
|
import { Collection, CollectionOptions, RepositoryType } from './collection';
|
2023-07-14 14:49:12 +08:00
|
|
|
import { CollectionGroupManager } from './collection-group-manager';
|
2022-01-30 11:11:36 +08:00
|
|
|
import { ImporterReader, ImportFileExtension } from './collection-importer';
|
2023-07-14 14:49:12 +08:00
|
|
|
import DatabaseUtils from './database-utils';
|
2022-12-02 22:22:11 +08:00
|
|
|
import ReferencesMap from './features/ReferencesMap';
|
|
|
|
import { referentialIntegrityCheck } from './features/referential-integrity-check';
|
2022-12-13 18:02:03 +08:00
|
|
|
import { ArrayFieldRepository } from './field-repository/array-field-repository';
|
2021-12-06 21:23:34 +08:00
|
|
|
import * as FieldTypes from './fields';
|
2022-01-30 11:11:36 +08:00
|
|
|
import { Field, FieldContext, RelationField } from './fields';
|
2022-12-02 22:22:11 +08:00
|
|
|
import { InheritedCollection } from './inherited-collection';
|
|
|
|
import InheritanceMap from './inherited-map';
|
2023-07-14 14:49:12 +08:00
|
|
|
import { registerBuiltInListeners } from './listeners';
|
2022-08-16 14:41:29 +08:00
|
|
|
import { MigrationItem, Migrations } from './migration';
|
2022-02-23 18:18:38 +08:00
|
|
|
import { Model } from './model';
|
2021-12-06 21:23:34 +08:00
|
|
|
import { ModelHook } from './model-hook';
|
|
|
|
import extendOperators from './operators';
|
2023-07-14 14:49:12 +08:00
|
|
|
import QueryInterface from './query-interface/query-interface';
|
|
|
|
import buildQueryInterface from './query-interface/query-interface-builder';
|
2022-01-18 16:38:03 +08:00
|
|
|
import { RelationRepository } from './relation-repository/relation-repository';
|
2022-01-30 11:11:36 +08:00
|
|
|
import { Repository } from './repository';
|
2022-10-14 17:22:32 +08:00
|
|
|
import {
|
|
|
|
AfterDefineCollectionListener,
|
|
|
|
BeforeDefineCollectionListener,
|
|
|
|
CreateListener,
|
|
|
|
CreateWithAssociationsListener,
|
|
|
|
DatabaseAfterDefineCollectionEventType,
|
|
|
|
DatabaseAfterRemoveCollectionEventType,
|
|
|
|
DatabaseBeforeDefineCollectionEventType,
|
|
|
|
DatabaseBeforeRemoveCollectionEventType,
|
|
|
|
DestroyListener,
|
|
|
|
EventType,
|
|
|
|
ModelCreateEventTypes,
|
|
|
|
ModelCreateWithAssociationsEventTypes,
|
|
|
|
ModelDestroyEventTypes,
|
|
|
|
ModelSaveEventTypes,
|
|
|
|
ModelSaveWithAssociationsEventTypes,
|
|
|
|
ModelUpdateEventTypes,
|
|
|
|
ModelUpdateWithAssociationsEventTypes,
|
|
|
|
ModelValidateEventTypes,
|
|
|
|
RemoveCollectionListener,
|
|
|
|
SaveListener,
|
|
|
|
SaveWithAssociationsListener,
|
|
|
|
SyncListener,
|
|
|
|
UpdateListener,
|
|
|
|
UpdateWithAssociationsListener,
|
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 21:56:01 +08:00
|
|
|
ValidateListener,
|
2022-10-14 17:22:32 +08:00
|
|
|
} from './types';
|
2023-02-16 10:53:04 +08:00
|
|
|
import { patchSequelizeQueryInterface, snakeCase } from './utils';
|
2023-05-18 14:51:51 +08:00
|
|
|
import { BaseValueParser, registerFieldValueParsers } from './value-parsers';
|
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 21:56:01 +08:00
|
|
|
import { ViewCollection } from './view-collection';
|
2020-10-24 15:34:43 +08:00
|
|
|
|
2023-04-25 13:12:14 +08:00
|
|
|
export type MergeOptions = merge.Options;
|
2020-10-24 15:34:43 +08:00
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
export interface PendingOptions {
|
|
|
|
field: RelationField;
|
2022-12-24 16:30:01 +08:00
|
|
|
model: ModelStatic<Model>;
|
2020-10-24 15:34:43 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
interface MapOf<T> {
|
|
|
|
[key: string]: T;
|
2021-01-13 16:23:15 +08:00
|
|
|
}
|
|
|
|
|
2022-02-15 00:20:25 +08:00
|
|
|
export interface IDatabaseOptions extends Options {
|
|
|
|
tablePrefix?: string;
|
2022-05-30 23:10:32 +08:00
|
|
|
migrator?: any;
|
2022-11-20 14:40:41 +08:00
|
|
|
usingBigIntForId?: boolean;
|
2023-02-13 21:38:47 +08:00
|
|
|
underscored?: boolean;
|
2022-02-15 00:20:25 +08:00
|
|
|
}
|
|
|
|
|
2022-05-30 23:10:32 +08:00
|
|
|
export type DatabaseOptions = IDatabaseOptions;
|
2020-12-15 20:16:55 +08:00
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
interface RegisterOperatorsContext {
|
|
|
|
db?: Database;
|
|
|
|
path?: string;
|
|
|
|
field?: Field;
|
2022-04-19 17:04:54 +08:00
|
|
|
app?: any;
|
2021-01-14 10:35:15 +08:00
|
|
|
}
|
|
|
|
|
2022-01-30 11:11:36 +08:00
|
|
|
export interface CleanOptions extends QueryInterfaceDropAllTablesOptions {
|
|
|
|
drop?: boolean;
|
|
|
|
}
|
|
|
|
|
2022-06-17 10:25:59 +08:00
|
|
|
export type AddMigrationsOptions = {
|
|
|
|
context?: any;
|
|
|
|
namespace?: string;
|
|
|
|
extensions?: string[];
|
|
|
|
directory: string;
|
|
|
|
};
|
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
type OperatorFunc = (value: any, ctx?: RegisterOperatorsContext) => any;
|
2021-01-14 10:35:15 +08:00
|
|
|
|
2023-02-26 11:58:45 +08:00
|
|
|
export const DialectVersionAccessors = {
|
|
|
|
sqlite: {
|
|
|
|
sql: 'select sqlite_version() as version',
|
|
|
|
get: (v: string) => v,
|
|
|
|
},
|
|
|
|
mysql: {
|
|
|
|
sql: 'select version() as version',
|
|
|
|
get: (v: string) => {
|
|
|
|
if (v.toLowerCase().includes('mariadb')) {
|
|
|
|
return '';
|
|
|
|
}
|
2023-08-04 18:19:45 +08:00
|
|
|
const m = /([\d+.]+)/.exec(v);
|
2023-02-26 11:58:45 +08:00
|
|
|
return m[0];
|
|
|
|
},
|
|
|
|
},
|
|
|
|
postgres: {
|
|
|
|
sql: 'select version() as version',
|
|
|
|
get: (v: string) => {
|
2023-08-04 18:19:45 +08:00
|
|
|
const m = /([\d+.]+)/.exec(v);
|
2023-02-26 11:58:45 +08:00
|
|
|
return semver.minVersion(m[0]).version;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2022-07-04 13:41:07 +08:00
|
|
|
class DatabaseVersion {
|
|
|
|
db: Database;
|
|
|
|
|
|
|
|
constructor(db: Database) {
|
|
|
|
this.db = db;
|
|
|
|
}
|
|
|
|
|
|
|
|
async satisfies(versions) {
|
2023-02-26 11:58:45 +08:00
|
|
|
const accessors = DialectVersionAccessors;
|
|
|
|
for (const dialect of Object.keys(accessors)) {
|
2022-07-04 13:41:07 +08:00
|
|
|
if (this.db.inDialect(dialect)) {
|
|
|
|
if (!versions?.[dialect]) {
|
|
|
|
return false;
|
|
|
|
}
|
2023-02-26 11:58:45 +08:00
|
|
|
const [result] = (await this.db.sequelize.query(accessors[dialect].sql)) as any;
|
|
|
|
return semver.satisfies(accessors[dialect].get(result?.[0]?.version), versions[dialect]);
|
2022-07-04 13:41:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
export class Database extends EventEmitter implements AsyncEmitter {
|
|
|
|
sequelize: Sequelize;
|
2022-05-30 23:10:32 +08:00
|
|
|
migrator: Umzug;
|
|
|
|
migrations: Migrations;
|
2021-12-06 21:23:34 +08:00
|
|
|
fieldTypes = new Map();
|
2023-02-16 23:56:00 +08:00
|
|
|
fieldValueParsers = new Map();
|
2022-02-15 00:20:25 +08:00
|
|
|
options: IDatabaseOptions;
|
2022-12-24 16:30:01 +08:00
|
|
|
models = new Map<string, ModelStatic<Model>>();
|
2021-12-06 21:23:34 +08:00
|
|
|
repositories = new Map<string, RepositoryType>();
|
|
|
|
operators = new Map();
|
|
|
|
collections = new Map<string, Collection>();
|
|
|
|
pendingFields = new Map<string, RelationField[]>();
|
2022-12-24 16:30:01 +08:00
|
|
|
modelCollection = new Map<ModelStatic<any>, Collection>();
|
2022-11-16 12:53:58 +08:00
|
|
|
tableNameCollectionMap = new Map<string, Collection>();
|
|
|
|
|
2023-03-05 14:45:56 +08:00
|
|
|
queryInterface: QueryInterface;
|
|
|
|
|
2023-02-16 10:53:04 +08:00
|
|
|
utils = new DatabaseUtils(this);
|
2022-10-31 22:45:39 +08:00
|
|
|
referenceMap = new ReferencesMap();
|
2022-11-16 12:53:58 +08:00
|
|
|
inheritanceMap = new InheritanceMap();
|
2020-10-24 15:34:43 +08:00
|
|
|
|
2023-01-08 12:45:02 +08:00
|
|
|
importedFrom = new Map<string, Array<string>>();
|
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
modelHook: ModelHook;
|
2022-07-04 13:41:07 +08:00
|
|
|
version: DatabaseVersion;
|
2020-10-24 15:34:43 +08:00
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
delayCollectionExtend = new Map<string, { collectionOptions: CollectionOptions; mergeOptions?: any }[]>();
|
|
|
|
|
2023-03-05 14:45:56 +08:00
|
|
|
logger: Logger;
|
|
|
|
|
2023-03-10 19:16:00 +08:00
|
|
|
collectionGroupManager = new CollectionGroupManager(this);
|
2023-05-19 16:39:00 +08:00
|
|
|
declare emitAsync: (event: string | symbol, ...args: any[]) => Promise<boolean>;
|
2023-03-10 19:16:00 +08:00
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
constructor(options: DatabaseOptions) {
|
2021-09-07 00:28:16 +08:00
|
|
|
super();
|
2023-02-18 09:27:54 +08:00
|
|
|
|
2022-07-04 13:41:07 +08:00
|
|
|
this.version = new DatabaseVersion(this);
|
|
|
|
|
2022-05-30 23:10:32 +08:00
|
|
|
const opts = {
|
|
|
|
sync: {
|
|
|
|
alter: {
|
|
|
|
drop: false,
|
2022-04-30 17:28:30 +08:00
|
|
|
},
|
2022-05-30 23:10:32 +08:00
|
|
|
force: false,
|
|
|
|
},
|
2022-08-04 21:27:55 +08:00
|
|
|
...lodash.clone(options),
|
2022-05-30 23:10:32 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
if (options.storage && options.storage !== ':memory:') {
|
|
|
|
if (!isAbsolute(options.storage)) {
|
|
|
|
opts.storage = resolve(process.cwd(), options.storage);
|
2022-05-19 00:40:55 +08:00
|
|
|
}
|
2021-09-07 00:28:16 +08:00
|
|
|
}
|
|
|
|
|
2022-06-01 11:41:46 +08:00
|
|
|
if (options.dialect === 'sqlite') {
|
|
|
|
delete opts.timezone;
|
2022-06-01 12:25:21 +08:00
|
|
|
} else if (!opts.timezone) {
|
|
|
|
opts.timezone = '+00:00';
|
2022-06-01 11:41:46 +08:00
|
|
|
}
|
|
|
|
|
2022-11-20 14:40:41 +08:00
|
|
|
if (options.dialect === 'postgres') {
|
|
|
|
// https://github.com/sequelize/sequelize/issues/1774
|
|
|
|
require('pg').defaults.parseInt8 = true;
|
|
|
|
}
|
2023-02-16 10:53:04 +08:00
|
|
|
this.options = opts;
|
2022-11-20 14:40:41 +08: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 21:56:01 +08:00
|
|
|
this.sequelize = new Sequelize(this.sequelizeOptions(this.options));
|
2023-02-16 10:53:04 +08:00
|
|
|
|
2023-03-05 14:45:56 +08:00
|
|
|
this.queryInterface = buildQueryInterface(this);
|
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
this.collections = new Map();
|
|
|
|
this.modelHook = new ModelHook(this);
|
2021-09-07 00:28:16 +08:00
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
this.on('afterDefineCollection', (collection: Collection) => {
|
|
|
|
// after collection defined, call bind method on pending fields
|
|
|
|
this.pendingFields.get(collection.name)?.forEach((field) => field.bind());
|
|
|
|
this.delayCollectionExtend.get(collection.name)?.forEach((collectionExtend) => {
|
|
|
|
collection.updateOptions(collectionExtend.collectionOptions, collectionExtend.mergeOptions);
|
|
|
|
});
|
|
|
|
});
|
2021-09-07 00:28:16 +08:00
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
// register database field types
|
2023-02-16 16:48:00 +08:00
|
|
|
for (const [name, field] of Object.entries<any>(FieldTypes)) {
|
2021-12-06 21:23:34 +08:00
|
|
|
if (['Field', 'RelationField'].includes(name)) {
|
|
|
|
continue;
|
2021-09-07 00:28:16 +08:00
|
|
|
}
|
2021-12-06 21:23:34 +08:00
|
|
|
let key = name.replace(/Field$/g, '');
|
|
|
|
key = key.substring(0, 1).toLowerCase() + key.substring(1);
|
|
|
|
this.registerFieldTypes({
|
|
|
|
[key]: field,
|
|
|
|
});
|
2021-09-07 00:28:16 +08:00
|
|
|
}
|
|
|
|
|
2023-02-16 23:56:00 +08:00
|
|
|
registerFieldValueParsers(this);
|
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
this.initOperators();
|
2022-05-30 23:10:32 +08:00
|
|
|
|
|
|
|
const migratorOptions: any = this.options.migrator || {};
|
|
|
|
|
|
|
|
const context = {
|
|
|
|
db: this,
|
|
|
|
sequelize: this.sequelize,
|
|
|
|
queryInterface: this.sequelize.getQueryInterface(),
|
|
|
|
...migratorOptions.context,
|
|
|
|
};
|
|
|
|
|
|
|
|
this.migrations = new Migrations(context);
|
2023-01-08 12:45:02 +08:00
|
|
|
|
2023-05-18 14:51:51 +08:00
|
|
|
this.sequelize.beforeDefine((model, opts) => {
|
|
|
|
if (this.options.tablePrefix) {
|
|
|
|
opts.tableName = `${this.options.tablePrefix}${opts.tableName || opts.modelName || opts.name.plural}`;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-01-08 12:45:02 +08:00
|
|
|
this.collection({
|
|
|
|
name: 'migrations',
|
|
|
|
autoGenId: false,
|
|
|
|
timestamps: false,
|
2023-03-10 19:16:00 +08:00
|
|
|
namespace: 'core.migration',
|
2023-02-13 09:57:03 +08:00
|
|
|
duplicator: 'required',
|
2023-08-04 09:37:06 +08:00
|
|
|
fields: [{ type: 'string', name: 'name', primaryKey: true }],
|
|
|
|
});
|
|
|
|
|
|
|
|
this.migrator = new Umzug({
|
|
|
|
logger: migratorOptions.logger || console,
|
|
|
|
migrations: this.migrations.callback(),
|
|
|
|
context,
|
|
|
|
storage: new SequelizeStorage({
|
2023-09-24 11:41:26 +08:00
|
|
|
tableName: `${this.options.tablePrefix || ''}migrations`,
|
|
|
|
modelName: 'migrations',
|
2023-08-04 09:37:06 +08:00
|
|
|
...migratorOptions.storage,
|
|
|
|
sequelize: this.sequelize,
|
|
|
|
}),
|
2023-01-08 12:45:02 +08:00
|
|
|
});
|
|
|
|
|
2022-10-31 22:45:39 +08:00
|
|
|
this.initListener();
|
2023-02-16 10:53:04 +08:00
|
|
|
patchSequelizeQueryInterface(this);
|
2022-10-31 22:45:39 +08:00
|
|
|
}
|
|
|
|
|
2023-03-05 14:45:56 +08:00
|
|
|
setLogger(logger: Logger) {
|
|
|
|
this.logger = logger;
|
|
|
|
}
|
|
|
|
|
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 21:56:01 +08:00
|
|
|
sequelizeOptions(options) {
|
|
|
|
if (options.dialect === 'postgres') {
|
|
|
|
options.hooks = {
|
|
|
|
afterConnect: async (connection) => {
|
|
|
|
await connection.query('SET search_path TO public;');
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
2022-10-31 22:45:39 +08:00
|
|
|
initListener() {
|
2023-04-06 09:05:47 +08:00
|
|
|
this.on('afterConnect', async (client) => {
|
|
|
|
if (this.inDialect('postgres')) {
|
|
|
|
await client.query('SET search_path = public');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-02-13 21:38:47 +08:00
|
|
|
this.on('beforeDefine', (model, options) => {
|
2023-04-02 11:27:04 +08:00
|
|
|
if (this.options.underscored && options.underscored === undefined) {
|
2023-02-13 21:38:47 +08:00
|
|
|
options.underscored = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-10-21 11:53:43 +08:00
|
|
|
this.on('afterCreate', async (instance) => {
|
|
|
|
instance?.toChangedWithAssociations?.();
|
|
|
|
});
|
|
|
|
|
|
|
|
this.on('afterUpdate', async (instance) => {
|
|
|
|
instance?.toChangedWithAssociations?.();
|
|
|
|
});
|
2022-10-31 22:45:39 +08:00
|
|
|
|
|
|
|
this.on('beforeDestroy', async (instance, options) => {
|
|
|
|
await referentialIntegrityCheck({
|
|
|
|
db: this,
|
|
|
|
referencedInstance: instance,
|
|
|
|
transaction: options.transaction,
|
|
|
|
});
|
|
|
|
});
|
2022-11-16 20:10:26 +08:00
|
|
|
|
|
|
|
this.on('afterRemoveCollection', (collection) => {
|
|
|
|
this.inheritanceMap.removeNode(collection.name);
|
|
|
|
});
|
2022-11-20 14:40:41 +08:00
|
|
|
|
|
|
|
this.on('afterDefine', (model) => {
|
|
|
|
if (lodash.get(this.options, 'usingBigIntForId', true)) {
|
|
|
|
const idAttribute = model.rawAttributes['id'];
|
|
|
|
if (idAttribute && idAttribute.primaryKey) {
|
|
|
|
model.rawAttributes['id'].type = DataTypes.BIGINT;
|
|
|
|
model.refreshAttributes();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2023-02-13 21:38:47 +08:00
|
|
|
|
2023-02-26 08:04:29 +08:00
|
|
|
this.on('afterUpdateCollection', (collection, options) => {
|
|
|
|
if (collection.options.schema) {
|
|
|
|
collection.model._schema = collection.options.schema;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-02-13 21:38:47 +08:00
|
|
|
this.on('beforeDefineCollection', (options) => {
|
2023-04-02 11:27:04 +08:00
|
|
|
if (this.options.underscored && options.underscored === undefined) {
|
|
|
|
options.underscored = true;
|
|
|
|
}
|
|
|
|
|
2023-02-13 21:38:47 +08:00
|
|
|
if (options.underscored) {
|
|
|
|
if (lodash.get(options, 'sortable.scopeKey')) {
|
|
|
|
options.sortable.scopeKey = snakeCase(options.sortable.scopeKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lodash.get(options, 'indexes')) {
|
|
|
|
// change index fields to snake case
|
|
|
|
options.indexes = options.indexes.map((index) => {
|
|
|
|
if (index.fields) {
|
|
|
|
index.fields = index.fields.map((field) => {
|
|
|
|
return snakeCase(field);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return index;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2023-02-16 10:53:04 +08:00
|
|
|
|
|
|
|
if (this.options.schema && !options.schema) {
|
|
|
|
options.schema = this.options.schema;
|
|
|
|
}
|
2023-02-13 21:38:47 +08:00
|
|
|
});
|
2023-03-17 09:20:56 +08:00
|
|
|
|
|
|
|
registerBuiltInListeners(this);
|
2022-05-30 23:10:32 +08:00
|
|
|
}
|
|
|
|
|
2022-08-16 14:41:29 +08:00
|
|
|
addMigration(item: MigrationItem) {
|
2022-05-30 23:10:32 +08:00
|
|
|
return this.migrations.add(item);
|
2021-09-07 00:28:16 +08:00
|
|
|
}
|
|
|
|
|
2022-06-17 10:25:59 +08:00
|
|
|
addMigrations(options: AddMigrationsOptions) {
|
|
|
|
const { namespace, context, extensions = ['js', 'ts'], directory } = options;
|
|
|
|
const patten = `${directory}/*.{${extensions.join(',')}}`;
|
|
|
|
const files = glob.sync(patten, {
|
|
|
|
ignore: ['**/*.d.ts'],
|
|
|
|
});
|
|
|
|
for (const file of files) {
|
|
|
|
let filename = basename(file);
|
|
|
|
filename = filename.substring(0, filename.lastIndexOf('.')) || filename;
|
|
|
|
this.migrations.add({
|
|
|
|
name: namespace ? `${namespace}/${filename}` : filename,
|
2022-09-19 09:23:01 +08:00
|
|
|
migration: requireModule(file),
|
2022-06-17 10:25:59 +08:00
|
|
|
context,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-22 14:25:10 +08:00
|
|
|
inDialect(...dialect: string[]) {
|
|
|
|
return dialect.includes(this.sequelize.getDialect());
|
|
|
|
}
|
|
|
|
|
2023-03-04 19:36:58 +08:00
|
|
|
escapeId(identifier: string) {
|
|
|
|
return this.inDialect('mysql') ? `\`${identifier}\`` : `"${identifier}"`;
|
|
|
|
}
|
|
|
|
|
2020-10-24 15:34:43 +08:00
|
|
|
/**
|
2021-12-06 21:23:34 +08:00
|
|
|
* Add collection to database
|
|
|
|
* @param options
|
2020-10-24 15:34:43 +08:00
|
|
|
*/
|
2021-12-06 21:23:34 +08:00
|
|
|
collection<Attributes = any, CreateAttributes = Attributes>(
|
|
|
|
options: CollectionOptions,
|
|
|
|
): Collection<Attributes, CreateAttributes> {
|
2023-03-10 19:16:00 +08:00
|
|
|
options = lodash.cloneDeep(options);
|
|
|
|
|
2023-02-13 21:38:47 +08:00
|
|
|
if (this.options.underscored) {
|
|
|
|
options.underscored = true;
|
|
|
|
}
|
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
this.emit('beforeDefineCollection', options);
|
|
|
|
|
2022-11-20 17:33:20 +08:00
|
|
|
const hasValidInheritsOptions = (() => {
|
|
|
|
return options.inherits && lodash.castArray(options.inherits).length > 0;
|
|
|
|
})();
|
|
|
|
|
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 21:56:01 +08:00
|
|
|
const hasViewOptions = options.viewName || options.view;
|
|
|
|
|
|
|
|
const collectionKlass = (() => {
|
|
|
|
if (hasValidInheritsOptions) {
|
|
|
|
return InheritedCollection;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hasViewOptions) {
|
|
|
|
return ViewCollection;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Collection;
|
|
|
|
})();
|
2021-12-06 21:23:34 +08: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 21:56:01 +08:00
|
|
|
const collection = new collectionKlass(options, { database: this });
|
2023-06-04 13:04:56 +08:00
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
this.collections.set(collection.name, collection);
|
|
|
|
|
|
|
|
this.emit('afterDefineCollection', collection);
|
|
|
|
|
|
|
|
return collection;
|
2020-10-24 15:34:43 +08:00
|
|
|
}
|
|
|
|
|
2022-02-15 00:20:25 +08:00
|
|
|
getTablePrefix() {
|
|
|
|
return this.options.tablePrefix || '';
|
|
|
|
}
|
|
|
|
|
2023-02-23 12:46:50 +08:00
|
|
|
getFieldByPath(path: string) {
|
|
|
|
if (!path) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const [collectionName, associationName, ...args] = path.split('.');
|
2023-04-25 13:12:14 +08:00
|
|
|
const collection = this.getCollection(collectionName);
|
2023-02-23 12:46:50 +08:00
|
|
|
|
|
|
|
if (!collection) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const field = collection.getField(associationName);
|
|
|
|
|
|
|
|
if (!field) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args.length > 0) {
|
|
|
|
return this.getFieldByPath(`${field?.target}.${args.join('.')}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return field;
|
|
|
|
}
|
|
|
|
|
2020-10-24 15:34:43 +08:00
|
|
|
/**
|
2021-12-06 21:23:34 +08:00
|
|
|
* get exists collection by its name
|
|
|
|
* @param name
|
2020-10-24 15:34:43 +08:00
|
|
|
*/
|
2021-12-06 21:23:34 +08:00
|
|
|
getCollection(name: string): Collection {
|
2023-01-09 07:35:48 +08:00
|
|
|
if (!name) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const [collectionName, associationName] = name.split('.');
|
2023-04-25 13:12:14 +08:00
|
|
|
const collection = this.collections.get(collectionName);
|
2023-01-09 07:35:48 +08:00
|
|
|
|
|
|
|
if (associationName) {
|
|
|
|
const target = collection.getField(associationName)?.target;
|
|
|
|
return target ? this.collections.get(target) : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return collection;
|
2020-10-24 15:34:43 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
hasCollection(name: string): boolean {
|
|
|
|
return this.collections.has(name);
|
2020-10-24 15:34:43 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
removeCollection(name: string) {
|
|
|
|
const collection = this.collections.get(name);
|
|
|
|
this.emit('beforeRemoveCollection', collection);
|
|
|
|
|
2022-11-22 16:19:48 +08:00
|
|
|
collection.resetFields();
|
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
const result = this.collections.delete(name);
|
|
|
|
|
2022-06-18 00:18:12 +08:00
|
|
|
this.sequelize.modelManager.removeModel(collection.model);
|
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
if (result) {
|
|
|
|
this.emit('afterRemoveCollection', collection);
|
|
|
|
}
|
2022-06-11 20:46:30 +08:00
|
|
|
|
2022-06-18 00:18:12 +08:00
|
|
|
return collection;
|
2020-10-24 15:34:43 +08:00
|
|
|
}
|
|
|
|
|
2022-02-12 12:38:57 +08:00
|
|
|
getModel<M extends Model>(name: string) {
|
2022-12-24 16:30:01 +08:00
|
|
|
return this.getCollection(name).model as ModelStatic<M>;
|
2022-02-12 12:38:57 +08:00
|
|
|
}
|
|
|
|
|
2022-01-23 12:42:35 +08:00
|
|
|
getRepository<R extends Repository>(name: string): R;
|
2023-05-19 16:39:00 +08:00
|
|
|
|
2022-01-18 16:38:03 +08:00
|
|
|
getRepository<R extends RelationRepository>(name: string, relationId: string | number): R;
|
2023-05-19 16:39:00 +08:00
|
|
|
|
2022-12-13 18:02:03 +08:00
|
|
|
getRepository<R extends ArrayFieldRepository>(name: string, relationId: string | number): R;
|
2022-01-18 16:38:03 +08:00
|
|
|
|
|
|
|
getRepository<R extends RelationRepository>(name: string, relationId?: string | number): Repository | R {
|
2023-01-09 07:35:48 +08:00
|
|
|
const [collection, relation] = name.split('.');
|
|
|
|
if (relation) {
|
2022-03-02 18:35:49 +08:00
|
|
|
return this.getRepository(collection)?.relation(relation)?.of(relationId) as R;
|
2022-01-18 16:38:03 +08:00
|
|
|
}
|
2022-03-02 18:35:49 +08:00
|
|
|
return this.getCollection(name)?.repository;
|
2022-01-18 16:38:03 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
addPendingField(field: RelationField) {
|
|
|
|
const associating = this.pendingFields;
|
|
|
|
const items = this.pendingFields.get(field.target) || [];
|
|
|
|
items.push(field);
|
|
|
|
associating.set(field.target, items);
|
2020-10-24 15:34:43 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
removePendingField(field: RelationField) {
|
|
|
|
const items = this.pendingFields.get(field.target) || [];
|
|
|
|
const index = items.indexOf(field);
|
|
|
|
if (index !== -1) {
|
|
|
|
delete items[index];
|
|
|
|
this.pendingFields.set(field.target, items);
|
2020-11-11 15:23:39 +08:00
|
|
|
}
|
2020-10-24 15:34:43 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
registerFieldTypes(fieldTypes: MapOf<typeof Field>) {
|
|
|
|
for (const [type, fieldType] of Object.entries(fieldTypes)) {
|
|
|
|
this.fieldTypes.set(type, fieldType);
|
|
|
|
}
|
2020-10-24 15:34:43 +08:00
|
|
|
}
|
|
|
|
|
2023-02-16 23:56:00 +08:00
|
|
|
registerFieldValueParsers(parsers: MapOf<any>) {
|
|
|
|
for (const [type, parser] of Object.entries(parsers)) {
|
|
|
|
this.fieldValueParsers.set(type, parser);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
buildFieldValueParser<T extends BaseValueParser>(field: Field, ctx: any) {
|
|
|
|
const Parser = this.fieldValueParsers.has(field.type)
|
|
|
|
? this.fieldValueParsers.get(field.type)
|
|
|
|
: this.fieldValueParsers.get('default');
|
|
|
|
const parser = new Parser(field, ctx);
|
|
|
|
return parser as T;
|
|
|
|
}
|
|
|
|
|
2022-12-24 16:30:01 +08:00
|
|
|
registerModels(models: MapOf<ModelStatic<any>>) {
|
2021-12-06 21:23:34 +08:00
|
|
|
for (const [type, schemaType] of Object.entries(models)) {
|
|
|
|
this.models.set(type, schemaType);
|
2020-11-11 15:23:39 +08:00
|
|
|
}
|
2020-10-24 15:34:43 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
registerRepositories(repositories: MapOf<RepositoryType>) {
|
|
|
|
for (const [type, schemaType] of Object.entries(repositories)) {
|
|
|
|
this.repositories.set(type, schemaType);
|
2020-10-24 15:34:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
initOperators() {
|
|
|
|
const operators = new Map();
|
|
|
|
|
|
|
|
// Sequelize 内置
|
|
|
|
for (const key in Op) {
|
|
|
|
operators.set('$' + key, Op[key]);
|
|
|
|
const val = Utils.underscoredIf(key, true);
|
|
|
|
operators.set('$' + val, Op[key]);
|
|
|
|
operators.set('$' + val.replace(/_/g, ''), Op[key]);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.operators = operators;
|
|
|
|
|
|
|
|
this.registerOperators({
|
2023-09-03 10:59:33 +08:00
|
|
|
...(extendOperators as unknown as MapOf<OperatorFunc>),
|
2021-12-06 21:23:34 +08:00
|
|
|
});
|
2020-10-24 15:34:43 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
registerOperators(operators: MapOf<OperatorFunc>) {
|
|
|
|
for (const [key, operator] of Object.entries(operators)) {
|
|
|
|
this.operators.set(key, operator);
|
|
|
|
}
|
|
|
|
}
|
2020-10-24 15:34:43 +08:00
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
buildField(options, context: FieldContext) {
|
|
|
|
const { type } = options;
|
2023-01-08 12:45:02 +08:00
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
const Field = this.fieldTypes.get(type);
|
2022-10-14 14:51:19 +08:00
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
if (!Field) {
|
|
|
|
throw Error(`unsupported field type ${type}`);
|
2020-10-24 15:34:43 +08:00
|
|
|
}
|
2022-10-14 14:51:19 +08:00
|
|
|
|
2023-06-04 13:04:56 +08:00
|
|
|
const { collection } = context;
|
|
|
|
|
|
|
|
if (options.field && collection.options.underscored && !collection.isView()) {
|
2023-02-13 21:38:47 +08:00
|
|
|
options.field = snakeCase(options.field);
|
|
|
|
}
|
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
return new Field(options, context);
|
|
|
|
}
|
2020-10-24 15:34:43 +08:00
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
async sync(options?: SyncOptions) {
|
|
|
|
const isMySQL = this.sequelize.getDialect() === 'mysql';
|
|
|
|
if (isMySQL) {
|
|
|
|
await this.sequelize.query('SET FOREIGN_KEY_CHECKS = 0', null);
|
2020-10-24 15:34:43 +08:00
|
|
|
}
|
2022-11-24 18:16:01 +08:00
|
|
|
|
2023-02-08 22:08:25 +08:00
|
|
|
if (this.options.schema && this.inDialect('postgres')) {
|
|
|
|
await this.sequelize.query(`CREATE SCHEMA IF NOT EXISTS "${this.options.schema}"`, null);
|
|
|
|
}
|
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
const result = await this.sequelize.sync(options);
|
2022-11-24 18:16:01 +08:00
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
if (isMySQL) {
|
|
|
|
await this.sequelize.query('SET FOREIGN_KEY_CHECKS = 1', null);
|
|
|
|
}
|
2022-11-16 12:53:58 +08:00
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
return result;
|
2020-10-24 15:34:43 +08:00
|
|
|
}
|
|
|
|
|
2022-01-30 11:11:36 +08:00
|
|
|
async clean(options: CleanOptions) {
|
2023-02-08 22:08:25 +08:00
|
|
|
const { drop, ...others } = options || {};
|
|
|
|
if (drop !== true) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.options.schema) {
|
|
|
|
const tableNames = (await this.sequelize.getQueryInterface().showAllTables()).map((table) => {
|
|
|
|
return `"${this.options.schema}"."${table}"`;
|
|
|
|
});
|
|
|
|
|
|
|
|
const skip = options.skip || [];
|
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
for (const tableName of tableNames) {
|
|
|
|
if (skip.includes(tableName)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
await this.sequelize.query(`DROP TABLE IF EXISTS ${tableName} CASCADE`);
|
|
|
|
}
|
|
|
|
return;
|
2022-01-30 11:11:36 +08:00
|
|
|
}
|
2023-02-08 22:08:25 +08: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 21:56:01 +08:00
|
|
|
await this.queryInterface.dropAll(options);
|
2022-01-30 11:11:36 +08:00
|
|
|
}
|
|
|
|
|
2023-02-13 21:38:47 +08:00
|
|
|
async collectionExistsInDb(name: string, options?: Transactionable) {
|
|
|
|
const collection = this.getCollection(name);
|
2023-03-05 14:45:56 +08:00
|
|
|
|
2023-02-13 21:38:47 +08:00
|
|
|
if (!collection) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-03-05 14:45:56 +08:00
|
|
|
return await this.queryInterface.collectionTableExists(collection, options);
|
2022-06-17 10:25:59 +08:00
|
|
|
}
|
|
|
|
|
2022-03-28 22:01:10 +08:00
|
|
|
public isSqliteMemory() {
|
|
|
|
return this.sequelize.getDialect() === 'sqlite' && lodash.get(this.options, 'storage') == ':memory:';
|
|
|
|
}
|
|
|
|
|
2023-02-09 17:04:32 +08:00
|
|
|
async auth(options: Omit<QueryOptions, 'retry'> & { retry?: number | Pick<QueryOptions, 'retry'> } = {}) {
|
2022-06-17 10:25:59 +08:00
|
|
|
const { retry = 10, ...others } = options;
|
2023-09-18 10:00:11 +08:00
|
|
|
const startingDelay = 50;
|
|
|
|
const timeMultiple = 2;
|
|
|
|
|
|
|
|
let attemptNumber = 1; // To track the current attempt number
|
|
|
|
|
2022-04-29 20:04:02 +08:00
|
|
|
const authenticate = async () => {
|
|
|
|
try {
|
|
|
|
await this.sequelize.authenticate(others);
|
|
|
|
console.log('Connection has been established successfully.');
|
|
|
|
} catch (error) {
|
2023-09-18 10:00:11 +08:00
|
|
|
console.log(`Attempt ${attemptNumber}/${retry}: Unable to connect to the database: ${error.message}`);
|
|
|
|
const nextDelay = startingDelay * Math.pow(timeMultiple, attemptNumber - 1);
|
|
|
|
console.log(`Will retry in ${nextDelay}ms...`);
|
|
|
|
attemptNumber++;
|
|
|
|
throw error; // Re-throw the error so that backoff can catch and handle it
|
2022-04-29 20:04:02 +08:00
|
|
|
}
|
|
|
|
};
|
2023-09-18 10:00:11 +08:00
|
|
|
|
|
|
|
try {
|
|
|
|
await backOff(authenticate, {
|
|
|
|
numOfAttempts: retry as number,
|
|
|
|
startingDelay: startingDelay,
|
|
|
|
timeMultiple: timeMultiple,
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
throw new Error('Connection failed, please check your database connection credentials and try again.');
|
|
|
|
}
|
2022-04-29 20:04:02 +08:00
|
|
|
}
|
|
|
|
|
2023-02-23 20:14:50 +08:00
|
|
|
async prepare() {
|
|
|
|
if (this.inDialect('postgres') && this.options.schema && this.options.schema != 'public') {
|
|
|
|
await this.sequelize.query(`CREATE SCHEMA IF NOT EXISTS "${this.options.schema}"`, null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-30 11:11:36 +08:00
|
|
|
async reconnect() {
|
2022-03-28 22:01:10 +08:00
|
|
|
if (this.isSqliteMemory()) {
|
|
|
|
return;
|
|
|
|
}
|
2022-01-30 11:11:36 +08:00
|
|
|
// @ts-ignore
|
|
|
|
const ConnectionManager = this.sequelize.dialect.connectionManager.constructor;
|
|
|
|
// @ts-ignore
|
|
|
|
const connectionManager = new ConnectionManager(this.sequelize.dialect, this.sequelize);
|
|
|
|
// @ts-ignore
|
|
|
|
this.sequelize.dialect.connectionManager = connectionManager;
|
|
|
|
// @ts-ignore
|
|
|
|
this.sequelize.connectionManager = connectionManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
closed() {
|
|
|
|
// @ts-ignore
|
|
|
|
return this.sequelize.connectionManager.pool._draining;
|
|
|
|
}
|
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
async close() {
|
2022-03-28 22:01:10 +08:00
|
|
|
if (this.isSqliteMemory()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-11 15:23:39 +08:00
|
|
|
return this.sequelize.close();
|
2020-10-24 15:34:43 +08:00
|
|
|
}
|
2020-12-15 20:16:55 +08:00
|
|
|
|
2022-10-14 17:22:32 +08:00
|
|
|
on(event: EventType, listener: any): this;
|
2023-05-19 16:39:00 +08:00
|
|
|
|
2022-10-14 17:22:32 +08:00
|
|
|
on(event: ModelValidateEventTypes, listener: SyncListener): this;
|
2023-05-19 16:39:00 +08:00
|
|
|
|
2022-10-14 17:22:32 +08:00
|
|
|
on(event: ModelValidateEventTypes, listener: ValidateListener): this;
|
2023-05-19 16:39:00 +08:00
|
|
|
|
2022-10-14 17:22:32 +08:00
|
|
|
on(event: ModelCreateEventTypes, listener: CreateListener): this;
|
2023-05-19 16:39:00 +08:00
|
|
|
|
2022-10-14 17:22:32 +08:00
|
|
|
on(event: ModelUpdateEventTypes, listener: UpdateListener): this;
|
2023-05-19 16:39:00 +08:00
|
|
|
|
2022-10-14 17:22:32 +08:00
|
|
|
on(event: ModelSaveEventTypes, listener: SaveListener): this;
|
2023-05-19 16:39:00 +08:00
|
|
|
|
2022-10-14 17:22:32 +08:00
|
|
|
on(event: ModelDestroyEventTypes, listener: DestroyListener): this;
|
2023-05-19 16:39:00 +08:00
|
|
|
|
2022-10-14 17:22:32 +08:00
|
|
|
on(event: ModelCreateWithAssociationsEventTypes, listener: CreateWithAssociationsListener): this;
|
2023-05-19 16:39:00 +08:00
|
|
|
|
2022-10-14 17:22:32 +08:00
|
|
|
on(event: ModelUpdateWithAssociationsEventTypes, listener: UpdateWithAssociationsListener): this;
|
2023-05-19 16:39:00 +08:00
|
|
|
|
2022-10-14 17:22:32 +08:00
|
|
|
on(event: ModelSaveWithAssociationsEventTypes, listener: SaveWithAssociationsListener): this;
|
2023-05-19 16:39:00 +08:00
|
|
|
|
2022-10-14 17:22:32 +08:00
|
|
|
on(event: DatabaseBeforeDefineCollectionEventType, listener: BeforeDefineCollectionListener): this;
|
2023-05-19 16:39:00 +08:00
|
|
|
|
2022-10-14 17:22:32 +08:00
|
|
|
on(event: DatabaseAfterDefineCollectionEventType, listener: AfterDefineCollectionListener): this;
|
2023-05-19 16:39:00 +08:00
|
|
|
|
2022-10-31 22:45:39 +08:00
|
|
|
on(
|
|
|
|
event: DatabaseBeforeRemoveCollectionEventType | DatabaseAfterRemoveCollectionEventType,
|
|
|
|
listener: RemoveCollectionListener,
|
|
|
|
): this;
|
2023-05-19 16:39:00 +08:00
|
|
|
|
2022-10-14 17:22:32 +08:00
|
|
|
on(event: EventType, listener: any): this {
|
2022-06-08 11:22:08 +08:00
|
|
|
// NOTE: to match if event is a sequelize or model type
|
|
|
|
const type = this.modelHook.match(event);
|
2021-12-06 21:23:34 +08:00
|
|
|
|
2022-06-08 11:22:08 +08:00
|
|
|
if (type && !this.modelHook.hasBoundEvent(type)) {
|
|
|
|
this.sequelize.addHook(type, this.modelHook.buildSequelizeHook(type));
|
|
|
|
this.modelHook.bindEvent(type);
|
2021-12-06 21:23:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return super.on(event, listener);
|
2021-09-29 07:38:05 +08:00
|
|
|
}
|
|
|
|
|
2022-08-04 21:56:13 +08:00
|
|
|
extendCollection(collectionOptions: CollectionOptions, mergeOptions?: MergeOptions) {
|
2023-02-13 09:57:03 +08:00
|
|
|
collectionOptions = lodash.cloneDeep(collectionOptions);
|
2022-08-04 21:56:13 +08:00
|
|
|
const collectionName = collectionOptions.name;
|
|
|
|
const existCollection = this.getCollection(collectionName);
|
|
|
|
if (existCollection) {
|
|
|
|
existCollection.updateOptions(collectionOptions, mergeOptions);
|
|
|
|
} else {
|
|
|
|
const existDelayExtends = this.delayCollectionExtend.get(collectionName) || [];
|
|
|
|
|
|
|
|
this.delayCollectionExtend.set(collectionName, [...existDelayExtends, { collectionOptions, mergeOptions }]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-08 12:45:02 +08:00
|
|
|
async import(options: {
|
|
|
|
directory: string;
|
|
|
|
from?: string;
|
|
|
|
extensions?: ImportFileExtension[];
|
|
|
|
}): Promise<Map<string, Collection>> {
|
2021-12-06 21:23:34 +08:00
|
|
|
const reader = new ImporterReader(options.directory, options.extensions);
|
|
|
|
const modules = await reader.read();
|
|
|
|
const result = new Map<string, Collection>();
|
|
|
|
|
|
|
|
for (const module of modules) {
|
|
|
|
if (module.extend) {
|
2022-08-04 21:56:13 +08:00
|
|
|
this.extendCollection(module.collectionOptions, module.mergeOptions);
|
2021-12-06 21:23:34 +08:00
|
|
|
} else {
|
|
|
|
const collection = this.collection(module);
|
2023-01-08 12:45:02 +08:00
|
|
|
|
|
|
|
if (options.from) {
|
|
|
|
this.importedFrom.set(options.from, [...(this.importedFrom.get(options.from) || []), collection.name]);
|
|
|
|
}
|
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
result.set(collection.name, collection);
|
2021-09-29 07:38:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
return result;
|
2020-12-29 14:53:39 +08:00
|
|
|
}
|
2021-12-06 21:23:34 +08:00
|
|
|
}
|
|
|
|
|
2022-08-04 21:56:13 +08:00
|
|
|
export function extendCollection(collectionOptions: CollectionOptions, mergeOptions?: MergeOptions) {
|
2021-12-06 21:23:34 +08:00
|
|
|
return {
|
|
|
|
collectionOptions,
|
|
|
|
mergeOptions,
|
|
|
|
extend: true,
|
|
|
|
};
|
2020-10-24 15:34:43 +08:00
|
|
|
}
|
2021-12-06 21:23:34 +08:00
|
|
|
|
2022-08-04 21:56:13 +08:00
|
|
|
export const extend = extendCollection;
|
|
|
|
|
2022-02-11 14:34:33 +08:00
|
|
|
export const defineCollection = (collectionOptions: CollectionOptions) => {
|
|
|
|
return collectionOptions;
|
|
|
|
};
|
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
applyMixins(Database, [AsyncEmitter]);
|
|
|
|
|
|
|
|
export default Database;
|