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.
Files changed (2) hide show
  1. package/dist/index.js +99 -52
  2. 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: boolean
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 connect() {
17
- await connectToDatabase()
18
- return getDatabaseCollection(this.collectionName)
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
- const collection = await this.connect()
23
-
24
- const doc = {
25
- ...data,
26
- createdAt: new Date(),
27
- updatedAt: new Date()
28
- }
29
-
30
- const result = await collection.insertOne(doc)
31
- return {
32
- success: true,
33
- message: "${name} berhasil dibuat",
34
- value: { ...doc, _id: result.insertedId } as ${pluralName}
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<${pluralName}[]>> {
39
- const collection = await this.connect()
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 as ${pluralName}[]
62
+ value: docs.map(doc => new ${name}(doc))
44
63
  }
45
64
  }
46
65
 
47
- async findById({ id }: ${name}Id): Promise<${name}Response<${pluralName} | null>> {
48
- const collection = await this.connect()
49
- const doc = await collection.findOne({ _id: new ObjectId(id) })
50
- return {
51
- success: true,
52
- value: doc as ${pluralName} | null
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 }: ${name}Id, data: Update${name}): Promise<${name}Response<${pluralName} | null>> {
57
- const collection = await this.connect()
58
-
59
- const result = await collection.findOneAndUpdate(
60
- { _id: new ObjectId(id) },
61
- {
62
- $set: {
63
- ...data,
64
- updatedAt: new Date()
65
- }
66
- },
67
- { returnDocument: 'after' }
68
- )
69
-
70
- return {
71
- success: true,
72
- message: result ? "${name} berhasil diupdate" : "${name} tidak ditemukan",
73
- value: result as ${pluralName} | null
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<void>> {
78
- const collection = await this.connect()
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} berhasil dihapus",
83
- value: undefined
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()}Repository = new ${name}Repository()`;
135
+ export const ${name.toLowerCase()} = new ${name}Repository()`;
89
136
  export {
90
137
  generateMongoRepository
91
138
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kontas-express",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
4
4
  "main": "dist/index.js",
5
5
  "scripts": {
6
6
  "build": "bun build ./index.js --outdir ./dist --target bun",