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