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.
Files changed (2) hide show
  1. package/dist/index.js +40 -58
  2. 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, 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"
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
- class ${name}Response<T> {
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}Repository {
42
- private collectionName = COLLECTION_NAME
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<${name}>> {
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 new ${name}Response({
31
+ return {
60
32
  success: true,
61
- message: "${name} berhasil dibuat",
62
- value: new ${name}({ ...doc, _id: result.insertedId })
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<${name}[]>> {
38
+ async findAll(): Promise<${name}Response<${pluralName}[]>> {
67
39
  const collection = await this.connect()
68
40
  const docs = await collection.find({}).toArray()
69
- return new ${name}Response({
41
+ return {
70
42
  success: true,
71
- value: docs.map(doc => new ${name}(doc))
72
- })
43
+ value: docs as ${pluralName}[]
44
+ }
73
45
  }
74
46
 
75
- async findById(id: string): Promise<${name}Response<${name} | null>> {
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 new ${name}Response({
50
+ return {
79
51
  success: true,
80
- value: doc ? new ${name}(doc) : null
81
- })
52
+ value: doc as ${pluralName} | null
53
+ }
82
54
  }
83
55
 
84
- async update(id: string, data: Update${name}): Promise<${name}Response<${name} | null>> {
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 new ${name}Response({
70
+ return {
99
71
  success: true,
100
- message: result ? "${name} berhasil diupdate" : "${name} tidak ditemukan",
101
- value: result ? new ${name}(result) : null
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: string): Promise<${name}Response<void>> {
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 new ${name}Response({
90
+ return {
109
91
  success: true,
110
- message: "${name} berhasil dihapus",
111
- value: undefined
112
- })
92
+ message: "${name} deleted successfully",
93
+ value: doc as ${pluralName}
94
+ }
113
95
  }
114
96
  }
115
97
 
116
- export const ${name.toLowerCase()}Repository = new ${name}Repository()`;
98
+ export const ${name.toLowerCase()} = new ${name}()`;
117
99
  export {
118
100
  generateMongoRepository
119
101
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kontas-express",
3
- "version": "1.0.6",
3
+ "version": "1.0.8",
4
4
  "main": "dist/index.js",
5
5
  "scripts": {
6
6
  "build": "bun build ./index.js --outdir ./dist --target bun",