Skip to content

Commit

Permalink
feat: allow to search strings that contain a word
Browse files Browse the repository at this point in the history
  • Loading branch information
dziraf committed Jul 31, 2023
1 parent 41eb9bf commit f861272
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
48 changes: 36 additions & 12 deletions src/Resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type { Knex } from 'knex';

import { ResourceMetadata } from './metadata/index.js';
import { Property } from './Property.js';
import { DatabaseDialect } from './dialects/index.js';

export class Resource extends BaseResource {
static override isAdapterFor(resource: any): boolean {
Expand All @@ -23,6 +24,8 @@ export class Resource extends BaseResource {

private knex: Knex;

private dialect: DatabaseDialect;

private propertyMap = new Map<string, Property>();

private tableName: string;
Expand All @@ -46,6 +49,7 @@ export class Resource extends BaseResource {
this.propertyMap.set(p.path(), p);
});
this.idColumn = info.idProperty.path();
this.dialect = info.dialect;
}

override databaseName(): string {
Expand Down Expand Up @@ -100,13 +104,17 @@ export class Resource extends BaseResource {
}

override async findOne(id: string): Promise<BaseRecord | null> {
const knex = this.schemaName ? this.knex(this.tableName).withSchema(this.schemaName) : this.knex(this.tableName);
const knex = this.schemaName
? this.knex(this.tableName).withSchema(this.schemaName)
: this.knex(this.tableName);
const res = await knex.where(this.idColumn, id);
return res[0] ? this.build(res[0]) : null;
}

override async findMany(ids: (string | number)[]): Promise<BaseRecord[]> {
const knex = this.schemaName ? this.knex(this.tableName).withSchema(this.schemaName) : this.knex(this.tableName);
const knex = this.schemaName
? this.knex(this.tableName).withSchema(this.schemaName)
: this.knex(this.tableName);
const res = await knex.whereIn(this.idColumn, ids);
return res.map((r) => this.build(r));
}
Expand All @@ -116,7 +124,9 @@ export class Resource extends BaseResource {
}

override async create(params: Record<string, any>): Promise<ParamsType> {
const knex = this.schemaName ? this.knex(this.tableName).withSchema(this.schemaName) : this.knex(this.tableName);
const knex = this.schemaName
? this.knex(this.tableName).withSchema(this.schemaName)
: this.knex(this.tableName);
await knex.insert(params);

return params;
Expand All @@ -126,25 +136,30 @@ export class Resource extends BaseResource {
id: string,
params: Record<string, any>,
): Promise<ParamsType> {
const knex = this.schemaName ? this.knex.withSchema(this.schemaName) : this.knex;
const knex = this.schemaName
? this.knex.withSchema(this.schemaName)
: this.knex;

await knex
.from(this.tableName)
.update(params)
.where(this.idColumn, id);
await knex.from(this.tableName).update(params).where(this.idColumn, id);

const knexQb = this.schemaName ? this.knex(this.tableName).withSchema(this.schemaName) : this.knex(this.tableName);
const knexQb = this.schemaName
? this.knex(this.tableName).withSchema(this.schemaName)
: this.knex(this.tableName);
const [row] = await knexQb.where(this.idColumn, id);
return row;
}

override async delete(id: string): Promise<void> {
const knex = this.schemaName ? this.knex.withSchema(this.schemaName) : this.knex;
const knex = this.schemaName
? this.knex.withSchema(this.schemaName)
: this.knex;
await knex.from(this.tableName).delete().where(this.idColumn, id);
}

private filterQuery(filter: Filter | undefined): Knex.QueryBuilder {
const knex = this.schemaName ? this.knex(this.tableName).withSchema(this.schemaName) : this.knex(this.tableName);
const knex = this.schemaName
? this.knex(this.tableName).withSchema(this.schemaName)
: this.knex(this.tableName);
const q = knex;

if (!filter) {
Expand All @@ -154,8 +169,17 @@ export class Resource extends BaseResource {
const { filters } = filter;

Object.entries(filters ?? {}).forEach(([key, filter]) => {
if (typeof filter.value === 'object') {
if (
typeof filter.value === 'object'
&& ['date', 'datetime'].includes(filter.property.type())
) {
q.whereBetween(key, [filter.value.from, filter.value.to]);
} else if (filter.property.type() === 'string') {
if (this.dialect === 'postgresql') {
q.whereILike(key, `%${filter.value}%`);
} else {
q.whereLike(key, `%${filter.value}%`);
}
} else {
q.where(key, filter.value);
}
Expand Down
1 change: 1 addition & 0 deletions src/metadata/ResourceMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ export class ResourceMetadata {
}

this.idProperty = idProperty;
this.dialect = dialect;
}
}

0 comments on commit f861272

Please sign in to comment.