@nocobase/server 0.9.0-alpha.2 → 0.9.1-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,7 +4,7 @@ import http, { IncomingMessage, ServerResponse } from 'http';
4
4
  import Application, { ApplicationOptions } from './application';
5
5
  declare type AppSelector = (req: IncomingMessage) => Application | string | undefined | null;
6
6
  export declare class AppManager extends EventEmitter {
7
- private app;
7
+ app: Application;
8
8
  applications: Map<string, Application>;
9
9
  constructor(app: Application);
10
10
  appSelector: AppSelector;
@@ -12,7 +12,7 @@ export declare class AppManager extends EventEmitter {
12
12
  removeApplication(name: string): Promise<void>;
13
13
  setAppSelector(selector: AppSelector): void;
14
14
  listen(...args: any[]): http.Server;
15
- getApplication(appName: string): Promise<null | Application>;
15
+ getApplication(appName: string, options?: {}): Promise<null | Application>;
16
16
  callback(): (req: IncomingMessage, res: ServerResponse) => Promise<void>;
17
17
  emitAsync: (event: string | symbol, ...args: any[]) => Promise<boolean>;
18
18
  }
@@ -107,13 +107,14 @@ class AppManager extends _events().default {
107
107
  return server.listen(...args);
108
108
  }
109
109
 
110
- getApplication(appName) {
110
+ getApplication(appName, options = {}) {
111
111
  var _this3 = this;
112
112
 
113
113
  return _asyncToGenerator(function* () {
114
114
  yield _this3.emitAsync('beforeGetApplication', {
115
115
  appManager: _this3,
116
- name: appName
116
+ name: appName,
117
+ options
117
118
  });
118
119
  return _this3.applications.get(appName);
119
120
  })();
@@ -124,17 +125,18 @@ class AppManager extends _events().default {
124
125
 
125
126
  return /*#__PURE__*/function () {
126
127
  var _ref3 = _asyncToGenerator(function* (req, res) {
127
- let handleApp = _this4.appSelector(req) || _this4.app;
128
+ const appManager = _this4.app.appManager;
129
+ let handleApp = appManager.appSelector(req) || appManager.app;
128
130
 
129
131
  if (typeof handleApp === 'string') {
130
- handleApp = yield _this4.getApplication(handleApp);
132
+ handleApp = yield appManager.getApplication(handleApp);
131
133
 
132
134
  if (!handleApp) {
133
135
  res.statusCode = 404;
134
136
  return res.end(JSON.stringify({
135
137
  redirectTo: process.env.APP_NOT_FOUND_REDIRECT_TO,
136
138
  errors: [{
137
- message: 'Not Found'
139
+ message: 'Application Not Found'
138
140
  }]
139
141
  }));
140
142
  }
@@ -30,6 +30,7 @@ export interface ApplicationOptions {
30
30
  acl?: boolean;
31
31
  logger?: AppLoggerOptions;
32
32
  pmSock?: string;
33
+ name?: string;
33
34
  }
34
35
  export interface DefaultState extends KoaDefaultState {
35
36
  currentUser?: any;
@@ -88,6 +89,7 @@ export declare class Application<StateT = DefaultState, ContextT = DefaultContex
88
89
  protected plugins: Map<string, Plugin<any>>;
89
90
  listenServer: Server;
90
91
  middleware: any;
92
+ stopped: boolean;
91
93
  constructor(options: ApplicationOptions);
92
94
  get db(): Database;
93
95
  get cache(): Cache;
@@ -100,6 +102,7 @@ export declare class Application<StateT = DefaultState, ContextT = DefaultContex
100
102
  get appManager(): AppManager;
101
103
  get logger(): Logger;
102
104
  get log(): Logger;
105
+ get name(): string;
103
106
  protected init(): void;
104
107
  private createDatabase;
105
108
  getVersion(): any;
@@ -127,5 +130,8 @@ export declare class Application<StateT = DefaultState, ContextT = DefaultContex
127
130
  install(options?: InstallOptions): Promise<void>;
128
131
  upgrade(options?: any): Promise<void>;
129
132
  emitAsync: (event: string | symbol, ...args: any[]) => Promise<boolean>;
133
+ toJSON(): {
134
+ appName: string;
135
+ };
130
136
  }
131
137
  export default Application;
@@ -148,6 +148,8 @@ class ApplicationVersion {
148
148
  if (!app.db.hasCollection('applicationVersion')) {
149
149
  app.db.collection({
150
150
  name: 'applicationVersion',
151
+ namespace: 'core',
152
+ duplicator: 'required',
151
153
  timestamps: false,
152
154
  fields: [{
153
155
  name: 'value',
@@ -232,6 +234,7 @@ class Application extends _koa().default {
232
234
  this._version = void 0;
233
235
  this.plugins = new Map();
234
236
  this.listenServer = void 0;
237
+ this.stopped = false;
235
238
  this.options = options;
236
239
  this.init();
237
240
  }
@@ -280,6 +283,10 @@ class Application extends _koa().default {
280
283
  return this._logger;
281
284
  }
282
285
 
286
+ get name() {
287
+ return this.options.name || 'main';
288
+ }
289
+
283
290
  init() {
284
291
  const options = this.options;
285
292
  const logger = (0, _logger().createAppLogger)(options.logger);
@@ -340,13 +347,15 @@ class Application extends _koa().default {
340
347
  }
341
348
 
342
349
  createDatabase(options) {
343
- return new (_database().default)(_objectSpread(_objectSpread({}, options.database instanceof _database().default ? options.database.options : options.database), {}, {
350
+ const db = new (_database().default)(_objectSpread(_objectSpread({}, options.database instanceof _database().default ? options.database.options : options.database), {}, {
344
351
  migrator: {
345
352
  context: {
346
353
  app: this
347
354
  }
348
355
  }
349
356
  }));
357
+ db.setLogger(this._logger);
358
+ return db;
350
359
  }
351
360
 
352
361
  getVersion() {
@@ -453,6 +462,7 @@ class Application extends _koa().default {
453
462
  yield _this7.dbVersionCheck({
454
463
  exit: true
455
464
  });
465
+ yield _this7.db.prepare();
456
466
 
457
467
  if ((argv === null || argv === void 0 ? void 0 : argv[2]) !== 'upgrade') {
458
468
  yield _this7.load({
@@ -508,6 +518,7 @@ class Application extends _koa().default {
508
518
  }
509
519
 
510
520
  yield _this8.emitAsync('afterStart', _this8, options);
521
+ _this8.stopped = false;
511
522
  })();
512
523
  }
513
524
 
@@ -519,6 +530,12 @@ class Application extends _koa().default {
519
530
  var _this9 = this;
520
531
 
521
532
  return _asyncToGenerator(function* () {
533
+ if (_this9.stopped) {
534
+ _this9.log.warn(`Application ${_this9.name} already stopped`);
535
+
536
+ return;
537
+ }
538
+
522
539
  yield _this9.emitAsync('beforeStop', _this9, options);
523
540
 
524
541
  try {
@@ -538,6 +555,7 @@ class Application extends _koa().default {
538
555
  }
539
556
 
540
557
  yield _this9.emitAsync('afterStop', _this9, options);
558
+ _this9.stopped = true;
541
559
  })();
542
560
  }
543
561
 
@@ -576,8 +594,8 @@ class Application extends _koa().default {
576
594
  plain: true
577
595
  });
578
596
 
579
- if ((result === null || result === void 0 ? void 0 : result.Value) === '1') {
580
- console.log(_chalk().default.red(`mysql variable 'lower_case_table_names' must be set to '0' or '2'`));
597
+ if ((result === null || result === void 0 ? void 0 : result.Value) === '1' && !_this11.db.options.underscored) {
598
+ console.log(`Your database lower_case_table_names=1, please add ${_chalk().default.yellow('DB_UNDERSCORED=true')} to the .env file`);
581
599
 
582
600
  if (options === null || options === void 0 ? void 0 : options.exit) {
583
601
  process.exit();
@@ -643,6 +661,12 @@ class Application extends _koa().default {
643
661
  })();
644
662
  }
645
663
 
664
+ toJSON() {
665
+ return {
666
+ appName: this.name
667
+ };
668
+ }
669
+
646
670
  }
647
671
 
648
672
  exports.Application = Application;
package/lib/helper.js CHANGED
@@ -81,7 +81,9 @@ function createI18n(options) {
81
81
 
82
82
  instance.init(_objectSpread({
83
83
  lng: 'en-US',
84
- resources: {}
84
+ resources: {},
85
+ keySeparator: false,
86
+ nsSeparator: false
85
87
  }, options.i18n));
86
88
  return instance;
87
89
  }
@@ -32,12 +32,13 @@ export declare class PluginManager {
32
32
  clone(): PluginManager;
33
33
  addStatic(plugin?: any, options?: any): any;
34
34
  generateClientFile(plugin: string, packageName: string): Promise<void>;
35
- add(plugin: any, options?: any, transaction?: any): any;
35
+ add(plugin: any, options?: any, transaction?: any): Promise<any>;
36
36
  load(options?: any): Promise<void>;
37
37
  install(options?: InstallOptions): Promise<void>;
38
38
  enable(name: string | string[]): Promise<void>;
39
39
  disable(name: string | string[]): Promise<void>;
40
40
  remove(name: string | string[]): Promise<void>;
41
+ static getPackageJson(packageName: string): any;
41
42
  static getPackageName(name: string): string;
42
43
  static getPluginPkgPrefix(): string[];
43
44
  static findPackage(name: string): Promise<string>;
@@ -371,7 +371,22 @@ class PluginManager {
371
371
  const t = transaction || (yield _this4.app.db.sequelize.transaction());
372
372
 
373
373
  try {
374
- const items = yield Promise.all(plugin.map(p => _this4.add(p, options, t)));
374
+ const items = [];
375
+
376
+ var _iterator3 = _createForOfIteratorHelper(plugin),
377
+ _step3;
378
+
379
+ try {
380
+ for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
381
+ const p = _step3.value;
382
+ items.push(yield _this4.add(p, options, t));
383
+ }
384
+ } catch (err) {
385
+ _iterator3.e(err);
386
+ } finally {
387
+ _iterator3.f();
388
+ }
389
+
375
390
  yield t.commit();
376
391
  return items;
377
392
  } catch (error) {
@@ -381,9 +396,6 @@ class PluginManager {
381
396
  }
382
397
 
383
398
  const packageName = yield PluginManager.findPackage(plugin);
384
-
385
- const packageJson = require(`${packageName}/package.json`);
386
-
387
399
  yield _this4.generateClientFile(plugin, packageName);
388
400
 
389
401
  const instance = _this4.addStatic(plugin, _objectSpread(_objectSpread({}, options), {}, {
@@ -396,6 +408,7 @@ class PluginManager {
396
408
  name: plugin
397
409
  }
398
410
  });
411
+ const packageJson = PluginManager.getPackageJson(packageName);
399
412
 
400
413
  if (!model) {
401
414
  const enabled = options.enabled,
@@ -403,7 +416,7 @@ class PluginManager {
403
416
  installed = options.installed,
404
417
  others = _objectWithoutProperties(options, _excluded);
405
418
 
406
- model = yield _this4.repository.create({
419
+ yield _this4.repository.create({
407
420
  transaction,
408
421
  values: {
409
422
  name: plugin,
@@ -424,14 +437,14 @@ class PluginManager {
424
437
  var _this5 = this;
425
438
 
426
439
  return _asyncToGenerator(function* () {
427
- var _iterator3 = _createForOfIteratorHelper(_this5.plugins),
428
- _step3;
440
+ var _iterator4 = _createForOfIteratorHelper(_this5.plugins),
441
+ _step4;
429
442
 
430
443
  try {
431
- for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
432
- const _step3$value = _slicedToArray(_step3.value, 2),
433
- name = _step3$value[0],
434
- plugin = _step3$value[1];
444
+ for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
445
+ const _step4$value = _slicedToArray(_step4.value, 2),
446
+ name = _step4$value[0],
447
+ plugin = _step4$value[1];
435
448
 
436
449
  if (!plugin.enabled) {
437
450
  continue;
@@ -440,19 +453,19 @@ class PluginManager {
440
453
  yield plugin.beforeLoad();
441
454
  }
442
455
  } catch (err) {
443
- _iterator3.e(err);
456
+ _iterator4.e(err);
444
457
  } finally {
445
- _iterator3.f();
458
+ _iterator4.f();
446
459
  }
447
460
 
448
- var _iterator4 = _createForOfIteratorHelper(_this5.plugins),
449
- _step4;
461
+ var _iterator5 = _createForOfIteratorHelper(_this5.plugins),
462
+ _step5;
450
463
 
451
464
  try {
452
- for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
453
- const _step4$value = _slicedToArray(_step4.value, 2),
454
- name = _step4$value[0],
455
- plugin = _step4$value[1];
465
+ for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) {
466
+ const _step5$value = _slicedToArray(_step5.value, 2),
467
+ name = _step5$value[0],
468
+ plugin = _step5$value[1];
456
469
 
457
470
  if (!plugin.enabled) {
458
471
  continue;
@@ -463,9 +476,9 @@ class PluginManager {
463
476
  yield _this5.app.emitAsync('afterLoadPlugin', plugin, options);
464
477
  }
465
478
  } catch (err) {
466
- _iterator4.e(err);
479
+ _iterator5.e(err);
467
480
  } finally {
468
- _iterator4.f();
481
+ _iterator5.f();
469
482
  }
470
483
  })();
471
484
  }
@@ -474,14 +487,14 @@ class PluginManager {
474
487
  var _this6 = this;
475
488
 
476
489
  return _asyncToGenerator(function* () {
477
- var _iterator5 = _createForOfIteratorHelper(_this6.plugins),
478
- _step5;
490
+ var _iterator6 = _createForOfIteratorHelper(_this6.plugins),
491
+ _step6;
479
492
 
480
493
  try {
481
- for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) {
482
- const _step5$value = _slicedToArray(_step5.value, 2),
483
- name = _step5$value[0],
484
- plugin = _step5$value[1];
494
+ for (_iterator6.s(); !(_step6 = _iterator6.n()).done;) {
495
+ const _step6$value = _slicedToArray(_step6.value, 2),
496
+ name = _step6$value[0],
497
+ plugin = _step6$value[1];
485
498
 
486
499
  if (!plugin.enabled) {
487
500
  continue;
@@ -492,9 +505,9 @@ class PluginManager {
492
505
  yield _this6.app.emitAsync('afterInstallPlugin', plugin, options);
493
506
  }
494
507
  } catch (err) {
495
- _iterator5.e(err);
508
+ _iterator6.e(err);
496
509
  } finally {
497
- _iterator5.f();
510
+ _iterator6.f();
498
511
  }
499
512
  })();
500
513
  }
@@ -508,12 +521,12 @@ class PluginManager {
508
521
  yield _this7.app.reload();
509
522
  yield _this7.app.db.sync();
510
523
 
511
- var _iterator6 = _createForOfIteratorHelper(pluginNames),
512
- _step6;
524
+ var _iterator7 = _createForOfIteratorHelper(pluginNames),
525
+ _step7;
513
526
 
514
527
  try {
515
- for (_iterator6.s(); !(_step6 = _iterator6.n()).done;) {
516
- const pluginName = _step6.value;
528
+ for (_iterator7.s(); !(_step7 = _iterator7.n()).done;) {
529
+ const pluginName = _step7.value;
517
530
 
518
531
  const plugin = _this7.app.getPlugin(pluginName);
519
532
 
@@ -525,9 +538,9 @@ class PluginManager {
525
538
  yield plugin.afterEnable();
526
539
  }
527
540
  } catch (err) {
528
- _iterator6.e(err);
541
+ _iterator7.e(err);
529
542
  } finally {
530
- _iterator6.f();
543
+ _iterator7.f();
531
544
  }
532
545
  } catch (error) {
533
546
  throw error;
@@ -543,12 +556,12 @@ class PluginManager {
543
556
  const pluginNames = yield _this8.repository.disable(name);
544
557
  yield _this8.app.reload();
545
558
 
546
- var _iterator7 = _createForOfIteratorHelper(pluginNames),
547
- _step7;
559
+ var _iterator8 = _createForOfIteratorHelper(pluginNames),
560
+ _step8;
548
561
 
549
562
  try {
550
- for (_iterator7.s(); !(_step7 = _iterator7.n()).done;) {
551
- const pluginName = _step7.value;
563
+ for (_iterator8.s(); !(_step8 = _iterator8.n()).done;) {
564
+ const pluginName = _step8.value;
552
565
 
553
566
  const plugin = _this8.app.getPlugin(pluginName);
554
567
 
@@ -559,9 +572,9 @@ class PluginManager {
559
572
  yield plugin.afterDisable();
560
573
  }
561
574
  } catch (err) {
562
- _iterator7.e(err);
575
+ _iterator8.e(err);
563
576
  } finally {
564
- _iterator7.f();
577
+ _iterator8.f();
565
578
  }
566
579
  } catch (error) {
567
580
  throw error;
@@ -575,12 +588,12 @@ class PluginManager {
575
588
  return _asyncToGenerator(function* () {
576
589
  const pluginNames = typeof name === 'string' ? [name] : name;
577
590
 
578
- var _iterator8 = _createForOfIteratorHelper(pluginNames),
579
- _step8;
591
+ var _iterator9 = _createForOfIteratorHelper(pluginNames),
592
+ _step9;
580
593
 
581
594
  try {
582
- for (_iterator8.s(); !(_step8 = _iterator8.n()).done;) {
583
- const pluginName = _step8.value;
595
+ for (_iterator9.s(); !(_step9 = _iterator9.n()).done;) {
596
+ const pluginName = _step9.value;
584
597
 
585
598
  const plugin = _this9.app.getPlugin(pluginName);
586
599
 
@@ -591,9 +604,9 @@ class PluginManager {
591
604
  yield plugin.remove();
592
605
  }
593
606
  } catch (err) {
594
- _iterator8.e(err);
607
+ _iterator9.e(err);
595
608
  } finally {
596
- _iterator8.f();
609
+ _iterator9.f();
597
610
  }
598
611
 
599
612
  yield _this9.repository.remove(name);
@@ -602,15 +615,19 @@ class PluginManager {
602
615
  })();
603
616
  }
604
617
 
618
+ static getPackageJson(packageName) {
619
+ return require(`${packageName}/package.json`);
620
+ }
621
+
605
622
  static getPackageName(name) {
606
623
  const prefixes = this.getPluginPkgPrefix();
607
624
 
608
- var _iterator9 = _createForOfIteratorHelper(prefixes),
609
- _step9;
625
+ var _iterator10 = _createForOfIteratorHelper(prefixes),
626
+ _step10;
610
627
 
611
628
  try {
612
- for (_iterator9.s(); !(_step9 = _iterator9.n()).done;) {
613
- const prefix = _step9.value;
629
+ for (_iterator10.s(); !(_step10 = _iterator10.n()).done;) {
630
+ const prefix = _step10.value;
614
631
 
615
632
  try {
616
633
  require.resolve(`${prefix}${name}`);
@@ -621,9 +638,9 @@ class PluginManager {
621
638
  }
622
639
  }
623
640
  } catch (err) {
624
- _iterator9.e(err);
641
+ _iterator10.e(err);
625
642
  } finally {
626
- _iterator9.f();
643
+ _iterator10.f();
627
644
  }
628
645
 
629
646
  throw new Error(`${name} plugin does not exist`);
@@ -646,12 +663,12 @@ class PluginManager {
646
663
 
647
664
  const prefixes = _this10.getPluginPkgPrefix();
648
665
 
649
- var _iterator10 = _createForOfIteratorHelper(prefixes),
650
- _step10;
666
+ var _iterator11 = _createForOfIteratorHelper(prefixes),
667
+ _step11;
651
668
 
652
669
  try {
653
- for (_iterator10.s(); !(_step10 = _iterator10.n()).done;) {
654
- const prefix = _step10.value;
670
+ for (_iterator11.s(); !(_step11 = _iterator11.n()).done;) {
671
+ const prefix = _step11.value;
655
672
 
656
673
  try {
657
674
  const packageName = `${prefix}${name}`;
@@ -666,9 +683,9 @@ class PluginManager {
666
683
  }
667
684
  }
668
685
  } catch (err) {
669
- _iterator10.e(err);
686
+ _iterator11.e(err);
670
687
  } finally {
671
- _iterator10.f();
688
+ _iterator11.f();
672
689
  }
673
690
  }
674
691
 
@@ -93,7 +93,10 @@ class PluginManagerRepository extends _database().Repository {
93
93
  var _this4 = this;
94
94
 
95
95
  return _asyncToGenerator(function* () {
96
- const items = yield _this4.find();
96
+ // sort plugins by id
97
+ const items = yield _this4.find({
98
+ sort: 'id'
99
+ });
97
100
 
98
101
  var _iterator = _createForOfIteratorHelper(items),
99
102
  _step;
@@ -1,5 +1,7 @@
1
1
  declare const _default: {
2
2
  name: string;
3
+ namespace: string;
4
+ duplicator: string;
3
5
  repository: string;
4
6
  fields: ({
5
7
  type: string;
@@ -6,6 +6,8 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.default = void 0;
7
7
  var _default = {
8
8
  name: 'applicationPlugins',
9
+ namespace: 'core',
10
+ duplicator: 'required',
9
11
  repository: 'PluginManagerRepository',
10
12
  fields: [{
11
13
  type: 'string',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nocobase/server",
3
- "version": "0.9.0-alpha.2",
3
+ "version": "0.9.1-alpha.2",
4
4
  "main": "lib/index.js",
5
5
  "types": "./lib/index.d.ts",
6
6
  "license": "Apache-2.0",
@@ -8,11 +8,11 @@
8
8
  "@hapi/topo": "^6.0.0",
9
9
  "@koa/cors": "^3.1.0",
10
10
  "@koa/router": "^9.4.0",
11
- "@nocobase/acl": "0.9.0-alpha.2",
12
- "@nocobase/actions": "0.9.0-alpha.2",
13
- "@nocobase/database": "0.9.0-alpha.2",
14
- "@nocobase/logger": "0.9.0-alpha.2",
15
- "@nocobase/resourcer": "0.9.0-alpha.2",
11
+ "@nocobase/acl": "0.9.1-alpha.2",
12
+ "@nocobase/actions": "0.9.1-alpha.2",
13
+ "@nocobase/database": "0.9.1-alpha.2",
14
+ "@nocobase/logger": "0.9.1-alpha.2",
15
+ "@nocobase/resourcer": "0.9.1-alpha.2",
16
16
  "chalk": "^4.1.1",
17
17
  "commander": "^9.2.0",
18
18
  "find-package-json": "^1.2.0",
@@ -27,5 +27,5 @@
27
27
  "devDependencies": {
28
28
  "@types/semver": "^7.3.9"
29
29
  },
30
- "gitHead": "b8f76ad38e60e677c5bb4aab0a4cdb28d98a0f49"
30
+ "gitHead": "d588a68eca4feed4642a4cb317301011266fe5c9"
31
31
  }