@tsed/adapters 8.11.0 → 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,135 +0,0 @@
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
- }
@@ -1,226 +0,0 @@
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
- });
@@ -1,20 +0,0 @@
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
- }
@@ -1,74 +0,0 @@
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
- });
@@ -1,7 +0,0 @@
1
- import {JsonEntityFn} from "@tsed/schema";
2
-
3
- export function Indexed(): PropertyDecorator {
4
- return JsonEntityFn((entity) => {
5
- entity.store.set("adapter:indexed", {});
6
- });
7
- }