fix many to many mapping errors, reconstruct BO layer
parent
8c0a2d311e
commit
e57036712e
@ -0,0 +1,11 @@
|
||||
ALTER TABLE repositories_collaborators
|
||||
DROP COLUMN createdat ;
|
||||
|
||||
ALTER TABLE repositories_collaborators
|
||||
DROP COLUMN updatedat ;
|
||||
|
||||
ALTER TABLE repositories_members
|
||||
DROP COLUMN createdat ;
|
||||
|
||||
ALTER TABLE repositories_members
|
||||
DROP COLUMN updatedat ;
|
@ -0,0 +1,15 @@
|
||||
import { Table, Column, Model, ForeignKey, PrimaryKey } from 'sequelize-typescript'
|
||||
import { Repository } from '../'
|
||||
|
||||
@Table({ freezeTableName: true, timestamps: true, tableName: 'repositories_collaborators' })
|
||||
export default class RepositoriesCollaborators extends Model<RepositoriesCollaborators> {
|
||||
@ForeignKey(() => Repository)
|
||||
@PrimaryKey
|
||||
@Column
|
||||
repositoryId: number
|
||||
|
||||
@ForeignKey(() => Repository)
|
||||
@PrimaryKey
|
||||
@Column
|
||||
collaboratorId: number
|
||||
}
|
@ -1,8 +1,8 @@
|
||||
import { Table, Column, Model, HasMany, AutoIncrement, PrimaryKey, AllowNull, DataType, Default, BelongsTo, ForeignKey } from 'sequelize-typescript'
|
||||
import { User, Repository, Interface } from './index'
|
||||
import { User, Repository, Interface } from '../'
|
||||
|
||||
@Table({ paranoid: true, freezeTableName: false, timestamps: true })
|
||||
export class Module extends Model<Module> {
|
||||
export default class Module extends Model<Module> {
|
||||
|
||||
@AutoIncrement
|
||||
@PrimaryKey
|
@ -0,0 +1,15 @@
|
||||
import { Table, Column, Model, ForeignKey, PrimaryKey } from 'sequelize-typescript'
|
||||
import { User, Organization } from '../'
|
||||
|
||||
@Table({ freezeTableName: true, timestamps: true, tableName: 'organizations_members' })
|
||||
export default class OrganizationsMembers extends Model<OrganizationsMembers> {
|
||||
@ForeignKey(() => User)
|
||||
@PrimaryKey
|
||||
@Column
|
||||
userId: number
|
||||
|
||||
@ForeignKey(() => Organization)
|
||||
@PrimaryKey
|
||||
@Column
|
||||
organizationId: number
|
||||
}
|
@ -1,11 +1,11 @@
|
||||
import { Table, Column, Model, AutoIncrement, PrimaryKey, AllowNull, DataType, Default, BelongsTo, ForeignKey } from 'sequelize-typescript'
|
||||
import { User, Interface, Module, Repository } from './index'
|
||||
import { User, Interface, Module, Repository } from '../'
|
||||
|
||||
enum SCOPES { REQUEST = 'request', RESPONSE = 'response' }
|
||||
enum TYPES { STRING = 'String', NUMBER = 'Number', BOOLEAN = 'Boolean', OBJECT = 'Object', ARRAY = 'Array', FUNCTION = 'Function', REGEXP = 'RegExp' }
|
||||
|
||||
@Table({ paranoid: true, freezeTableName: false, timestamps: true })
|
||||
export class Property extends Model<Property> {
|
||||
export default class Property extends Model<Property> {
|
||||
public static TYPES = TYPES
|
||||
public static SCOPES = SCOPES
|
||||
|
@ -0,0 +1,15 @@
|
||||
import { Table, Column, Model, ForeignKey, PrimaryKey } from 'sequelize-typescript'
|
||||
import { Repository, User } from '../'
|
||||
|
||||
@Table({ freezeTableName: true, timestamps: true, tableName: 'repositories_members' })
|
||||
export default class RepositoriesMembers extends Model<RepositoriesMembers> {
|
||||
@ForeignKey(() => User)
|
||||
@PrimaryKey
|
||||
@Column
|
||||
userId: number
|
||||
|
||||
@ForeignKey(() => Repository)
|
||||
@PrimaryKey
|
||||
@Column
|
||||
repositoryId: number
|
||||
}
|
@ -1,9 +1,12 @@
|
||||
export { QueryInclude } from './queryInclude'
|
||||
export { Interface } from './interface'
|
||||
export { Logger } from './logger'
|
||||
export { Module } from './module'
|
||||
export { Notification } from './notification'
|
||||
export { Organization } from './organization'
|
||||
export { Property } from './property'
|
||||
export { Repository } from './repository'
|
||||
export { User } from './user'
|
||||
export { default as QueryInclude } from './util/queryInclude'
|
||||
export { default as Interface } from './bo/interface'
|
||||
export { default as Logger } from './bo/logger'
|
||||
export { default as Module } from './bo/module'
|
||||
export { default as Notification } from './bo/notification'
|
||||
export { default as Organization } from './bo/organization'
|
||||
export { default as Property } from './bo/property'
|
||||
export { default as Repository } from './bo/repository'
|
||||
export { default as User } from './bo/user'
|
||||
export { default as OrganizationsMembers } from './bo/organizationsMembers'
|
||||
export { default as RepositoriesCollaborators } from './bo/repositoriesCollaborators'
|
||||
export { default as RepositoriesMembers } from './bo/repositoriesMembers'
|
@ -1,58 +0,0 @@
|
||||
// require('colors')
|
||||
const Sequelize = require('sequelize')
|
||||
const config = require('../../config')
|
||||
const chalk = require('chalk')
|
||||
const now = () => new Date().toISOString().replace(/T/, ' ').replace(/Z/, '')
|
||||
const logging = process.env.NODE_ENV === 'development'
|
||||
? (sql) => {
|
||||
sql = sql.replace('Executing (default): ', '')
|
||||
console.log(`${chalk.bold('SQL')} ${now()} ${chalk.gray(sql)}`)
|
||||
}
|
||||
: console.log
|
||||
|
||||
const sequelize = new Sequelize({
|
||||
database: config.db.database,
|
||||
username: config.db.username,
|
||||
password: config.db.password,
|
||||
host: config.db.host,
|
||||
port: config.db.port,
|
||||
dialect: config.db.dialect,
|
||||
pool: config.db.pool,
|
||||
logging: config.db.logging ? logging : false
|
||||
})
|
||||
|
||||
sequelize.authenticate()
|
||||
.then((/* err */) => {
|
||||
// console.log('Connection has been established successfully.');
|
||||
console.log('----------------------------------------')
|
||||
console.log('DATABASE √')
|
||||
console.log(' HOST %s', config.db.host)
|
||||
console.log(' PORT %s', config.db.port)
|
||||
console.log(' DATABASE %s', config.db.database)
|
||||
console.log('----------------------------------------')
|
||||
})
|
||||
.catch(err => {
|
||||
console.log('Unable to connect to the database:', err)
|
||||
})
|
||||
|
||||
module.exports = sequelize
|
||||
|
||||
// module.exports = {
|
||||
// Sequelize,
|
||||
// sequelize,
|
||||
// id: { type: Sequelize.BIGINT(11).UNSIGNED, primaryKey: true, allowNull: false, autoIncrement: true },
|
||||
// attributes: {
|
||||
// create_date: { type: Sequelize.DATE, allowNull: false, defaultValue: Sequelize.NOW, comment: '创建时间' },
|
||||
// update_date: { type: Sequelize.DATE, allowNull: false, defaultValue: Sequelize.NOW, comment: '更新时间' },
|
||||
// delete_date: { type: Sequelize.DATE, allowNull: true, comment: '删除时间' },
|
||||
// reserve: { type: Sequelize.STRING, allowNull: true, comment: '备用' }
|
||||
// },
|
||||
// options: {
|
||||
// // freezeTableName: true,
|
||||
// // createdAt: 'create_date',
|
||||
// // updatedAt: 'update_date',
|
||||
// // deletedAt: 'delete_date',
|
||||
// paranoid: true
|
||||
// },
|
||||
// exclude: ['password', 'create_date', 'delete_date', 'reserve'] // 'update_date',
|
||||
// }
|
@ -1,12 +0,0 @@
|
||||
const Sequelize = require('sequelize')
|
||||
const sequelize = require('./sequelize')
|
||||
const { id } = require('./helper')
|
||||
module.exports = sequelize.define('user', {
|
||||
id,
|
||||
fullname: { type: Sequelize.STRING(32), allowNull: false, comment: '姓名' },
|
||||
password: { type: Sequelize.STRING(32), allowNull: true, comment: '密码' },
|
||||
email: { type: Sequelize.STRING(128), allowNull: false, unique: true, comment: '邮箱' }
|
||||
}, {
|
||||
paranoid: true,
|
||||
comment: '用户'
|
||||
})
|
@ -0,0 +1,12 @@
|
||||
declare interface IHelper {
|
||||
include: string[],
|
||||
exclude: {
|
||||
generalities: string[]
|
||||
}
|
||||
}
|
||||
export let Helper: IHelper = {
|
||||
include: [],
|
||||
exclude: {
|
||||
generalities: ['createdAt', 'updatedAt', 'deletedAt', 'reserve']
|
||||
}
|
||||
}
|
@ -1,101 +0,0 @@
|
||||
// let Random = require('mockjs').Random
|
||||
const { mock } = require('mockjs')
|
||||
|
||||
const scopes = ['request', 'response']
|
||||
const methods = ['GET', 'POST', 'PUT', 'DELETE']
|
||||
const types = ['String', 'Number', 'Boolean', 'Object', 'Array', 'Function', 'RegExp']
|
||||
const values = ['@INT', '@FLOAT', '@TITLE', '@NAME']
|
||||
|
||||
let USER_ID = 100000000
|
||||
let ORGANIZATION_ID = 1
|
||||
let REPOSITORY_ID = 1
|
||||
let MODULE_ID = 1
|
||||
let INTERFACE_ID = 1
|
||||
let PROPERTY_ID = 1
|
||||
|
||||
module.exports = {
|
||||
BO_ADMIN: { id: USER_ID++, fullname: 'admin', email: 'admin@rap2.com', password: 'admin' },
|
||||
BO_MOZHI: { id: USER_ID++, fullname: '墨智', email: 'mozhi@rap2.com', password: 'mozhi' },
|
||||
BO_USER_COUNT: 10,
|
||||
BO_USER_FN: () => mock({
|
||||
id: USER_ID++,
|
||||
fullname: '@cname',
|
||||
email: '@email',
|
||||
password: '@word(6)'
|
||||
}),
|
||||
BO_ORGANIZATION_COUNT: 3,
|
||||
BO_ORGANIZATION_FN: (source) => {
|
||||
return Object.assign(
|
||||
mock({
|
||||
id: ORGANIZATION_ID++,
|
||||
name: '组织@ctitle(5)',
|
||||
description: '@cparagraph',
|
||||
logo: '@url',
|
||||
creatorId: undefined,
|
||||
owner: undefined,
|
||||
members: ''
|
||||
}),
|
||||
source
|
||||
)
|
||||
},
|
||||
BO_REPOSITORY_COUNT: 3,
|
||||
BO_REPOSITORY_FN: (source) => {
|
||||
return Object.assign(
|
||||
mock({
|
||||
id: REPOSITORY_ID++,
|
||||
name: '仓库@ctitle',
|
||||
description: '@cparagraph',
|
||||
logo: '@url'
|
||||
}),
|
||||
source
|
||||
)
|
||||
},
|
||||
BO_MODULE_COUNT: 3,
|
||||
BO_MODULE_FN: (source) => {
|
||||
return Object.assign(
|
||||
mock({
|
||||
id: MODULE_ID++,
|
||||
name: '模块@ctitle(4)',
|
||||
description: '@cparagraph',
|
||||
repositoryId: undefined,
|
||||
creatorId: undefined
|
||||
}),
|
||||
source
|
||||
)
|
||||
},
|
||||
BO_INTERFACE_COUNT: 3,
|
||||
BO_INTERFACE_FN: (source) => {
|
||||
return Object.assign(
|
||||
mock({
|
||||
id: INTERFACE_ID++,
|
||||
name: '接口@ctitle(4)',
|
||||
url: '/@word(5)/@word(5)/@word(5).json',
|
||||
'method|1': methods,
|
||||
description: '@cparagraph',
|
||||
creatorId: undefined,
|
||||
lockerId: undefined,
|
||||
repositoryId: undefined,
|
||||
moduleId: undefined
|
||||
}),
|
||||
source
|
||||
)
|
||||
},
|
||||
BO_PROPERTY_COUNT: 6,
|
||||
BO_PROPERTY_FN: (source) => {
|
||||
return Object.assign(
|
||||
mock({
|
||||
id: PROPERTY_ID++,
|
||||
'scope|1': scopes,
|
||||
name: '@word(6)',
|
||||
'type|1': types,
|
||||
'value|1': values,
|
||||
description: '@csentence',
|
||||
creatorId: undefined,
|
||||
repositoryId: undefined,
|
||||
moduleId: undefined,
|
||||
interfaceId: undefined
|
||||
}),
|
||||
source
|
||||
)
|
||||
}
|
||||
}
|
@ -1,152 +0,0 @@
|
||||
const sequelize = require('../../src/models/sequelize')
|
||||
const { User, Organization, Repository, Module, Interface, Property } = require('../../src/models/index')
|
||||
const { BO_ADMIN, BO_MOZHI } = require('./bo')
|
||||
const { BO_USER_FN, BO_ORGANIZATION_FN, BO_REPOSITORY_FN, BO_MODULE_FN, BO_INTERFACE_FN, BO_PROPERTY_FN } = require('./bo')
|
||||
const { BO_USER_COUNT, BO_ORGANIZATION_COUNT, BO_REPOSITORY_COUNT, BO_MODULE_COUNT, BO_INTERFACE_COUNT, BO_PROPERTY_COUNT } = require('./bo')
|
||||
const EMPTY_WHERE = { where: {} }
|
||||
|
||||
async function init () {
|
||||
await sequelize.drop()
|
||||
await sequelize.sync({
|
||||
force: true,
|
||||
logging: console.log
|
||||
})
|
||||
await User.destroy(EMPTY_WHERE)
|
||||
await Organization.destroy(EMPTY_WHERE)
|
||||
await Repository.destroy(EMPTY_WHERE)
|
||||
await Module.destroy(EMPTY_WHERE)
|
||||
await Interface.destroy(EMPTY_WHERE)
|
||||
await Property.destroy(EMPTY_WHERE)
|
||||
|
||||
// 用户
|
||||
await User.create(BO_ADMIN)
|
||||
await User.create(BO_MOZHI)
|
||||
for (let i = 0; i < BO_USER_COUNT; i++) {
|
||||
await User.create(BO_USER_FN())
|
||||
}
|
||||
|
||||
let users = await User.findAll()
|
||||
|
||||
// 用户 admin 仓库
|
||||
for (let BO_REPOSITORY_INDEX = 0; BO_REPOSITORY_INDEX < BO_REPOSITORY_COUNT; BO_REPOSITORY_INDEX++) {
|
||||
let repository = await Repository.create(
|
||||
BO_REPOSITORY_FN({ creatorId: BO_ADMIN.id, ownerId: BO_ADMIN.id })
|
||||
)
|
||||
await repository.setMembers(
|
||||
users.filter(user => user.id !== BO_ADMIN.id)
|
||||
)
|
||||
await initRepository(repository)
|
||||
}
|
||||
|
||||
// 用户 mozhi 的仓库
|
||||
for (let BO_REPOSITORY_INDEX = 0; BO_REPOSITORY_INDEX < BO_REPOSITORY_COUNT; BO_REPOSITORY_INDEX++) {
|
||||
let repository = await Repository.create(
|
||||
BO_REPOSITORY_FN({ creatorId: BO_MOZHI.id, ownerId: BO_MOZHI.id })
|
||||
)
|
||||
await repository.setMembers(
|
||||
users.filter(user => user.id !== BO_MOZHI.id)
|
||||
)
|
||||
await initRepository(repository)
|
||||
}
|
||||
|
||||
// 团队
|
||||
for (let BO_ORGANIZATION_INDEX = 0; BO_ORGANIZATION_INDEX < BO_ORGANIZATION_COUNT; BO_ORGANIZATION_INDEX++) {
|
||||
let organization = await Organization.create(
|
||||
BO_ORGANIZATION_FN({ creatorId: BO_ADMIN.id, ownerId: BO_ADMIN.id })
|
||||
)
|
||||
await organization.setMembers(
|
||||
users.filter(user => user.id !== BO_ADMIN.id)
|
||||
)
|
||||
// 团队的仓库
|
||||
for (let BO_REPOSITORY_INDEX = 0; BO_REPOSITORY_INDEX < BO_REPOSITORY_COUNT; BO_REPOSITORY_INDEX++) {
|
||||
let repository = await Repository.create(
|
||||
BO_REPOSITORY_FN({ creatorId: BO_ADMIN.id, ownerId: BO_ADMIN.id, organizationId: organization.id })
|
||||
)
|
||||
await repository.setMembers(
|
||||
users.filter(user => user.id !== BO_ADMIN.id)
|
||||
)
|
||||
await initRepository(repository)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function initRepository (repository) {
|
||||
// 模块
|
||||
for (let BO_MODULE_INDEX = 0; BO_MODULE_INDEX < BO_MODULE_COUNT; BO_MODULE_INDEX++) {
|
||||
let mod = await Module.create(
|
||||
BO_MODULE_FN({ creatorId: repository.creatorId, repositoryId: repository.id })
|
||||
)
|
||||
await repository.addModule(mod)
|
||||
// 接口
|
||||
for (let BO_INTERFACE_INDEX = 0; BO_INTERFACE_INDEX < BO_INTERFACE_COUNT; BO_INTERFACE_INDEX++) {
|
||||
let itf = await Interface.create(
|
||||
BO_INTERFACE_FN({ creatorId: mod.creatorId, repositoryId: repository.id, moduleId: mod.id })
|
||||
)
|
||||
await mod.addInterface(itf)
|
||||
// 属性
|
||||
for (let BO_PROPERTY_INDEX = 0; BO_PROPERTY_INDEX < BO_PROPERTY_COUNT; BO_PROPERTY_INDEX++) {
|
||||
let prop = await Property.create(
|
||||
BO_PROPERTY_FN({ creatorId: itf.creatorId, repositoryId: repository.id, moduleId: mod.id, interfaceId: itf.id })
|
||||
)
|
||||
await itf.addProperty(prop)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function after () {
|
||||
let exclude = ['password', 'createdAt', 'updatedAt', 'deletedAt']
|
||||
let repositories = await Repository.findAll({
|
||||
attributes: { exclude: [] },
|
||||
include: [
|
||||
{ model: User, as: 'creator', attributes: { exclude }, required: true },
|
||||
{ model: User, as: 'owner', attributes: { exclude }, required: true },
|
||||
{ model: Organization, as: 'organization', attributes: { exclude }, required: false },
|
||||
{ model: User, as: 'locker', attributes: { exclude }, required: false },
|
||||
{ model: User, as: 'members', attributes: { exclude }, through: { attributes: [] }, required: true },
|
||||
{ model: Module,
|
||||
as: 'modules',
|
||||
attributes: { exclude },
|
||||
// through: { attributes: [] },
|
||||
include: [
|
||||
{
|
||||
model: Interface,
|
||||
as: 'interfaces',
|
||||
attributes: { exclude },
|
||||
// through: { attributes: [] },
|
||||
include: [
|
||||
{
|
||||
model: Property,
|
||||
as: 'properties',
|
||||
attributes: { exclude },
|
||||
// through: { attributes: [] },
|
||||
required: true
|
||||
}
|
||||
],
|
||||
required: true
|
||||
}
|
||||
],
|
||||
required: true
|
||||
}
|
||||
],
|
||||
offset: 0,
|
||||
limit: 100
|
||||
})
|
||||
// console.log(JSON.stringify(repositories, null, 2))
|
||||
console.log(repositories.map(item => item.toJSON()))
|
||||
|
||||
let admin = await User.findById(BO_ADMIN.id)
|
||||
// for (let k in admin) console.log(k)
|
||||
let owned = await admin.getOwnedOrganizations()
|
||||
console.log(owned.map(item => item.toJSON()))
|
||||
|
||||
let mozhi = await User.findById(BO_MOZHI.id)
|
||||
for (let k in mozhi) console.log(k)
|
||||
let joined = await mozhi.getJoinedOrganizations()
|
||||
console.log(joined.map(item => item.toJSON()))
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
init,
|
||||
after
|
||||
}
|
@ -1,10 +0,0 @@
|
||||
const { init, after } = require('./delos')
|
||||
/**
|
||||
* initialize database
|
||||
*/
|
||||
async function main () {
|
||||
await init()
|
||||
await after()
|
||||
}
|
||||
|
||||
main()
|
Loading…
Reference in New Issue