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