kontas-express 1.0.9 → 1.0.11
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/dist/index.js +206 -0
- package/package.json +1 -1
package/dist/index.js
CHANGED
@@ -133,6 +133,212 @@ export class ${name}Repository {
|
|
133
133
|
}
|
134
134
|
|
135
135
|
export const ${name.toLowerCase()} = new ${name}Repository()`;
|
136
|
+
// src/database/postgresql/repository.template.js
|
137
|
+
var generatePostgresRepository = (name, pluralName) => `import { getPool } from "../../config"
|
138
|
+
import type { Create${name}, Update${name}, ${pluralName}, ${name}Id } from "./${name.toLowerCase()}.schema"
|
139
|
+
import { ${name} } from "./${name.toLowerCase()}.schema"
|
140
|
+
|
141
|
+
type ${name}Response<T> = {
|
142
|
+
success: true
|
143
|
+
message?: string
|
144
|
+
value: T
|
145
|
+
} | {
|
146
|
+
success: false
|
147
|
+
message: string
|
148
|
+
error: unknown
|
149
|
+
value: null
|
150
|
+
}
|
151
|
+
|
152
|
+
export class ${name}Repository {
|
153
|
+
private readonly tableName = "${pluralName.toLowerCase()}"
|
154
|
+
|
155
|
+
async create(data: Readonly<Create${name}>): Promise<${name}Response<${pluralName}>> {
|
156
|
+
try {
|
157
|
+
const pool = await getPool()
|
158
|
+
|
159
|
+
const fields = Object.keys(data)
|
160
|
+
const values = Object.values(data)
|
161
|
+
const placeholders = values.map((_, i) => \`$\${i + 1}\`).join(', ')
|
162
|
+
|
163
|
+
const query = \`
|
164
|
+
INSERT INTO \${this.tableName}
|
165
|
+
(\${fields.join(', ')}, created_at, updated_at)
|
166
|
+
VALUES (\${placeholders}, NOW(), NOW())
|
167
|
+
RETURNING *
|
168
|
+
\`
|
169
|
+
|
170
|
+
const result = await pool.query(query, values)
|
171
|
+
return {
|
172
|
+
success: true,
|
173
|
+
message: "${name} created successfully",
|
174
|
+
value: new ${name}(result.rows[0]) as ${pluralName}
|
175
|
+
}
|
176
|
+
} catch (error) {
|
177
|
+
return {
|
178
|
+
success: false,
|
179
|
+
message: "Failed to create ${name.toLowerCase()}",
|
180
|
+
error,
|
181
|
+
value: null
|
182
|
+
}
|
183
|
+
}
|
184
|
+
}
|
185
|
+
|
186
|
+
async findAll(): Promise<${name}Response<${name}[]>> {
|
187
|
+
try {
|
188
|
+
const pool = await getPool()
|
189
|
+
const result = await pool.query(\`SELECT * FROM \${this.tableName}\`)
|
190
|
+
|
191
|
+
return {
|
192
|
+
success: true,
|
193
|
+
value: result.rows.map(row => new ${name}(row))
|
194
|
+
}
|
195
|
+
} catch (error) {
|
196
|
+
return {
|
197
|
+
success: false,
|
198
|
+
message: "Failed to fetch ${pluralName.toLowerCase()}",
|
199
|
+
error,
|
200
|
+
value: null
|
201
|
+
}
|
202
|
+
}
|
203
|
+
}
|
204
|
+
|
205
|
+
async findById({ id }: Readonly<${name}Id>): Promise<${name}Response<${name} | null>> {
|
206
|
+
try {
|
207
|
+
const pool = await getPool()
|
208
|
+
const result = await pool.query(
|
209
|
+
\`SELECT * FROM \${this.tableName} WHERE id = $1\`,
|
210
|
+
[id]
|
211
|
+
)
|
212
|
+
|
213
|
+
return {
|
214
|
+
success: true,
|
215
|
+
value: result.rows[0] ? new ${name}(result.rows[0]) : null
|
216
|
+
}
|
217
|
+
} catch (error) {
|
218
|
+
return {
|
219
|
+
success: false,
|
220
|
+
message: "Failed to find ${name.toLowerCase()}",
|
221
|
+
error,
|
222
|
+
value: null
|
223
|
+
}
|
224
|
+
}
|
225
|
+
}
|
226
|
+
|
227
|
+
async update({ id }: Readonly<${name}Id>, data: Readonly<Update${name}>): Promise<${name}Response<${pluralName} | null>> {
|
228
|
+
try {
|
229
|
+
const pool = await getPool()
|
230
|
+
|
231
|
+
const fields = Object.keys(data)
|
232
|
+
const values = Object.values(data)
|
233
|
+
const setClause = fields
|
234
|
+
.map((field, i) => \`\${field} = $\${i + 1}\`)
|
235
|
+
.join(', ')
|
236
|
+
|
237
|
+
const query = \`
|
238
|
+
UPDATE \${this.tableName}
|
239
|
+
SET \${setClause}, updated_at = NOW()
|
240
|
+
WHERE id = $\${values.length + 1}
|
241
|
+
RETURNING *
|
242
|
+
\`
|
243
|
+
|
244
|
+
const result = await pool.query(query, [...values, id])
|
245
|
+
|
246
|
+
return {
|
247
|
+
success: true,
|
248
|
+
message: result.rows[0] ? "${name} updated successfully" : "${name} not found",
|
249
|
+
value: result.rows[0] ? new ${name}(result.rows[0]) as ${pluralName} : null
|
250
|
+
}
|
251
|
+
} catch (error) {
|
252
|
+
return {
|
253
|
+
success: false,
|
254
|
+
message: "Failed to update ${name.toLowerCase()}",
|
255
|
+
error,
|
256
|
+
value: null
|
257
|
+
}
|
258
|
+
}
|
259
|
+
}
|
260
|
+
|
261
|
+
async delete({ id }: ${name}Id): Promise<${name}Response<${pluralName} | null>> {
|
262
|
+
try {
|
263
|
+
const pool = await getPool()
|
264
|
+
|
265
|
+
const result = await pool.query(
|
266
|
+
\`DELETE FROM \${this.tableName} WHERE id = $1 RETURNING *\`,
|
267
|
+
[id]
|
268
|
+
)
|
269
|
+
|
270
|
+
return {
|
271
|
+
success: true,
|
272
|
+
message: result.rows[0] ? "${name} deleted successfully" : "${name} not found",
|
273
|
+
value: result.rows[0] ? new ${name}(result.rows[0]) as ${pluralName} : null
|
274
|
+
}
|
275
|
+
} catch (error) {
|
276
|
+
return {
|
277
|
+
success: false,
|
278
|
+
message: "Failed to delete ${name.toLowerCase()}",
|
279
|
+
error,
|
280
|
+
value: null
|
281
|
+
}
|
282
|
+
}
|
283
|
+
}
|
284
|
+
}
|
285
|
+
|
286
|
+
export const ${name.toLowerCase()} = new ${name}Repository()`;
|
287
|
+
// src/database/postgresql/migration.template.js
|
288
|
+
var generatePostgresMigration = (name, pluralName, fields) => {
|
289
|
+
const pgTypes = {
|
290
|
+
string: "VARCHAR(255)",
|
291
|
+
number: "INTEGER",
|
292
|
+
boolean: "BOOLEAN",
|
293
|
+
date: "TIMESTAMP"
|
294
|
+
};
|
295
|
+
const columns = fields.map((field) => {
|
296
|
+
const pgType = pgTypes[field.type] || "VARCHAR(255)";
|
297
|
+
return ` ${field.name} ${pgType}`;
|
298
|
+
}).join(`,
|
299
|
+
`);
|
300
|
+
return `import { getPool } from "../../config"
|
301
|
+
|
302
|
+
export async function up() {
|
303
|
+
const pool = await getPool()
|
304
|
+
|
305
|
+
await pool.query(\`
|
306
|
+
CREATE TABLE IF NOT EXISTS ${pluralName.toLowerCase()} (
|
307
|
+
id SERIAL PRIMARY KEY,
|
308
|
+
${columns},
|
309
|
+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
310
|
+
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
311
|
+
)
|
312
|
+
\`)
|
313
|
+
|
314
|
+
console.log('\u2705 Migration ${name} created successfully')
|
315
|
+
}
|
316
|
+
|
317
|
+
export async function down() {
|
318
|
+
const pool = await getPool()
|
319
|
+
|
320
|
+
await pool.query(\`
|
321
|
+
DROP TABLE IF EXISTS ${pluralName.toLowerCase()}
|
322
|
+
\`)
|
323
|
+
|
324
|
+
console.log('\u2705 Migration ${name} dropped successfully')
|
325
|
+
}
|
326
|
+
|
327
|
+
// Jalankan migration
|
328
|
+
if (process.argv[2] === 'up') {
|
329
|
+
up()
|
330
|
+
.catch(console.error)
|
331
|
+
.finally(() => process.exit())
|
332
|
+
}
|
333
|
+
|
334
|
+
if (process.argv[2] === 'down') {
|
335
|
+
down()
|
336
|
+
.catch(console.error)
|
337
|
+
.finally(() => process.exit())
|
338
|
+
}`;
|
339
|
+
};
|
136
340
|
export {
|
341
|
+
generatePostgresRepository,
|
342
|
+
generatePostgresMigration,
|
137
343
|
generateMongoRepository
|
138
344
|
};
|