mirror of
https://gitee.com/nocobase/nocobase.git
synced 2024-12-05 21:57:45 +08:00
28b73d7143
* feat: add examples * fix: tsconfig-paths/register * feat: more examples * fix: test errors * feat: update examples * docs: example list * fix: updates * feat: example for test case * feat: api client * fix: updates * feat: tree structures * fix: adjacency-list
55 lines
1.1 KiB
TypeScript
55 lines
1.1 KiB
TypeScript
/*
|
|
# 树结构设计 —— 邻接表
|
|
|
|
yarn run:example database/collections/tree/adjacency-list
|
|
*/
|
|
import { Database } from '@nocobase/database';
|
|
import { uid } from '@nocobase/utils';
|
|
|
|
const db = new 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: `t_${uid()}_`,
|
|
});
|
|
|
|
db.collection({
|
|
name: 'categories',
|
|
tree: 'adjacency-list',
|
|
fields: [
|
|
{
|
|
type: 'string',
|
|
name: 'name',
|
|
},
|
|
{
|
|
type: 'string',
|
|
name: 'description',
|
|
},
|
|
{
|
|
type: 'belongsTo',
|
|
name: 'parent',
|
|
target: 'categories',
|
|
foreignKey: 'parentId',
|
|
},
|
|
{
|
|
type: 'hasMany',
|
|
name: 'children',
|
|
target: 'categories',
|
|
foreignKey: 'parentId',
|
|
},
|
|
],
|
|
});
|
|
|
|
(async () => {
|
|
await db.sync();
|
|
await db.close();
|
|
})();
|
|
|
|
export default db;
|