Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
noam-honig committed May 12, 2024
1 parent 68581ce commit c0a6762
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 11 deletions.
5 changes: 4 additions & 1 deletion projects/core/src/remult3/RepositoryImplementation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1433,7 +1433,10 @@ abstract class rowHelperBase<T> {
}
}
}
await promiseAll([...this.fields], (x) => x.load())
await promiseAll(
[...this.fields].filter((f) => !getRelationFieldInfo(f.metadata)),
(x) => x.load(),
)
}
}

Expand Down
8 changes: 5 additions & 3 deletions projects/tests/tests/h.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import { Entity, EntityBase, Field, FieldType, Fields } from '../../core/'
},
})
@Entity<h>('h', {
saving: (self) => {
if (self.refH) self.refHId = self.refH.id
else self.refHId = ''
saving: async (self) => {
if (self.refH) {
await self.$.refH.load()
self.refHId = self.refH.id
} else self.refHId = ''
},
allowApiCrud: true,
})
Expand Down
114 changes: 107 additions & 7 deletions projects/tests/tests/test-many-to-one-relation-in-entity.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import { InMemoryDataProvider } from '../../core/src//data-providers/in-memory-database'
import type { rowHelperImplementation } from '../../core/src/remult3/RepositoryImplementation'
import type { EntityFilter } from '../../core'
import { Entity, EntityBase, Field, Fields, getEntityRef } from '../../core'
import {
Entity,
EntityBase,
Field,
Fields,
getEntityRef,
Relations,
} from '../../core'
import { Remult } from '../../core/src/context'

import {
entityFilterToJson,
Filter,
} from '../../core/src/filter/filter-interfaces'


import { DataApi } from '../../core/src/data-api'

import { afterEach, beforeEach, describe, expect, it } from 'vitest'
Expand All @@ -18,6 +24,7 @@ import { Categories, Language, Products } from './entities-for-tests'
import { h } from './h'
import { TestDataApiResponse } from './TestDataApiResponse'
import { actionInfo } from '../../core/internals'
import { entity } from './dynamic-classes.js'

@Entity('products')
class ProductsEager extends EntityBase {
Expand Down Expand Up @@ -284,11 +291,11 @@ describe('many to one relation', () => {
await c.save()
await p.save()
expect(p.category).toBeNull()
; (
p._ as unknown as rowHelperImplementation<Products>
)._updateEntityBasedOnApi({
category: 1,
})
;(
p._ as unknown as rowHelperImplementation<Products>
)._updateEntityBasedOnApi({
category: 1,
})
expect(p.$.category.inputValue).toBe('1')
await p.$.category.load()
expect(p.category.id).toBe(c.id)
Expand Down Expand Up @@ -972,3 +979,96 @@ describe('Test many to one without active record', () => {
expect(getEntityRef(t).fields.contact.getId()).toBe(c.id)
})
})
describe('test api loading stuff', () => {
const category = entity('category', {
id: Fields.integer(),
})
const task = entity(
'tasks',
{
id: Fields.integer(),
categoryId: Fields.integer(),
category: Relations.toOne(() => category, 'categoryId'),
},
{
allowApiCrud: true,
},
)

it('test api doesnt load too much', async () => {
let mem = new InMemoryDataProvider()
let c = new Remult()
c.dataProvider = mem

await c.repo(category).insert([{ id: 1 }, { id: 2 }])
await c.repo(task).insert({ id: 11, categoryId: 1 })
c = new Remult() //clear the cache;
c.dataProvider = mem
let fetches = []
c.dataProvider = {
transaction: undefined,
getEntityDataProvider: (e) => {
let r = mem.getEntityDataProvider(e)
return {
find: (x) => {
fetches.push(x.where.toJson().id)
return r.find(x)
},
count: (...args) => r.count(...args),
delete: (...args) => r.delete(...args),
insert: (...args) => r.insert(...args),
update: (...args) => r.update(...args),
}
},
}
let api = new DataApi(c.repo(task), c)
let t = new TestDataApiResponse()
let done = new Done()
t.success = (d) => {
expect(d.id).toBe(11)
expect(d.categoryId).toBe(2)
expect(fetches).toEqual([11])
done.ok()
}
await api.put(t, 11, { categoryId: 2 })
done.test()
})
it('test api doesnt load too much on insert', async () => {
let mem = new InMemoryDataProvider()
let c = new Remult()
c.dataProvider = mem

await c.repo(category).insert([{ id: 1 }, { id: 2 }])

c = new Remult() //clear the cache;
c.dataProvider = mem
let fetches = []
c.dataProvider = {
transaction: undefined,
getEntityDataProvider: (e) => {
let r = mem.getEntityDataProvider(e)
return {
find: (x) => {
fetches.push(x.where.toJson().id)
return r.find(x)
},
count: (...args) => r.count(...args),
delete: (...args) => r.delete(...args),
insert: (...args) => r.insert(...args),
update: (...args) => r.update(...args),
}
},
}
let api = new DataApi(c.repo(task), c)
let t = new TestDataApiResponse()
let done = new Done()
t.created = (d) => {
expect(d.id).toBe(11)
expect(d.categoryId).toBe(1)
expect(fetches).toEqual([])
done.ok()
}
await api.post(t, { id: 11, categoryId: 1 })
done.test()
})
})

0 comments on commit c0a6762

Please sign in to comment.