feat: add examples

This commit is contained in:
chenos 2021-09-16 00:37:43 +08:00
parent 7a1e5807a9
commit 09c42abdcc
9 changed files with 343 additions and 0 deletions

View File

@ -0,0 +1,77 @@
import { Application } from '@nocobase/server/src';
import path from 'path';
const keys = __dirname.split(path.sep);
const slug = keys[keys.length - 2];
const options = {
database: {
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,
dialect: process.env.DB_DIALECT as any,
dialectOptions: {
charset: 'utf8mb4',
collate: 'utf8mb4_unicode_ci',
},
hooks: {
beforeDefine(model, options) {
options.tableName = `examples_${slug}_${options.tableName || options.name.plural}`;
},
},
},
resourcer: {
prefix: `/api/examples/${slug}`,
},
};
console.log(options);
const app = new Application(options);
// 用户
app.collection({
name: 'users',
fields: [
{ type: 'string', name: 'username', unique: true },
{ type: 'password', name: 'password', unique: true },
{ type: 'hasMany', name: 'posts', foreignKey: 'author_id' },
],
});
// 文章
app.collection({
name: 'posts',
fields: [
{ type: 'string', name: 'title' },
{ type: 'text', name: 'content' },
{ type: 'belongsToMany', name: 'tags' },
{ type: 'hasMany', name: 'comments' },
{ type: 'belongsTo', name: 'author', target: 'users' },
],
});
// 标签
app.collection({
name: 'tags',
fields: [
{ type: 'string', name: 'name' },
{ type: 'belongsToMany', name: 'posts' },
],
});
// 评论
app.collection({
name: 'comments',
fields: [
{ type: 'text', name: 'content' },
{ type: 'belongsTo', name: 'user' },
],
});
app.parse(process.argv);
// yarn examples collections db sync
// yarn examples collections start

4
examples/index.ts Normal file
View File

@ -0,0 +1,4 @@
const example = process.argv[2];
process.argv.splice(2, 1);
console.log(process.argv);
require(`./${example}/src`);

View File

View File

View File

View File

View File

@ -0,0 +1,65 @@
import { Application } from '@nocobase/server/src';
import path from 'path';
const keys = __dirname.split(path.sep);
const slug = keys[keys.length - 2];
const options = {
database: {
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,
dialect: process.env.DB_DIALECT as any,
dialectOptions: {
charset: 'utf8mb4',
collate: 'utf8mb4_unicode_ci',
},
hooks: {
beforeDefine(model, options) {
options.tableName = `examples_${slug}_${options.tableName || options.name.plural}`;
},
},
},
resourcer: {
prefix: `/api/examples/${slug}`,
},
};
console.log(options);
const app = new Application(options);
app.resource({
name: 'server',
actions: {
async getTime(ctx, next) {
ctx.body = new Date();
await next();
}
},
});
app.parse(process.argv);
/*
yarn examples simple db sync
yarn examples simple start
curl --location --request POST 'http://localhost:3000/api/examples/simple/users' \
--header 'Content-Type: application/json' \
--data-raw '{
"username": "abc",
"password": "123456"
}'
curl --location --request GET 'http://localhost:5051/api/examples/simple/users'
*/

134
examples/saas/src/index.ts Normal file
View File

@ -0,0 +1,134 @@
import { Application } from '@nocobase/server/src';
import path from 'path';
import mount from 'koa-mount';
import compose from 'koa-compose';
const keys = __dirname.split(path.sep);
const slug = keys[keys.length - 2];
const apps = new Map<string, Application>();
function createApp(opts) {
const { name, prefix } = opts;
const options = {
database: {
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,
dialect: process.env.DB_DIALECT as any,
dialectOptions: {
charset: 'utf8mb4',
collate: 'utf8mb4_unicode_ci',
},
hooks: {
beforeDefine(model, options) {
options.tableName = `examples_${slug}_${name}_${options.tableName || options.name.plural}`;
},
},
},
resourcer: {
prefix,
},
};
console.log(options);
const app = new Application(options);
if (name) {
apps.set(name, app);
}
app.resource({
name: 'server',
actions: {
async getInfo(ctx, next) {
ctx.body = name;
await next();
}
},
});
app.collection({
name: 'users',
fields: [
{ type: 'string', name: 'username' },
{ type: 'password', name: 'password' },
],
});
return app;
}
const app = createApp({
name: 'main',
prefix: `/api/examples/${slug}/main`
});
app.collection({
name: 'applications',
fields: [
{ type: 'string', name: 'name', unique: true },
],
});
app.command('app-create').argument('<appName>').action(async (appName) => {
const App = app.db.getModel('applications');
const server = await App.create({
name: appName,
});
const api = createApp({
name: appName,
prefix: `/api/examples/${slug}/${appName}`,
});
await api.db.sync();
await api.destroy();
console.log(server.toJSON());
await app.destroy();
});
app.command('dbsync')
.option('-f, --force')
.option('--app [app]')
.action(async (...args) => {
const cli = args.pop();
const force = cli.opts()?.force;
const appName = cli.opts()?.app;
console.log('ac ac', cli.opts());
const api = apps.get(appName) || app;
await api.load();
await api.db.sync(
force
? {
force: true,
alter: {
drop: true,
},
}
: {},
);
await api.destroy();
});
app.use(async function(ctx, next) {
const appName = ctx.path.split('/')[4];
if (appName === 'main') {
return next();
}
const App = ctx.db.getModel('applications');
const model = await App.findOne({
where: { name: appName },
});
console.log({ appName, model })
if (!model) {
return next();
}
if (!apps.has(appName)) {
const app1 = createApp({
name: appName,
prefix: `/api/examples/${slug}/${appName}`
});
apps.set(appName, app1);
}
const server = apps.get(appName);
await compose(server.middleware)(ctx, next);
});
app.parse(process.argv);

View File

@ -0,0 +1,63 @@
import { Application } from '@nocobase/server/src';
import path from 'path';
const keys = __dirname.split(path.sep);
const slug = keys[keys.length - 2];
const options = {
database: {
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,
dialect: process.env.DB_DIALECT as any,
dialectOptions: {
charset: 'utf8mb4',
collate: 'utf8mb4_unicode_ci',
},
hooks: {
beforeDefine(model, options) {
options.tableName = `examples_${slug}_${options.tableName || options.name.plural}`;
},
},
},
resourcer: {
prefix: `/api/examples/${slug}`,
},
};
console.log(options);
const app = new Application(options);
app.collection({
name: 'users',
fields: [
{ type: 'string', name: 'username' },
{ type: 'password', name: 'password' },
],
});
app.parse(process.argv);
/*
yarn examples simple db sync
yarn examples simple start
curl --location --request POST 'http://localhost:3000/api/examples/simple/users' \
--header 'Content-Type: application/json' \
--data-raw '{
"username": "abc",
"password": "123456"
}'
curl --location --request GET 'http://localhost:5051/api/examples/simple/users'
*/