Skip to content

Commit

Permalink
Merge branch 'Dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
Arteha committed Dec 12, 2019
2 parents b048737 + 6ffd84d commit 5547cd3
Show file tree
Hide file tree
Showing 15 changed files with 312 additions and 77 deletions.
3 changes: 2 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ src/
spec/
package-lock\.json
yarn\.lock
*.db
*\.db
ormconfig\.js
3 changes: 2 additions & 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.5",
"version": "0.1.6-alpha.2",
"description": "TypeORM adapter for AdminBro",
"keywords": [
"typeorm",
Expand All @@ -27,6 +27,7 @@
"author": "Artem Zabolotnyi <1arteha1@gmail.com>",
"license": "MIT",
"peerDependencies": {
"reflect-metadata": ">=0.1.13",
"admin-bro": ">=1.4.0",
"typeorm": ">=0.2.1"
},
Expand Down
5 changes: 2 additions & 3 deletions src/Database.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { Resource } from "./Resource";
import { Connection, BaseEntity } from "typeorm";
import { BaseDatabase } from "admin-bro";

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

export class Database extends (BaseDatabase as any)
export class Database extends BaseDatabase
{
public constructor(public readonly connection: Connection)
{
Expand Down
90 changes: 90 additions & 0 deletions src/ExtendedRecord.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { BaseRecord, CurrentAdmin } from "admin-bro";
import RecordJSON from "admin-bro/types/src/backend/decorators/record-json.interface";
import { BaseEntity } from "typeorm";
import { Resource } from "./Resource";

export class ExtendedRecord extends BaseRecord
{
private _instance: BaseEntity | null = null;
private _customTitle: string | null = null;

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

if(instance)
this._instance = instance;
}

public setTitle(value: string): void
{
this._customTitle = value;
}

public title(): string
{
if(this._customTitle != null)
return this._customTitle;

return super.title() || "";
}

public toJSON(currentAdmin?: CurrentAdmin): RecordJSON
{
const obj = super.toJSON(currentAdmin);
obj.title = this.title(); // sorry but, .toJSON() doesn't take title from .title()
if(this._instance)
{
// I Also patched "dots" to normal JSON. Because now we can edit json field like a string:
/*
{
"params": {
"id": 93,
"name": "4",
"carrierId": 1,
"countryId": 5,
"zeros": 3,
"zipCodes.0": 9,
"zipCodes.1": 12,
"zipCodes.2": 19,
"zipCodes.3": 22,
"zipCodes.4": 24,
"zipCodes.5.0": 33000,
"zipCodes.5.1": 34999,
"zipCodes.6.0": 39000,
"zipCodes.6.1": 40999,
"zipCodes.7": 42,
"zipCodes.8": 44,
"zipCodes.9": 47,
"zipCodes.10": 49,
"exceptions": [],
"carrier.id": 1,
"carrier.name": "leman",
"carrier.oilRate": 1.336,
"carrier.category": "all",
"carrier.hasSpecialHandling": true,
"country.id": 5,
"country.name": "SPAIN"
},
...
}
* */
obj.params = {};
for (const n in this._instance)
{
const value = this._instance[n];
const property = this.resource.property(n);
if(property)
{
const type = property.type();
if(type == "mixed")
obj.params[n] = JSON.stringify(value);
else
obj.params[n] = value;
}
}
}

return obj;
}
}
10 changes: 8 additions & 2 deletions src/Property.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,20 @@ export class Property extends BaseProperty
public column: ColumnMetadata;

constructor(column: ColumnMetadata)
{
{
// for reference fields take database name (with ...Id)
const path = column.referencedColumn ? column.databaseName : column.propertyPath;
super({ path });
this.column = column;
}

public isEditable() {
public name()
{
return this.column.propertyName;
}

public isEditable()
{
return !this.isId()
&& !this.column.isCreateDate
&& !this.column.isUpdateDate;
Expand Down
Loading

0 comments on commit 5547cd3

Please sign in to comment.