Skip to content

Commit

Permalink
Merge branch 'Dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
Arteha committed Oct 31, 2019
2 parents a1cf9f1 + 1e4d6fc commit c266917
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 31 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "admin-bro-typeorm",
"version": "0.1.3",
"version": "0.1.4",
"description": "TypeORM adapter for AdminBro",
"keywords": [
"typeorm",
Expand Down
16 changes: 13 additions & 3 deletions src/ExtendedRecord.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
import { BaseEntity } from "typeorm";
import { Resource } from "./Resource";

const {BaseRecord} = require("admin-bro");
const { BaseRecord } = require("admin-bro");

const objectObject = ({}).toString();

export class ExtendedRecord extends (BaseRecord as any)
{
private _title: string | null;
private patchedParams: Object = {};

constructor(instance: BaseEntity | null | undefined, resource: Resource)
{
super(instance, resource);

if(instance)
if (instance)
{
for (const n in instance)
{
const value = instance[ n ];
if (value instanceof Object && !(value instanceof Date))
this.patchedParams[ n ] = JSON.stringify(value);
}

const title = instance.toString();
this._title = title != objectObject ? title : null;
}
}

public title(): string
{
if(this._title != null)
if (this._title != null)
return this._title;
else
return super.title();
Expand All @@ -31,6 +39,8 @@ export class ExtendedRecord extends (BaseRecord as any)
public toJSON(...args: any[]): Object
{
const obj = super.toJSON(args);
for(let p in this.patchedParams)
obj.params[p] = this.patchedParams[p];
obj.title = this.title();
return obj;
}
Expand Down
70 changes: 43 additions & 27 deletions src/Resource.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {Property} from "./Property";
import {BaseEntity, Repository} from "typeorm";
import {convertFilter} from "./utils/convertFilter";
import {ExtendedRecord} from "./ExtendedRecord";
import { Property } from "./Property";
import { BaseEntity, Repository } from "typeorm";
import { convertFilter } from "./utils/convertFilter";
import { ExtendedRecord } from "./ExtendedRecord";

const {BaseResource, ValidationError} = require("admin-bro");
const { BaseResource, ValidationError } = require("admin-bro");

export class Resource extends (BaseResource as any)
{
Expand Down Expand Up @@ -41,12 +41,12 @@ export class Resource extends (BaseResource as any)

public properties()
{
return [...this.propsArray];
return [ ...this.propsArray ];
}

public property(path: string): Property
public property(path: string): Property | null
{
return this.propsObject[path];
return this.propsObject[ path ] || null;
}

public async count(filter)
Expand All @@ -58,35 +58,35 @@ export class Resource extends (BaseResource as any)

public async populate(baseRecords, property: Property)
{
const fks: Array<any> = baseRecords.map(baseRecord => baseRecord.params[property.name()]);
const fks: Array<any> = baseRecords.map(baseRecord => baseRecord.params[ property.name() ]);

const instances = await this.model.findByIds(fks);
const instancesRecord: Record<string, BaseEntity> = {};
for (const instance of instances)
{
if(instance.hasId())
instancesRecord[(instance as any).id] = instance;
if (instance.hasId())
instancesRecord[ (instance as any).id ] = instance;
}

baseRecords.forEach((baseRecord) =>
{
const fk = baseRecord.params[property.name()];
const instance = instancesRecord[fk];
if(instance)
baseRecord.populated[property.name()] = new ExtendedRecord(instance, this);
const fk = baseRecord.params[ property.name() ];
const instance = instancesRecord[ fk ];
if (instance)
baseRecord.populated[ property.name() ] = new ExtendedRecord(instance, this);
});
return baseRecords;
}

public async find(filter, {limit = 10, offset = 0, sort = {}})
public async find(filter, { limit = 10, offset = 0, sort = {} })
{
const {direction, sortBy} = sort as any;
const { direction, sortBy } = sort as any;
const instances = await this.model.find({
where: convertFilter(filter),
take: limit,
skip: offset,
order: {
[sortBy]: (direction || "asc").toUpperCase()
[ sortBy ]: (direction || "asc").toUpperCase()
}
});
return instances.map(instance => new ExtendedRecord(instance, this));
Expand All @@ -104,18 +104,19 @@ export class Resource extends (BaseResource as any)

public async create(params: any): Promise<any>
{
const instance = await this.model.create(params);
const instance = await this.model.create(this.prepareParams(params));
await instance.save();
return instance;
}

public async update(pk, params: any = {})
{
const instance = await this.model.findOne(pk);
if(instance)
if (instance)
{
params = this.prepareParams(params);
for (const p in params)
instance[p] = params[p];
instance[ p ] = params[ p ];

await instance.save();
return instance;
Expand All @@ -132,8 +133,8 @@ export class Resource extends (BaseResource as any)
{
const errors = Object.keys(originalError.errors).reduce((memo, key) =>
{
const {path, message, validatorKey} = originalError.errors[key];
memo[path] = {message, kind: validatorKey}; // eslint-disable-line no-param-reassign
const { path, message, validatorKey } = originalError.errors[ key ];
memo[ path ] = { message, kind: validatorKey }; // eslint-disable-line no-param-reassign
return memo;
}, {});
return new ValidationError(`${this.name()} validation failed`, errors);
Expand All @@ -145,16 +146,31 @@ export class Resource extends (BaseResource as any)
for (const col of columns)
{
const property = new Property(col, this.id(), this.model);
this.propsObject[col.propertyName] = property;
this.propsObject[ col.propertyName ] = property;
this.propsArray.push(property);
}
}

private prepareParams(params: Object): Object
{
for(const p in params)
{
const property = this.property(p);
if(property && property.type() == "object")
params[p] = JSON.parse(params[p]);
}
return params;
}

public static isAdapterFor(rawResource: any)
{
try
{ return rawResource.getRepository() instanceof Repository; }
catch(e)
{ return false; }
{
return rawResource.getRepository() instanceof Repository;
}
catch (e)
{
return false;
}
}
}

0 comments on commit c266917

Please sign in to comment.