-
Notifications
You must be signed in to change notification settings - Fork 1
/
active_record.ts
executable file
·111 lines (86 loc) · 3.57 KB
/
active_record.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
import { ActiveRecordInterface } from "./active_record_interface";
import { DatabaseType } from "./database/database_type";
import { NoSqlActiveRecord } from "./nosql/nosql_active_record";
import { RelationalActiveRecord } from "./relational/relational_active_record";
import { Messages } from "./lib/messages";
import { SchemaProperty } from "./lib/schema_property";
import { ActiveRecordError } from "./lib/active_record_error";
export class ActiveRecord<T> implements ActiveRecordInterface<T> {
private nosqlActiveRecord!: NoSqlActiveRecord<T>;
private relationalActiveRecord!: RelationalActiveRecord<T>;
private databaseType = DatabaseType.NOSQL;
constructor(modelName: string, schema: Record<string, SchemaProperty>, databaseType = DatabaseType.NOSQL) {
this.databaseType = databaseType;
if (this.databaseType === DatabaseType.NOSQL) {
this.nosqlActiveRecord = new NoSqlActiveRecord<T>(modelName, schema);
} else {
this.relationalActiveRecord = new RelationalActiveRecord<T>(modelName, schema);
}
}
async find(query: any): Promise<T[]> {
if (this.databaseType === DatabaseType.NOSQL) {
return this.nosqlActiveRecord.find(query);
}
return this.relationalActiveRecord.find(query);
}
async findOne(query: any): Promise<T | null> {
if (this.databaseType === DatabaseType.NOSQL) {
return await this.nosqlActiveRecord.findOne(query);
}
return this.relationalActiveRecord.findOne(query);
}
async create(data: T): Promise<T> {
if (this.databaseType === DatabaseType.NOSQL) {
return await this.nosqlActiveRecord.create(data);
}
return this.relationalActiveRecord.create(data);
}
async update(id: string | number, data: Partial<T>): Promise<T> {
if (this.databaseType === DatabaseType.NOSQL) {
return await this.nosqlActiveRecord.update(id, data);
}
return this.relationalActiveRecord.update(id, data);
}
async delete(id: string | number): Promise<void> {
if (this.databaseType === DatabaseType.NOSQL) {
return await this.nosqlActiveRecord.delete(id);
}
return this.relationalActiveRecord.delete(id);
}
async aggregate(pipeline: any[]): Promise<any> {
if (this.databaseType === DatabaseType.RELATIONAL) {
throw new ActiveRecordError(Messages.RELATIONAL_DATABASE_NOT_SUPPORTED);
}
return await this.nosqlActiveRecord.aggregate(pipeline);
}
async index(keys: string[]): Promise<void> {
if (this.databaseType === DatabaseType.RELATIONAL) {
throw new ActiveRecordError(Messages.RELATIONAL_DATABASE_NOT_SUPPORTED);
}
return await this.nosqlActiveRecord.index(keys);
}
async query(sql: string, params?: any[]): Promise<any> {
if (this.databaseType === DatabaseType.NOSQL) {
throw new ActiveRecordError(Messages.NOSQL_DATABASE_NOT_SUPPORTED);
}
return this.relationalActiveRecord.query(sql, params);
}
async beginTransaction(): Promise<void> {
if (this.databaseType === DatabaseType.NOSQL) {
throw new ActiveRecordError(Messages.NOSQL_DATABASE_NOT_SUPPORTED);
}
return this.relationalActiveRecord.beginTransaction();
}
async commitTransaction(): Promise<void> {
if (this.databaseType === DatabaseType.NOSQL) {
throw new ActiveRecordError(Messages.NOSQL_DATABASE_NOT_SUPPORTED);
}
await this.relationalActiveRecord.commitTransaction();
}
async rollbackTransaction(): Promise<void> {
if (this.databaseType === DatabaseType.NOSQL) {
throw new ActiveRecordError(Messages.NOSQL_DATABASE_NOT_SUPPORTED);
}
await this.relationalActiveRecord.rollbackTransaction();
}
}