-
Notifications
You must be signed in to change notification settings - Fork 10
/
BaseResource.ts
135 lines (116 loc) · 4.02 KB
/
BaseResource.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
import { ResourceIdKeynameDef, ResourceItem } from '@nextgis/ngw-connector';
import { Type } from '@nextgis/utils';
// import { DeepPartial } from '../common/DeepPartial';
import { Connection } from '../connection/Connection';
import { ConnectionOptions } from '../connection/ConnectionOptions';
import { CannotExecuteResourceNotExistError } from '../error/CannotExecuteResourceNotExistError';
import { SyncOptions } from './SyncOptions';
import { NgwResource } from '../decorator/NgwResource';
import { NgwResourceOptions } from '../options/NgwResourceOptions';
import { getMetadataArgsStorage } from '..';
import { ResourceMetadataArgs } from '../metadata-args/ResourceMetadataArgs';
import {
ToTypescript,
ToTypescriptOptions,
} from '../options/ToTypescriptOptions';
import { ValidateErrorType } from '../types/ValidateErrorType';
// type QueryDeepPartialEntity<T> = DeepPartial<T>;
// type InsertResult = any;
// type DeleteResult = any;
// type UpdateResult = any;
// export interface BaseResourceStatic {
// item?: ResourceItem;
// connection?: Connection;
// new (...args: any[]): BaseResourceInstant;
// getMetadata(
// resource: typeof BaseResource,
// parent: number,
// options: SyncOptions
// ): Record<string, any> | undefined;
// connect(
// resourceOptions: ResourceDefinition,
// connectionOrOptions: Connection | ConnectionOptions
// ): Promise<typeof BaseResource>;
// sync(
// options: SyncOptions,
// connectionOrOptions: Connection | ConnectionOptions
// ): void;
// }
// export type BaseResourceInstant = Record<string, any>;
/**
* Base abstract resource for all resources.
*/
export class BaseResource {
static connection?: Connection;
static item?: ResourceItem;
static receive(
item: ResourceItem,
ResCls?: typeof BaseResource,
): typeof BaseResource {
ResCls = ResCls ?? this;
const options: NgwResourceOptions = {
display_name: item.resource.display_name,
type: item.resource.cls,
};
const decorator = NgwResource({ ...options });
const ReceivedResource = decorator(class extends ResCls {});
ReceivedResource.item = item;
return ReceivedResource;
}
static validate(): ValidateErrorType[] {
return [];
}
static clone<T extends typeof BaseResource = typeof BaseResource>(
options: Partial<NgwResourceOptions> = {},
): T {
const metadataArgsStorage = getMetadataArgsStorage();
const table = metadataArgsStorage.filterTables(
this,
)[0] as ResourceMetadataArgs;
const tableOptions = { ...table };
// keyname is unique resource property
delete tableOptions.keyname;
delete tableOptions.target;
const decorator = NgwResource({ ...tableOptions, ...options });
const ResourceClone = decorator(class extends this {});
ResourceClone.connection = undefined;
ResourceClone.item = undefined;
metadataArgsStorage.filterColumns(this).forEach((x) => {
metadataArgsStorage.columns.push({ ...x, target: ResourceClone });
});
return ResourceClone;
}
static getNgwPayload(
resource: Type<BaseResource>,
parent: number,
options: SyncOptions,
): Record<string, any> | undefined {
return undefined;
}
static async connect<T extends typeof BaseResource = typeof BaseResource>(
resourceOptions: ResourceIdKeynameDef,
connectionOrOptions: Connection | ConnectionOptions,
): Promise<T> {
const connection = Connection.create(connectionOrOptions);
await connection.connect();
const item = await connection.driver.getResource(resourceOptions);
if (item) {
this.connection = connection;
this.item = item;
return this as T;
} else {
throw new CannotExecuteResourceNotExistError(resourceOptions, connection);
}
}
static sync(
options: SyncOptions,
connectionOrOptions: Connection | ConnectionOptions,
): void {
console.log('upload');
}
static toTypescript(opt?: ToTypescriptOptions): ToTypescript {
throw Error(
'TypeScript code generation is not implement for this resource',
);
}
}