brackets-memory-db 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +73 -0
- package/dist/index.js +188 -0
- package/package.json +40 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { CrudInterface, OmitId, Table, Database } from 'brackets-manager';
|
|
2
|
+
export declare class InMemoryDatabase implements CrudInterface {
|
|
3
|
+
protected data: Database;
|
|
4
|
+
/**
|
|
5
|
+
* @param data "import" data from external
|
|
6
|
+
*/
|
|
7
|
+
setData(data: Database): void;
|
|
8
|
+
/**
|
|
9
|
+
* @param partial Filter
|
|
10
|
+
*/
|
|
11
|
+
makeFilter(partial: any): (entry: any) => boolean;
|
|
12
|
+
/**
|
|
13
|
+
* Clearing all of the data
|
|
14
|
+
*/
|
|
15
|
+
reset(): void;
|
|
16
|
+
insert<T>(table: Table, value: OmitId<T>): Promise<number>;
|
|
17
|
+
/**
|
|
18
|
+
* Inserts multiple values in the database.
|
|
19
|
+
*
|
|
20
|
+
* @param table Where to insert.
|
|
21
|
+
* @param values What to insert.
|
|
22
|
+
*/
|
|
23
|
+
insert<T>(table: Table, values: OmitId<T>[]): Promise<boolean>;
|
|
24
|
+
/**
|
|
25
|
+
* Gets all data from a table in the database.
|
|
26
|
+
*
|
|
27
|
+
* @param table Where to get from.
|
|
28
|
+
*/
|
|
29
|
+
select<T>(table: Table): Promise<T[] | null>;
|
|
30
|
+
/**
|
|
31
|
+
* Gets specific data from a table in the database.
|
|
32
|
+
*
|
|
33
|
+
* @param table Where to get from.
|
|
34
|
+
* @param id What to get.
|
|
35
|
+
*/
|
|
36
|
+
select<T>(table: Table, id: number): Promise<T | null>;
|
|
37
|
+
/**
|
|
38
|
+
* Gets data from a table in the database with a filter.
|
|
39
|
+
*
|
|
40
|
+
* @param table Where to get from.
|
|
41
|
+
* @param filter An object to filter data.
|
|
42
|
+
*/
|
|
43
|
+
select<T>(table: Table, filter: Partial<T>): Promise<T[] | null>;
|
|
44
|
+
/**
|
|
45
|
+
* Updates data in a table.
|
|
46
|
+
*
|
|
47
|
+
* @param table Where to update.
|
|
48
|
+
* @param id What to update.
|
|
49
|
+
* @param value How to update.
|
|
50
|
+
*/
|
|
51
|
+
update<T>(table: Table, id: number, value: T): Promise<boolean>;
|
|
52
|
+
/**
|
|
53
|
+
* Updates data in a table.
|
|
54
|
+
*
|
|
55
|
+
* @param table Where to update.
|
|
56
|
+
* @param filter An object to filter data.
|
|
57
|
+
* @param value How to update.
|
|
58
|
+
*/
|
|
59
|
+
update<T>(table: Table, filter: Partial<T>, value: Partial<T>): Promise<boolean>;
|
|
60
|
+
/**
|
|
61
|
+
* Empties a table completely.
|
|
62
|
+
*
|
|
63
|
+
* @param table Where to delete everything.
|
|
64
|
+
*/
|
|
65
|
+
delete(table: Table): Promise<boolean>;
|
|
66
|
+
/**
|
|
67
|
+
* Delete data in a table, based on a filter.
|
|
68
|
+
*
|
|
69
|
+
* @param table Where to delete in.
|
|
70
|
+
* @param filter An object to filter data.
|
|
71
|
+
*/
|
|
72
|
+
delete<T>(table: Table, filter: Partial<T>): Promise<boolean>;
|
|
73
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.InMemoryDatabase = void 0;
|
|
4
|
+
class InMemoryDatabase {
|
|
5
|
+
data = {
|
|
6
|
+
participant: [],
|
|
7
|
+
stage: [],
|
|
8
|
+
group: [],
|
|
9
|
+
round: [],
|
|
10
|
+
match: [],
|
|
11
|
+
match_game: [],
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* @param data "import" data from external
|
|
15
|
+
*/
|
|
16
|
+
setData(data) {
|
|
17
|
+
this.data = data;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* @param partial Filter
|
|
21
|
+
*/
|
|
22
|
+
makeFilter(partial) {
|
|
23
|
+
return (entry) => {
|
|
24
|
+
let result = true;
|
|
25
|
+
for (const key of Object.keys(partial))
|
|
26
|
+
result = result && entry[key] === partial[key];
|
|
27
|
+
return result;
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Clearing all of the data
|
|
32
|
+
*/
|
|
33
|
+
reset() {
|
|
34
|
+
this.data = {
|
|
35
|
+
participant: [],
|
|
36
|
+
stage: [],
|
|
37
|
+
group: [],
|
|
38
|
+
round: [],
|
|
39
|
+
match: [],
|
|
40
|
+
match_game: [],
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Implementation of insert
|
|
45
|
+
*
|
|
46
|
+
* @param table Where to insert.
|
|
47
|
+
* @param values What to insert.
|
|
48
|
+
*/
|
|
49
|
+
insert(table, values) {
|
|
50
|
+
let id;
|
|
51
|
+
id = this.data[table].length;
|
|
52
|
+
if (!Array.isArray(values)) {
|
|
53
|
+
try {
|
|
54
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
55
|
+
// @ts-ignore
|
|
56
|
+
this.data[table].push({ id, ...values });
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
return new Promise((resolve) => {
|
|
60
|
+
resolve(-1);
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
return new Promise((resolve) => {
|
|
64
|
+
resolve(id);
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
try {
|
|
68
|
+
values.map((object) => {
|
|
69
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
70
|
+
// @ts-ignore
|
|
71
|
+
this.data[table].push({ id: id++, ...object });
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
catch (error) {
|
|
75
|
+
return new Promise((resolve) => {
|
|
76
|
+
resolve(false);
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
return new Promise((resolve) => {
|
|
80
|
+
resolve(true);
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* @param table Where to get from.
|
|
85
|
+
* @param arg Arg.
|
|
86
|
+
*/
|
|
87
|
+
select(table, arg) {
|
|
88
|
+
try {
|
|
89
|
+
if (arg === undefined) {
|
|
90
|
+
return new Promise((resolve) => {
|
|
91
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
92
|
+
// @ts-ignore
|
|
93
|
+
resolve(this.data[table]);
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
if (typeof arg === 'number') {
|
|
97
|
+
return new Promise((resolve) => {
|
|
98
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
99
|
+
// @ts-ignore
|
|
100
|
+
resolve(this.data[table][arg]);
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
return new Promise((resolve) => {
|
|
104
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
105
|
+
// @ts-ignore
|
|
106
|
+
resolve(this.data[table].filter(this.makeFilter(arg)) || null);
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
catch (error) {
|
|
110
|
+
return new Promise((resolve) => {
|
|
111
|
+
resolve(null);
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Updates data in a table.
|
|
117
|
+
*
|
|
118
|
+
* @param table Where to update.
|
|
119
|
+
* @param arg
|
|
120
|
+
* @param value How to update.
|
|
121
|
+
*/
|
|
122
|
+
update(table, arg, value) {
|
|
123
|
+
if (typeof arg === 'number') {
|
|
124
|
+
try {
|
|
125
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
126
|
+
// @ts-ignore
|
|
127
|
+
this.data[table][arg] = value;
|
|
128
|
+
return new Promise((resolve) => {
|
|
129
|
+
resolve(true);
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
catch (error) {
|
|
133
|
+
return new Promise((resolve) => {
|
|
134
|
+
resolve(false);
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
139
|
+
// @ts-ignore
|
|
140
|
+
const values = this.data[table].filter(this.makeFilter(arg));
|
|
141
|
+
if (!values) {
|
|
142
|
+
return new Promise((resolve) => {
|
|
143
|
+
resolve(false);
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
values.forEach((v) => {
|
|
147
|
+
const existing = this.data[table][v.id];
|
|
148
|
+
for (const key in value) {
|
|
149
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
150
|
+
// @ts-ignore
|
|
151
|
+
existing[key] = value[key];
|
|
152
|
+
}
|
|
153
|
+
this.data[table][v.id] = existing;
|
|
154
|
+
});
|
|
155
|
+
return new Promise((resolve) => {
|
|
156
|
+
resolve(true);
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Delete data in a table, based on a filter.
|
|
161
|
+
*
|
|
162
|
+
* @param table Where to delete in.
|
|
163
|
+
* @param filter An object to filter data.
|
|
164
|
+
*/
|
|
165
|
+
delete(table, filter) {
|
|
166
|
+
const values = this.data[table];
|
|
167
|
+
if (!values) {
|
|
168
|
+
return new Promise((resolve) => {
|
|
169
|
+
resolve(false);
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
if (!filter) {
|
|
173
|
+
this.data[table] = [];
|
|
174
|
+
return new Promise((resolve) => {
|
|
175
|
+
resolve(true);
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
const predicate = this.makeFilter(filter);
|
|
179
|
+
const negativeFilter = (value) => !predicate(value);
|
|
180
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
181
|
+
// @ts-ignore
|
|
182
|
+
this.data[table] = values.filter(negativeFilter);
|
|
183
|
+
return new Promise((resolve) => {
|
|
184
|
+
resolve(true);
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
exports.InMemoryDatabase = InMemoryDatabase;
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "brackets-memory-db",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "An in-memory database for brackets-manager.js",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"types",
|
|
9
|
+
"model",
|
|
10
|
+
"brackets",
|
|
11
|
+
"tournament",
|
|
12
|
+
"manager",
|
|
13
|
+
"storage",
|
|
14
|
+
"memory"
|
|
15
|
+
],
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "tsc",
|
|
21
|
+
"prepare": "npm run build",
|
|
22
|
+
"prepublishOnly": "npm run build"
|
|
23
|
+
},
|
|
24
|
+
"repository": {
|
|
25
|
+
"type": "git",
|
|
26
|
+
"url": "git+https://github.com/Drarig29/brackets-storage.git"
|
|
27
|
+
},
|
|
28
|
+
"author": "Corentin Girard",
|
|
29
|
+
"license": "ISC",
|
|
30
|
+
"bugs": {
|
|
31
|
+
"url": "https://github.com/Drarig29/brackets-storage/issues"
|
|
32
|
+
},
|
|
33
|
+
"homepage": "https://github.com/Drarig29/brackets-storage#readme",
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"typescript": "^4.4.4"
|
|
36
|
+
},
|
|
37
|
+
"peerDependencies": {
|
|
38
|
+
"brackets-manager": "^1.3.9"
|
|
39
|
+
}
|
|
40
|
+
}
|