Skip to content

Commit

Permalink
Definition of object type added
Browse files Browse the repository at this point in the history
  • Loading branch information
ackava committed Dec 3, 2024
1 parent babc300 commit 57a42e3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
4 changes: 3 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
node_modules/
node_modules/
src/tests
dist/tests
12 changes: 11 additions & 1 deletion src/model/EntityQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ export default class EntityQuery<T = any>
}
}

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
async updateSelect(p?, f?): Promise<T[]> {
const updateStatement = this.getUpdateStatement(p, f, true);

Expand Down Expand Up @@ -350,6 +352,8 @@ export default class EntityQuery<T = any>
return updateStatement;
}

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
async toArray(): Promise<T[]> {
const results: T[] = [];
for await (const iterator of this.enumerate()) {
Expand All @@ -358,6 +362,8 @@ export default class EntityQuery<T = any>
return results;
}

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
async *enumerate(): AsyncGenerator<T, any, unknown> {

await using scope = new AsyncDisposableScope();
Expand Down Expand Up @@ -451,6 +457,8 @@ export default class EntityQuery<T = any>
}
}

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
async firstOrFail(errorMessage = `No records found for ${this.type?.name || "Table"}`): Promise<T> {
const first = await this.first();
if (first) {
Expand All @@ -459,7 +467,9 @@ export default class EntityQuery<T = any>
throw new Error(errorMessage);
}

async first(): Promise<T> {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
async first(): Promise<T>{
for await(const iterator of this.limit(1).enumerate()) {
return iterator;
}
Expand Down
10 changes: 5 additions & 5 deletions src/model/IFilterWithParameter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ export type IFilterExpression<P = any, T = any> = [input: P, x: (p: P) => (s: T)
export type IFieldsAsNumbers<T> = { [P in keyof T]: number };

export interface IBaseQuery<T> {
enumerate(): AsyncGenerator<T>;
enumerate(): T extends object ? AsyncGenerator<T> : never;

firstOrFail(errorMessage?: string): Promise<T>;
first(): Promise<T>;
firstOrFail(errorMessage?: string): T extends object ? Promise<T> : never;
first(): T extends object ? Promise<T> : never;

some(): Promise<boolean>;

select<P, TR>(parameters: P, fx: (p: P) => (x: T) => TR): IBaseQuery<TR>;
map<P, TR>(parameters: P, fx: (p: P) => (x: T) => TR): IBaseQuery<TR>;

toArray(): Promise<T[]>;
toArray(): T extends object ? Promise<T[]> : never;

toQuery(): { text: string, values: any[]};

Expand All @@ -42,7 +42,7 @@ export interface IBaseQuery<T> {
include<TR>(fx: (x: T) => TR | TR[]): IBaseQuery<T>;

update<P>(parameters: P, fx: (p: P) => (x:T) => Partial<T>): Promise<number>;
updateSelect<P>(parameters: P, fx: (p: P) => (x:T) => Partial<T>): Promise<T[]>;
updateSelect<P>(parameters: P, fx: (p: P) => (x:T) => Partial<T>): T extends object ? Promise<T[]> : never;

/**
* Warning !! Be careful, this will delete rows from the database and neither soft delete nor any other events will be invoked.
Expand Down
5 changes: 5 additions & 0 deletions src/tests/db-tests/tests/select-items-sum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ export default async function(this: TestConfig) {

assert.notEqual(null, report);

// const all = await context.orders.asQuery().map(void 0, (p) => (x) => x.total)
// .first();

// assert.notEqual(null, all);

// const r = await context.users.all()
// .trace(console.log)
// .sum(void 0, (p) => (x) => ({
Expand Down

0 comments on commit 57a42e3

Please sign in to comment.