@tsed/adapters 8.0.1 → 8.0.3

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.
@@ -0,0 +1,226 @@
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, getJsonSchema, Name, Property} from "@tsed/schema";
5
+
6
+ import {Adapter} from "../domain/Adapter.js";
7
+ import {Adapters} from "../services/Adapters.js";
8
+ import {MemoryAdapter} from "./MemoryAdapter.js";
9
+
10
+ class BaseClient {
11
+ @Format("date-time")
12
+ createdAt: Date;
13
+ }
14
+
15
+ class Client extends BaseClient {
16
+ @Name("id")
17
+ _id: string;
18
+
19
+ @Property()
20
+ name: string;
21
+ }
22
+
23
+ describe("MemoryAdapter", () => {
24
+ let adapter: Adapter<Client>;
25
+ beforeEach(() => PlatformTest.create());
26
+ afterEach(() => PlatformTest.reset());
27
+ beforeEach(() => {
28
+ adapter = PlatformTest.get<Adapters>(Adapters).invokeAdapter<any>({
29
+ collectionName: "clients",
30
+ model: Client,
31
+ adapter: MemoryAdapter
32
+ });
33
+ });
34
+ describe("getSchema", () => {
35
+ it("should", () => {
36
+ expect(getJsonSchema(Client)).toEqual({
37
+ properties: {
38
+ createdAt: {
39
+ format: "date-time",
40
+ type: "string"
41
+ },
42
+ id: {
43
+ type: "string"
44
+ },
45
+ name: {
46
+ type: "string"
47
+ }
48
+ },
49
+ type: "object"
50
+ });
51
+ });
52
+ });
53
+
54
+ describe("create()", () => {
55
+ it("should create a new instance", async () => {
56
+ const base = deserialize(
57
+ {
58
+ name: faker.person.jobTitle(),
59
+ createdAt: faker.date.past()
60
+ },
61
+ {type: Client}
62
+ );
63
+
64
+ const client = await adapter.create(base);
65
+
66
+ expect(client).toBeInstanceOf(Client);
67
+ expect(typeof client._id).toBe("string");
68
+ expect(client.name).toBe(base.name);
69
+ expect(client.createdAt).toEqual(base.createdAt);
70
+ });
71
+
72
+ it("should create a new instance with expireAt", async () => {
73
+ const base = {
74
+ name: faker.person.jobTitle()
75
+ };
76
+
77
+ const client = await adapter.create(base, new Date());
78
+
79
+ expect(client).toBeInstanceOf(Client);
80
+ expect(typeof client._id).toBe("string");
81
+ expect(client.name).toBe(base.name);
82
+ });
83
+ });
84
+
85
+ describe("upsert()", () => {
86
+ it("should create a new instance if not exists", async () => {
87
+ const base: any = {
88
+ name: faker.person.jobTitle()
89
+ };
90
+
91
+ const client = await adapter.upsert(base._id, base);
92
+
93
+ expect(client).toBeInstanceOf(Client);
94
+ expect(typeof client._id).toBe("string");
95
+ expect(client.name).toBe(base.name);
96
+ });
97
+
98
+ it("should update instance if exists", async () => {
99
+ const base: any = {
100
+ name: faker.person.jobTitle()
101
+ };
102
+
103
+ const client = await adapter.upsert(base._id, base);
104
+ const client2 = await adapter.upsert(client._id, client);
105
+
106
+ expect(client2).toBeInstanceOf(Client);
107
+ expect(typeof client2._id).toBe("string");
108
+ expect(client2.name).toBe(base.name);
109
+ });
110
+ });
111
+
112
+ describe("updateOne()", () => {
113
+ it("should update an instance", async () => {
114
+ const base = {
115
+ name: faker.person.jobTitle()
116
+ };
117
+
118
+ const client = await adapter.create(base);
119
+
120
+ const update = {
121
+ _id: client._id,
122
+ name: faker.person.jobTitle()
123
+ };
124
+
125
+ const client2 = await adapter.updateOne({_id: client._id}, update);
126
+
127
+ expect(client2).toBeInstanceOf(Client);
128
+ expect(typeof client2?._id).toBe("string");
129
+ expect(client2?.name).not.toBe(base.name);
130
+ expect(client2?.name).toBe(update.name);
131
+ });
132
+ it("should return undefined", async () => {
133
+ const base = {
134
+ name: faker.person.jobTitle()
135
+ };
136
+
137
+ const client = await adapter.updateOne({_id: faker.string.uuid()}, base);
138
+
139
+ expect(client).toBeUndefined();
140
+ });
141
+ });
142
+
143
+ describe("findById()", () => {
144
+ it("should find by ID", async () => {
145
+ const base = {
146
+ name: faker.person.jobTitle()
147
+ };
148
+
149
+ const client = await adapter.create(base);
150
+ const result = await adapter.findById(client._id);
151
+
152
+ expect(result).toBeInstanceOf(Client);
153
+ expect(result?._id).toBe(client._id);
154
+ expect(result?.name).toBe(base.name);
155
+ });
156
+ });
157
+
158
+ describe("findOne()", () => {
159
+ it("should find one item", async () => {
160
+ const base = {
161
+ name: faker.person.jobTitle()
162
+ };
163
+
164
+ const client = await adapter.create(base);
165
+
166
+ const result = await adapter.findOne({
167
+ name: base.name
168
+ });
169
+
170
+ expect(result).toBeInstanceOf(Client);
171
+ expect(result?._id).toBe(client._id);
172
+ expect(result?.name).toBe(base.name);
173
+ });
174
+ });
175
+ describe("findAll()", () => {
176
+ it("should find all items", async () => {
177
+ const base = {
178
+ name: faker.person.jobTitle()
179
+ };
180
+
181
+ await adapter.create(base);
182
+
183
+ const result = await adapter.findAll({
184
+ name: base.name
185
+ });
186
+
187
+ expect(result[0]).toBeInstanceOf(Client);
188
+ expect(result[0].name).toBe(base.name);
189
+ });
190
+ });
191
+ describe("deleteById()", () => {
192
+ it("should delete an item by id", async () => {
193
+ const base = {
194
+ name: faker.person.jobTitle()
195
+ };
196
+
197
+ const client = await adapter.create(base);
198
+
199
+ const result = await adapter.deleteById(client._id);
200
+
201
+ expect(result).toBeInstanceOf(Client);
202
+ expect(result?.name).toBe(base.name);
203
+ });
204
+ });
205
+ describe("deleteMany()", () => {
206
+ it("should delete many", async () => {
207
+ const base = {
208
+ name: faker.person.jobTitle()
209
+ };
210
+
211
+ const client = await adapter.create(base);
212
+
213
+ await adapter.create({
214
+ name: faker.person.jobTitle()
215
+ });
216
+ await adapter.create({
217
+ name: faker.person.jobTitle()
218
+ });
219
+
220
+ const result = await adapter.deleteMany(client);
221
+
222
+ expect(result[0]).toBeInstanceOf(Client);
223
+ expect(result[0]?.name).toBe(base.name);
224
+ });
225
+ });
226
+ });
@@ -0,0 +1,20 @@
1
+ import {Configuration, Injectable, Opts, ProviderScope, Scope} from "@tsed/di";
2
+ import {Low, Memory} from "lowdb";
3
+
4
+ import {AdapterModel, LowDbAdapter} from "./LowDbAdapter.js";
5
+
6
+ @Injectable()
7
+ @Scope(ProviderScope.INSTANCE)
8
+ export class MemoryAdapter<T extends AdapterModel> extends LowDbAdapter<T> {
9
+ constructor(@Opts options: any, @Configuration() configuration: Configuration) {
10
+ super(options, configuration);
11
+
12
+ this.db = new Low(new Memory(), {
13
+ collection: []
14
+ });
15
+ }
16
+
17
+ $onInit() {
18
+ return this.db.write();
19
+ }
20
+ }
@@ -0,0 +1,74 @@
1
+ import {Injectable} from "@tsed/di";
2
+ import {PlatformTest} from "@tsed/platform-http/testing";
3
+ import {Property} from "@tsed/schema";
4
+
5
+ import {MemoryAdapter} from "../adapters/MemoryAdapter.js";
6
+ import {Adapter} from "../domain/Adapter.js";
7
+ import {Indexed} from "./indexed.js";
8
+ import {InjectAdapter} from "./injectAdapter.js";
9
+
10
+ describe("Indexed", () => {
11
+ beforeEach(() => PlatformTest.create());
12
+ afterEach(() => PlatformTest.create());
13
+ it("should inject adapter with indexed keys", async () => {
14
+ class Client {
15
+ @Indexed()
16
+ prop: string;
17
+ }
18
+
19
+ const stub = vi.fn();
20
+ @Injectable()
21
+ class Clients {
22
+ @InjectAdapter("client", Client)
23
+ adapter: Adapter<Client>;
24
+
25
+ $onInit() {
26
+ stub();
27
+ }
28
+ }
29
+
30
+ const clients = await PlatformTest.invoke<Clients>(Clients);
31
+
32
+ expect(clients.adapter).toBeInstanceOf(MemoryAdapter);
33
+ expect(stub).toHaveBeenCalledWith();
34
+ expect(clients.adapter.collectionName).toBe("client");
35
+ expect(clients.adapter.model).toBe(Client);
36
+ expect(clients.adapter.indexes).toEqual([
37
+ {
38
+ options: {},
39
+ propertyKey: "prop"
40
+ }
41
+ ]);
42
+ });
43
+ it("should inject adapter with indexed keys (by decorator)", async () => {
44
+ class Client {
45
+ @Property()
46
+ prop: string;
47
+ }
48
+
49
+ const stub = vi.fn();
50
+
51
+ @Injectable()
52
+ class Clients {
53
+ @InjectAdapter({model: Client, indexes: {prop: {}}})
54
+ adapter: Adapter<Client>;
55
+
56
+ $onInit() {
57
+ stub();
58
+ }
59
+ }
60
+
61
+ const clients = await PlatformTest.invoke<Clients>(Clients);
62
+
63
+ expect(clients.adapter).toBeInstanceOf(MemoryAdapter);
64
+ expect(stub).toHaveBeenCalledWith();
65
+ expect(clients.adapter.collectionName).toBe("Clients");
66
+ expect(clients.adapter.model).toBe(Client);
67
+ expect(clients.adapter.indexes).toEqual([
68
+ {
69
+ options: {},
70
+ propertyKey: "prop"
71
+ }
72
+ ]);
73
+ });
74
+ });
@@ -0,0 +1,7 @@
1
+ import {JsonEntityFn} from "@tsed/schema";
2
+
3
+ export function Indexed(): PropertyDecorator {
4
+ return JsonEntityFn((entity) => {
5
+ entity.store.set("adapter:indexed", {});
6
+ });
7
+ }
@@ -0,0 +1,292 @@
1
+ import {Injectable} from "@tsed/di";
2
+ import {PlatformTest} from "@tsed/platform-http/testing";
3
+ import {Name, Property} from "@tsed/schema";
4
+
5
+ import {MemoryAdapter} from "../adapters/MemoryAdapter.js";
6
+ import {Adapter} from "../domain/Adapter.js";
7
+ import {adapter, InjectAdapter} from "./injectAdapter.js";
8
+
9
+ describe("InjectAdapter", () => {
10
+ beforeEach(() => PlatformTest.create());
11
+ afterEach(() => PlatformTest.create());
12
+ describe("using decorator @InjectAdapter()", () => {
13
+ it("should inject adapter (model and collectionName)", async () => {
14
+ class Client {
15
+ @Property()
16
+ _id: string;
17
+
18
+ @Name("client_id")
19
+ clientId: string;
20
+ }
21
+
22
+ const stub = vi.fn();
23
+
24
+ @Injectable()
25
+ class Clients {
26
+ @InjectAdapter("client", Client)
27
+ adapter: Adapter<Client>;
28
+
29
+ $onInit() {
30
+ stub();
31
+ }
32
+ }
33
+
34
+ const clients = await PlatformTest.invoke<Clients>(Clients);
35
+
36
+ expect(clients.adapter).toBeInstanceOf(MemoryAdapter);
37
+ expect(stub).toHaveBeenCalledWith();
38
+ expect(clients.adapter.collectionName).toBe("client");
39
+ expect(clients.adapter.model).toBe(Client);
40
+
41
+ const client = new Client();
42
+ client.clientId = "test";
43
+
44
+ await clients.adapter.create(client);
45
+
46
+ const items = (clients.adapter as MemoryAdapter<Client>).collection;
47
+ expect(items).toEqual([
48
+ {
49
+ _id: expect.any(String),
50
+ clientId: "test"
51
+ }
52
+ ]);
53
+ });
54
+ it("should inject adapter (model, collectionName and useAlias true)", async () => {
55
+ class Client {
56
+ @Property()
57
+ _id: string;
58
+
59
+ @Name("client_id")
60
+ clientId: string;
61
+ }
62
+
63
+ const stub = vi.fn();
64
+
65
+ @Injectable()
66
+ class Clients {
67
+ @InjectAdapter("client", Client, {useAlias: true})
68
+ adapter: Adapter<Client>;
69
+
70
+ $onInit() {
71
+ stub();
72
+ }
73
+ }
74
+
75
+ const clients = await PlatformTest.invoke<Clients>(Clients);
76
+
77
+ expect(clients.adapter).toBeInstanceOf(MemoryAdapter);
78
+ expect(stub).toHaveBeenCalledWith();
79
+ expect(clients.adapter.collectionName).toBe("client");
80
+ expect(clients.adapter.model).toBe(Client);
81
+ expect(clients.adapter.useAlias).toEqual(true);
82
+
83
+ const client = new Client();
84
+ client.clientId = "test";
85
+
86
+ await clients.adapter.create(client);
87
+
88
+ const items = (clients.adapter as MemoryAdapter<Client>).collection;
89
+ expect(items).toEqual([
90
+ {
91
+ _id: expect.any(String),
92
+ client_id: "test"
93
+ }
94
+ ]);
95
+ });
96
+ it("should inject adapter (model only)", async () => {
97
+ class Client {}
98
+
99
+ @Injectable()
100
+ class Clients {
101
+ @InjectAdapter(Client)
102
+ adapter: Adapter<Client>;
103
+ }
104
+
105
+ const clients = await PlatformTest.invoke<Clients>(Clients);
106
+
107
+ expect(clients.adapter).toBeInstanceOf(MemoryAdapter);
108
+ expect(clients.adapter.collectionName).toBe("Clients");
109
+ expect(clients.adapter.model).toBe(Client);
110
+ });
111
+ it("should inject adapter (model only 2)", async () => {
112
+ class Entity {}
113
+
114
+ @Injectable()
115
+ class Clients {
116
+ @InjectAdapter(Entity)
117
+ adapter: Adapter<Entity>;
118
+ }
119
+
120
+ const clients = await PlatformTest.invoke<Clients>(Clients);
121
+
122
+ expect(clients.adapter).toBeInstanceOf(MemoryAdapter);
123
+ expect(clients.adapter.collectionName).toBe("Entities");
124
+ expect(clients.adapter.model).toBe(Entity);
125
+ });
126
+ it("should inject adapter (object)", async () => {
127
+ class Client {}
128
+
129
+ @Injectable()
130
+ class Clients {
131
+ @InjectAdapter({model: Client, indexes: {}})
132
+ adapter: Adapter<Client>;
133
+ }
134
+
135
+ const clients = await PlatformTest.invoke<Clients>(Clients);
136
+
137
+ expect(clients.adapter).toBeInstanceOf(MemoryAdapter);
138
+ expect(clients.adapter.collectionName).toBe("Clients");
139
+ expect(clients.adapter.model).toBe(Client);
140
+ });
141
+ it("should inject adapter (without $onInit)", async () => {
142
+ class Client {}
143
+
144
+ @Injectable()
145
+ class Clients {
146
+ @InjectAdapter("client", Client)
147
+ adapter: Adapter<Client>;
148
+ }
149
+
150
+ const clients = await PlatformTest.invoke<Clients>(Clients);
151
+
152
+ expect(clients.adapter).toBeInstanceOf(MemoryAdapter);
153
+ });
154
+ });
155
+ describe("using function adapter()", () => {
156
+ it("should inject adapter (model and collectionName)", async () => {
157
+ class Client {
158
+ @Property()
159
+ _id: string;
160
+
161
+ @Name("client_id")
162
+ clientId: string;
163
+ }
164
+
165
+ const stub = vi.fn();
166
+
167
+ @Injectable()
168
+ class Clients {
169
+ adapter = adapter("client", Client);
170
+
171
+ $onInit() {
172
+ stub();
173
+ }
174
+ }
175
+
176
+ const clients = await PlatformTest.invoke<Clients>(Clients);
177
+
178
+ expect(clients.adapter).toBeInstanceOf(MemoryAdapter);
179
+ expect(stub).toHaveBeenCalledWith();
180
+ expect(clients.adapter.collectionName).toBe("client");
181
+ expect(clients.adapter.model).toBe(Client);
182
+
183
+ const client = new Client();
184
+ client.clientId = "test";
185
+
186
+ await clients.adapter.create(client);
187
+
188
+ const items = (clients.adapter as MemoryAdapter<Client>).collection;
189
+ expect(items).toEqual([
190
+ {
191
+ _id: expect.any(String),
192
+ clientId: "test"
193
+ }
194
+ ]);
195
+ });
196
+ it("should inject adapter (model, collectionName and useAlias true)", async () => {
197
+ class Client {
198
+ @Property()
199
+ _id: string;
200
+
201
+ @Name("client_id")
202
+ clientId: string;
203
+ }
204
+
205
+ const stub = vi.fn();
206
+
207
+ @Injectable()
208
+ class Clients {
209
+ adapter = adapter("client", Client, {useAlias: true});
210
+
211
+ $onInit() {
212
+ stub();
213
+ }
214
+ }
215
+
216
+ const clients = await PlatformTest.invoke<Clients>(Clients);
217
+
218
+ expect(clients.adapter).toBeInstanceOf(MemoryAdapter);
219
+ expect(stub).toHaveBeenCalledWith();
220
+ expect(clients.adapter.collectionName).toBe("client");
221
+ expect(clients.adapter.model).toBe(Client);
222
+ expect(clients.adapter.useAlias).toEqual(true);
223
+
224
+ const client = new Client();
225
+ client.clientId = "test";
226
+
227
+ await clients.adapter.create(client);
228
+
229
+ const items = (clients.adapter as MemoryAdapter<Client>).collection;
230
+ expect(items).toEqual([
231
+ {
232
+ _id: expect.any(String),
233
+ client_id: "test"
234
+ }
235
+ ]);
236
+ });
237
+ it("should inject adapter (model only)", async () => {
238
+ class Client {}
239
+
240
+ @Injectable()
241
+ class Clients {
242
+ adapter = adapter(Client);
243
+ }
244
+
245
+ const clients = await PlatformTest.invoke<Clients>(Clients);
246
+
247
+ expect(clients.adapter).toBeInstanceOf(MemoryAdapter);
248
+ expect(clients.adapter.collectionName).toBe("Clients");
249
+ expect(clients.adapter.model).toBe(Client);
250
+ });
251
+ it("should inject adapter (model only 2)", async () => {
252
+ class Entity {}
253
+
254
+ @Injectable()
255
+ class Clients {
256
+ adapter: Adapter<Entity> = adapter(Entity);
257
+ }
258
+
259
+ const clients = await PlatformTest.invoke<Clients>(Clients);
260
+
261
+ expect(clients.adapter).toBeInstanceOf(MemoryAdapter);
262
+ expect(clients.adapter.collectionName).toBe("Entities");
263
+ expect(clients.adapter.model).toBe(Entity);
264
+ });
265
+ it("should inject adapter (object)", async () => {
266
+ class Client {}
267
+
268
+ @Injectable()
269
+ class Clients {
270
+ adapter = adapter({model: Client, indexes: {}});
271
+ }
272
+
273
+ const clients = await PlatformTest.invoke<Clients>(Clients);
274
+
275
+ expect(clients.adapter).toBeInstanceOf(MemoryAdapter);
276
+ expect(clients.adapter.collectionName).toBe("Clients");
277
+ expect(clients.adapter.model).toBe(Client);
278
+ });
279
+ it("should inject adapter (without $onInit)", async () => {
280
+ class Client {}
281
+
282
+ @Injectable()
283
+ class Clients {
284
+ adapter = adapter("client", Client);
285
+ }
286
+
287
+ const clients = await PlatformTest.invoke<Clients>(Clients);
288
+
289
+ expect(clients.adapter).toBeInstanceOf(MemoryAdapter);
290
+ });
291
+ });
292
+ });
@@ -0,0 +1,62 @@
1
+ import {classOf, isClass, isString, Type} from "@tsed/core";
2
+ import {Inject, inject} from "@tsed/di";
3
+
4
+ import type {Adapter} from "../domain/Adapter.js";
5
+ import {AdapterInvokeOptions, Adapters} from "../services/Adapters.js";
6
+
7
+ function mapOptions(args: any[]): AdapterInvokeOptions {
8
+ return args.reduce((options: AdapterInvokeOptions, item, index) => {
9
+ if (isString(item)) {
10
+ return {
11
+ ...options,
12
+ collectionName: item
13
+ };
14
+ }
15
+
16
+ if (isClass(item) && classOf(item) !== Object) {
17
+ return {
18
+ ...options,
19
+ model: item
20
+ };
21
+ }
22
+
23
+ return {...options, ...item};
24
+ }, {});
25
+ }
26
+
27
+ export function adapter<Model>(options: AdapterInvokeOptions<Model>): Adapter<Model>;
28
+ export function adapter<Model>(model: Type<Model>, options?: Partial<Omit<AdapterInvokeOptions, "client">>): Adapter<Model>;
29
+ export function adapter<Model>(
30
+ collectionName: string,
31
+ model: Type<Model>,
32
+ options?: Partial<Omit<AdapterInvokeOptions, "collectionName" | "client">>
33
+ ): Adapter<Model>;
34
+ export function adapter(...args: any[]) {
35
+ const options: AdapterInvokeOptions = mapOptions(args);
36
+ const adapters = inject(Adapters);
37
+
38
+ return adapters.invokeAdapter(options);
39
+ }
40
+
41
+ /**
42
+ * Inject the adapter in the property.
43
+ * @param options
44
+ * @constructor
45
+ */
46
+ export function InjectAdapter(options: AdapterInvokeOptions): PropertyDecorator;
47
+ export function InjectAdapter(model: Type<any>, options?: Partial<Omit<AdapterInvokeOptions, "client">>): PropertyDecorator;
48
+ export function InjectAdapter(
49
+ collectionName: string,
50
+ model: Type<any>,
51
+ options?: Partial<Omit<AdapterInvokeOptions, "collectionName" | "client">>
52
+ ): PropertyDecorator;
53
+ export function InjectAdapter(...args: any[]): PropertyDecorator {
54
+ const options: AdapterInvokeOptions = mapOptions(args);
55
+ const symbol = Symbol();
56
+
57
+ return Inject(Adapters, {
58
+ transform: (adapters: Adapters, {self}) => {
59
+ return (self[symbol] = self[symbol] || adapters.invokeAdapter(options));
60
+ }
61
+ });
62
+ }