kontas-express 1.0.7 → 1.0.9
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/dist/index.js +99 -52
- package/package.json +1 -1
package/dist/index.js
CHANGED
@@ -3,89 +3,136 @@
|
|
3
3
|
var generateMongoRepository = (name, pluralName) => `import { ObjectId } from "mongodb"
|
4
4
|
import { connectToDatabase, getDatabaseCollection } from "../../config"
|
5
5
|
import type { Create${name}, Update${name}, ${pluralName}, ${name}Id } from "./${name.toLowerCase()}.schema"
|
6
|
+
import { ${name} } from "./${name.toLowerCase()}.schema"
|
6
7
|
|
7
8
|
type ${name}Response<T> = {
|
8
|
-
success:
|
9
|
+
success: true
|
9
10
|
message?: string
|
10
11
|
value: T
|
12
|
+
} | {
|
13
|
+
success: false
|
14
|
+
message: string
|
15
|
+
error: unknown
|
16
|
+
value: null
|
11
17
|
}
|
12
18
|
|
13
19
|
export class ${name}Repository {
|
14
|
-
private collectionName = "${pluralName}"
|
20
|
+
private readonly collectionName = "${pluralName}"
|
21
|
+
private collection: ReturnType<typeof getDatabaseCollection> | null = null
|
15
22
|
|
16
|
-
async
|
17
|
-
|
18
|
-
|
23
|
+
private async getCollection() {
|
24
|
+
if (!this.collection) {
|
25
|
+
await connectToDatabase()
|
26
|
+
this.collection = getDatabaseCollection(this.collectionName)
|
27
|
+
}
|
28
|
+
return this.collection
|
19
29
|
}
|
20
30
|
|
21
|
-
async create(data: Create${name}): Promise<${name}Response<${pluralName}>> {
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
31
|
+
async create(data: Readonly<Create${name}>): Promise<${name}Response<${pluralName}>> {
|
32
|
+
try {
|
33
|
+
const collection = await this.getCollection()
|
34
|
+
|
35
|
+
const doc = {
|
36
|
+
...data,
|
37
|
+
createdAt: new Date(),
|
38
|
+
updatedAt: new Date()
|
39
|
+
} as const
|
40
|
+
|
41
|
+
const result = await collection.insertOne(doc)
|
42
|
+
return {
|
43
|
+
success: true,
|
44
|
+
message: "${name} created successfully",
|
45
|
+
value: new ${name}({ ...doc, _id: result.insertedId }) as ${pluralName}
|
46
|
+
}
|
47
|
+
} catch (error) {
|
48
|
+
return {
|
49
|
+
success: false,
|
50
|
+
message: "Failed to create ${name.toLowerCase()}",
|
51
|
+
error,
|
52
|
+
value: null
|
53
|
+
}
|
35
54
|
}
|
36
55
|
}
|
37
56
|
|
38
|
-
async findAll(): Promise<${name}Response<${
|
39
|
-
const collection = await this.
|
40
|
-
const docs = await collection.find({}).toArray()
|
57
|
+
async findAll(): Promise<${name}Response<${name}[]>> {
|
58
|
+
const collection = await this.getCollection()
|
59
|
+
const docs = await collection.find({}).toArray() as ${pluralName}[]
|
41
60
|
return {
|
42
61
|
success: true,
|
43
|
-
value: docs
|
62
|
+
value: docs.map(doc => new ${name}(doc))
|
44
63
|
}
|
45
64
|
}
|
46
65
|
|
47
|
-
async findById({ id }:
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
66
|
+
async findById({ id }: Readonly<${name}Id>): Promise<${name}Response<${name} | null>> {
|
67
|
+
try {
|
68
|
+
const collection = await this.getCollection()
|
69
|
+
const doc = await collection.findOne({ _id: new ObjectId(id) }) as ${pluralName} | null
|
70
|
+
return {
|
71
|
+
success: true,
|
72
|
+
value: doc ? new ${name}(doc) : null
|
73
|
+
}
|
74
|
+
} catch (error) {
|
75
|
+
return {
|
76
|
+
success: false,
|
77
|
+
message: "Failed to find ${name.toLowerCase()}",
|
78
|
+
error,
|
79
|
+
value: null
|
80
|
+
}
|
53
81
|
}
|
54
82
|
}
|
55
83
|
|
56
|
-
async update({ id }:
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
84
|
+
async update({ id }: Readonly<${name}Id>, data: Readonly<Update${name}>): Promise<${name}Response<${pluralName} | null>> {
|
85
|
+
try {
|
86
|
+
const collection = await this.getCollection()
|
87
|
+
|
88
|
+
const result = await collection.findOneAndUpdate(
|
89
|
+
{ _id: new ObjectId(id) },
|
90
|
+
{
|
91
|
+
$set: {
|
92
|
+
...data,
|
93
|
+
updatedAt: new Date()
|
94
|
+
}
|
95
|
+
},
|
96
|
+
{ returnDocument: 'after' }
|
97
|
+
)
|
98
|
+
|
99
|
+
return {
|
100
|
+
success: true,
|
101
|
+
message: result ? "${name} updated successfully" : "${name} not found",
|
102
|
+
value: result ? new ${name}(result) as ${pluralName} : null
|
103
|
+
}
|
104
|
+
} catch (error) {
|
105
|
+
return {
|
106
|
+
success: false,
|
107
|
+
message: "Failed to update ${name.toLowerCase()}",
|
108
|
+
error,
|
109
|
+
value: null
|
110
|
+
}
|
74
111
|
}
|
75
112
|
}
|
76
113
|
|
77
|
-
async delete({ id }: ${name}Id): Promise<${name}Response
|
78
|
-
const collection = await this.
|
114
|
+
async delete({ id }: ${name}Id): Promise<${name}Response<${pluralName} | null>> {
|
115
|
+
const collection = await this.getCollection()
|
116
|
+
|
117
|
+
const doc = await collection.findOne({ _id: new ObjectId(id) })
|
118
|
+
if (!doc) {
|
119
|
+
return {
|
120
|
+
success: true,
|
121
|
+
message: "${name} not found",
|
122
|
+
value: null
|
123
|
+
}
|
124
|
+
}
|
125
|
+
|
79
126
|
await collection.deleteOne({ _id: new ObjectId(id) })
|
80
127
|
return {
|
81
128
|
success: true,
|
82
|
-
message: "${name}
|
83
|
-
value:
|
129
|
+
message: "${name} deleted successfully",
|
130
|
+
value: new ${name}(doc) as ${pluralName}
|
84
131
|
}
|
85
132
|
}
|
86
133
|
}
|
87
134
|
|
88
|
-
export const ${name.toLowerCase()}
|
135
|
+
export const ${name.toLowerCase()} = new ${name}Repository()`;
|
89
136
|
export {
|
90
137
|
generateMongoRepository
|
91
138
|
};
|