kontas-express 1.0.3 → 1.0.5
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/README.md +1 -15
- package/dist/index.js +151 -2
- package/package.json +1 -1
package/README.md
CHANGED
@@ -1,15 +1 @@
|
|
1
|
-
# kontas-express
|
2
|
-
|
3
|
-
To install dependencies:
|
4
|
-
|
5
|
-
```bash
|
6
|
-
bun install
|
7
|
-
```
|
8
|
-
|
9
|
-
To run:
|
10
|
-
|
11
|
-
```bash
|
12
|
-
bun run index.js
|
13
|
-
```
|
14
|
-
|
15
|
-
This project was created using `bun init` in bun v1.1.43. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime.
|
1
|
+
# kontas-express
|
package/dist/index.js
CHANGED
@@ -1,3 +1,152 @@
|
|
1
1
|
// @bun
|
2
|
-
//
|
3
|
-
|
2
|
+
// src/database/mongodb/repository.template.js
|
3
|
+
var generateMongoRepository = (name, fields) => `// @ts-check
|
4
|
+
import { ObjectId } from "mongodb"
|
5
|
+
import { connectToDatabase, getDatabaseCollection } from "../config"
|
6
|
+
import type { Create${name}, Update${name}, ${name}Id, ${name} } from "./${name.toLowerCase()}.schema"
|
7
|
+
import { COLLECTION_NAME } from "./${name.toLowerCase()}.schema"
|
8
|
+
|
9
|
+
// Response type
|
10
|
+
/** @template T */
|
11
|
+
class ${name}Response {
|
12
|
+
/** @type {boolean} */
|
13
|
+
success
|
14
|
+
/** @type {string=} */
|
15
|
+
message
|
16
|
+
/** @type {T} */
|
17
|
+
value
|
18
|
+
|
19
|
+
/**
|
20
|
+
* @param {{
|
21
|
+
* success: boolean,
|
22
|
+
* message?: string,
|
23
|
+
* value: T
|
24
|
+
* }} params
|
25
|
+
*/
|
26
|
+
constructor(params) {
|
27
|
+
this.success = params.success
|
28
|
+
this.message = params.message
|
29
|
+
this.value = params.value
|
30
|
+
}
|
31
|
+
}
|
32
|
+
|
33
|
+
class ${name} {
|
34
|
+
/** @type {ObjectId} */
|
35
|
+
_id
|
36
|
+
${fields.map((f) => `/** @type {${f.type}} */
|
37
|
+
${f.name}`).join(`
|
38
|
+
`)}
|
39
|
+
/** @type {Date} */
|
40
|
+
createdAt
|
41
|
+
/** @type {Date} */
|
42
|
+
updatedAt
|
43
|
+
|
44
|
+
/** @param {any} data */
|
45
|
+
constructor(data) {
|
46
|
+
Object.assign(this, {
|
47
|
+
...data,
|
48
|
+
_id: data._id instanceof ObjectId ? data._id : new ObjectId(data._id),
|
49
|
+
createdAt: new Date(data.createdAt),
|
50
|
+
updatedAt: new Date(data.updatedAt)
|
51
|
+
})
|
52
|
+
}
|
53
|
+
}
|
54
|
+
|
55
|
+
export class ${name}Repository {
|
56
|
+
/** @type {string} */
|
57
|
+
collectionName = COLLECTION_NAME
|
58
|
+
|
59
|
+
async connect() {
|
60
|
+
await connectToDatabase()
|
61
|
+
return getDatabaseCollection(this.collectionName)
|
62
|
+
}
|
63
|
+
|
64
|
+
/**
|
65
|
+
* @param {Create${name}} data
|
66
|
+
* @returns {Promise<${name}Response<${name}>>}
|
67
|
+
*/
|
68
|
+
async create(data) {
|
69
|
+
const collection = await this.connect()
|
70
|
+
|
71
|
+
const doc = {
|
72
|
+
...data,
|
73
|
+
createdAt: new Date(),
|
74
|
+
updatedAt: new Date()
|
75
|
+
}
|
76
|
+
|
77
|
+
const result = await collection.insertOne(doc)
|
78
|
+
return new ${name}Response({
|
79
|
+
success: true,
|
80
|
+
message: "${name} berhasil dibuat",
|
81
|
+
value: new ${name}({ ...doc, _id: result.insertedId })
|
82
|
+
})
|
83
|
+
}
|
84
|
+
|
85
|
+
/** @returns {Promise<${name}Response<${name}[]>>} */
|
86
|
+
async findAll() {
|
87
|
+
const collection = await this.connect()
|
88
|
+
const docs = await collection.find({}).toArray()
|
89
|
+
return new ${name}Response({
|
90
|
+
success: true,
|
91
|
+
value: docs.map(doc => new ${name}(doc))
|
92
|
+
})
|
93
|
+
}
|
94
|
+
|
95
|
+
/**
|
96
|
+
* @param {string} id
|
97
|
+
* @returns {Promise<${name}Response<${name} | null>>}
|
98
|
+
*/
|
99
|
+
async findById(id) {
|
100
|
+
const collection = await this.connect()
|
101
|
+
const doc = await collection.findOne({ _id: new ObjectId(id) })
|
102
|
+
return new ${name}Response({
|
103
|
+
success: true,
|
104
|
+
value: doc ? new ${name}(doc) : null
|
105
|
+
})
|
106
|
+
}
|
107
|
+
|
108
|
+
/**
|
109
|
+
* @param {string} id
|
110
|
+
* @param {Update${name}} data
|
111
|
+
* @returns {Promise<${name}Response<${name} | null>>}
|
112
|
+
*/
|
113
|
+
async update(id, data) {
|
114
|
+
const collection = await this.connect()
|
115
|
+
|
116
|
+
const result = await collection.findOneAndUpdate(
|
117
|
+
{ _id: new ObjectId(id) },
|
118
|
+
{
|
119
|
+
$set: {
|
120
|
+
...data,
|
121
|
+
updatedAt: new Date()
|
122
|
+
}
|
123
|
+
},
|
124
|
+
{ returnDocument: 'after' }
|
125
|
+
)
|
126
|
+
|
127
|
+
return new ${name}Response({
|
128
|
+
success: true,
|
129
|
+
message: result ? "${name} berhasil diupdate" : "${name} tidak ditemukan",
|
130
|
+
value: result ? new ${name}(result) : null
|
131
|
+
})
|
132
|
+
}
|
133
|
+
|
134
|
+
/**
|
135
|
+
* @param {string} id
|
136
|
+
* @returns {Promise<${name}Response<void>>}
|
137
|
+
*/
|
138
|
+
async delete(id) {
|
139
|
+
const collection = await this.connect()
|
140
|
+
await collection.deleteOne({ _id: new ObjectId(id) })
|
141
|
+
return new ${name}Response({
|
142
|
+
success: true,
|
143
|
+
message: "${name} berhasil dihapus",
|
144
|
+
value: undefined
|
145
|
+
})
|
146
|
+
}
|
147
|
+
}
|
148
|
+
|
149
|
+
export const ${name.toLowerCase()}Repository = new ${name}Repository()`;
|
150
|
+
export {
|
151
|
+
generateMongoRepository
|
152
|
+
};
|