kontas-express 1.0.4 → 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.
Files changed (2) hide show
  1. package/dist/index.js +118 -2
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -1,3 +1,119 @@
1
1
  // @bun
2
- // index.js
3
- console.log("Kontas Express Framework");
2
+ // src/database/mongodb/repository.template.js
3
+ var generateMongoRepository = (name, fields) => `import { ObjectId } from "mongodb"
4
+ import { connectToDatabase, getDatabaseCollection } from "../config"
5
+ import type { Create${name}, Update${name}, ${name}Id, ${name} } from "./${name.toLowerCase()}.schema"
6
+ import { COLLECTION_NAME } from "./${name.toLowerCase()}.schema"
7
+
8
+ class ${name}Response<T> {
9
+ success: boolean
10
+ message?: string
11
+ 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
+ }
40
+
41
+ export class ${name}Repository {
42
+ private collectionName = COLLECTION_NAME
43
+
44
+ async connect() {
45
+ await connectToDatabase()
46
+ return getDatabaseCollection(this.collectionName)
47
+ }
48
+
49
+ async create(data: Create${name}): Promise<${name}Response<${name}>> {
50
+ const collection = await this.connect()
51
+
52
+ const doc = {
53
+ ...data,
54
+ createdAt: new Date(),
55
+ updatedAt: new Date()
56
+ }
57
+
58
+ const result = await collection.insertOne(doc)
59
+ return new ${name}Response({
60
+ success: true,
61
+ message: "${name} berhasil dibuat",
62
+ value: new ${name}({ ...doc, _id: result.insertedId })
63
+ })
64
+ }
65
+
66
+ async findAll(): Promise<${name}Response<${name}[]>> {
67
+ const collection = await this.connect()
68
+ const docs = await collection.find({}).toArray()
69
+ return new ${name}Response({
70
+ success: true,
71
+ value: docs.map(doc => new ${name}(doc))
72
+ })
73
+ }
74
+
75
+ async findById(id: string): Promise<${name}Response<${name} | null>> {
76
+ const collection = await this.connect()
77
+ const doc = await collection.findOne({ _id: new ObjectId(id) })
78
+ return new ${name}Response({
79
+ success: true,
80
+ value: doc ? new ${name}(doc) : null
81
+ })
82
+ }
83
+
84
+ async update(id: string, data: Update${name}): Promise<${name}Response<${name} | null>> {
85
+ const collection = await this.connect()
86
+
87
+ const result = await collection.findOneAndUpdate(
88
+ { _id: new ObjectId(id) },
89
+ {
90
+ $set: {
91
+ ...data,
92
+ updatedAt: new Date()
93
+ }
94
+ },
95
+ { returnDocument: 'after' }
96
+ )
97
+
98
+ return new ${name}Response({
99
+ success: true,
100
+ message: result ? "${name} berhasil diupdate" : "${name} tidak ditemukan",
101
+ value: result ? new ${name}(result) : null
102
+ })
103
+ }
104
+
105
+ async delete(id: string): Promise<${name}Response<void>> {
106
+ const collection = await this.connect()
107
+ await collection.deleteOne({ _id: new ObjectId(id) })
108
+ return new ${name}Response({
109
+ success: true,
110
+ message: "${name} berhasil dihapus",
111
+ value: undefined
112
+ })
113
+ }
114
+ }
115
+
116
+ export const ${name.toLowerCase()}Repository = new ${name}Repository()`;
117
+ export {
118
+ generateMongoRepository
119
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kontas-express",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "main": "dist/index.js",
5
5
  "scripts": {
6
6
  "build": "bun build ./index.js --outdir ./dist --target bun",