@tsed/adapters 8.0.1 → 8.0.2
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/package.json +16 -15
- package/src/adapters/FileSyncAdapter.spec.ts +167 -0
- package/src/adapters/FileSyncAdapter.ts +36 -0
- package/src/adapters/LowDbAdapter.spec.ts +205 -0
- package/src/adapters/LowDbAdapter.ts +135 -0
- package/src/adapters/MemoryAdapter.spec.ts +226 -0
- package/src/adapters/MemoryAdapter.ts +20 -0
- package/src/decorators/indexed.spec.ts +74 -0
- package/src/decorators/indexed.ts +7 -0
- package/src/decorators/injectAdapter.spec.ts +292 -0
- package/src/decorators/injectAdapter.ts +62 -0
- package/src/domain/Adapter.ts +114 -0
- package/src/domain/AdaptersSettings.ts +29 -0
- package/src/index.ts +11 -0
- package/src/services/Adapters.ts +21 -0
package/package.json
CHANGED
|
@@ -2,13 +2,14 @@
|
|
|
2
2
|
"name": "@tsed/adapters",
|
|
3
3
|
"description": "Database adapters for Ts.ED",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"version": "8.0.
|
|
5
|
+
"version": "8.0.2",
|
|
6
6
|
"source": "./src/index.ts",
|
|
7
7
|
"main": "./lib/esm/index.js",
|
|
8
8
|
"module": "./lib/esm/index.js",
|
|
9
9
|
"typings": "./lib/types/index.d.ts",
|
|
10
10
|
"exports": {
|
|
11
11
|
".": {
|
|
12
|
+
"@tsed/source": "./src/index.ts",
|
|
12
13
|
"types": "./lib/types/index.d.ts",
|
|
13
14
|
"import": "./lib/esm/index.js",
|
|
14
15
|
"default": "./lib/esm/index.js"
|
|
@@ -32,26 +33,26 @@
|
|
|
32
33
|
"uuid": "^10.0.0"
|
|
33
34
|
},
|
|
34
35
|
"devDependencies": {
|
|
35
|
-
"@tsed/ajv": "8.0.
|
|
36
|
-
"@tsed/barrels": "8.0.
|
|
37
|
-
"@tsed/core": "8.0.
|
|
38
|
-
"@tsed/di": "8.0.
|
|
39
|
-
"@tsed/json-mapper": "8.0.
|
|
40
|
-
"@tsed/platform-http": "8.0.
|
|
41
|
-
"@tsed/schema": "8.0.
|
|
42
|
-
"@tsed/typescript": "8.0.
|
|
36
|
+
"@tsed/ajv": "8.0.2",
|
|
37
|
+
"@tsed/barrels": "8.0.2",
|
|
38
|
+
"@tsed/core": "8.0.2",
|
|
39
|
+
"@tsed/di": "8.0.2",
|
|
40
|
+
"@tsed/json-mapper": "8.0.2",
|
|
41
|
+
"@tsed/platform-http": "8.0.2",
|
|
42
|
+
"@tsed/schema": "8.0.2",
|
|
43
|
+
"@tsed/typescript": "8.0.2",
|
|
43
44
|
"@types/fs-extra": "11.0.4",
|
|
44
45
|
"eslint": "9.12.0",
|
|
45
46
|
"typescript": "5.4.5",
|
|
46
47
|
"vitest": "2.1.2"
|
|
47
48
|
},
|
|
48
49
|
"peerDependencies": {
|
|
49
|
-
"@tsed/ajv": "8.0.
|
|
50
|
-
"@tsed/core": "8.0.
|
|
51
|
-
"@tsed/di": "8.0.
|
|
52
|
-
"@tsed/json-mapper": "8.0.
|
|
53
|
-
"@tsed/platform-http": "8.0.
|
|
54
|
-
"@tsed/schema": "8.0.
|
|
50
|
+
"@tsed/ajv": "8.0.2",
|
|
51
|
+
"@tsed/core": "8.0.2",
|
|
52
|
+
"@tsed/di": "8.0.2",
|
|
53
|
+
"@tsed/json-mapper": "8.0.2",
|
|
54
|
+
"@tsed/platform-http": "8.0.2",
|
|
55
|
+
"@tsed/schema": "8.0.2"
|
|
55
56
|
},
|
|
56
57
|
"peerDependenciesMeta": {
|
|
57
58
|
"@tsed/ajv": {
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import {faker} from "@faker-js/faker";
|
|
2
|
+
import {PlatformTest} from "@tsed/platform-http/testing";
|
|
3
|
+
import {Property} from "@tsed/schema";
|
|
4
|
+
|
|
5
|
+
import {Adapter} from "../domain/Adapter.js";
|
|
6
|
+
import {Adapters} from "../services/Adapters.js";
|
|
7
|
+
import {FileSyncAdapter} from "./FileSyncAdapter.js";
|
|
8
|
+
|
|
9
|
+
class Client {
|
|
10
|
+
@Property()
|
|
11
|
+
_id: string;
|
|
12
|
+
|
|
13
|
+
@Property()
|
|
14
|
+
name: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
describe("FileSyncAdapter", () => {
|
|
18
|
+
describe("writable", () => {
|
|
19
|
+
let adapter: Adapter<Client>;
|
|
20
|
+
beforeEach(() => PlatformTest.create());
|
|
21
|
+
afterEach(() => PlatformTest.reset());
|
|
22
|
+
beforeEach(() => {
|
|
23
|
+
adapter = PlatformTest.get<Adapters>(Adapters).invokeAdapter<any>({
|
|
24
|
+
collectionName: "clients",
|
|
25
|
+
model: Client,
|
|
26
|
+
adapter: FileSyncAdapter
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
afterAll(async () => {
|
|
31
|
+
await adapter.deleteMany({});
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
describe("create()", () => {
|
|
35
|
+
it("should create a new instance", async () => {
|
|
36
|
+
const base = {
|
|
37
|
+
name: faker.person.jobTitle()
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const client = await adapter.create(base);
|
|
41
|
+
|
|
42
|
+
expect(client).toBeInstanceOf(Client);
|
|
43
|
+
expect(typeof client._id).toBe("string");
|
|
44
|
+
expect(client.name).toBe(base.name);
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
describe("findById()", () => {
|
|
49
|
+
it("should create a new instance", async () => {
|
|
50
|
+
const base = {
|
|
51
|
+
name: faker.person.jobTitle()
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const client = await adapter.create(base);
|
|
55
|
+
const result = await adapter.findById(client._id);
|
|
56
|
+
|
|
57
|
+
expect(result).toBeInstanceOf(Client);
|
|
58
|
+
expect(result?._id).toBe(client._id);
|
|
59
|
+
expect(result?.name).toBe(base.name);
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
describe("findOne()", () => {
|
|
64
|
+
it("should create a new instance", async () => {
|
|
65
|
+
const base = {
|
|
66
|
+
name: faker.person.jobTitle()
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const client = await adapter.create(base);
|
|
70
|
+
|
|
71
|
+
const result = await adapter.findOne({
|
|
72
|
+
name: base.name
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
expect(result).toBeInstanceOf(Client);
|
|
76
|
+
expect(result?._id).toBe(client._id);
|
|
77
|
+
expect(result?.name).toBe(base.name);
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
describe("deleteOne()", () => {
|
|
81
|
+
it("should delete instance", async () => {
|
|
82
|
+
const base = {
|
|
83
|
+
name: faker.person.jobTitle()
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
const client = await adapter.create(base);
|
|
87
|
+
|
|
88
|
+
const result = await adapter.deleteOne({
|
|
89
|
+
name: base.name
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
expect(result).toBeInstanceOf(Client);
|
|
93
|
+
expect(result?._id).toBe(client._id);
|
|
94
|
+
expect(result?.name).toBe(base.name);
|
|
95
|
+
|
|
96
|
+
const result2 = await adapter.deleteOne({
|
|
97
|
+
name: base.name
|
|
98
|
+
});
|
|
99
|
+
expect(result2).toBeUndefined();
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
describe("readOnly", () => {
|
|
104
|
+
let adapter: Adapter<Client>;
|
|
105
|
+
beforeEach(() => PlatformTest.create());
|
|
106
|
+
afterEach(() => PlatformTest.reset());
|
|
107
|
+
beforeEach(() => {
|
|
108
|
+
adapter = PlatformTest.get<Adapters>(Adapters).invokeAdapter<any>({
|
|
109
|
+
collectionName: "clients",
|
|
110
|
+
model: Client,
|
|
111
|
+
readOnly: true,
|
|
112
|
+
adapter: FileSyncAdapter
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
afterAll(async () => {
|
|
117
|
+
await adapter.deleteMany({});
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
describe("create()", () => {
|
|
121
|
+
it("should create a new instance", async () => {
|
|
122
|
+
const base = {
|
|
123
|
+
name: faker.person.jobTitle()
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
const client = await adapter.create(base);
|
|
127
|
+
|
|
128
|
+
expect(client).toBeInstanceOf(Client);
|
|
129
|
+
expect(typeof client._id).toBe("string");
|
|
130
|
+
expect(client.name).toBe(base.name);
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
describe("findById()", () => {
|
|
135
|
+
it("should create a new instance", async () => {
|
|
136
|
+
const base = {
|
|
137
|
+
name: faker.person.jobTitle()
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
const client = await adapter.create(base);
|
|
141
|
+
const result = await adapter.findById(client._id);
|
|
142
|
+
|
|
143
|
+
expect(result).toBeInstanceOf(Client);
|
|
144
|
+
expect(result?._id).toBe(client._id);
|
|
145
|
+
expect(result?.name).toBe(base.name);
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
describe("findOne()", () => {
|
|
150
|
+
it("should create a new instance", async () => {
|
|
151
|
+
const base = {
|
|
152
|
+
name: faker.person.jobTitle()
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
const client = await adapter.create(base);
|
|
156
|
+
|
|
157
|
+
const result = await adapter.findOne({
|
|
158
|
+
name: base.name
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
expect(result).toBeInstanceOf(Client);
|
|
162
|
+
expect(result?._id).toBe(client._id);
|
|
163
|
+
expect(result?.name).toBe(base.name);
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
});
|
|
167
|
+
});
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import {nameOf} from "@tsed/core";
|
|
2
|
+
import {Configuration, Injectable, Opts, ProviderScope, Scope} from "@tsed/di";
|
|
3
|
+
import fs from "fs-extra";
|
|
4
|
+
import {LowSync} from "lowdb";
|
|
5
|
+
import {JSONFileSync} from "lowdb/node";
|
|
6
|
+
import {dirname} from "path";
|
|
7
|
+
|
|
8
|
+
import {AdapterConstructorOptions} from "../domain/Adapter.js";
|
|
9
|
+
import {AdapterModel, LowDbAdapter, type LowModel} from "./LowDbAdapter.js";
|
|
10
|
+
|
|
11
|
+
export interface FileSyncAdapterConstructorOptions extends AdapterConstructorOptions {
|
|
12
|
+
readOnly: true;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
@Injectable()
|
|
16
|
+
@Scope(ProviderScope.INSTANCE)
|
|
17
|
+
export class FileSyncAdapter<T extends AdapterModel> extends LowDbAdapter<T> {
|
|
18
|
+
constructor(@Opts options: FileSyncAdapterConstructorOptions, @Configuration() configuration: Configuration) {
|
|
19
|
+
super(options, configuration);
|
|
20
|
+
|
|
21
|
+
fs.ensureDirSync(dirname(this.dbFilePath));
|
|
22
|
+
|
|
23
|
+
const file = new JSONFileSync<LowModel<T>>(this.dbFilePath);
|
|
24
|
+
|
|
25
|
+
this.db = new LowSync<LowModel<T>>(file, {
|
|
26
|
+
collectionName: this.collectionName,
|
|
27
|
+
modelName: nameOf(this.model),
|
|
28
|
+
collection: []
|
|
29
|
+
});
|
|
30
|
+
this.db.write();
|
|
31
|
+
|
|
32
|
+
if (options.readOnly) {
|
|
33
|
+
file.write = (() => {}) as any;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import {faker} from "@faker-js/faker";
|
|
2
|
+
import {deserialize} from "@tsed/json-mapper";
|
|
3
|
+
import {PlatformTest} from "@tsed/platform-http/testing";
|
|
4
|
+
import {Format, Name, Property} from "@tsed/schema";
|
|
5
|
+
|
|
6
|
+
import {Adapter, Adapters, MemoryAdapter} from "../../src/index.js";
|
|
7
|
+
|
|
8
|
+
class BaseClient {
|
|
9
|
+
@Format("date-time")
|
|
10
|
+
createdAt: Date;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
class Client extends BaseClient {
|
|
14
|
+
@Name("id")
|
|
15
|
+
_id: string;
|
|
16
|
+
|
|
17
|
+
@Property()
|
|
18
|
+
name: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
describe("MemoryAdapter", () => {
|
|
22
|
+
let adapter: Adapter<Client>;
|
|
23
|
+
beforeEach(() => PlatformTest.create());
|
|
24
|
+
afterEach(() => PlatformTest.reset());
|
|
25
|
+
beforeEach(() => {
|
|
26
|
+
adapter = PlatformTest.get<Adapters>(Adapters).invokeAdapter<any>({
|
|
27
|
+
collectionName: "clients",
|
|
28
|
+
model: Client,
|
|
29
|
+
adapter: MemoryAdapter
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
describe("create()", () => {
|
|
34
|
+
it("should create a new instance", async () => {
|
|
35
|
+
const base = deserialize(
|
|
36
|
+
{
|
|
37
|
+
name: faker.person.firstName(),
|
|
38
|
+
createdAt: faker.date.past()
|
|
39
|
+
},
|
|
40
|
+
{type: Client}
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
const client = await adapter.create(base);
|
|
44
|
+
|
|
45
|
+
expect(client).toBeInstanceOf(Client);
|
|
46
|
+
expect(typeof client._id).toBe("string");
|
|
47
|
+
expect(client.name).toBe(base.name);
|
|
48
|
+
expect(client.createdAt).toEqual(base.createdAt);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("should create a new instance with expireAt", async () => {
|
|
52
|
+
const base = {
|
|
53
|
+
name: faker.person.firstName()
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const client = await adapter.create(base, new Date());
|
|
57
|
+
|
|
58
|
+
expect(client).toBeInstanceOf(Client);
|
|
59
|
+
expect(typeof client._id).toBe("string");
|
|
60
|
+
expect(client.name).toBe(base.name);
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
describe("upsert()", () => {
|
|
65
|
+
it("should create a new instance if not exists", async () => {
|
|
66
|
+
const base: any = {
|
|
67
|
+
name: faker.person.firstName()
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const client = await adapter.upsert(base._id, base);
|
|
71
|
+
|
|
72
|
+
expect(client).toBeInstanceOf(Client);
|
|
73
|
+
expect(typeof client._id).toBe("string");
|
|
74
|
+
expect(client.name).toBe(base.name);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("should update instance if exists", async () => {
|
|
78
|
+
const base: any = {
|
|
79
|
+
name: faker.person.firstName()
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
const client = await adapter.upsert(base._id, base);
|
|
83
|
+
const client2 = await adapter.upsert(client._id, client);
|
|
84
|
+
|
|
85
|
+
expect(client2).toBeInstanceOf(Client);
|
|
86
|
+
expect(typeof client2._id).toBe("string");
|
|
87
|
+
expect(client2.name).toBe(base.name);
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
describe("updateOne()", () => {
|
|
92
|
+
it("should update an instance", async () => {
|
|
93
|
+
const base = {
|
|
94
|
+
name: faker.person.firstName()
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
const client = await adapter.create(base);
|
|
98
|
+
|
|
99
|
+
const update = {
|
|
100
|
+
_id: client._id,
|
|
101
|
+
name: faker.person.firstName()
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
const client2 = await adapter.updateOne({_id: client._id}, update);
|
|
105
|
+
|
|
106
|
+
expect(client2).toBeInstanceOf(Client);
|
|
107
|
+
expect(typeof client2?._id).toBe("string");
|
|
108
|
+
expect(client2?.name).not.toBe(base.name);
|
|
109
|
+
expect(client2?.name).toBe(update.name);
|
|
110
|
+
});
|
|
111
|
+
it("should return undefined", async () => {
|
|
112
|
+
const base = {
|
|
113
|
+
name: faker.person.firstName()
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
const client = await adapter.updateOne({_id: faker.string.uuid()}, base);
|
|
117
|
+
|
|
118
|
+
expect(client).toBeUndefined();
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
describe("findById()", () => {
|
|
123
|
+
it("should find by ID", async () => {
|
|
124
|
+
const base = {
|
|
125
|
+
name: faker.person.firstName()
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
const client = await adapter.create(base);
|
|
129
|
+
const result = await adapter.findById(client._id);
|
|
130
|
+
|
|
131
|
+
expect(result).toBeInstanceOf(Client);
|
|
132
|
+
expect(result?._id).toBe(client._id);
|
|
133
|
+
expect(result?.name).toBe(base.name);
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
describe("findOne()", () => {
|
|
138
|
+
it("should find one item", async () => {
|
|
139
|
+
const base = {
|
|
140
|
+
name: faker.person.firstName()
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
const client = await adapter.create(base);
|
|
144
|
+
|
|
145
|
+
const result = await adapter.findOne({
|
|
146
|
+
name: base.name
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
expect(result).toBeInstanceOf(Client);
|
|
150
|
+
expect(result?._id).toBe(client._id);
|
|
151
|
+
expect(result?.name).toBe(base.name);
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
describe("findAll()", () => {
|
|
155
|
+
it("should find all items", async () => {
|
|
156
|
+
const base = {
|
|
157
|
+
name: faker.person.firstName()
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
await adapter.create(base);
|
|
161
|
+
|
|
162
|
+
const result = await adapter.findAll({
|
|
163
|
+
name: base.name
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
expect(result[0]).toBeInstanceOf(Client);
|
|
167
|
+
expect(result[0].name).toBe(base.name);
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
describe("deleteById()", () => {
|
|
171
|
+
it("should delete an item by id", async () => {
|
|
172
|
+
const base = {
|
|
173
|
+
name: faker.person.firstName()
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
const client = await adapter.create(base);
|
|
177
|
+
|
|
178
|
+
const result = await adapter.deleteById(client._id);
|
|
179
|
+
|
|
180
|
+
expect(result).toBeInstanceOf(Client);
|
|
181
|
+
expect(result?.name).toBe(base.name);
|
|
182
|
+
});
|
|
183
|
+
});
|
|
184
|
+
describe("deleteMany()", () => {
|
|
185
|
+
it("should delete many", async () => {
|
|
186
|
+
const base = {
|
|
187
|
+
name: faker.person.firstName()
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
const client = await adapter.create(base);
|
|
191
|
+
|
|
192
|
+
await adapter.create({
|
|
193
|
+
name: faker.person.firstName()
|
|
194
|
+
});
|
|
195
|
+
await adapter.create({
|
|
196
|
+
name: faker.person.firstName()
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
const result = await adapter.deleteMany(client);
|
|
200
|
+
|
|
201
|
+
expect(result[0]).toBeInstanceOf(Client);
|
|
202
|
+
expect(result[0]?.name).toBe(base.name);
|
|
203
|
+
});
|
|
204
|
+
});
|
|
205
|
+
});
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import {cleanObject} from "@tsed/core";
|
|
2
|
+
import _ from "lodash";
|
|
3
|
+
import isMatch from "lodash/isMatch.js";
|
|
4
|
+
import type {Low, LowSync} from "lowdb";
|
|
5
|
+
import {v4 as uuid} from "uuid";
|
|
6
|
+
|
|
7
|
+
import {Adapter} from "../domain/Adapter.js";
|
|
8
|
+
|
|
9
|
+
export interface AdapterModel {
|
|
10
|
+
_id: string;
|
|
11
|
+
expires_at?: Date;
|
|
12
|
+
|
|
13
|
+
[key: string]: any;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface LowModel<T> {
|
|
17
|
+
collection: T[];
|
|
18
|
+
collectionName?: string;
|
|
19
|
+
modelName?: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export class LowDbAdapter<T extends AdapterModel> extends Adapter<T> {
|
|
23
|
+
protected db: LowSync<LowModel<T>> | Low<LowModel<T>>;
|
|
24
|
+
|
|
25
|
+
get collection() {
|
|
26
|
+
return this.db.data!.collection!;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
protected get dbFilePath() {
|
|
30
|
+
const lowdbDir = this.configuration.get("adapters.lowdbDir", ".db");
|
|
31
|
+
return `${lowdbDir}/${this.collectionName}.json`;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
public async create(payload: Partial<T>, expiresAt?: Date): Promise<T> {
|
|
35
|
+
if (expiresAt) {
|
|
36
|
+
payload.expires_at = expiresAt;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
payload._id = uuid();
|
|
40
|
+
|
|
41
|
+
await this.validate(payload as T);
|
|
42
|
+
|
|
43
|
+
await this.db.update(({collection}) => collection.push(this.serialize(payload)));
|
|
44
|
+
|
|
45
|
+
return this.deserialize(payload);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
public async upsert(id: string, payload: T, expiresAt?: Date): Promise<T> {
|
|
49
|
+
let item = await this.findById(id);
|
|
50
|
+
|
|
51
|
+
if (!item) {
|
|
52
|
+
payload._id = id || uuid();
|
|
53
|
+
|
|
54
|
+
await this.validate(payload as T);
|
|
55
|
+
|
|
56
|
+
const item = this.serialize(payload);
|
|
57
|
+
item.expires_at = expiresAt;
|
|
58
|
+
|
|
59
|
+
await this.db.update(({collection}) => collection.push(item));
|
|
60
|
+
|
|
61
|
+
return this.deserialize(item);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return (await this.update(id, payload, expiresAt)) as T;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
update(id: string, payload: T, expiresAt?: Date): Promise<T | undefined> {
|
|
68
|
+
return this.updateOne({_id: id}, payload, expiresAt);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
public async updateOne(predicate: Partial<T & any>, payload: T, expiresAt?: Date): Promise<T | undefined> {
|
|
72
|
+
let index = _.findIndex(this.collection, cleanObject(predicate));
|
|
73
|
+
|
|
74
|
+
if (index === -1) {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
let item = this.deserialize(this.collection[index]);
|
|
79
|
+
|
|
80
|
+
Object.assign(item, payload, {_id: item._id});
|
|
81
|
+
|
|
82
|
+
await this.validate(item as T);
|
|
83
|
+
|
|
84
|
+
item.expires_at = expiresAt || item.expires_at;
|
|
85
|
+
this.db.update(({collection}) => (collection[index] = item));
|
|
86
|
+
|
|
87
|
+
return this.deserialize(item);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
findOne(predicate: Partial<T & any>): Promise<T | undefined> {
|
|
91
|
+
const item = _.find(this.collection, cleanObject(predicate));
|
|
92
|
+
|
|
93
|
+
return this.deserialize(item);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
findById(_id: string): Promise<T | undefined> {
|
|
97
|
+
return this.findOne({_id});
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
public async findAll(predicate: Partial<T & any> = {}): Promise<T[]> {
|
|
101
|
+
return _.filter(this.collection, cleanObject(predicate)).map((item) => this.deserialize(item));
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
public deleteOne(predicate: Partial<T & any>): Promise<T | undefined> {
|
|
105
|
+
const item = _.find<T>(this.collection, cleanObject(predicate));
|
|
106
|
+
|
|
107
|
+
if (item) {
|
|
108
|
+
_.remove(this.collection, ({_id}) => _id === item._id);
|
|
109
|
+
|
|
110
|
+
return Promise.resolve(this.deserialize(item));
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return Promise.resolve(undefined);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
public deleteById(_id: string): Promise<T | undefined> {
|
|
117
|
+
return this.deleteOne({_id} as any);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
public async deleteMany(predicate: Partial<T>): Promise<T[]> {
|
|
121
|
+
let removedItems: T[] = [];
|
|
122
|
+
|
|
123
|
+
this.db.update((data) => {
|
|
124
|
+
_.remove(data.collection, (item) => {
|
|
125
|
+
if (isMatch(item, cleanObject(predicate))) {
|
|
126
|
+
removedItems.push(this.deserialize(item));
|
|
127
|
+
return true;
|
|
128
|
+
}
|
|
129
|
+
return false;
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
return removedItems;
|
|
134
|
+
}
|
|
135
|
+
}
|