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 24, 2019
2 parents be9db06 + 99fd6e3 commit 77ec96f
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 49 deletions.
12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "admin-bro-typeorm",
"version": "0.1.6-alpha.2",
"version": "0.1.6-alpha.3",
"description": "TypeORM adapter for AdminBro",
"keywords": [
"typeorm",
Expand All @@ -27,16 +27,17 @@
"author": "Artem Zabolotnyi <[email protected]>",
"license": "MIT",
"peerDependencies": {
"reflect-metadata": ">=0.1.13",
"admin-bro": ">=1.4.0",
"admin-bro": ">=1.6.0-beta.5",
"admin-bro-expressjs": ">=0.4.0",
"typeorm": ">=0.2.1"
},
"optionalDependencies": {},
"devDependencies": {
"@types/chai": "^4.2.4",
"@types/mocha": "^5.2.7",
"@types/node": "12.0.10",
"admin-bro": ">=1.4.0",
"admin-bro": "^1.6.0-beta.5",
"admin-bro-expressjs": "^0.4.0",
"chai": "^4.2.0",
"class-validator": "^0.11.0",
"mocha": "^6.2.2",
Expand All @@ -46,5 +47,8 @@
"tslint": "^5.20.0",
"typeorm": "^0.2.1",
"typescript": "^3.6.3"
},
"dependencies": {
"reflect-metadata": "^0.1.13"
}
}
35 changes: 1 addition & 34 deletions src/ExtendedRecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,40 +35,7 @@ export class ExtendedRecord extends BaseRecord
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"
},
...
}
* */
// patched strange objects ({"key.deepKey": 123}) to normal JSON.
obj.params = {};
for (const n in this._instance)
{
Expand Down
41 changes: 38 additions & 3 deletions src/Property.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,47 @@
import "reflect-metadata";
import { ColumnMetadata } from "typeorm/metadata/ColumnMetadata";
import { DATA_TYPES } from "./utils/data-types";

import { BaseProperty, PropertyType } from "admin-bro";
import {Resource} from "./Resource";
import {TEXTAREA_SYMBOL} from "./symbols/TEXTAREA_SYMBOL";
import { SEARCH_FIELD_SYMBOL } from "./symbols/SEARCH_FIELD_SYMBOL";

export class Property extends BaseProperty
{
public column: ColumnMetadata;
public resource: Resource;
public columnPosition: number;

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

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

isTitle(): boolean
{
const name = this.name();
const key = Reflect.getMetadata(SEARCH_FIELD_SYMBOL, this.resource.model);
if(key != undefined)
return key == this.name();
else
{
const firstProp = this.resource.properties()[0];
if(firstProp)
return firstProp.name() == name;
return false;
}
}

public isEditable()
{
return !this.isId()
Expand All @@ -41,6 +63,10 @@ export class Property extends BaseProperty
return null;
}

public position() {
return this.columnPosition || 0;
}

public availableValues()
{
const values = this.column.enum;
Expand All @@ -51,6 +77,7 @@ export class Property extends BaseProperty

public type(): PropertyType
{

let type: PropertyType | null = null;
if (typeof this.column.type == "function")
{
Expand All @@ -70,6 +97,14 @@ export class Property extends BaseProperty
if (!type)
console.warn(`Unhandled type: ${this.column.type}`);

return type || "string";
type = type || "string";

if(type == "string" && Reflect.getMetadata(TEXTAREA_SYMBOL, this.resource.model, this.name()))
return "textarea";
// TODO: Uncomment this in future.
/*if(Reflect.getMetadata(TEXTAREA_SYMBOL, this.resource.model, this.name()))
return "textarea";*/

return type;
}
}
8 changes: 4 additions & 4 deletions src/Resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { SEARCH_FIELD_SYMBOL } from "./symbols/SEARCH_FIELD_SYMBOL";
export class Resource extends BaseResource
{
public static validate: any;
private model: typeof BaseEntity;
public model: typeof BaseEntity;
private propsObject: Record<string, Property> = {};

constructor(model: typeof BaseEntity)
Expand Down Expand Up @@ -46,7 +46,7 @@ export class Resource extends BaseResource
{
// Reverse properties as temporary fix of columns direction.
// TODO: remove .reverse() when "admin-bro" will be fixed.
return Object.values(this.propsObject).reverse();
return Object.values(this.propsObject);
}

public property(path: string): Property | null
Expand Down Expand Up @@ -200,9 +200,9 @@ export class Resource extends BaseResource
private prepareProps()
{
const columns = this.model.getRepository().metadata.columns;
for (const col of columns)
for (let i = 0; i < columns.length; i++)
{
const property = new Property(col);
const property = new Property(columns[i], this, i);
this.propsObject[property.path()] = property;
}
}
Expand Down
14 changes: 14 additions & 0 deletions src/decorators/Textarea.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import "reflect-metadata";
import { TEXTAREA_SYMBOL } from "../symbols/TEXTAREA_SYMBOL";
import { BaseEntity } from "typeorm";

export function Textarea(name?: string)
{
return function (target: BaseEntity, propertyKey: string | undefined): void
{
if (propertyKey == undefined)
throw new TypeError();

Reflect.defineMetadata(TEXTAREA_SYMBOL, true, target.constructor, name || propertyKey);
};
}
1 change: 1 addition & 0 deletions src/symbols/TEXTAREA_SYMBOL.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const TEXTAREA_SYMBOL = Symbol("textarea");
12 changes: 9 additions & 3 deletions src/utils/convertFilter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BaseEntity, FindConditions, Between, MoreThanOrEqual, LessThanOrEqual } from "typeorm";
import { BaseEntity, FindConditions, Between, MoreThanOrEqual, LessThanOrEqual, Like } from "typeorm";
import { Filter } from "admin-bro";

function safeParseJSON(json: string)
Expand All @@ -24,7 +24,13 @@ export function convertFilter(filter?: Filter): FindConditions<BaseEntity>
for (const n in filters)
{
const one = filters[n];
if(["boolean", "number", "float", "mixed"].includes(one.property.type()))
if(["number", "float", "reference"].includes(one.property.type()))
{
const val = Number(one.value);
if(!isNaN(val))
where[n] = val;
}
else if(["boolean", "mixed"].includes(one.property.type()))
where[n] = safeParseJSON(one.value);
else if(["date", "datetime"].includes(one.property.type()))
{
Expand All @@ -36,7 +42,7 @@ export function convertFilter(filter?: Filter): FindConditions<BaseEntity>
where[n] = LessThanOrEqual(new Date(one.value.to));
}
else
where[n] = one.value;
where[n] = Like(`%${one.value}%`);
}
return where;
}
Expand Down
3 changes: 3 additions & 0 deletions src/utils/getTitle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ export async function getTitle(instance: BaseEntity | null | undefined): Promise
const prop = instance[meta];
if(prop instanceof Function)
return `${await (prop.bind(instance))()}`;
else if(prop instanceof Promise)
return `${await prop}`;
return `${prop}`;
}
else
{
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
"module": "commonjs",
"target": "es6",
"lib": [
"dom",
"es2018"
],
"sourceMap": true,
"noImplicitAny": false,
"strictNullChecks": true,
"esModuleInterop": false,
"esModuleInterop": true,
"declaration": true,
"declarationDir": "./lib",
"outDir": "./lib",
Expand Down

0 comments on commit 77ec96f

Please sign in to comment.