@tsed/adapters 8.11.1 → 8.11.2-rc.1

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.
Files changed (34) hide show
  1. package/lib/esm/adapters/FileSyncAdapter.js +0 -1
  2. package/lib/esm/adapters/LowDbAdapter.js +0 -1
  3. package/lib/esm/adapters/MemoryAdapter.js +0 -1
  4. package/lib/esm/decorators/indexed.js +0 -1
  5. package/lib/esm/decorators/injectAdapter.js +0 -1
  6. package/lib/esm/domain/Adapter.js +0 -1
  7. package/lib/esm/domain/AdaptersSettings.js +0 -1
  8. package/lib/esm/index.js +0 -1
  9. package/lib/esm/services/Adapters.js +0 -1
  10. package/package.json +13 -10
  11. package/lib/esm/adapters/FileSyncAdapter.js.map +0 -1
  12. package/lib/esm/adapters/LowDbAdapter.js.map +0 -1
  13. package/lib/esm/adapters/MemoryAdapter.js.map +0 -1
  14. package/lib/esm/decorators/indexed.js.map +0 -1
  15. package/lib/esm/decorators/injectAdapter.js.map +0 -1
  16. package/lib/esm/domain/Adapter.js.map +0 -1
  17. package/lib/esm/domain/AdaptersSettings.js.map +0 -1
  18. package/lib/esm/index.js.map +0 -1
  19. package/lib/esm/services/Adapters.js.map +0 -1
  20. package/src/adapters/FileSyncAdapter.spec.ts +0 -167
  21. package/src/adapters/FileSyncAdapter.ts +0 -37
  22. package/src/adapters/LowDbAdapter.spec.ts +0 -205
  23. package/src/adapters/LowDbAdapter.ts +0 -135
  24. package/src/adapters/MemoryAdapter.spec.ts +0 -226
  25. package/src/adapters/MemoryAdapter.ts +0 -20
  26. package/src/decorators/indexed.spec.ts +0 -74
  27. package/src/decorators/indexed.ts +0 -7
  28. package/src/decorators/injectAdapter.spec.ts +0 -292
  29. package/src/decorators/injectAdapter.ts +0 -62
  30. package/src/domain/Adapter.ts +0 -114
  31. package/src/domain/AdaptersSettings.ts +0 -29
  32. package/src/index.ts +0 -11
  33. package/src/services/Adapters.ts +0 -21
  34. package/vitest.config.mts +0 -21
@@ -1,292 +0,0 @@
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
- });
@@ -1,62 +0,0 @@
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
- }
@@ -1,114 +0,0 @@
1
- import {AjvService} from "@tsed/ajv";
2
- import {classOf, isArray, isPlainObject, nameOf, Type} from "@tsed/core";
3
- import {Configuration, Inject, Opts} from "@tsed/di";
4
- import {deserialize, JsonDeserializerOptions, JsonSerializerOptions, serialize} from "@tsed/json-mapper";
5
- import {getPropertiesStores} from "@tsed/schema";
6
-
7
- export interface AdapterConstructorOptions<T = any> extends Record<string, any> {
8
- model: Type<T> | Object;
9
- collectionName?: string;
10
- useAlias?: string;
11
- indexes?: {
12
- [propertyKey: string]: Record<string, any>;
13
- };
14
- }
15
-
16
- function getIndexes(model: any) {
17
- return [...getPropertiesStores(model).entries()]
18
- .filter(([, entity]) => entity.store.has("adapter:indexed"))
19
- .map(([propertyKey, entity]) => {
20
- return {
21
- propertyKey: String(propertyKey),
22
- options: entity.store.get("adapter:indexed")
23
- };
24
- });
25
- }
26
-
27
- function mapIndexes(indexes: {[p: string]: Record<string, any>} | {propertyKey: string; options: any}[]) {
28
- return Object.entries(indexes).reduce((indexes: any[], [propertyKey, options]) => {
29
- return indexes.concat({
30
- propertyKey,
31
- options
32
- });
33
- }, []);
34
- }
35
-
36
- function pluralize(name: string) {
37
- return name.slice(-1) === "y" ? `${name.slice(0, name.length - 1)}ies` : name + "s";
38
- }
39
-
40
- export abstract class Adapter<Model = any> {
41
- readonly model: Type<Model> | Object;
42
- readonly collectionName: string;
43
- readonly indexes: {propertyKey: string; options: any}[];
44
- readonly useAlias: boolean = false;
45
-
46
- @Inject()
47
- protected ajvService: AjvService;
48
-
49
- constructor(
50
- @Opts options: AdapterConstructorOptions,
51
- @Configuration() protected configuration: Configuration
52
- ) {
53
- this.model = options.model;
54
- this.collectionName = options.collectionName || pluralize(nameOf(options.model));
55
-
56
- let indexes = options.indexes || configuration.get(`adapters.${this.collectionName}.indexes`) || getIndexes(this.model);
57
-
58
- if (!isArray(indexes)) {
59
- indexes = mapIndexes(indexes);
60
- }
61
-
62
- this.indexes = indexes;
63
- this.useAlias = !!options.useAlias;
64
- }
65
-
66
- async validate(value: Model): Promise<void> {
67
- if (this.getModel()) {
68
- await this.ajvService.validate(this.serialize(value), {
69
- useAlias: this.useAlias,
70
- type: this.model
71
- });
72
- }
73
- }
74
-
75
- abstract create(value: Partial<Omit<Model, "_id">>, expiresAt?: Date): Promise<Model>;
76
-
77
- abstract update(id: string, value: Model, expiresAt?: Date): Promise<Model | undefined>;
78
-
79
- abstract updateOne(predicate: Partial<Model>, value: Partial<Model>, expiresAt?: Date): Promise<Model | undefined>;
80
-
81
- abstract upsert(id: string, value: Model, expiresAt?: Date): Promise<Model>;
82
-
83
- abstract findOne(predicate: Partial<Model>): Promise<Model | undefined>;
84
-
85
- abstract findById(id: string): Promise<Model | undefined>;
86
-
87
- abstract findAll(predicate?: Partial<Model>): Promise<Model[]>;
88
-
89
- abstract deleteOne(predicate: Partial<Model>): Promise<Model | undefined>;
90
-
91
- abstract deleteMany(predicate: Partial<Model>): Promise<Model[]>;
92
-
93
- abstract deleteById(id: string): Promise<Model | undefined>;
94
-
95
- getModel() {
96
- return (this.model as unknown) !== Object ? this.model : undefined;
97
- }
98
-
99
- protected deserialize(obj: any, opts?: JsonDeserializerOptions) {
100
- return deserialize(obj, {
101
- useAlias: this.useAlias,
102
- ...opts,
103
- type: this.getModel()
104
- });
105
- }
106
-
107
- protected serialize(obj: any, opts?: JsonSerializerOptions) {
108
- return serialize(obj, {
109
- useAlias: this.useAlias,
110
- ...opts,
111
- type: isPlainObject(obj) ? this.getModel() : classOf(obj)
112
- });
113
- }
114
- }
@@ -1,29 +0,0 @@
1
- import {Type} from "@tsed/core";
2
-
3
- import {Adapter} from "./Adapter.js";
4
-
5
- export interface AdaptersSettings {
6
- /**
7
- * Injectable service to manage database connexion
8
- */
9
- Adapter?: Type<Adapter>;
10
- /**
11
- *
12
- */
13
- lowdbDir?: string;
14
- /**
15
- * Use the connection name for the RedisAdapter.
16
- */
17
- connectionName?: string;
18
-
19
- [key: string]: any;
20
- }
21
-
22
- declare global {
23
- namespace TsED {
24
- interface Configuration {
25
- // @ts-ignore
26
- adapters: AdaptersSettings;
27
- }
28
- }
29
- }
package/src/index.ts DELETED
@@ -1,11 +0,0 @@
1
- /**
2
- * @file Automatically generated by @tsed/barrels.
3
- */
4
- export * from "./adapters/FileSyncAdapter.js";
5
- export * from "./adapters/LowDbAdapter.js";
6
- export * from "./adapters/MemoryAdapter.js";
7
- export * from "./decorators/indexed.js";
8
- export * from "./decorators/injectAdapter.js";
9
- export * from "./domain/Adapter.js";
10
- export * from "./domain/AdaptersSettings.js";
11
- export * from "./services/Adapters.js";
@@ -1,21 +0,0 @@
1
- import {Type} from "@tsed/core";
2
- import {constant, inject, injectable} from "@tsed/di";
3
-
4
- import {MemoryAdapter} from "../adapters/MemoryAdapter.js";
5
- import {Adapter, AdapterConstructorOptions} from "../domain/Adapter.js";
6
-
7
- export interface AdapterInvokeOptions<Model = any> extends AdapterConstructorOptions<Model> {
8
- adapter?: Type<Adapter<Model>>;
9
- }
10
-
11
- export class Adapters {
12
- invokeAdapter<T = any>(options: AdapterInvokeOptions): Adapter<T> {
13
- const {adapter = constant("adapters.Adapter", MemoryAdapter), ...props} = options;
14
-
15
- return inject<Adapter<T>>(adapter, {
16
- useOpts: props
17
- });
18
- }
19
- }
20
-
21
- injectable(Adapters);
package/vitest.config.mts DELETED
@@ -1,21 +0,0 @@
1
- // @ts-ignore
2
- import {presets} from "@tsed/vitest/presets";
3
- import {defineConfig} from "vitest/config";
4
-
5
- export default defineConfig(
6
- {
7
- ...presets,
8
- test: {
9
- ...presets.test,
10
- coverage: {
11
- ...presets.test.coverage,
12
- thresholds: {
13
- statements: 0,
14
- branches: 0,
15
- functions: 0,
16
- lines: 0
17
- }
18
- }
19
- }
20
- }
21
- );