@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.
@@ -0,0 +1,114 @@
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
+ }
@@ -0,0 +1,29 @@
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 ADDED
@@ -0,0 +1,11 @@
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";
@@ -0,0 +1,21 @@
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);