Skip to content

Commit

Permalink
feat: plugin-mock-collections (nocobase#2988)
Browse files Browse the repository at this point in the history
* feat: plugin-mock-collections

* fix: mock bug

* feat: field interfaces

* fix: field interface

* fix: formula

* fix: file collection

* fix: map

* refactor: change api path from :create to :mock

* fix: avoid test failed

* chore: remove useless code

* fix: mock records

* fix: association

* feat: custom data

* fix: mockAttachment

* fix: count

---------

Co-authored-by: Rain <[email protected]>
  • Loading branch information
chenos and zhangzhonghe authored Nov 10, 2023
1 parent ea4b88e commit 3b5b732
Show file tree
Hide file tree
Showing 56 changed files with 2,250 additions and 25 deletions.
3 changes: 3 additions & 0 deletions .env.e2e.example
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ APP_KEY=test-key-e2e
SOCKET_PATH=storage/gateway-e2e.sock
__E2E__=true

# 启用 mock-collections 插件
APPEND_PRESET_BUILT_IN_PLUGINS=mock-collections

API_BASE_PATH=/api/
API_BASE_URL=

Expand Down
1 change: 1 addition & 0 deletions packages/core/client/src/__tests__/e2e/menu.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ test.describe('menu page', () => {
await page.getByLabel(pageTitle).hover();
await page.getByLabel(pageTitle).getByLabel('designer-schema-settings').hover();
await page.getByLabel('Edit').click();
await page.mouse.move(300, 0);
await page.getByRole('textbox').click();
await page.getByRole('textbox').fill(newPagetitle);
await page.getByRole('button', { name: 'OK' }).click();
Expand Down
23 changes: 13 additions & 10 deletions packages/core/database/src/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ import lodash from 'lodash';
import {
Association,
BulkCreateOptions,
ModelStatic,
Op,
Sequelize,
FindAndCountOptions as SequelizeAndCountOptions,
CountOptions as SequelizeCountOptions,
CreateOptions as SequelizeCreateOptions,
DestroyOptions as SequelizeDestroyOptions,
FindAndCountOptions as SequelizeAndCountOptions,
FindOptions as SequelizeFindOptions,
ModelStatic,
Op,
Sequelize,
Transactionable,
UpdateOptions as SequelizeUpdateOptions,
Transactionable,
WhereOperators,
} from 'sequelize';
import { Collection } from './collection';
Expand Down Expand Up @@ -217,6 +217,7 @@ export interface AggregateOptions {
interface FirstOrCreateOptions extends Transactionable {
filterKeys: string[];
values?: Values;
hooks?: boolean;
}

export class Repository<TModelAttributes extends {} = any, TCreationAttributes extends {} = TModelAttributes> {
Expand Down Expand Up @@ -459,7 +460,7 @@ export class Repository<TModelAttributes extends {} = any, TCreationAttributes e
* Get the first record matching the attributes or create it.
*/
async firstOrCreate(options: FirstOrCreateOptions) {
const { filterKeys, values, transaction } = options;
const { filterKeys, values, transaction, hooks } = options;
const filter = Repository.valuesToFilter(values, filterKeys);

const instance = await this.findOne({ filter, transaction });
Expand All @@ -468,24 +469,26 @@ export class Repository<TModelAttributes extends {} = any, TCreationAttributes e
return instance;
}

return this.create({ values, transaction });
return this.create({ values, transaction, hooks });
}

async updateOrCreate(options: FirstOrCreateOptions) {
const { filterKeys, values, transaction } = options;
const { filterKeys, values, transaction, hooks } = options;
const filter = Repository.valuesToFilter(values, filterKeys);

const instance = await this.findOne({ filter, transaction });

if (instance) {
console.log(filter, instance.toJSON(), instance.get(this.collection.model.primaryKeyAttribute));
return await this.update({
filterByTk: instance.get(this.collection.model.primaryKeyAttribute),
filterByTk: instance.get(this.collection.filterTargetKey || this.collection.model.primaryKeyAttribute),
values,
transaction,
hooks,
});
}

return this.create({ values, transaction });
return this.create({ values, transaction, hooks });
}

/**
Expand Down
17 changes: 2 additions & 15 deletions packages/core/test/src/client/e2eUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,24 +381,11 @@ const createCollections = async (collectionSettings: CollectionSetting | Collect

const state = await api.storageState();
const headers = getHeaders(state);
// const defaultCollectionSetting: Partial<CollectionSetting> = {
// template: 'general',
// logging: true,
// autoGenId: true,
// createdBy: true,
// updatedBy: true,
// createdAt: true,
// updatedAt: true,
// sortable: true,
// view: false,
// };

collectionSettings = Array.isArray(collectionSettings) ? collectionSettings : [collectionSettings];

const result = await api.post(`/api/collections:create`, {
const result = await api.post(`/api/collections:mock`, {
headers,
// data: collectionSettings.map((item) => Object.assign(defaultCollectionSetting, item)),
data: collectionSettings.filter((item) => !['users', 'roles'].includes(item.name)),
data: collectionSettings,
});

if (!result.ok()) {
Expand Down
2 changes: 2 additions & 0 deletions packages/plugins/@nocobase/plugin-mock-collections/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/node_modules
/src
Loading

0 comments on commit 3b5b732

Please sign in to comment.