kontas-express 1.0.9 → 1.0.10
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 +152 -0
- package/package.json +1 -1
package/dist/index.js
CHANGED
@@ -132,7 +132,159 @@ export class ${name}Repository {
|
|
132
132
|
}
|
133
133
|
}
|
134
134
|
|
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
|
+
|
135
286
|
export const ${name.toLowerCase()} = new ${name}Repository()`;
|
136
287
|
export {
|
288
|
+
generatePostgresRepository,
|
137
289
|
generateMongoRepository
|
138
290
|
};
|