Skip to content

Commit

Permalink
fixed api prefilter
Browse files Browse the repository at this point in the history
  • Loading branch information
noam-honig committed Jun 19, 2023
1 parent 5341348 commit 6892ae9
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,10 @@

All notable changes to this project will be documented in this file.

## [0.20.6] - 2023-06-19

- Fixed an issue when apiPrefilter was an arrow function, it did not affect get of a specific resource

## [0.20.5] - 2023-06-11

- fixed issue #200 transactions on mongo db
Expand Down
14 changes: 11 additions & 3 deletions misc/dts-compare/remult-mongo.d.ts
@@ -1,11 +1,19 @@
import type { MongoClient, Db } from 'mongodb';
import type { MongoClient, Db, ClientSession } from 'mongodb';
import { DataProvider, EntityDataProvider, EntityFilter, EntityMetadata, Remult } from '.';
import { RepositoryOverloads } from './src/remult3';
export declare class MongoDataProvider implements DataProvider {
private db;
private client;
constructor(db: Db, client: MongoClient);
static getDb(remult?: Remult): Db;
constructor(db: Db, client: MongoClient, options?: {
session?: ClientSession;
disableTransactions?: boolean;
});
session?: ClientSession;
disableTransactions: boolean;
static getDb(remult?: Remult): {
db: Db;
session: ClientSession;
};
getEntityDataProvider(entity: EntityMetadata<any>): EntityDataProvider;
transaction(action: (dataProvider: DataProvider) => Promise<void>): Promise<void>;
static filterToRaw<entityType>(entity: RepositoryOverloads<entityType>, condition: EntityFilter<entityType>): Promise<{
Expand Down
1 change: 1 addition & 0 deletions misc/dts-compare/src/shared-tests/db-tests-setup.d.ts
Expand Up @@ -13,6 +13,7 @@ export declare function testInMemory(key: string, what: dbTestWhatSignature, foc
export declare const TestDbs: {
restDataProvider: string;
mongo: string;
inMemory: string;
};
export declare type dbTestWhatSignature = ((db: {
db: DataProvider;
Expand Down
10 changes: 8 additions & 2 deletions projects/core/src/data-api.ts
Expand Up @@ -195,10 +195,16 @@ export class DataApi<T = any> {
private async doOnId(response: DataApiResponse, id: any, what: (row: T) => Promise<void>) {
try {


var where: EntityFilter<any>[] = [this.repository.metadata.idMetadata.getIdFilter(id)];
if (this.repository.metadata.options.apiPrefilter) {
if (typeof this.repository.metadata.options.apiPrefilter === "function")
where.push(await this.repository.metadata.options.apiPrefilter());
else
where.push(this.repository.metadata.options.apiPrefilter);
}

await this.repository.find({
where: { $and: [this.repository.metadata.options.apiPrefilter, this.repository.metadata.idMetadata.getIdFilter(id)] } as EntityFilter<any>
where: { $and: where } as EntityFilter<any>
})
.then(async r => {
if (r.length == 0)
Expand Down
15 changes: 15 additions & 0 deletions projects/core/src/tests/test-data-api/predefined-filter.spec.ts
Expand Up @@ -131,6 +131,21 @@ describe("data api", () => {
await api.put(t, 1, { name: 'YAEL' });
d.test();
});
it("put id 1 works with predefined filterand shouldnt return anything", async () => {
let [c, remult] = await createData(async (i) => {
await i(1, 'noam', 'a');
await i(2, 'yael', 'b');
await i(3, 'yoni', 'a');
}, CategoriesForThisTest2);
var api = new DataApi(c, remult);
let t = new TestDataApiResponse();
let d = new Done();
t.notFound = () => {
d.ok();
};
await api.put(t, 1, { name: 'YAEL' });
d.test();
});
it("getArray works with predefined filter", async () => {
let [c, remult] = await createData(async (i) => {
await i(1, 'noam', 'a');
Expand Down
Expand Up @@ -42,7 +42,11 @@ export class ProductsComponent {

}

@Entity("categories", { allowApiRead: false })
@Entity<Category>("categories", {
allowApiCrud: true, apiPrefilter: () => ({
id: { $ne: "clj30u9o500000kr3956ph9ep" }
})
})
export class Category {
@Fields.cuid()
id = ''
Expand Down

0 comments on commit 6892ae9

Please sign in to comment.