tachybase_todo/packages/core/database/src/database.ts

589 lines
17 KiB
TypeScript
Raw Normal View History

import { applyMixins, AsyncEmitter, requireModule } from '@nocobase/utils';
Application (#175) * feat: getRepository * getRepository return type * export action * add: acl * feat: setResourceAction * feat: action alias * chore: code struct * feat: removeResourceAction * chore: file name * ignorecase * remove ACL * feat: ACL * feat: role toJSON * using emit * chore: test * feat: plugin-acl * feat: acl with predicate * grant universal action test * grant action test * update resource action test * revoke resource action * usingActionsConfig switch * plugin-ui-schema-storage * remove global acl instance * fix: collection manager with sqlite * add own action listener * add acl middleware * add acl allowConfigure strategy option * add plugin-acl allowConfigure * change acl resourceName * add acl middleware merge params * bugfix * append fields on acl action params * acl middleware parse template * fix: collection-manager migrate * add acl association field test * feat(plugin-acl): grant association field actions * chore(plugin-acl): type name * feat(plugin-acl): regrant actions on resource action update * feat(plugin-acl): regrant action on field destroy * fix(plugin-acl): test * fix(plugin-acl): test run * feat(plugin-acl): set default role * feat(plugin-users): set user default role * test(plugin-users): create user with role * feat(plugin-users): create user with role * feat(application): application hook * feat(database): reconnect * feat(database): application life cycle * feat(database): sync with option * feat(database): hook position * feat(database): hook position * feat(database): remove load in start * fix(application): get plugin * feat(test): loadAndInstall * feat: improve code * feat: improve code * fix: listen options * fix: bug * test(database): add test case Co-authored-by: chenos <chenlinxh@gmail.com>
2022-01-30 11:11:36 +08:00
import merge from 'deepmerge';
import { EventEmitter } from 'events';
import glob from 'glob';
import lodash from 'lodash';
import { basename, isAbsolute, resolve } from 'path';
import semver from 'semver';
2022-04-29 20:04:02 +08:00
import {
ModelCtor,
Op,
Options,
QueryInterfaceDropAllTablesOptions,
QueryOptions,
Sequelize,
SyncOptions,
Transactionable,
Utils,
2022-04-29 20:04:02 +08:00
} from 'sequelize';
import { SequelizeStorage, Umzug } from 'umzug';
2021-12-06 21:23:34 +08:00
import { Collection, CollectionOptions, RepositoryType } from './collection';
Application (#175) * feat: getRepository * getRepository return type * export action * add: acl * feat: setResourceAction * feat: action alias * chore: code struct * feat: removeResourceAction * chore: file name * ignorecase * remove ACL * feat: ACL * feat: role toJSON * using emit * chore: test * feat: plugin-acl * feat: acl with predicate * grant universal action test * grant action test * update resource action test * revoke resource action * usingActionsConfig switch * plugin-ui-schema-storage * remove global acl instance * fix: collection manager with sqlite * add own action listener * add acl middleware * add acl allowConfigure strategy option * add plugin-acl allowConfigure * change acl resourceName * add acl middleware merge params * bugfix * append fields on acl action params * acl middleware parse template * fix: collection-manager migrate * add acl association field test * feat(plugin-acl): grant association field actions * chore(plugin-acl): type name * feat(plugin-acl): regrant actions on resource action update * feat(plugin-acl): regrant action on field destroy * fix(plugin-acl): test * fix(plugin-acl): test run * feat(plugin-acl): set default role * feat(plugin-users): set user default role * test(plugin-users): create user with role * feat(plugin-users): create user with role * feat(application): application hook * feat(database): reconnect * feat(database): application life cycle * feat(database): sync with option * feat(database): hook position * feat(database): hook position * feat(database): remove load in start * fix(application): get plugin * feat(test): loadAndInstall * feat: improve code * feat: improve code * fix: listen options * fix: bug * test(database): add test case Co-authored-by: chenos <chenlinxh@gmail.com>
2022-01-30 11:11:36 +08:00
import { ImporterReader, ImportFileExtension } from './collection-importer';
2021-12-06 21:23:34 +08:00
import * as FieldTypes from './fields';
Application (#175) * feat: getRepository * getRepository return type * export action * add: acl * feat: setResourceAction * feat: action alias * chore: code struct * feat: removeResourceAction * chore: file name * ignorecase * remove ACL * feat: ACL * feat: role toJSON * using emit * chore: test * feat: plugin-acl * feat: acl with predicate * grant universal action test * grant action test * update resource action test * revoke resource action * usingActionsConfig switch * plugin-ui-schema-storage * remove global acl instance * fix: collection manager with sqlite * add own action listener * add acl middleware * add acl allowConfigure strategy option * add plugin-acl allowConfigure * change acl resourceName * add acl middleware merge params * bugfix * append fields on acl action params * acl middleware parse template * fix: collection-manager migrate * add acl association field test * feat(plugin-acl): grant association field actions * chore(plugin-acl): type name * feat(plugin-acl): regrant actions on resource action update * feat(plugin-acl): regrant action on field destroy * fix(plugin-acl): test * fix(plugin-acl): test run * feat(plugin-acl): set default role * feat(plugin-users): set user default role * test(plugin-users): create user with role * feat(plugin-users): create user with role * feat(application): application hook * feat(database): reconnect * feat(database): application life cycle * feat(database): sync with option * feat(database): hook position * feat(database): hook position * feat(database): remove load in start * fix(application): get plugin * feat(test): loadAndInstall * feat: improve code * feat: improve code * fix: listen options * fix: bug * test(database): add test case Co-authored-by: chenos <chenlinxh@gmail.com>
2022-01-30 11:11:36 +08:00
import { Field, FieldContext, RelationField } from './fields';
import { MigrationItem, Migrations } from './migration';
import { Model } from './model';
2021-12-06 21:23:34 +08:00
import { ModelHook } from './model-hook';
import extendOperators from './operators';
import { RelationRepository } from './relation-repository/relation-repository';
Application (#175) * feat: getRepository * getRepository return type * export action * add: acl * feat: setResourceAction * feat: action alias * chore: code struct * feat: removeResourceAction * chore: file name * ignorecase * remove ACL * feat: ACL * feat: role toJSON * using emit * chore: test * feat: plugin-acl * feat: acl with predicate * grant universal action test * grant action test * update resource action test * revoke resource action * usingActionsConfig switch * plugin-ui-schema-storage * remove global acl instance * fix: collection manager with sqlite * add own action listener * add acl middleware * add acl allowConfigure strategy option * add plugin-acl allowConfigure * change acl resourceName * add acl middleware merge params * bugfix * append fields on acl action params * acl middleware parse template * fix: collection-manager migrate * add acl association field test * feat(plugin-acl): grant association field actions * chore(plugin-acl): type name * feat(plugin-acl): regrant actions on resource action update * feat(plugin-acl): regrant action on field destroy * fix(plugin-acl): test * fix(plugin-acl): test run * feat(plugin-acl): set default role * feat(plugin-users): set user default role * test(plugin-users): create user with role * feat(plugin-users): create user with role * feat(application): application hook * feat(database): reconnect * feat(database): application life cycle * feat(database): sync with option * feat(database): hook position * feat(database): hook position * feat(database): remove load in start * fix(application): get plugin * feat(test): loadAndInstall * feat: improve code * feat: improve code * fix: listen options * fix: bug * test(database): add test case Co-authored-by: chenos <chenlinxh@gmail.com>
2022-01-30 11:11:36 +08:00
import { Repository } from './repository';
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,
ValidateListener,
} from './types';
import { referentialIntegrityCheck } from './features/referential-integrity-check';
import ReferencesMap from './features/ReferencesMap';
feat: collection inheritance (#1069) * chore: test * chore: inherited-collection class * feat: collection inherit * feat: collection inherit * feat: inhertis sync runner * test: get parents fields * feat: collection inherit style promote * feat: sync * feat: sync alter table * feat: pgOnly Test * fix: child collection create api * feat: replace parent field * chore: reload parent fields * test: reload collection test * feat: details are displayed according to conditions * fix: typo * feat: inheritance map class * chore: is parent node * feat: display where child row created from * fix: find with appends * feat: add parent collection fields * fix: create table * feat: load fields for all children * refactor: sync fields from parent * test: has one field inhertis * feat: replace child association target * feat: should not replace child field when parent field update * test: should update inherit field when parent field update * feat: only the blocks directly inherited from the current data are displayed * fix: inherit from multiple collections * feat: only the blocks directly inherited from the current data are displayed * fix: test * feat: parent collection expend * fix: test * test: belongsToMany inherits * test: belongsToMany inherits * feat: block display * feat: collection inherite * feat: collection inherite * feat: multiple inherits * fix: sync runner * feat: collection inherite * feat: collecton inherits * feat: cannot be modified after inheritance and saving * feat: collection inherit for graph * feat: collection inherits * fix: drop inhertied field * fix: should throw error when type conflit * feat: output inherited fields * feat: bulk update collection fields * feat: collection fields * feat: collection fields * test: create relation with child table * fix: test * fix: test * fix: test * feat: style impove * test: should not replace field with difference type * feat: add text * fix: throw error when replace field with difference type * feat: overriding * feat: kan bankanban group fields * feat: calendar block fields * feat: kan bankanban group fields * fix: test * feat: relationship fields * feat: should delete child's field when parent field deleted * feat: foreign key filter * fix: build error & multiple inherit destory field * fix: test * chore: disable error * feat: no recursive update associations (#1091) * feat: update associations * fix(collection-manager): should update uiSchema * chore: flip if * feat: mutile inherits * feat: db dialect * feat: inherits show by database * chore: git hash into docker image * fix: js gzip * fix: dockerfile * chore: error message * feat: overriding * feat: overriding * feat: overriding * feat: local * feat: filter fields by interface * fix: database logging env * test: replace hasOne target * feat: add view * feat: local * chore: enable error * fix: update docs Co-authored-by: katherinehhh <katherine_15995@163.com> Co-authored-by: chenos <chenlinxh@gmail.com>
2022-11-16 12:53:58 +08:00
import { InheritedCollection } from './inherited-collection';
import InheritanceMap from './inherited-map';
2020-10-24 15:34:43 +08:00
2021-12-06 21:23:34 +08:00
export interface MergeOptions extends merge.Options {}
2020-10-24 15:34:43 +08:00
2021-12-06 21:23:34 +08:00
export interface PendingOptions {
field: RelationField;
model: ModelCtor<Model>;
2020-10-24 15:34:43 +08:00
}
2021-12-06 21:23:34 +08:00
interface MapOf<T> {
[key: string]: T;
}
2022-02-15 00:20:25 +08:00
export interface IDatabaseOptions extends Options {
tablePrefix?: string;
migrator?: any;
2022-02-15 00:20:25 +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;
app?: any;
2021-01-14 10:35:15 +08:00
}
Application (#175) * feat: getRepository * getRepository return type * export action * add: acl * feat: setResourceAction * feat: action alias * chore: code struct * feat: removeResourceAction * chore: file name * ignorecase * remove ACL * feat: ACL * feat: role toJSON * using emit * chore: test * feat: plugin-acl * feat: acl with predicate * grant universal action test * grant action test * update resource action test * revoke resource action * usingActionsConfig switch * plugin-ui-schema-storage * remove global acl instance * fix: collection manager with sqlite * add own action listener * add acl middleware * add acl allowConfigure strategy option * add plugin-acl allowConfigure * change acl resourceName * add acl middleware merge params * bugfix * append fields on acl action params * acl middleware parse template * fix: collection-manager migrate * add acl association field test * feat(plugin-acl): grant association field actions * chore(plugin-acl): type name * feat(plugin-acl): regrant actions on resource action update * feat(plugin-acl): regrant action on field destroy * fix(plugin-acl): test * fix(plugin-acl): test run * feat(plugin-acl): set default role * feat(plugin-users): set user default role * test(plugin-users): create user with role * feat(plugin-users): create user with role * feat(application): application hook * feat(database): reconnect * feat(database): application life cycle * feat(database): sync with option * feat(database): hook position * feat(database): hook position * feat(database): remove load in start * fix(application): get plugin * feat(test): loadAndInstall * feat: improve code * feat: improve code * fix: listen options * fix: bug * test(database): add test case Co-authored-by: chenos <chenlinxh@gmail.com>
2022-01-30 11:11:36 +08:00
export interface CleanOptions extends QueryInterfaceDropAllTablesOptions {
drop?: boolean;
}
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
class DatabaseVersion {
db: Database;
constructor(db: Database) {
this.db = db;
}
async satisfies(versions) {
const dialects = {
sqlite: {
sql: 'select sqlite_version() as version',
get: (v) => v,
},
mysql: {
sql: 'select version() as version',
get: (v) => {
const m = /([\d+\.]+)/.exec(v);
return m[0];
},
},
postgres: {
sql: 'select version() as version',
get: (v) => {
const m = /([\d+\.]+)/.exec(v);
return semver.minVersion(m[0]).version;
},
},
};
for (const dialect of Object.keys(dialects)) {
if (this.db.inDialect(dialect)) {
if (!versions?.[dialect]) {
return false;
}
const [result] = (await this.db.sequelize.query(dialects[dialect].sql)) as any;
return semver.satisfies(dialects[dialect].get(result?.[0]?.version), versions[dialect]);
}
}
return false;
}
}
2021-12-06 21:23:34 +08:00
export class Database extends EventEmitter implements AsyncEmitter {
sequelize: Sequelize;
migrator: Umzug;
migrations: Migrations;
2021-12-06 21:23:34 +08:00
fieldTypes = new Map();
2022-02-15 00:20:25 +08:00
options: IDatabaseOptions;
models = new Map<string, ModelCtor<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[]>();
modelCollection = new Map<ModelCtor<any>, Collection>();
feat: collection inheritance (#1069) * chore: test * chore: inherited-collection class * feat: collection inherit * feat: collection inherit * feat: inhertis sync runner * test: get parents fields * feat: collection inherit style promote * feat: sync * feat: sync alter table * feat: pgOnly Test * fix: child collection create api * feat: replace parent field * chore: reload parent fields * test: reload collection test * feat: details are displayed according to conditions * fix: typo * feat: inheritance map class * chore: is parent node * feat: display where child row created from * fix: find with appends * feat: add parent collection fields * fix: create table * feat: load fields for all children * refactor: sync fields from parent * test: has one field inhertis * feat: replace child association target * feat: should not replace child field when parent field update * test: should update inherit field when parent field update * feat: only the blocks directly inherited from the current data are displayed * fix: inherit from multiple collections * feat: only the blocks directly inherited from the current data are displayed * fix: test * feat: parent collection expend * fix: test * test: belongsToMany inherits * test: belongsToMany inherits * feat: block display * feat: collection inherite * feat: collection inherite * feat: multiple inherits * fix: sync runner * feat: collection inherite * feat: collecton inherits * feat: cannot be modified after inheritance and saving * feat: collection inherit for graph * feat: collection inherits * fix: drop inhertied field * fix: should throw error when type conflit * feat: output inherited fields * feat: bulk update collection fields * feat: collection fields * feat: collection fields * test: create relation with child table * fix: test * fix: test * fix: test * feat: style impove * test: should not replace field with difference type * feat: add text * fix: throw error when replace field with difference type * feat: overriding * feat: kan bankanban group fields * feat: calendar block fields * feat: kan bankanban group fields * fix: test * feat: relationship fields * feat: should delete child's field when parent field deleted * feat: foreign key filter * fix: build error & multiple inherit destory field * fix: test * chore: disable error * feat: no recursive update associations (#1091) * feat: update associations * fix(collection-manager): should update uiSchema * chore: flip if * feat: mutile inherits * feat: db dialect * feat: inherits show by database * chore: git hash into docker image * fix: js gzip * fix: dockerfile * chore: error message * feat: overriding * feat: overriding * feat: overriding * feat: local * feat: filter fields by interface * fix: database logging env * test: replace hasOne target * feat: add view * feat: local * chore: enable error * fix: update docs Co-authored-by: katherinehhh <katherine_15995@163.com> Co-authored-by: chenos <chenlinxh@gmail.com>
2022-11-16 12:53:58 +08:00
tableNameCollectionMap = new Map<string, Collection>();
referenceMap = new ReferencesMap();
feat: collection inheritance (#1069) * chore: test * chore: inherited-collection class * feat: collection inherit * feat: collection inherit * feat: inhertis sync runner * test: get parents fields * feat: collection inherit style promote * feat: sync * feat: sync alter table * feat: pgOnly Test * fix: child collection create api * feat: replace parent field * chore: reload parent fields * test: reload collection test * feat: details are displayed according to conditions * fix: typo * feat: inheritance map class * chore: is parent node * feat: display where child row created from * fix: find with appends * feat: add parent collection fields * fix: create table * feat: load fields for all children * refactor: sync fields from parent * test: has one field inhertis * feat: replace child association target * feat: should not replace child field when parent field update * test: should update inherit field when parent field update * feat: only the blocks directly inherited from the current data are displayed * fix: inherit from multiple collections * feat: only the blocks directly inherited from the current data are displayed * fix: test * feat: parent collection expend * fix: test * test: belongsToMany inherits * test: belongsToMany inherits * feat: block display * feat: collection inherite * feat: collection inherite * feat: multiple inherits * fix: sync runner * feat: collection inherite * feat: collecton inherits * feat: cannot be modified after inheritance and saving * feat: collection inherit for graph * feat: collection inherits * fix: drop inhertied field * fix: should throw error when type conflit * feat: output inherited fields * feat: bulk update collection fields * feat: collection fields * feat: collection fields * test: create relation with child table * fix: test * fix: test * fix: test * feat: style impove * test: should not replace field with difference type * feat: add text * fix: throw error when replace field with difference type * feat: overriding * feat: kan bankanban group fields * feat: calendar block fields * feat: kan bankanban group fields * fix: test * feat: relationship fields * feat: should delete child's field when parent field deleted * feat: foreign key filter * fix: build error & multiple inherit destory field * fix: test * chore: disable error * feat: no recursive update associations (#1091) * feat: update associations * fix(collection-manager): should update uiSchema * chore: flip if * feat: mutile inherits * feat: db dialect * feat: inherits show by database * chore: git hash into docker image * fix: js gzip * fix: dockerfile * chore: error message * feat: overriding * feat: overriding * feat: overriding * feat: local * feat: filter fields by interface * fix: database logging env * test: replace hasOne target * feat: add view * feat: local * chore: enable error * fix: update docs Co-authored-by: katherinehhh <katherine_15995@163.com> Co-authored-by: chenos <chenlinxh@gmail.com>
2022-11-16 12:53:58 +08:00
inheritanceMap = new InheritanceMap();
2020-10-24 15:34:43 +08:00
2021-12-06 21:23:34 +08:00
modelHook: ModelHook;
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 }[]>();
constructor(options: DatabaseOptions) {
super();
2020-10-24 15:34:43 +08:00
feat: improve code (#978) * feat: 图形化管理数据表 * feat: 图形化管理数据表 * feat: 图形化管理数据表 * feat: 图形化管理数据表 * feat: 完善图形化管理数据表 * feat: 完善图形化管理数据表 * feat: 完善图形化管理数据表 * feat: 完善图形化管理数据表 * feat: 完善图形化管理数据表 * feat: 完善图形化管理数据表 * feat: 完善图形化管理数据表 * feat: 完善图形化管理数据表 * feat: 完善图形化管理数据表 * feat: 完善图形化管理数据表 * feat: 完善图形化管理数据表 * feat: 完善图形化管理数据表 * feat: 完善图形化管理数据表 * feat: 完善图形化管理数据表 * feat: 完善图形化管理数据表 * feat: 完善图形化管理数据表 * feat: 完善图形化管理数据表 * feat: 样式优化图形化管理数据表 * feat: 样式优化图形化管理数据表 * feat: 样式优化图形化管理数据表 * feat: 样式优化图形化管理数据表 * feat: 样式优化图形化管理数据表 * feat: 样式优化图形化管理数据表 * feat: 样式优化图形化管理数据表 * feat: 样式优化图形化管理数据表 * feat: 样式优化图形化管理数据表 * feat: 样式优化图形化管理数据表 * feat: 样式优化图形化管理数据表 * feat: 样式优化图形化管理数据表 * feat: 样式优化图形化管理数据表 * feat: 样式优化图形化管理数据表 * feat: 样式优化图形化管理数据表 * feat: 样式优化图形化管理数据表 * feat: 样式优化图形化管理数据表 * feat: 样式优化图形化管理数据表 * feat: 样式优化图形化管理数据表 * feat: 样式优化图形化管理数据表 * feat: 样式优化图形化管理数据表 * feat(collection-manager): add foreignKey Field and support relate field record foreignKey info through collection record into collections and foreignKey field record info fields * fix(collection-manager): if has through collection then don't create through collections record * fix(client/route-switch): skip sub routes * feat: 添加graphpostion * feat: 图形化collection新增表时刷新数据 * fix(collection-manager): refactor afterCreateForRelateField * feat: 图形化collection存储位置 * feat: 图形化collection存储位置 * feat: 图形化collection存储位置 * feat: 图形化collection存储位置 * feat: 图形化collection存储位置 * feat: 图形化collection存储位置 * feat: 图形化collection存储位置 * feat: 图形化collection存储位置 * feat: 图形化collection存储位置 * feat: 图形化collection存储位置 * feat: 图形化样式优化 * feat: styling * feat: 图形化样式优化 * feat: 图形化样式优化 * feat: 图形化数据表多语言完善 * feat: 图形化数据表多语言完善 * feat: improve code * feat: 图形化数据表连线样式修改 * feat: 图形化数据表样式修改 * feat: 图形化数据表样式修改 * feat: 图形化数据表样式修改 * feat: 图形化数据表样式修改 * fix(collection-manager): fix afterCreateForRelateField * feat: 样式优化 * feat: 样式优化 * feat: afterCreateForForeignKeyField * fix: timestamps: false * feat: 连线锚点优化 * fix(collection-manager): when del foreign key field, relate fields will be del too * fix: update package.json * fix: update package.json * feat: 文件名大小写 * feat: 连线锚点优化 * feat: 连线锚点通过计算得到样式优化 * feat: 连线锚点通过计算得到样式优化 * fix: fk * fix: remove index * feat: 连线hover时高亮 * fix: test error * feat: 初始化计算位置 * feat: 初始化时计算坐标位置 * feat: 初始化时计算坐标位置 * feat: improve code (#933) * fix: built in * feat: 没有关系字段时也要连线 * feat: 自关联也要连线 * fix: styling * feat: 滚动条问题 * feat: 拖拽优化 * feat: 画布paddig优化 * feat: 编辑时支持反向关联字段配置 * feat: 画布拖拽滚动优化 * feat: 画布拖拽滚动优化 * fix: reload * feat: 修复数据表新建重叠 * fix: refreshCM & refreshGM * feat: 修复表达式输入框显示异常 * feat: 渲染性能优化(增量渲染) * feat: 渲染性能优化(增量渲染) * feat: 渲染性能优化(增量渲染) * fix: 消除代码警告 * fix: 消除代码警告 * feat: 渲染性能优化(增量渲染) * feat: 渲染性能优化(增量渲染) * feat: 渲染性能优化(增量渲染) * feat: 渲染性能优化(增量渲染) * feat: 渲染性能优化(增量渲染) * feat: 渲染性能优化(增量渲染) * feat: 渲染性能优化(增量渲染) * feat: 渲染性能优化 * feat: 渲染性能优化 * feat: 外键生成在位置在前面 * feat: 限制表最多显示10个字段其余滚动 * feat: 移动表位置的连线重新计算最优位置 * fix: error * feat: 布局自动换行 * fix: test error * fix: xpipe.eq * fix: upgrade error * fix: upgrade error * feat: 选中表时只显示和目标表关联的表和连线 * fix: maxListenersExceededWarning * feat: remove graph-collection-manager * fix: remove graph-collection-manager * fix: update yarn.lock Co-authored-by: 唐小爱 <tangxiaoai@192.168.0.103> Co-authored-by: lyf-coder <lyf-coder@foxmail.com> Co-authored-by: katherinehhh <katherine_15995@163.com>
2022-10-28 15:09:14 +08:00
// this.setMaxListeners(100);
this.version = new DatabaseVersion(this);
const opts = {
sync: {
alter: {
drop: false,
2022-04-30 17:28:30 +08:00
},
force: false,
},
...lodash.clone(options),
};
if (options.storage && options.storage !== ':memory:') {
if (!isAbsolute(options.storage)) {
opts.storage = resolve(process.cwd(), options.storage);
feat: build, cli, devtools, sdk, docs... * feat: nocobase build * chore: update build scripts * chore: update build scripts * chore(versions): 😊 publish v0.7.0-alpha.33 * chore: independent version * chore: nocobase build * chore(versions): 😊 publish v0.7.0-alpha.34 * feat: nocobase-cli * feat: nocobase-cli * chore: update dependencies * feat: improve code * refactor: create-nocobase-app * chore(versions): 😊 publish v0.7.0-alpha.35 * feat: @nocobase/devtools * chore(versions): 😊 publish v0.7.0-alpha.36 * chore: update dependencies * chore(versions): 😊 publish v0.7.0-alpha.37 * feat: improve code * chore(versions): 😊 publish v0.7.0-alpha.38 * feat: improve code * chore(versions): 😊 publish v0.7.0-alpha.39 * feat: update deps * chore(versions): 😊 publish v0.7.0-alpha.40 * chore: update devDependencies * chore(versions): 😊 publish v0.7.0-alpha.41 * fix: postinstall * chore(versions): 😊 publish v0.7.0-alpha.42 * chore: improve code * chore(versions): 😊 publish v0.7.0-alpha.43 * chore: execa * chore(versions): 😊 publish v0.7.0-alpha.44 * chore(cli): allow unknown option * chore(versions): 😊 publish v0.7.0-alpha.45 * fix: default envs * chore(versions): 😊 publish v0.7.0-alpha.45 * fix: package argument for build command * chore(versions): 😊 publish v0.7.0-alpha.46 * fix: improve code * chore(versions): 😊 publish v0.7.0-alpha.48 * feat: clean & doc * chore(versions): 😊 publish v0.7.0-alpha.49 * feat: compilation tips * feat: upgrade command * chore(versions): 😊 publish v0.7.0-alpha.50 * fix: unexpected token ] in JSON * chore(versions): 😊 publish v0.7.0-alpha.51 * fix: upgrade command * chore(versions): 😊 publish v0.7.0-alpha.52 * fix: remove export action from available action * fix: db sync after upgrade * chore(versions): 😊 publish v0.7.0-alpha.53 * feat: upgrade log * chore(versions): 😊 publish v0.7.0-alpha.54 * docs: updates * feat: updates * docs(cli): update usage description * feat: updates * docs: updates * docs: updates * docs: toc * feat: sdk * docs: updates * docs: updates * docs: updates * Update index.md * docs: updates * Update release-notes.md * Update roadmap.md * Update index.md * Update contributing.md * Update contributing.md * Update index.md * Update index.md * Update nocobase-cli.md * Update nocobase-cli.md * fix: user plugin initialization data * Update env.md * Update env.md * Update directory-structure.md * Update index.md * Update action-api.md * Update filter-operators.md * docs: update thanks.md * Update index.md * Update javascript-sdk.md * Update rest-api.md * Update installation.md * Update installation.md * Update upgrading.md * Update upgrading.md * Update upgrading.md * Update installation.md * Update installation.md * Create release-notes.md * Update release-notes.md * feat: updates * feat: update docs * feat: update release-notes.md * feat: switch language * feat: updates * Add files via upload * Add files via upload * Update important-features.md * Update thanks.md * feat: nocobase postinstall * Update index.md * Create why-different.md * Update why-different.md * Create who-is-for.md * Rename who-is-for.md to who.md * feat: update docs * Rename why-different.md to why.md * Update why.md * Update menus.ts * Update why-nocobase.md * Create who.md * Create why.md * feat: updates * chore(versions): 😊 publish v0.7.0-alpha.55 * feat: tips * Update who.md * Update who.md * feat: update docs * feat: update doc menus * fix: plugin client dist * docs: update contributing.md * docs: update readme.md * docs: update readme.md * docs: update readme.md * Update functional-zoning.md * fix: br Co-authored-by: Zhou <zhou.working@gmail.com>
2022-05-19 00:40:55 +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
}
this.sequelize = new Sequelize(opts);
this.options = opts;
2021-12-06 21:23:34 +08:00
this.collections = new Map();
this.modelHook = new ModelHook(this);
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-12-06 21:23:34 +08:00
// register database field types
for (const [name, field] of Object.entries(FieldTypes)) {
if (['Field', 'RelationField'].includes(name)) {
continue;
}
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-12-06 21:23:34 +08:00
this.initOperators();
const migratorOptions: any = this.options.migrator || {};
const context = {
db: this,
sequelize: this.sequelize,
queryInterface: this.sequelize.getQueryInterface(),
...migratorOptions.context,
};
this.migrations = new Migrations(context);
this.migrator = new Umzug({
logger: migratorOptions.logger || console,
migrations: this.migrations.callback(),
context,
storage: new SequelizeStorage({
modelName: `${this.options.tablePrefix || ''}migrations`,
...migratorOptions.storage,
sequelize: this.sequelize,
}),
});
this.sequelize.beforeDefine((model, opts) => {
if (this.options.tablePrefix) {
opts.tableName = `${this.options.tablePrefix}${opts.tableName || opts.modelName || opts.name.plural}`;
}
});
this.initListener();
}
initListener() {
this.on('afterCreate', async (instance) => {
instance?.toChangedWithAssociations?.();
});
this.on('afterUpdate', async (instance) => {
instance?.toChangedWithAssociations?.();
});
this.on('beforeDestroy', async (instance, options) => {
await referentialIntegrityCheck({
db: this,
referencedInstance: instance,
transaction: options.transaction,
});
});
}
addMigration(item: MigrationItem) {
return this.migrations.add(item);
}
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,
migration: requireModule(file),
context,
});
}
}
inDialect(...dialect: string[]) {
return dialect.includes(this.sequelize.getDialect());
}
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> {
this.emit('beforeDefineCollection', options);
feat: collection inheritance (#1069) * chore: test * chore: inherited-collection class * feat: collection inherit * feat: collection inherit * feat: inhertis sync runner * test: get parents fields * feat: collection inherit style promote * feat: sync * feat: sync alter table * feat: pgOnly Test * fix: child collection create api * feat: replace parent field * chore: reload parent fields * test: reload collection test * feat: details are displayed according to conditions * fix: typo * feat: inheritance map class * chore: is parent node * feat: display where child row created from * fix: find with appends * feat: add parent collection fields * fix: create table * feat: load fields for all children * refactor: sync fields from parent * test: has one field inhertis * feat: replace child association target * feat: should not replace child field when parent field update * test: should update inherit field when parent field update * feat: only the blocks directly inherited from the current data are displayed * fix: inherit from multiple collections * feat: only the blocks directly inherited from the current data are displayed * fix: test * feat: parent collection expend * fix: test * test: belongsToMany inherits * test: belongsToMany inherits * feat: block display * feat: collection inherite * feat: collection inherite * feat: multiple inherits * fix: sync runner * feat: collection inherite * feat: collecton inherits * feat: cannot be modified after inheritance and saving * feat: collection inherit for graph * feat: collection inherits * fix: drop inhertied field * fix: should throw error when type conflit * feat: output inherited fields * feat: bulk update collection fields * feat: collection fields * feat: collection fields * test: create relation with child table * fix: test * fix: test * fix: test * feat: style impove * test: should not replace field with difference type * feat: add text * fix: throw error when replace field with difference type * feat: overriding * feat: kan bankanban group fields * feat: calendar block fields * feat: kan bankanban group fields * fix: test * feat: relationship fields * feat: should delete child's field when parent field deleted * feat: foreign key filter * fix: build error & multiple inherit destory field * fix: test * chore: disable error * feat: no recursive update associations (#1091) * feat: update associations * fix(collection-manager): should update uiSchema * chore: flip if * feat: mutile inherits * feat: db dialect * feat: inherits show by database * chore: git hash into docker image * fix: js gzip * fix: dockerfile * chore: error message * feat: overriding * feat: overriding * feat: overriding * feat: local * feat: filter fields by interface * fix: database logging env * test: replace hasOne target * feat: add view * feat: local * chore: enable error * fix: update docs Co-authored-by: katherinehhh <katherine_15995@163.com> Co-authored-by: chenos <chenlinxh@gmail.com>
2022-11-16 12:53:58 +08:00
const collection = options.inherits
? new InheritedCollection(options, {
database: this,
})
: new Collection(options, {
database: this,
});
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 || '';
}
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 {
return this.collections.get(name);
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);
const result = this.collections.delete(name);
this.sequelize.modelManager.removeModel(collection.model);
2021-12-06 21:23:34 +08:00
if (result) {
this.emit('afterRemoveCollection', collection);
}
return collection;
2020-10-24 15:34:43 +08:00
}
getModel<M extends Model>(name: string) {
return this.getCollection(name).model as ModelCtor<M>;
}
getRepository<R extends Repository>(name: string): R;
getRepository<R extends RelationRepository>(name: string, relationId: string | number): R;
getRepository<R extends RelationRepository>(name: string, relationId?: string | number): Repository | R {
if (relationId) {
const [collection, relation] = name.split('.');
2022-03-02 18:35:49 +08:00
return this.getRepository(collection)?.relation(relation)?.of(relationId) as R;
}
2022-03-02 18:35:49 +08:00
return this.getCollection(name)?.repository;
}
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-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
}
2021-12-06 21:23:34 +08:00
registerModels(models: MapOf<ModelCtor<any>>) {
for (const [type, schemaType] of Object.entries(models)) {
this.models.set(type, schemaType);
}
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({
...extendOperators,
});
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;
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
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
}
2021-12-06 21:23:34 +08:00
const result = await this.sequelize.sync(options);
if (isMySQL) {
await this.sequelize.query('SET FOREIGN_KEY_CHECKS = 1', null);
}
feat: collection inheritance (#1069) * chore: test * chore: inherited-collection class * feat: collection inherit * feat: collection inherit * feat: inhertis sync runner * test: get parents fields * feat: collection inherit style promote * feat: sync * feat: sync alter table * feat: pgOnly Test * fix: child collection create api * feat: replace parent field * chore: reload parent fields * test: reload collection test * feat: details are displayed according to conditions * fix: typo * feat: inheritance map class * chore: is parent node * feat: display where child row created from * fix: find with appends * feat: add parent collection fields * fix: create table * feat: load fields for all children * refactor: sync fields from parent * test: has one field inhertis * feat: replace child association target * feat: should not replace child field when parent field update * test: should update inherit field when parent field update * feat: only the blocks directly inherited from the current data are displayed * fix: inherit from multiple collections * feat: only the blocks directly inherited from the current data are displayed * fix: test * feat: parent collection expend * fix: test * test: belongsToMany inherits * test: belongsToMany inherits * feat: block display * feat: collection inherite * feat: collection inherite * feat: multiple inherits * fix: sync runner * feat: collection inherite * feat: collecton inherits * feat: cannot be modified after inheritance and saving * feat: collection inherit for graph * feat: collection inherits * fix: drop inhertied field * fix: should throw error when type conflit * feat: output inherited fields * feat: bulk update collection fields * feat: collection fields * feat: collection fields * test: create relation with child table * fix: test * fix: test * fix: test * feat: style impove * test: should not replace field with difference type * feat: add text * fix: throw error when replace field with difference type * feat: overriding * feat: kan bankanban group fields * feat: calendar block fields * feat: kan bankanban group fields * fix: test * feat: relationship fields * feat: should delete child's field when parent field deleted * feat: foreign key filter * fix: build error & multiple inherit destory field * fix: test * chore: disable error * feat: no recursive update associations (#1091) * feat: update associations * fix(collection-manager): should update uiSchema * chore: flip if * feat: mutile inherits * feat: db dialect * feat: inherits show by database * chore: git hash into docker image * fix: js gzip * fix: dockerfile * chore: error message * feat: overriding * feat: overriding * feat: overriding * feat: local * feat: filter fields by interface * fix: database logging env * test: replace hasOne target * feat: add view * feat: local * chore: enable error * fix: update docs Co-authored-by: katherinehhh <katherine_15995@163.com> Co-authored-by: chenos <chenlinxh@gmail.com>
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
}
Application (#175) * feat: getRepository * getRepository return type * export action * add: acl * feat: setResourceAction * feat: action alias * chore: code struct * feat: removeResourceAction * chore: file name * ignorecase * remove ACL * feat: ACL * feat: role toJSON * using emit * chore: test * feat: plugin-acl * feat: acl with predicate * grant universal action test * grant action test * update resource action test * revoke resource action * usingActionsConfig switch * plugin-ui-schema-storage * remove global acl instance * fix: collection manager with sqlite * add own action listener * add acl middleware * add acl allowConfigure strategy option * add plugin-acl allowConfigure * change acl resourceName * add acl middleware merge params * bugfix * append fields on acl action params * acl middleware parse template * fix: collection-manager migrate * add acl association field test * feat(plugin-acl): grant association field actions * chore(plugin-acl): type name * feat(plugin-acl): regrant actions on resource action update * feat(plugin-acl): regrant action on field destroy * fix(plugin-acl): test * fix(plugin-acl): test run * feat(plugin-acl): set default role * feat(plugin-users): set user default role * test(plugin-users): create user with role * feat(plugin-users): create user with role * feat(application): application hook * feat(database): reconnect * feat(database): application life cycle * feat(database): sync with option * feat(database): hook position * feat(database): hook position * feat(database): remove load in start * fix(application): get plugin * feat(test): loadAndInstall * feat: improve code * feat: improve code * fix: listen options * fix: bug * test(database): add test case Co-authored-by: chenos <chenlinxh@gmail.com>
2022-01-30 11:11:36 +08:00
async clean(options: CleanOptions) {
const { drop, ...others } = options;
if (drop) {
await this.sequelize.getQueryInterface().dropAllTables(others);
}
}
async collectionExistsInDb(name, options?: Transactionable) {
const tables = await this.sequelize.getQueryInterface().showAllTables({
transaction: options?.transaction,
});
return !!tables.find((table) => table === `${this.getTablePrefix()}${name}`);
}
public isSqliteMemory() {
return this.sequelize.getDialect() === 'sqlite' && lodash.get(this.options, 'storage') == ':memory:';
}
async auth(options: QueryOptions & { retry?: number } = {}) {
const { retry = 10, ...others } = options;
2022-04-29 20:04:02 +08:00
const delay = (ms) => new Promise((yea) => setTimeout(yea, ms));
feat: build, cli, devtools, sdk, docs... * feat: nocobase build * chore: update build scripts * chore: update build scripts * chore(versions): 😊 publish v0.7.0-alpha.33 * chore: independent version * chore: nocobase build * chore(versions): 😊 publish v0.7.0-alpha.34 * feat: nocobase-cli * feat: nocobase-cli * chore: update dependencies * feat: improve code * refactor: create-nocobase-app * chore(versions): 😊 publish v0.7.0-alpha.35 * feat: @nocobase/devtools * chore(versions): 😊 publish v0.7.0-alpha.36 * chore: update dependencies * chore(versions): 😊 publish v0.7.0-alpha.37 * feat: improve code * chore(versions): 😊 publish v0.7.0-alpha.38 * feat: improve code * chore(versions): 😊 publish v0.7.0-alpha.39 * feat: update deps * chore(versions): 😊 publish v0.7.0-alpha.40 * chore: update devDependencies * chore(versions): 😊 publish v0.7.0-alpha.41 * fix: postinstall * chore(versions): 😊 publish v0.7.0-alpha.42 * chore: improve code * chore(versions): 😊 publish v0.7.0-alpha.43 * chore: execa * chore(versions): 😊 publish v0.7.0-alpha.44 * chore(cli): allow unknown option * chore(versions): 😊 publish v0.7.0-alpha.45 * fix: default envs * chore(versions): 😊 publish v0.7.0-alpha.45 * fix: package argument for build command * chore(versions): 😊 publish v0.7.0-alpha.46 * fix: improve code * chore(versions): 😊 publish v0.7.0-alpha.48 * feat: clean & doc * chore(versions): 😊 publish v0.7.0-alpha.49 * feat: compilation tips * feat: upgrade command * chore(versions): 😊 publish v0.7.0-alpha.50 * fix: unexpected token ] in JSON * chore(versions): 😊 publish v0.7.0-alpha.51 * fix: upgrade command * chore(versions): 😊 publish v0.7.0-alpha.52 * fix: remove export action from available action * fix: db sync after upgrade * chore(versions): 😊 publish v0.7.0-alpha.53 * feat: upgrade log * chore(versions): 😊 publish v0.7.0-alpha.54 * docs: updates * feat: updates * docs(cli): update usage description * feat: updates * docs: updates * docs: updates * docs: toc * feat: sdk * docs: updates * docs: updates * docs: updates * Update index.md * docs: updates * Update release-notes.md * Update roadmap.md * Update index.md * Update contributing.md * Update contributing.md * Update index.md * Update index.md * Update nocobase-cli.md * Update nocobase-cli.md * fix: user plugin initialization data * Update env.md * Update env.md * Update directory-structure.md * Update index.md * Update action-api.md * Update filter-operators.md * docs: update thanks.md * Update index.md * Update javascript-sdk.md * Update rest-api.md * Update installation.md * Update installation.md * Update upgrading.md * Update upgrading.md * Update upgrading.md * Update installation.md * Update installation.md * Create release-notes.md * Update release-notes.md * feat: updates * feat: update docs * feat: update release-notes.md * feat: switch language * feat: updates * Add files via upload * Add files via upload * Update important-features.md * Update thanks.md * feat: nocobase postinstall * Update index.md * Create why-different.md * Update why-different.md * Create who-is-for.md * Rename who-is-for.md to who.md * feat: update docs * Rename why-different.md to why.md * Update why.md * Update menus.ts * Update why-nocobase.md * Create who.md * Create why.md * feat: updates * chore(versions): 😊 publish v0.7.0-alpha.55 * feat: tips * Update who.md * Update who.md * feat: update docs * feat: update doc menus * fix: plugin client dist * docs: update contributing.md * docs: update readme.md * docs: update readme.md * docs: update readme.md * Update functional-zoning.md * fix: br Co-authored-by: Zhou <zhou.working@gmail.com>
2022-05-19 00:40:55 +08:00
let count = 1;
2022-04-29 20:04:02 +08:00
const authenticate = async () => {
try {
await this.sequelize.authenticate(others);
console.log('Connection has been established successfully.');
return true;
} catch (error) {
if (count >= retry) {
2022-04-29 20:04:02 +08:00
throw error;
}
feat: build, cli, devtools, sdk, docs... * feat: nocobase build * chore: update build scripts * chore: update build scripts * chore(versions): 😊 publish v0.7.0-alpha.33 * chore: independent version * chore: nocobase build * chore(versions): 😊 publish v0.7.0-alpha.34 * feat: nocobase-cli * feat: nocobase-cli * chore: update dependencies * feat: improve code * refactor: create-nocobase-app * chore(versions): 😊 publish v0.7.0-alpha.35 * feat: @nocobase/devtools * chore(versions): 😊 publish v0.7.0-alpha.36 * chore: update dependencies * chore(versions): 😊 publish v0.7.0-alpha.37 * feat: improve code * chore(versions): 😊 publish v0.7.0-alpha.38 * feat: improve code * chore(versions): 😊 publish v0.7.0-alpha.39 * feat: update deps * chore(versions): 😊 publish v0.7.0-alpha.40 * chore: update devDependencies * chore(versions): 😊 publish v0.7.0-alpha.41 * fix: postinstall * chore(versions): 😊 publish v0.7.0-alpha.42 * chore: improve code * chore(versions): 😊 publish v0.7.0-alpha.43 * chore: execa * chore(versions): 😊 publish v0.7.0-alpha.44 * chore(cli): allow unknown option * chore(versions): 😊 publish v0.7.0-alpha.45 * fix: default envs * chore(versions): 😊 publish v0.7.0-alpha.45 * fix: package argument for build command * chore(versions): 😊 publish v0.7.0-alpha.46 * fix: improve code * chore(versions): 😊 publish v0.7.0-alpha.48 * feat: clean & doc * chore(versions): 😊 publish v0.7.0-alpha.49 * feat: compilation tips * feat: upgrade command * chore(versions): 😊 publish v0.7.0-alpha.50 * fix: unexpected token ] in JSON * chore(versions): 😊 publish v0.7.0-alpha.51 * fix: upgrade command * chore(versions): 😊 publish v0.7.0-alpha.52 * fix: remove export action from available action * fix: db sync after upgrade * chore(versions): 😊 publish v0.7.0-alpha.53 * feat: upgrade log * chore(versions): 😊 publish v0.7.0-alpha.54 * docs: updates * feat: updates * docs(cli): update usage description * feat: updates * docs: updates * docs: updates * docs: toc * feat: sdk * docs: updates * docs: updates * docs: updates * Update index.md * docs: updates * Update release-notes.md * Update roadmap.md * Update index.md * Update contributing.md * Update contributing.md * Update index.md * Update index.md * Update nocobase-cli.md * Update nocobase-cli.md * fix: user plugin initialization data * Update env.md * Update env.md * Update directory-structure.md * Update index.md * Update action-api.md * Update filter-operators.md * docs: update thanks.md * Update index.md * Update javascript-sdk.md * Update rest-api.md * Update installation.md * Update installation.md * Update upgrading.md * Update upgrading.md * Update upgrading.md * Update installation.md * Update installation.md * Create release-notes.md * Update release-notes.md * feat: updates * feat: update docs * feat: update release-notes.md * feat: switch language * feat: updates * Add files via upload * Add files via upload * Update important-features.md * Update thanks.md * feat: nocobase postinstall * Update index.md * Create why-different.md * Update why-different.md * Create who-is-for.md * Rename who-is-for.md to who.md * feat: update docs * Rename why-different.md to why.md * Update why.md * Update menus.ts * Update why-nocobase.md * Create who.md * Create why.md * feat: updates * chore(versions): 😊 publish v0.7.0-alpha.55 * feat: tips * Update who.md * Update who.md * feat: update docs * feat: update doc menus * fix: plugin client dist * docs: update contributing.md * docs: update readme.md * docs: update readme.md * docs: update readme.md * Update functional-zoning.md * fix: br Co-authored-by: Zhou <zhou.working@gmail.com>
2022-05-19 00:40:55 +08:00
console.log('reconnecting...', count);
2022-04-29 20:04:02 +08:00
++count;
await delay(500);
return await authenticate();
}
};
return await authenticate();
}
Application (#175) * feat: getRepository * getRepository return type * export action * add: acl * feat: setResourceAction * feat: action alias * chore: code struct * feat: removeResourceAction * chore: file name * ignorecase * remove ACL * feat: ACL * feat: role toJSON * using emit * chore: test * feat: plugin-acl * feat: acl with predicate * grant universal action test * grant action test * update resource action test * revoke resource action * usingActionsConfig switch * plugin-ui-schema-storage * remove global acl instance * fix: collection manager with sqlite * add own action listener * add acl middleware * add acl allowConfigure strategy option * add plugin-acl allowConfigure * change acl resourceName * add acl middleware merge params * bugfix * append fields on acl action params * acl middleware parse template * fix: collection-manager migrate * add acl association field test * feat(plugin-acl): grant association field actions * chore(plugin-acl): type name * feat(plugin-acl): regrant actions on resource action update * feat(plugin-acl): regrant action on field destroy * fix(plugin-acl): test * fix(plugin-acl): test run * feat(plugin-acl): set default role * feat(plugin-users): set user default role * test(plugin-users): create user with role * feat(plugin-users): create user with role * feat(application): application hook * feat(database): reconnect * feat(database): application life cycle * feat(database): sync with option * feat(database): hook position * feat(database): hook position * feat(database): remove load in start * fix(application): get plugin * feat(test): loadAndInstall * feat: improve code * feat: improve code * fix: listen options * fix: bug * test(database): add test case Co-authored-by: chenos <chenlinxh@gmail.com>
2022-01-30 11:11:36 +08:00
async reconnect() {
if (this.isSqliteMemory()) {
return;
}
Application (#175) * feat: getRepository * getRepository return type * export action * add: acl * feat: setResourceAction * feat: action alias * chore: code struct * feat: removeResourceAction * chore: file name * ignorecase * remove ACL * feat: ACL * feat: role toJSON * using emit * chore: test * feat: plugin-acl * feat: acl with predicate * grant universal action test * grant action test * update resource action test * revoke resource action * usingActionsConfig switch * plugin-ui-schema-storage * remove global acl instance * fix: collection manager with sqlite * add own action listener * add acl middleware * add acl allowConfigure strategy option * add plugin-acl allowConfigure * change acl resourceName * add acl middleware merge params * bugfix * append fields on acl action params * acl middleware parse template * fix: collection-manager migrate * add acl association field test * feat(plugin-acl): grant association field actions * chore(plugin-acl): type name * feat(plugin-acl): regrant actions on resource action update * feat(plugin-acl): regrant action on field destroy * fix(plugin-acl): test * fix(plugin-acl): test run * feat(plugin-acl): set default role * feat(plugin-users): set user default role * test(plugin-users): create user with role * feat(plugin-users): create user with role * feat(application): application hook * feat(database): reconnect * feat(database): application life cycle * feat(database): sync with option * feat(database): hook position * feat(database): hook position * feat(database): remove load in start * fix(application): get plugin * feat(test): loadAndInstall * feat: improve code * feat: improve code * fix: listen options * fix: bug * test(database): add test case Co-authored-by: chenos <chenlinxh@gmail.com>
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() {
if (this.isSqliteMemory()) {
return;
}
return this.sequelize.close();
2020-10-24 15:34:43 +08:00
}
2020-12-15 20:16:55 +08:00
on(event: EventType, listener: any): this;
on(event: ModelValidateEventTypes, listener: SyncListener): this;
on(event: ModelValidateEventTypes, listener: ValidateListener): this;
on(event: ModelCreateEventTypes, listener: CreateListener): this;
on(event: ModelUpdateEventTypes, listener: UpdateListener): this;
on(event: ModelSaveEventTypes, listener: SaveListener): this;
on(event: ModelDestroyEventTypes, listener: DestroyListener): this;
on(event: ModelCreateWithAssociationsEventTypes, listener: CreateWithAssociationsListener): this;
on(event: ModelUpdateWithAssociationsEventTypes, listener: UpdateWithAssociationsListener): this;
on(event: ModelSaveWithAssociationsEventTypes, listener: SaveWithAssociationsListener): this;
on(event: DatabaseBeforeDefineCollectionEventType, listener: BeforeDefineCollectionListener): this;
on(event: DatabaseAfterDefineCollectionEventType, listener: AfterDefineCollectionListener): this;
on(
event: DatabaseBeforeRemoveCollectionEventType | DatabaseAfterRemoveCollectionEventType,
listener: RemoveCollectionListener,
): this;
on(event: EventType, listener: any): this {
// 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
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
}
extendCollection(collectionOptions: CollectionOptions, mergeOptions?: MergeOptions) {
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 }]);
}
}
2021-12-06 21:23:34 +08:00
async import(options: { directory: string; extensions?: ImportFileExtension[] }): Promise<Map<string, Collection>> {
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) {
this.extendCollection(module.collectionOptions, module.mergeOptions);
2021-12-06 21:23:34 +08:00
} else {
const collection = this.collection(module);
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
feat: build, cli, devtools, sdk, docs... * feat: nocobase build * chore: update build scripts * chore: update build scripts * chore(versions): 😊 publish v0.7.0-alpha.33 * chore: independent version * chore: nocobase build * chore(versions): 😊 publish v0.7.0-alpha.34 * feat: nocobase-cli * feat: nocobase-cli * chore: update dependencies * feat: improve code * refactor: create-nocobase-app * chore(versions): 😊 publish v0.7.0-alpha.35 * feat: @nocobase/devtools * chore(versions): 😊 publish v0.7.0-alpha.36 * chore: update dependencies * chore(versions): 😊 publish v0.7.0-alpha.37 * feat: improve code * chore(versions): 😊 publish v0.7.0-alpha.38 * feat: improve code * chore(versions): 😊 publish v0.7.0-alpha.39 * feat: update deps * chore(versions): 😊 publish v0.7.0-alpha.40 * chore: update devDependencies * chore(versions): 😊 publish v0.7.0-alpha.41 * fix: postinstall * chore(versions): 😊 publish v0.7.0-alpha.42 * chore: improve code * chore(versions): 😊 publish v0.7.0-alpha.43 * chore: execa * chore(versions): 😊 publish v0.7.0-alpha.44 * chore(cli): allow unknown option * chore(versions): 😊 publish v0.7.0-alpha.45 * fix: default envs * chore(versions): 😊 publish v0.7.0-alpha.45 * fix: package argument for build command * chore(versions): 😊 publish v0.7.0-alpha.46 * fix: improve code * chore(versions): 😊 publish v0.7.0-alpha.48 * feat: clean & doc * chore(versions): 😊 publish v0.7.0-alpha.49 * feat: compilation tips * feat: upgrade command * chore(versions): 😊 publish v0.7.0-alpha.50 * fix: unexpected token ] in JSON * chore(versions): 😊 publish v0.7.0-alpha.51 * fix: upgrade command * chore(versions): 😊 publish v0.7.0-alpha.52 * fix: remove export action from available action * fix: db sync after upgrade * chore(versions): 😊 publish v0.7.0-alpha.53 * feat: upgrade log * chore(versions): 😊 publish v0.7.0-alpha.54 * docs: updates * feat: updates * docs(cli): update usage description * feat: updates * docs: updates * docs: updates * docs: toc * feat: sdk * docs: updates * docs: updates * docs: updates * Update index.md * docs: updates * Update release-notes.md * Update roadmap.md * Update index.md * Update contributing.md * Update contributing.md * Update index.md * Update index.md * Update nocobase-cli.md * Update nocobase-cli.md * fix: user plugin initialization data * Update env.md * Update env.md * Update directory-structure.md * Update index.md * Update action-api.md * Update filter-operators.md * docs: update thanks.md * Update index.md * Update javascript-sdk.md * Update rest-api.md * Update installation.md * Update installation.md * Update upgrading.md * Update upgrading.md * Update upgrading.md * Update installation.md * Update installation.md * Create release-notes.md * Update release-notes.md * feat: updates * feat: update docs * feat: update release-notes.md * feat: switch language * feat: updates * Add files via upload * Add files via upload * Update important-features.md * Update thanks.md * feat: nocobase postinstall * Update index.md * Create why-different.md * Update why-different.md * Create who-is-for.md * Rename who-is-for.md to who.md * feat: update docs * Rename why-different.md to why.md * Update why.md * Update menus.ts * Update why-nocobase.md * Create who.md * Create why.md * feat: updates * chore(versions): 😊 publish v0.7.0-alpha.55 * feat: tips * Update who.md * Update who.md * feat: update docs * feat: update doc menus * fix: plugin client dist * docs: update contributing.md * docs: update readme.md * docs: update readme.md * docs: update readme.md * Update functional-zoning.md * fix: br Co-authored-by: Zhou <zhou.working@gmail.com>
2022-05-19 00:40:55 +08:00
declare emitAsync: (event: string | symbol, ...args: any[]) => Promise<boolean>;
2021-12-06 21:23:34 +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
export const extend = extendCollection;
export const defineCollection = (collectionOptions: CollectionOptions) => {
return collectionOptions;
};
2021-12-06 21:23:34 +08:00
applyMixins(Database, [AsyncEmitter]);
export default Database;