test: load through collection with primaryKeys (#3093)
This commit is contained in:
parent
f4df696bfa
commit
fd454d78b3
@ -20,6 +20,106 @@ describe('collections repository', () => {
|
||||
await app.destroy();
|
||||
});
|
||||
|
||||
it('should load through table with foreignKey', async () => {
|
||||
await Collection.repository.create({
|
||||
values: {
|
||||
name: 'postsTags',
|
||||
autoGenId: false,
|
||||
fields: [
|
||||
{
|
||||
type: 'integer',
|
||||
name: 'postId',
|
||||
primaryKey: true,
|
||||
},
|
||||
{
|
||||
type: 'integer',
|
||||
name: 'tagId',
|
||||
primaryKey: true,
|
||||
},
|
||||
{
|
||||
type: 'boolean',
|
||||
name: 'default',
|
||||
},
|
||||
],
|
||||
},
|
||||
context: {},
|
||||
});
|
||||
|
||||
await Collection.repository.create({
|
||||
values: {
|
||||
name: 'posts',
|
||||
fields: [
|
||||
{
|
||||
type: 'string',
|
||||
name: 'title',
|
||||
},
|
||||
{
|
||||
type: 'belongsToMany',
|
||||
target: 'tags',
|
||||
name: 'tags',
|
||||
through: 'postsTags',
|
||||
foreignKey: 'postId',
|
||||
otherKey: 'tagId',
|
||||
sourceKey: 'id',
|
||||
targetKey: 'id',
|
||||
},
|
||||
],
|
||||
},
|
||||
context: {},
|
||||
});
|
||||
|
||||
await Collection.repository.create({
|
||||
values: {
|
||||
name: 'tags',
|
||||
fields: [
|
||||
{
|
||||
type: 'string',
|
||||
name: 'name',
|
||||
},
|
||||
{
|
||||
type: 'belongsToMany',
|
||||
target: 'posts',
|
||||
name: 'posts',
|
||||
through: 'postsTags',
|
||||
foreignKey: 'tagId',
|
||||
otherKey: 'postId',
|
||||
sourceKey: 'id',
|
||||
targetKey: 'id',
|
||||
},
|
||||
],
|
||||
},
|
||||
context: {},
|
||||
});
|
||||
|
||||
const postsCollection = db.getCollection('posts');
|
||||
|
||||
const p1 = await postsCollection.repository.create({
|
||||
values: {
|
||||
title: 'test',
|
||||
tags: [
|
||||
{
|
||||
name: 't1',
|
||||
},
|
||||
{
|
||||
name: 't2',
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
|
||||
const throughCollection = db.getCollection('postsTags');
|
||||
console.log(throughCollection.model.primaryKeyAttributes);
|
||||
|
||||
await throughCollection.repository.update({
|
||||
filter: {
|
||||
postId: p1.get('id'),
|
||||
},
|
||||
values: {
|
||||
default: true,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('should create collection with sortable option', async () => {
|
||||
await Collection.repository.create({
|
||||
values: {
|
||||
|
@ -62,14 +62,13 @@ export class CollectionRepository extends Repository {
|
||||
// sort graph nodes
|
||||
const sortedNames = graphlib.alg.topsort(graph);
|
||||
|
||||
const lazyCollectionFields: {
|
||||
[key: string]: Array<string>;
|
||||
} = {};
|
||||
const lazyCollectionFields = new Map<string, Array<string>>();
|
||||
|
||||
for (const instanceName of sortedNames) {
|
||||
if (!nameMap[instanceName]) continue;
|
||||
|
||||
// skip load collection field
|
||||
// can be a true value or an array of field names
|
||||
const skipField = (() => {
|
||||
// skip load collection field if collection is view
|
||||
if (viewCollections.includes(instanceName)) {
|
||||
@ -88,7 +87,7 @@ export class CollectionRepository extends Repository {
|
||||
})();
|
||||
|
||||
if (lodash.isArray(skipField) && skipField.length) {
|
||||
lazyCollectionFields[instanceName] = skipField;
|
||||
lazyCollectionFields.set(instanceName, skipField);
|
||||
}
|
||||
|
||||
this.database.logger.debug(`load ${instanceName} collection`);
|
||||
@ -105,7 +104,7 @@ export class CollectionRepository extends Repository {
|
||||
}
|
||||
|
||||
// load lazy collection field
|
||||
for (const [collectionName, skipField] of Object.entries(lazyCollectionFields)) {
|
||||
for (const [collectionName, skipField] of lazyCollectionFields) {
|
||||
this.database.logger.debug(`load ${collectionName} collection fields`);
|
||||
this.app.setMaintainingMessage(`load ${collectionName} collection fields`);
|
||||
await nameMap[collectionName].loadFields({ includeFields: skipField });
|
||||
|
Loading…
Reference in New Issue
Block a user