feat: add examples

This commit is contained in:
chenos 2022-08-08 14:42:48 +08:00
parent ba0e61873e
commit b848b9cd67
5 changed files with 108 additions and 4 deletions

62
examples/app/multi-app.ts Normal file
View File

@ -0,0 +1,62 @@
import { Application } from '@nocobase/server';
import { IncomingMessage } from 'http';
const app = new Application({
database: {
logging: process.env.DB_LOGGING === 'on' ? console.log : false,
dialect: process.env.DB_DIALECT as any,
storage: process.env.DB_STORAGE,
username: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
host: process.env.DB_HOST,
port: process.env.DB_PORT as any,
timezone: process.env.DB_TIMEZONE,
tablePrefix: process.env.DB_TABLE_PREFIX,
},
resourcer: {
prefix: '/api',
},
plugins: [],
});
if (require.main === module) {
app.runAsCLI();
}
const subApp1 = app.appManager.createApplication('sub1', {
database: app.db,
resourcer: {
prefix: '/sub1/api/',
},
});
app.resourcer.define({
name: 'test',
actions: {
async list(ctx) {
ctx.body = 'test list';
},
},
});
subApp1.resourcer.define({
name: 'test',
actions: {
async list(ctx) {
ctx.body = 'sub1 test list';
},
},
});
app.appManager.setAppSelector((req: IncomingMessage) => {
if (req.url.startsWith('/sub1/api')) {
return subApp1;
}
return null;
});
export default app;
// http://localhost:13000/api/test:list
// http://localhost:13000/sub1/api/test:list

View File

@ -0,0 +1,37 @@
import { Application } from '@nocobase/server';
const app = new Application({
database: {
logging: process.env.DB_LOGGING === 'on' ? console.log : false,
dialect: process.env.DB_DIALECT as any,
storage: process.env.DB_STORAGE,
username: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
host: process.env.DB_HOST,
port: process.env.DB_PORT as any,
timezone: process.env.DB_TIMEZONE,
tablePrefix: process.env.DB_TABLE_PREFIX,
},
resourcer: {
prefix: '/api',
},
plugins: [],
});
app.resource({
name: 'test',
actions: {
async list(ctx) {
ctx.body = 'test list';
},
},
});
if (require.main === module) {
app.runAsCLI();
}
export default app;
// http://localhost:13000/api/test:list

View File

@ -23,7 +23,8 @@
"clean": "nocobase clean", "clean": "nocobase clean",
"version:alpha": "lerna version prerelease --preid alpha --force-publish=* --no-git-tag-version -m \"chore(versions): publish packages %s\"", "version:alpha": "lerna version prerelease --preid alpha --force-publish=* --no-git-tag-version -m \"chore(versions): publish packages %s\"",
"release:force": "lerna publish from-package --yes", "release:force": "lerna publish from-package --yes",
"release": "lerna publish" "release": "lerna publish",
"run:example": "ts-node-dev -r dotenv/config"
}, },
"resolutions": { "resolutions": {
"@types/react": "^17.0.0", "@types/react": "^17.0.0",

View File

@ -4,7 +4,7 @@ import config from './config';
const app = new Application(config); const app = new Application(config);
if (require.main === module) { if (require.main === module) {
app.parse(); app.runAsCLI();
} }
export default app; export default app;

View File

@ -3,7 +3,7 @@ import { registerActions } from '@nocobase/actions';
import Database, { Collection, CollectionOptions, IDatabaseOptions } from '@nocobase/database'; import Database, { Collection, CollectionOptions, IDatabaseOptions } from '@nocobase/database';
import Resourcer, { ResourceOptions } from '@nocobase/resourcer'; import Resourcer, { ResourceOptions } from '@nocobase/resourcer';
import { applyMixins, AsyncEmitter } from '@nocobase/utils'; import { applyMixins, AsyncEmitter } from '@nocobase/utils';
import { Command, CommandOptions } from 'commander'; import { Command, CommandOptions, ParseOptions } from 'commander';
import { Server } from 'http'; import { Server } from 'http';
import { i18n, InitOptions } from 'i18next'; import { i18n, InitOptions } from 'i18next';
import Koa from 'koa'; import Koa from 'koa';
@ -247,8 +247,12 @@ export class Application<StateT = DefaultState, ContextT = DefaultContext> exten
} }
async parse(argv = process.argv) { async parse(argv = process.argv) {
return this.runAsCLI(argv);
}
async runAsCLI(argv?: readonly string[], options?: ParseOptions) {
await this.load(); await this.load();
return this.cli.parseAsync(argv); return this.cli.parseAsync(argv, options);
} }
async start(options: StartOptions = {}) { async start(options: StartOptions = {}) {