Skip to content

Commit

Permalink
Release 9.2.4
Browse files Browse the repository at this point in the history
* Fix PromiseLike signatures. Remove ChainablePromiseLike
  • Loading branch information
jgeurts committed Dec 28, 2021
1 parent 15d62b7 commit 59c2f94
Show file tree
Hide file tree
Showing 15 changed files with 222 additions and 36 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## 9.2.4 - 2021-12-28

- Fix PromiseLike signatures. Remove ChainablePromiseLike

## 9.2.3 - 2021-12-27

- Update npms
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bigal",
"version": "9.2.3",
"version": "9.2.4",
"description": "A fast and lightweight orm for postgres and node.js, written in typescript.",
"main": "index.js",
"types": "index.d.ts",
Expand Down
4 changes: 0 additions & 4 deletions src/ChainablePromiseLike.ts

This file was deleted.

36 changes: 20 additions & 16 deletions src/ReadonlyRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,13 @@ export class ReadonlyRepository<T extends Entity> implements IReadonlyRepository

return this as FindOneResult<T, Omit<QueryResult<T>, TProperty> & PickAsType<T, TProperty, TValue>>;
},
async then(resolve: (result: QueryResult<T> | null) => Promise<QueryResult<T>> | QueryResult<T> | null, reject: (err: Error) => void): Promise<QueryResult<T> | null> {
async then<TResult = QueryResult<T> | null, TErrorResult = void>(
resolve: (result: QueryResult<T> | null) => PromiseLike<TResult> | TResult,
reject: (error: Error) => PromiseLike<TErrorResult> | TErrorResult,
): Promise<TErrorResult | TResult> {
try {
if (_.isString(where)) {
throw new Error('The query cannot be a string, it must be an object');
return await reject(new Error('The query cannot be a string, it must be an object'));
}

const { query, params } = getSelectQueryAndParams({
Expand Down Expand Up @@ -239,9 +242,7 @@ export class ReadonlyRepository<T extends Entity> implements IReadonlyRepository
typedException.stack = stack;
}

reject(typedException);

return null;
return reject(typedException);
}
},
};
Expand Down Expand Up @@ -401,11 +402,13 @@ export class ReadonlyRepository<T extends Entity> implements IReadonlyRepository
const safePage = Math.max(page, 1);
return this.skip(safePage * paginateLimit - paginateLimit).limit(paginateLimit);
},
async then(resolve: (result: QueryResult<T>[]) => QueryResult<T>[], reject: (err: Error) => void): Promise<QueryResult<T>[]> {
async then<TResult = QueryResult<T>[], TErrorResult = void>(
resolve: (result: QueryResult<T>[]) => PromiseLike<TResult> | TResult,
reject: (error: Error) => PromiseLike<TErrorResult> | TErrorResult,
): Promise<TErrorResult | TResult> {
try {
if (_.isString(where)) {
reject(new Error('The query cannot be a string, it must be an object'));
return [];
return await reject(new Error('The query cannot be a string, it must be an object'));
}

const { query, params } = getSelectQueryAndParams({
Expand All @@ -426,7 +429,7 @@ export class ReadonlyRepository<T extends Entity> implements IReadonlyRepository
await modelInstance.populateFields(entities, populates);
}

return resolve(entities);
return await resolve(entities);
} catch (ex) {
const typedException = ex as Error;
if (typedException.stack) {
Expand All @@ -435,8 +438,7 @@ export class ReadonlyRepository<T extends Entity> implements IReadonlyRepository
typedException.stack = stack;
}

reject(typedException);
return [];
return reject(typedException);
}
},
};
Expand Down Expand Up @@ -464,7 +466,10 @@ export class ReadonlyRepository<T extends Entity> implements IReadonlyRepository

return this;
},
async then(resolve: (result: number) => number, reject: (err: Error) => void): Promise<number> {
async then<TResult = number, TErrorResult = void>(
resolve: (result: number) => PromiseLike<TResult> | TResult,
reject: (error: Error) => PromiseLike<TErrorResult> | TErrorResult,
): Promise<TErrorResult | TResult> {
try {
const { query, params } = getCountQueryAndParams({
repositoriesByModelNameLowered: modelInstance._repositoriesByModelNameLowered,
Expand All @@ -475,7 +480,7 @@ export class ReadonlyRepository<T extends Entity> implements IReadonlyRepository
const result = await modelInstance._pool.query<{ count: string }>(query, params);

const originalValue = result.rows[0].count;
return resolve(Number(originalValue));
return await resolve(Number(originalValue));
} catch (ex) {
const typedException = ex as Error;
if (typedException.stack) {
Expand All @@ -484,8 +489,7 @@ export class ReadonlyRepository<T extends Entity> implements IReadonlyRepository
typedException.stack = stack;
}

reject(typedException);
return 0;
return reject(typedException);
}
},
};
Expand Down Expand Up @@ -540,7 +544,7 @@ export class ReadonlyRepository<T extends Entity> implements IReadonlyRepository

protected _buildInstances(rows: Partial<QueryResult<T>>[]): QueryResult<T>[] {
if (_.isNil(rows)) {
return rows;
return [];
}

return rows.map((row: Partial<QueryResult<T>>) => this._buildInstance(row));
Expand Down
9 changes: 6 additions & 3 deletions src/Repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,10 @@ export class Repository<T extends Entity> extends ReadonlyRepository<T> implemen

return this;
},
async then(resolve: (result: QueryResult<T>[] | void) => QueryResult<T>[] | void, reject: (err: Error) => void): Promise<QueryResult<T>[] | void> {
async then<TResult = QueryResult<T>[], TErrorResult = void>(
resolve: (result: QueryResult<T>[] | void) => PromiseLike<TResult> | TResult,
reject: (error: Error) => PromiseLike<TErrorResult> | TErrorResult,
): Promise<TErrorResult | TResult> {
if (_.isString(where)) {
return reject(new Error('The query cannot be a string, it must be an object'));
}
Expand All @@ -239,10 +242,10 @@ export class Repository<T extends Entity> extends ReadonlyRepository<T> implemen
const result = await modelInstance._pool.query<Partial<QueryResult<T>>>(query, params);

if (returnRecords) {
return resolve(modelInstance._buildInstances(result.rows));
return await resolve(modelInstance._buildInstances(result.rows));
}

return resolve();
return await resolve();
} catch (ex) {
const typedException = ex as Error;
if (typedException.stack) {
Expand Down
3 changes: 1 addition & 2 deletions src/query/CountResult.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type { ChainablePromiseLike } from '../ChainablePromiseLike';
import type { Entity } from '../Entity';

import type { WhereQuery } from './WhereQuery';

export interface CountResult<TEntity extends Entity> extends ChainablePromiseLike<number> {
export interface CountResult<TEntity extends Entity> extends PromiseLike<number> {
where(args: WhereQuery<TEntity>): CountResult<TEntity> | number;
}
3 changes: 1 addition & 2 deletions src/query/DestroyResult.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type { ChainablePromiseLike } from '../ChainablePromiseLike';
import type { Entity } from '../Entity';

import type { WhereQuery } from './WhereQuery';

export interface DestroyResult<TEntity extends Entity, TReturn> extends ChainablePromiseLike<TReturn> {
export interface DestroyResult<TEntity extends Entity, TReturn> extends PromiseLike<TReturn> {
where(args: WhereQuery<TEntity>): DestroyResult<TEntity, TReturn>;
}
3 changes: 1 addition & 2 deletions src/query/FindOneResult.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import type { ChainablePromiseLike } from '../ChainablePromiseLike';
import type { Entity } from '../Entity';
import type { GetValueType, PickAsPopulated, PickByValueType, PickAsType, QueryResult } from '../types';

import type { PopulateArgs } from './PopulateArgs';
import type { Sort } from './Sort';
import type { WhereQuery } from './WhereQuery';

export interface FindOneResult<T extends Entity, TReturn> extends ChainablePromiseLike<TReturn | null> {
export interface FindOneResult<T extends Entity, TReturn> extends PromiseLike<TReturn | null> {
where(args: WhereQuery<T>): FindOneResult<T, TReturn>;
populate<TProperty extends string & keyof PickByValueType<T, Entity> & keyof T>(
propertyName: TProperty,
Expand Down
3 changes: 1 addition & 2 deletions src/query/FindResult.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { ChainablePromiseLike } from '../ChainablePromiseLike';
import type { Entity } from '../Entity';
import type { PickByValueType, GetValueType, PickAsPopulated } from '../types';

Expand All @@ -7,7 +6,7 @@ import type { PopulateArgs } from './PopulateArgs';
import type { Sort } from './Sort';
import type { WhereQuery } from './WhereQuery';

export interface FindResult<T extends Entity, TReturn> extends ChainablePromiseLike<TReturn[]> {
export interface FindResult<T extends Entity, TReturn> extends PromiseLike<TReturn[]> {
where(args: WhereQuery<T>): FindResult<T, TReturn>;
populate<TProperty extends string & keyof PickByValueType<T, Entity> & keyof T>(
propertyName: TProperty,
Expand Down
29 changes: 29 additions & 0 deletions tests/models/LevelOne.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { column, primaryColumn, table, Entity } from '../../src';

import type { LevelTwo } from './LevelTwo';

@table({
name: 'level_one',
})
export class LevelOne extends Entity {
@primaryColumn({ type: 'string' })
public id!: string;

@column({
type: 'string',
required: true,
})
public one!: string;

@column({
type: 'string',
})
public foo?: string;

@column({
required: true,
model: 'LevelTwo',
name: 'level_two_id',
})
public levelTwo!: LevelTwo | string;
}
20 changes: 20 additions & 0 deletions tests/models/LevelThree.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { column, primaryColumn, table, Entity } from '../../src';

@table({
name: 'level_three',
})
export class LevelThree extends Entity {
@primaryColumn({ type: 'string' })
public id!: string;

@column({
type: 'string',
})
public foo?: string;

@column({
type: 'string',
required: true,
})
public three!: string;
}
29 changes: 29 additions & 0 deletions tests/models/LevelTwo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { column, primaryColumn, table, Entity } from '../../src';

import type { LevelThree } from './LevelThree';

@table({
name: 'level_two',
})
export class LevelTwo extends Entity {
@primaryColumn({ type: 'string' })
public id!: string;

@column({
type: 'string',
required: true,
})
public two!: string;

@column({
type: 'string',
})
public foo?: string;

@column({
required: true,
model: 'LevelThree',
name: 'level_three_id',
})
public levelThree!: LevelThree | string;
}
3 changes: 3 additions & 0 deletions tests/models/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
export * from './Category';
export * from './Classroom';
export * from './KitchenSink';
export * from './LevelOne';
export * from './LevelTwo';
export * from './LevelThree';
export * from './ParkingSpace';
export * from './Product';
export * from './ProductCategory';
Expand Down
Loading

0 comments on commit 59c2f94

Please sign in to comment.