refactor: change the field type names to uppercase

This commit is contained in:
chenos 2020-12-10 14:47:13 +08:00
parent e86b573296
commit 437c49d211
12 changed files with 181 additions and 107 deletions

View File

@ -1,8 +1,13 @@
import { Context, Next } from '.'; import { Context, Next } from '.';
import { list, get, create, update, destroy } from './common'; import { list, get, create, update, destroy } from './common';
import { HasOne, BelongsTo, BelongsToMany, HasMany, Model, Relation } from '@nocobase/database'; import {
import { Op } from 'sequelize'; Model,
Relation,
HASONE,
BELONGSTO,
BELONGSTOMANY,
HASMANY,
} from '@nocobase/database';
/** /**
* *
@ -116,9 +121,9 @@ export async function remove(ctx: Context, next: Next) {
const options = TargetModel.parseApiJson({ const options = TargetModel.parseApiJson({
fields, fields,
}); });
if (resourceField instanceof HasOne || resourceField instanceof BelongsTo) { if (resourceField instanceof HASONE || resourceField instanceof BELONGSTO) {
ctx.body = await associated[setAccessor](null); ctx.body = await associated[setAccessor](null);
} else if (resourceField instanceof HasMany || resourceField instanceof BelongsToMany) { } else if (resourceField instanceof HASMANY || resourceField instanceof BELONGSTOMANY) {
const [model]: Model[] = await associated[getAccessor]({ const [model]: Model[] = await associated[getAccessor]({
...options, ...options,
where: { where: {
@ -153,7 +158,7 @@ export async function toggle(ctx: Context, next: Next) {
const options = TargetModel.parseApiJson({ const options = TargetModel.parseApiJson({
fields, fields,
}); });
if (resourceField instanceof HasOne || resourceField instanceof BelongsTo) { if (resourceField instanceof HASONE || resourceField instanceof BELONGSTO) {
const m1 = await associated[getAccessor](); const m1 = await associated[getAccessor]();
if (m1 && m1[resourceKeyAttribute || resourceField.options.targetKey || TargetModel.primaryKeyAttribute] == resourceKey) { if (m1 && m1[resourceKeyAttribute || resourceField.options.targetKey || TargetModel.primaryKeyAttribute] == resourceKey) {
ctx.body = await associated[setAccessor](null); ctx.body = await associated[setAccessor](null);
@ -168,7 +173,7 @@ export async function toggle(ctx: Context, next: Next) {
}); });
ctx.body = await associated[setAccessor](m2); ctx.body = await associated[setAccessor](m2);
} }
} else if (resourceField instanceof HasMany || resourceField instanceof BelongsToMany) { } else if (resourceField instanceof HASMANY || resourceField instanceof BELONGSTOMANY) {
const [model]: Model[] = await associated[getAccessor]({ const [model]: Model[] = await associated[getAccessor]({
...options, ...options,
where: { where: {

View File

@ -1,5 +1,11 @@
import { Context, Next } from '.'; import { Context, Next } from '.';
import { Relation, Model, HasOne, HasMany, BelongsTo, BelongsToMany } from '@nocobase/database'; import {
Model,
HASONE,
HASMANY,
BELONGSTO,
BELONGSTOMANY,
} from '@nocobase/database';
import { DEFAULT_PAGE, DEFAULT_PER_PAGE } from '@nocobase/resourcer'; import { DEFAULT_PAGE, DEFAULT_PER_PAGE } from '@nocobase/resourcer';
import { Utils, Op } from 'sequelize'; import { Utils, Op } from 'sequelize';
import _ from 'lodash'; import _ from 'lodash';
@ -159,10 +165,10 @@ export async function get(ctx: Context, next: Next) {
const options = TargetModel.parseApiJson({ const options = TargetModel.parseApiJson({
fields, fields,
}); });
if (resourceField instanceof HasOne || resourceField instanceof BelongsTo) { if (resourceField instanceof HASONE || resourceField instanceof BELONGSTO) {
const model: Model = await associated[getAccessor]({ ...options, context: ctx }); const model: Model = await associated[getAccessor]({ ...options, context: ctx });
ctx.body = model; ctx.body = model;
} else if (resourceField instanceof HasMany || resourceField instanceof BelongsToMany) { } else if (resourceField instanceof HASMANY || resourceField instanceof BELONGSTOMANY) {
const [model]: Model[] = await associated[getAccessor]({ const [model]: Model[] = await associated[getAccessor]({
...options, ...options,
where: { where: {
@ -219,7 +225,7 @@ export async function update(ctx: Context, next: Next) {
throw new Error(`${associatedName} associated model invalid`); throw new Error(`${associatedName} associated model invalid`);
} }
const { get: getAccessor } = resourceField.getAccessors(); const { get: getAccessor } = resourceField.getAccessors();
if (resourceField instanceof HasOne || resourceField instanceof BelongsTo) { if (resourceField instanceof HASONE || resourceField instanceof BELONGSTO) {
let model: Model = await associated[getAccessor]({ transaction, context: ctx }); let model: Model = await associated[getAccessor]({ transaction, context: ctx });
if (model) { if (model) {
// @ts-ignore // @ts-ignore
@ -227,7 +233,7 @@ export async function update(ctx: Context, next: Next) {
await model.updateAssociations(values, { transaction, context: ctx }); await model.updateAssociations(values, { transaction, context: ctx });
ctx.body = model; ctx.body = model;
} }
} else if (resourceField instanceof HasMany || resourceField instanceof BelongsToMany) { } else if (resourceField instanceof HASMANY || resourceField instanceof BELONGSTOMANY) {
const TargetModel = ctx.db.getModel(resourceField.getTarget()); const TargetModel = ctx.db.getModel(resourceField.getTarget());
const [model]: Model[] = await associated[getAccessor]({ const [model]: Model[] = await associated[getAccessor]({
where: { where: {
@ -237,7 +243,7 @@ export async function update(ctx: Context, next: Next) {
context: ctx, context: ctx,
}); });
if (resourceField instanceof BelongsToMany) { if (resourceField instanceof BELONGSTOMANY) {
const throughName = resourceField.getThroughName(); const throughName = resourceField.getThroughName();
if (typeof values[throughName] === 'object') { if (typeof values[throughName] === 'object') {
const ThroughModel = resourceField.getThroughModel(); const ThroughModel = resourceField.getThroughModel();
@ -315,12 +321,12 @@ export async function destroy(ctx: Context, next: Next) {
const {get: getAccessor, remove: removeAccessor, set: setAccessor} = resourceField.getAccessors(); const {get: getAccessor, remove: removeAccessor, set: setAccessor} = resourceField.getAccessors();
const TargetModel = ctx.db.getModel(resourceField.getTarget()); const TargetModel = ctx.db.getModel(resourceField.getTarget());
const { where } = TargetModel.parseApiJson({ filter, context: ctx }); const { where } = TargetModel.parseApiJson({ filter, context: ctx });
if (resourceField instanceof HasOne || resourceField instanceof BelongsTo) { if (resourceField instanceof HASONE || resourceField instanceof BELONGSTO) {
const model: Model = await associated[getAccessor](commonOptions); const model: Model = await associated[getAccessor](commonOptions);
await associated[setAccessor](null, commonOptions); await associated[setAccessor](null, commonOptions);
// @ts-ignore // @ts-ignore
ctx.body = await model.destroy(commonOptions); ctx.body = await model.destroy(commonOptions);
} else if (resourceField instanceof HasMany || resourceField instanceof BelongsToMany) { } else if (resourceField instanceof HASMANY || resourceField instanceof BELONGSTOMANY) {
const primaryKey = resourceKeyAttribute || resourceField.options.targetKey || TargetModel.primaryKeyAttribute; const primaryKey = resourceKeyAttribute || resourceField.options.targetKey || TargetModel.primaryKeyAttribute;
const models: Model[] = await associated[getAccessor]({ const models: Model[] = await associated[getAccessor]({
where: resourceKey ? { [primaryKey]: resourceKey } : where, where: resourceKey ? { [primaryKey]: resourceKey } : where,
@ -371,11 +377,11 @@ export async function sort(ctx: Context, next: Next) {
} = ctx.action.params; } = ctx.action.params;
if (associated && resourceField) { if (associated && resourceField) {
if (resourceField instanceof HasOne || resourceField instanceof BelongsTo) { if (resourceField instanceof HASONE || resourceField instanceof BELONGSTO) {
throw new Error(`the association (${resourceName} belongs to ${associatedName}) cannot be sorted`); throw new Error(`the association (${resourceName} belongs to ${associatedName}) cannot be sorted`);
} }
// TODO(feature) // TODO(feature)
if (resourceField instanceof BelongsToMany) { if (resourceField instanceof BELONGSTOMANY) {
throw new Error('sorting for belongs to many association has not been implemented'); throw new Error('sorting for belongs to many association has not been implemented');
} }
} }
@ -406,7 +412,7 @@ export async function sort(ctx: Context, next: Next) {
const transaction = await ctx.db.sequelize.transaction(); const transaction = await ctx.db.sequelize.transaction();
const { where = {} } = Model.parseApiJson({ filter }); const { where = {} } = Model.parseApiJson({ filter });
if (associated && resourceField instanceof HasMany) { if (associated && resourceField instanceof HASMANY) {
where[resourceField.options.foreignKey] = associatedKey; where[resourceField.options.foreignKey] = associatedKey;
} }

View File

@ -1,6 +1,6 @@
import { Context, Next, associate } from '../actions'; import { Context, Next } from '../actions';
import { Action } from '@nocobase/resourcer'; import { Action } from '@nocobase/resourcer';
import { HasOne, HasMany, BelongsTo, BelongsToMany, Model } from '@nocobase/database'; import { HASONE, HASMANY, BELONGSTO, BELONGSTOMANY } from '@nocobase/database';
export async function associated(ctx: Context, next: Next) { export async function associated(ctx: Context, next: Next) {
if (!(ctx.action instanceof Action)) { if (!(ctx.action instanceof Action)) {
@ -23,12 +23,12 @@ export async function associated(ctx: Context, next: Next) {
let key: string; let key: string;
switch (true) { switch (true) {
case field instanceof BelongsTo: case field instanceof BELONGSTO:
key = field.options.targetKey || Model.primaryKeyAttribute; key = field.options.targetKey || Model.primaryKeyAttribute;
break; break;
case field instanceof HasOne: case field instanceof HASONE:
case field instanceof HasMany: case field instanceof HASMANY:
case field instanceof BelongsToMany: case field instanceof BELONGSTOMANY:
key = field.options.sourceKey; key = field.options.sourceKey;
break; break;
} }

View File

@ -1,6 +1,10 @@
import { getDatabase } from './'; import { getDatabase } from './';
import { import {
HasMany, HasOne, Integer, BelongsTo, BelongsToMany HASMANY as HasMany,
HASONE as HasOne,
INTEGER as Integer,
BELONGSTO as BelongsTo,
BELONGSTOMANY as BelongsToMany,
} from '../fields'; } from '../fields';
import { DataTypes } from 'sequelize'; import { DataTypes } from 'sequelize';
import Database from '..'; import Database from '..';

View File

@ -2,9 +2,9 @@ import { getDatabase } from './';
import Database from '../database'; import Database from '../database';
import Table from '../table'; import Table from '../table';
import Model from '../model'; import Model from '../model';
import { FieldContext, Integer, registerField } from '../fields'; import { FieldContext, INTEGER, registerField } from '../fields';
class TestHookField extends Integer { class TestHookField extends INTEGER {
constructor(options: any, context: FieldContext) { constructor(options: any, context: FieldContext) {
super(options, context); super(options, context);
const Model = context.sourceTable.getModel(); const Model = context.sourceTable.getModel();
@ -157,4 +157,58 @@ describe('hooks', () => {
const test = await Test3.create({}); const test = await Test3.create({});
expect(test.get('arr')).toEqual([ 1, 3, 2, 4 ]); expect(test.get('arr')).toEqual([ 1, 3, 2, 4 ]);
}); });
it.only('add hook in custom field', async () => {
const table = db.table({
name: 'test3',
fields: [
{
type: 'json',
name: 'arr',
defaultValue: [],
},
],
hooks: {
beforeCreate(model) {
model.set('name', 'beforeCreate1');
const arr = model.get('arr') as any[];
arr.push(3);
model.set('arr', arr);
console.log('beforeCreate in table');
},
afterCreate(model) {
model.set('name', 'afterCreate1');
const arr = model.get('arr') as any[];
arr.push(4);
model.set('arr', arr);
console.log('afterCreate in table');
},
},
});
table.addField({
type: 'string',
name: 'title',
});
// await table.sync();
let Test3 = db.getModel('test3');
Test3.beforeCreate((model) => {
const arr = model.get('arr') as any[];
arr.push(5);
model.set('arr', arr);
});
Test3.afterCreate((model) => {
const arr = model.get('arr') as any[];
arr.push(6);
model.set('arr', arr);
});
// 通过 table 新增
table.addField({
type: 'testHook',
name: 'testHook',
});
await table.sync();
Test3 = db.getModel('test3');
const test = await Test3.create({});
expect(test.get('arr')).toEqual([ 3, 5, 1, 4, 6, 2 ]);
});
}); });

View File

@ -1,25 +1,25 @@
import { import {
buildField, buildField,
Boolean,
Integer,
String,
Text,
HasOne,
HasMany,
BelongsTo,
BelongsToMany,
Float,
Double,
Real,
Decimal,
Column, Column,
Time, BOOLEAN as Boolean,
Date, INTEGER as Integer,
DateOnly, STRING as String,
Array, TEXT as Text,
Json, HASONE as HasOne,
Jsonb, HASMANY as HasMany,
Password BELONGSTO as BelongsTo,
BELONGSTOMANY as BelongsToMany,
FLOAT as Float,
DOUBLE as Double,
REAL as Real,
DECIMAL as Decimal,
TIME as Time,
DATE as Date,
DATEONLY as DateOnly,
ARRAY as Array,
JSON as Json,
JSONB as Jsonb,
PASSWORD as Password,
} from '../fields'; } from '../fields';
import { DataTypes } from 'sequelize'; import { DataTypes } from 'sequelize';
import { ABSTRACT } from 'sequelize/lib/data-types'; import { ABSTRACT } from 'sequelize/lib/data-types';

View File

@ -81,13 +81,13 @@ export class Column extends Field {
} }
} }
export class Boolean extends Column { export class BOOLEAN extends Column {
} }
export class Number extends Column { export class NUMBER extends Column {
} }
export class Integer extends Number { export class INTEGER extends NUMBER {
public readonly options: Options.IntegerOptions; public readonly options: Options.IntegerOptions;
@ -117,7 +117,7 @@ export class Integer extends Number {
} }
} }
export class Float extends Number { export class FLOAT extends NUMBER {
public readonly options: Options.FloatOptions; public readonly options: Options.FloatOptions;
@ -130,7 +130,7 @@ export class Float extends Number {
} }
} }
export class Double extends Number { export class DOUBLE extends NUMBER {
public readonly options: Options.DoubleOptions; public readonly options: Options.DoubleOptions;
public getAttributeOptions() { public getAttributeOptions() {
@ -142,7 +142,7 @@ export class Double extends Number {
} }
} }
export class Decimal extends Number { export class DECIMAL extends NUMBER {
public readonly options: Options.DecimalOptions; public readonly options: Options.DecimalOptions;
@ -155,7 +155,7 @@ export class Decimal extends Number {
} }
} }
export class Real extends Number { export class REAL extends NUMBER {
public readonly options: Options.RealOptions; public readonly options: Options.RealOptions;
@ -168,7 +168,7 @@ export class Real extends Number {
} }
} }
export class String extends Column { export class STRING extends Column {
public readonly options: Options.StringOptions; public readonly options: Options.StringOptions;
@ -182,7 +182,7 @@ export class String extends Column {
} }
} }
export class Text extends Column { export class TEXT extends Column {
public readonly options: Options.TextOptions; public readonly options: Options.TextOptions;
@ -206,10 +206,10 @@ export class Text extends Column {
} }
} }
export class Time extends Column { export class TIME extends Column {
} }
export class Date extends Column { export class DATE extends Column {
public readonly options: Options.DateOptions; public readonly options: Options.DateOptions;
@ -223,13 +223,13 @@ export class Date extends Column {
} }
} }
export class DateOnly extends Column { export class DATEONLY extends Column {
} }
export class Virtual extends Column { export class VIRTUAL extends Column {
} }
export class Reference extends Virtual { export class REFERENCE extends VIRTUAL {
public getDataType() { public getDataType() {
return DataTypes.VIRTUAL; return DataTypes.VIRTUAL;
@ -248,7 +248,7 @@ export class Reference extends Virtual {
} }
} }
export class Formula extends Virtual { export class FORMULA extends VIRTUAL {
public getDataType() { public getDataType() {
return DataTypes.VIRTUAL; return DataTypes.VIRTUAL;
@ -284,7 +284,7 @@ export class Formula extends Virtual {
} }
} }
export class Password extends String { export class PASSWORD extends STRING {
public getDataType() { public getDataType() {
return DataTypes.STRING; return DataTypes.STRING;
@ -307,7 +307,7 @@ export class Password extends String {
} }
} }
export class Array extends Column { export class ARRAY extends Column {
public readonly options: Options.ArrayOptions; public readonly options: Options.ArrayOptions;
@ -324,10 +324,10 @@ export class Array extends Column {
} }
} }
export class Json extends Column { export class JSON extends Column {
} }
export class Jsonb extends Column { export class JSONB extends Column {
} }
export interface HasOneAccessors { export interface HasOneAccessors {
@ -371,16 +371,16 @@ export interface BelongsToManyAccessors {
export abstract class Relation extends Field { export abstract class Relation extends Field {
public getAssociationType() { public getAssociationType() {
if (this instanceof HasOne) { if (this instanceof HASONE) {
return 'hasOne'; return 'hasOne';
} }
if (this instanceof HasMany) { if (this instanceof HASMANY) {
return 'hasMany'; return 'hasMany';
} }
if (this instanceof BelongsTo) { if (this instanceof BELONGSTO) {
return 'belongsTo'; return 'belongsTo';
} }
if (this instanceof BelongsToMany) { if (this instanceof BELONGSTOMANY) {
return 'belongsToMany'; return 'belongsToMany';
} }
} }
@ -390,10 +390,10 @@ export abstract class Relation extends Field {
if (target) { if (target) {
return target; return target;
} }
if (this instanceof HasMany) { if (this instanceof HASMANY) {
return name; return name;
} }
if (this instanceof BelongsToMany) { if (this instanceof BELONGSTOMANY) {
return name; return name;
} }
return Utils.pluralize(name); return Utils.pluralize(name);
@ -462,7 +462,7 @@ class HasOneOrMany extends Relation {
} }
} }
export class HasOne extends HasOneOrMany { export class HASONE extends HasOneOrMany {
public readonly options: Options.HasOneOptions; public readonly options: Options.HasOneOptions;
@ -489,7 +489,7 @@ export class HasOne extends HasOneOrMany {
} }
} }
export class HasMany extends HasOneOrMany { export class HASMANY extends HasOneOrMany {
public readonly options: Options.HasManyOptions; public readonly options: Options.HasManyOptions;
@ -512,7 +512,7 @@ export class HasMany extends HasOneOrMany {
} }
} }
export class BelongsTo extends Relation { export class BELONGSTO extends Relation {
public readonly options: Options.BelongsToOptions; public readonly options: Options.BelongsToOptions;
@ -567,7 +567,7 @@ export class BelongsTo extends Relation {
} }
} }
export class BelongsToMany extends Relation { export class BELONGSTOMANY extends Relation {
public readonly options: Options.BelongsToManyOptions; public readonly options: Options.BelongsToManyOptions;

View File

@ -110,15 +110,14 @@ export function buildField(options: FieldOptions, context: Fields.FieldContext)
registerFields({ registerFields({
...Fields, ...Fields,
// aliases // aliases
Int: Fields.Integer, INT: Fields.INTEGER,
TinyInt: Fields.Integer, TINYINT: Fields.INTEGER,
TinyInteger: Fields.Integer, TINYINTEGER: Fields.INTEGER,
SmallInt: Fields.Integer, SMALLINT: Fields.INTEGER,
SmallInteger: Fields.Integer, SMALLINTEGER: Fields.INTEGER,
MediumInt: Fields.Integer, MEDIUMINT: Fields.INTEGER,
MediumInteger: Fields.Integer, MEDIUMINTEGER: Fields.INTEGER,
BigInt: Fields.Integer, BIGINT: Fields.INTEGER,
BigInteger: Fields.Integer, BIGINTEGER: Fields.INTEGER,
Timestamp: Fields.Date, TIMESTAMP: Fields.DATE,
Creator: Fields.BelongsTo,
}); });

View File

@ -2,7 +2,13 @@ import {
Model as SequelizeModel, Op, Sequelize, ProjectionAlias, Utils, SaveOptions, Model as SequelizeModel, Op, Sequelize, ProjectionAlias, Utils, SaveOptions,
} from 'sequelize'; } from 'sequelize';
import Database from './database'; import Database from './database';
import { HasOne, HasMany, BelongsTo, BelongsToMany, getDataTypeKey } from './fields'; import {
getDataTypeKey,
HASONE,
HASMANY,
BELONGSTO,
BELONGSTOMANY,
} from './fields';
import { toInclude } from './utils'; import { toInclude } from './utils';
export interface ApiJsonOptions { export interface ApiJsonOptions {
@ -269,7 +275,7 @@ export abstract class Model extends SequelizeModel {
await this[accessors.set](data, opts); await this[accessors.set](data, opts);
} else if (typeof data === 'object') { } else if (typeof data === 'object') {
const Target = association.getTargetModel(); const Target = association.getTargetModel();
const targetAttribute = association instanceof BelongsTo const targetAttribute = association instanceof BELONGSTO
? association.options.targetKey ? association.options.targetKey
: association.options.sourceKey; : association.options.sourceKey;
if (data[targetAttribute]) { if (data[targetAttribute]) {
@ -407,7 +413,7 @@ export abstract class Model extends SequelizeModel {
} }
} }
if (association instanceof BelongsToMany) { if (association instanceof BELONGSTOMANY) {
belongsToManyList.push({ belongsToManyList.push({
item, item,
target target
@ -424,8 +430,8 @@ export abstract class Model extends SequelizeModel {
// 后处理 belongsToMany 的更新内容 // 后处理 belongsToMany 的更新内容
if (belongsToManyList.length) { if (belongsToManyList.length) {
const ThroughModel = (association as BelongsToMany).getThroughModel(); const ThroughModel = (association as BELONGSTOMANY).getThroughModel();
const throughName = (association as BelongsToMany).getThroughName(); const throughName = (association as BELONGSTOMANY).getThroughName();
for (const { item, target } of belongsToManyList) { for (const { item, target } of belongsToManyList) {
const throughValues = item[throughName]; const throughValues = item[throughName];
@ -453,11 +459,11 @@ export abstract class Model extends SequelizeModel {
const table = this.database.getTable(this.constructor.name); const table = this.database.getTable(this.constructor.name);
const association = table.getAssociations().get(key); const association = table.getAssociations().get(key);
switch (true) { switch (true) {
case association instanceof BelongsTo: case association instanceof BELONGSTO:
case association instanceof HasOne: case association instanceof HASONE:
return this.updateSingleAssociation(key, data, options); return this.updateSingleAssociation(key, data, options);
case association instanceof HasMany: case association instanceof HASMANY:
case association instanceof BelongsToMany: case association instanceof BELONGSTOMANY:
return this.updateMultipleAssociation(key, data, options); return this.updateMultipleAssociation(key, data, options);
} }
} }

View File

@ -10,8 +10,8 @@ import {
buildField, buildField,
FieldOptions, FieldOptions,
Relation, Relation,
BelongsTo, BELONGSTO,
BelongsToMany, BELONGSTOMANY,
} from './fields'; } from './fields';
import Database from './database'; import Database from './database';
import { Model, ModelCtor } from './model'; import { Model, ModelCtor } from './model';
@ -166,7 +166,7 @@ export class Table {
if (this.database.isDefined(target)) { if (this.database.isDefined(target)) {
const TargetModel = this.database.getModel(target); const TargetModel = this.database.getModel(target);
// 如果关系表在之后才定义,未设置 targetKey 时targetKey 默认值需要在 target model 初始化之后才能取到 // 如果关系表在之后才定义,未设置 targetKey 时targetKey 默认值需要在 target model 初始化之后才能取到
if (association instanceof BelongsTo || association instanceof BelongsToMany) { if (association instanceof BELONGSTO || association instanceof BELONGSTOMANY) {
association.updateOptionsAfterTargetModelBeDefined(); association.updateOptionsAfterTargetModelBeDefined();
} }
this.Model[type](TargetModel, association.getAssociationOptions()); this.Model[type](TargetModel, association.getAssociationOptions());
@ -235,7 +235,7 @@ export class Table {
for (const association of this.associations.values()) { for (const association of this.associations.values()) {
const target = association.getTarget(); const target = association.getTarget();
names.add(target); names.add(target);
if (association instanceof BelongsToMany) { if (association instanceof BELONGSTOMANY) {
names.add(association.getThroughName()); names.add(association.getThroughName());
} }
} }

View File

@ -1,6 +1,6 @@
import { actions } from '@nocobase/actions'; import { actions } from '@nocobase/actions';
import { PASSWORD } from '@nocobase/database';
import cryptoRandomString from 'crypto-random-string'; import cryptoRandomString from 'crypto-random-string';
import { Password } from "@nocobase/database";
export default async (ctx: actions.Context, next: actions.Next) => { export default async (ctx: actions.Context, next: actions.Next) => {
const { values } = ctx.action.params; const { values } = ctx.action.params;
@ -14,7 +14,7 @@ export default async (ctx: actions.Context, next: actions.Next) => {
if (!user) { if (!user) {
ctx.throw(401, 'Unauthorized'); ctx.throw(401, 'Unauthorized');
} }
if (!Password.verify(values.password, user.password)) { if (!PASSWORD.verify(values.password, user.password)) {
ctx.throw(401, 'Unauthorized'); ctx.throw(401, 'Unauthorized');
} }
if (!user.token) { if (!user.token) {

View File

@ -2,7 +2,7 @@ import qs from 'qs';
import compose from 'koa-compose'; import compose from 'koa-compose';
import { pathToRegexp } from 'path-to-regexp'; import { pathToRegexp } from 'path-to-regexp';
import Resourcer, { getNameByParams, KoaMiddlewareOptions, parseRequest, ResourcerContext } from '@nocobase/resourcer'; import Resourcer, { getNameByParams, KoaMiddlewareOptions, parseRequest, ResourcerContext } from '@nocobase/resourcer';
import Database, { BelongsTo, BelongsToMany, HasMany, HasOne } from '@nocobase/database'; import Database, { BELONGSTO, BELONGSTOMANY, HASMANY, HASONE } from '@nocobase/database';
interface MiddlewareOptions extends KoaMiddlewareOptions { interface MiddlewareOptions extends KoaMiddlewareOptions {
resourcer?: Resourcer; resourcer?: Resourcer;
@ -49,18 +49,18 @@ export function middleware(options: MiddlewareOptions = {}) {
} }
if (database.isDefined(tableName)) { if (database.isDefined(tableName)) {
const table = database.getTable(tableName); const table = database.getTable(tableName);
const field = table.getField(names[0]) as BelongsTo | HasMany | BelongsToMany | HasOne; const field = table.getField(names[0]) as BELONGSTO | HASMANY | BELONGSTOMANY | HASONE;
if (names.length == 0 || field) { if (names.length == 0 || field) {
let resourceType = 'single'; let resourceType = 'single';
let actions = {}; let actions = {};
if (field) { if (field) {
if (field instanceof HasOne) { if (field instanceof HASONE) {
resourceType = 'hasOne'; resourceType = 'hasOne';
} else if (field instanceof HasMany) { } else if (field instanceof HASMANY) {
resourceType = 'hasMany'; resourceType = 'hasMany';
} else if (field instanceof BelongsTo) { } else if (field instanceof BELONGSTO) {
resourceType = 'belongsTo'; resourceType = 'belongsTo';
} else if (field instanceof BelongsToMany) { } else if (field instanceof BELONGSTOMANY) {
resourceType = 'belongsToMany'; resourceType = 'belongsToMany';
} }
if (field.options.actions) { if (field.options.actions) {