kontas-express 1.0.5 → 1.0.7

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 +27 -88
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -1,71 +1,24 @@
1
1
  // @bun
2
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"
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"
8
6
 
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
- }
7
+ type ${name}Response<T> = {
8
+ success: boolean
9
+ message?: string
10
+ value: T
53
11
  }
54
12
 
55
13
  export class ${name}Repository {
56
- /** @type {string} */
57
- collectionName = COLLECTION_NAME
14
+ private collectionName = "${pluralName}"
58
15
 
59
16
  async connect() {
60
17
  await connectToDatabase()
61
18
  return getDatabaseCollection(this.collectionName)
62
19
  }
63
20
 
64
- /**
65
- * @param {Create${name}} data
66
- * @returns {Promise<${name}Response<${name}>>}
67
- */
68
- async create(data) {
21
+ async create(data: Create${name}): Promise<${name}Response<${pluralName}>> {
69
22
  const collection = await this.connect()
70
23
 
71
24
  const doc = {
@@ -75,42 +28,32 @@ export class ${name}Repository {
75
28
  }
76
29
 
77
30
  const result = await collection.insertOne(doc)
78
- return new ${name}Response({
31
+ return {
79
32
  success: true,
80
33
  message: "${name} berhasil dibuat",
81
- value: new ${name}({ ...doc, _id: result.insertedId })
82
- })
34
+ value: { ...doc, _id: result.insertedId } as ${pluralName}
35
+ }
83
36
  }
84
37
 
85
- /** @returns {Promise<${name}Response<${name}[]>>} */
86
- async findAll() {
38
+ async findAll(): Promise<${name}Response<${pluralName}[]>> {
87
39
  const collection = await this.connect()
88
40
  const docs = await collection.find({}).toArray()
89
- return new ${name}Response({
41
+ return {
90
42
  success: true,
91
- value: docs.map(doc => new ${name}(doc))
92
- })
43
+ value: docs as ${pluralName}[]
44
+ }
93
45
  }
94
46
 
95
- /**
96
- * @param {string} id
97
- * @returns {Promise<${name}Response<${name} | null>>}
98
- */
99
- async findById(id) {
47
+ async findById({ id }: ${name}Id): Promise<${name}Response<${pluralName} | null>> {
100
48
  const collection = await this.connect()
101
49
  const doc = await collection.findOne({ _id: new ObjectId(id) })
102
- return new ${name}Response({
50
+ return {
103
51
  success: true,
104
- value: doc ? new ${name}(doc) : null
105
- })
52
+ value: doc as ${pluralName} | null
53
+ }
106
54
  }
107
55
 
108
- /**
109
- * @param {string} id
110
- * @param {Update${name}} data
111
- * @returns {Promise<${name}Response<${name} | null>>}
112
- */
113
- async update(id, data) {
56
+ async update({ id }: ${name}Id, data: Update${name}): Promise<${name}Response<${pluralName} | null>> {
114
57
  const collection = await this.connect()
115
58
 
116
59
  const result = await collection.findOneAndUpdate(
@@ -124,25 +67,21 @@ export class ${name}Repository {
124
67
  { returnDocument: 'after' }
125
68
  )
126
69
 
127
- return new ${name}Response({
70
+ return {
128
71
  success: true,
129
72
  message: result ? "${name} berhasil diupdate" : "${name} tidak ditemukan",
130
- value: result ? new ${name}(result) : null
131
- })
73
+ value: result as ${pluralName} | null
74
+ }
132
75
  }
133
76
 
134
- /**
135
- * @param {string} id
136
- * @returns {Promise<${name}Response<void>>}
137
- */
138
- async delete(id) {
77
+ async delete({ id }: ${name}Id): Promise<${name}Response<void>> {
139
78
  const collection = await this.connect()
140
79
  await collection.deleteOne({ _id: new ObjectId(id) })
141
- return new ${name}Response({
80
+ return {
142
81
  success: true,
143
82
  message: "${name} berhasil dihapus",
144
83
  value: undefined
145
- })
84
+ }
146
85
  }
147
86
  }
148
87
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kontas-express",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
4
4
  "main": "dist/index.js",
5
5
  "scripts": {
6
6
  "build": "bun build ./index.js --outdir ./dist --target bun",