-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathResource.ts
184 lines (152 loc) · 4.83 KB
/
Resource.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import {
BaseRecord,
BaseResource,
Filter,
ParamsType,
SupportedDatabasesType,
} from 'adminjs';
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 {
return resource instanceof ResourceMetadata;
}
private knex: Knex;
private dialect: DatabaseDialect;
private propertyMap = new Map<string, Property>();
private tableName: string;
private schemaName: string | null;
private _database: string;
private _properties: Property[];
private idColumn: string;
constructor(info: ResourceMetadata) {
super(info.tableName);
this.knex = info.knex;
this.schemaName = info.schemaName;
this.tableName = info.tableName;
this._database = info.database;
this._properties = info.properties;
this._properties.forEach((p) => {
this.propertyMap.set(p.path(), p);
});
this.idColumn = info.idProperty.path();
this.dialect = info.dialect;
}
override databaseName(): string {
return this._database;
}
// eslint-disable-next-line class-methods-use-this
override databaseType(): SupportedDatabasesType | string {
return 'Postgres';
}
override id(): string {
return this.tableName;
}
override properties(): Property[] {
return this._properties;
}
override property(path: string): Property | null {
return this.propertyMap.get(path) ?? null;
}
override async count(filter: Filter): Promise<number> {
const [r] = await this.filterQuery(filter).count('* as cnt');
return r.cnt;
}
override async find(
filter: Filter,
options: {
limit?: number;
offset?: number;
sort?: {
sortBy?: string;
direction?: 'asc' | 'desc';
};
},
): Promise<BaseRecord[]> {
const query = this.filterQuery(filter);
if (options.limit) {
query.limit(options.limit);
}
if (options.offset) {
query.offset(options.offset);
}
if (options.sort?.sortBy) {
query.orderBy(options.sort.sortBy, options.sort.direction);
}
const rows: any[] = await query;
return rows.map((row) => new BaseRecord(row, this));
}
override async findOne(id: string): Promise<BaseRecord | null> {
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 res = await knex.whereIn(this.idColumn, ids);
return res.map((r) => this.build(r));
}
override build(params: Record<string, any>): BaseRecord {
return new BaseRecord(params, this);
}
override async create(params: Record<string, any>): Promise<ParamsType> {
const knex = this.schemaName
? this.knex(this.tableName).withSchema(this.schemaName)
: this.knex(this.tableName);
await knex.insert(params);
return params;
}
override async update(
id: string,
params: Record<string, any>,
): Promise<ParamsType> {
const knex = this.schemaName
? this.knex.withSchema(this.schemaName)
: this.knex;
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 [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;
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 q = knex;
if (!filter) {
return q;
}
const { filters } = filter;
Object.entries(filters ?? {}).forEach(([key, filter]) => {
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' && !filter.property.availableValues()) {
if (this.dialect === 'postgresql') {
q.whereILike(key, `%${filter.value}%`);
} else {
q.whereLike(key, `%${filter.value}%`);
}
} else {
q.where(key, filter.value);
}
});
return q;
}
}