rad-api 1.0.0 → 1.0.3

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/index.js CHANGED
@@ -1,11 +1,11 @@
1
- #!/usr/bin/env node
1
+ #!/usr/bin/env node
2
2
 
3
3
  import { parseArgs } from './src/utils/parser.js';
4
4
  import { generate } from './src/commands/generate.js';
5
5
 
6
6
  async function main() {
7
7
  const args = parseArgs(process.argv.slice(2));
8
- console.log(args);
8
+ // console.log(args);
9
9
  try {
10
10
  if (args.command === 'generate') {
11
11
  if (!args.table) {
@@ -14,6 +14,7 @@ console.log(args);
14
14
  process.exit(1);
15
15
  }
16
16
  if (args.type) {
17
+ console.log(`Generating ${args.type} for table: ${args.table}`);
17
18
  await generate(args.type, args.table, args.path);
18
19
  }
19
20
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rad-api",
3
- "version": "1.0.0",
3
+ "version": "1.0.3",
4
4
  "description": "RAD pour créer des routes, controller, model ... etc",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -2,6 +2,7 @@ import { readTemplate, writeFile, getOutputPath, fileExists } from '../utils/fil
2
2
  import { formatTableName } from '../utils/stringHelper.js';
3
3
 
4
4
  export async function generate(type, tableName, pathOverride='output') {
5
+ console.log("Generating...", type, tableName, pathOverride);
5
6
  try {
6
7
  // Valider le nom de la table
7
8
  if (!tableName || tableName.trim() === '') {
@@ -12,7 +13,7 @@ export async function generate(type, tableName, pathOverride='output') {
12
13
  const { tableSingular, tablePlural } = formatTableName(tableName);
13
14
 
14
15
  // Récupérer le template
15
- let content = readTemplate('route');
16
+ let content = readTemplate(type);
16
17
 
17
18
  // Remplacer les placeholders
18
19
  content = content.replace(/{{TABLE_SINGULAR}}/g, tableSingular);
@@ -1,31 +1,86 @@
1
- import { PrismaMariaDb } from '@prisma/adapter-mariadb';
2
- import { PrismaClient } from '../../../generated/prisma/client.js';
1
+ import { getPrisma } from '../../services/prisma.service.js';
3
2
 
4
- const maria = new PrismaMariaDb({
5
- host: process.env.DATABASE_HOST,
6
- port: process.env.DATABASE_PORT,
7
- user: process.env.DATABASE_USER,
8
- password: process.env.DATABASE_PASSWORD,
9
- database: process.env.DATABASE_NAME
10
- });
3
+ const prisma = getPrisma();
11
4
 
12
- const prisma = new PrismaClient({ adapter: maria });
5
+ export const findAll = async (req, res) => {
6
+ req.log?.info('findAll {{TABLE_PLURAL}} endpoint');
7
+ try {
8
+ const all{{TABLE_SINGULAR}} = await prisma.{{TABLE_PLURAL}}.findMany();
9
+ console.log('All {{TABLE_PLURAL}}:', JSON.stringify(all{{TABLE_SINGULAR}}, null, 2));
10
+ res.status(200).json(all{{TABLE_SINGULAR}});
11
+ } catch (error) {
12
+ req.log?.error('Error fetching {{TABLE_PLURAL}}:', error);
13
+ return res.status(500).json({ msg: 'Internal Server Error' });
14
+ }
15
+ };
13
16
 
14
- export const all = async (req, res) => {
15
- req.log?.info('all {{TABLE_PLURAL}} endpoint');
17
+ export const findById = async (req, res) => {
18
+ req.log?.info('findById {{TABLE_SINGULAR}} endpoint');
16
19
  try {
17
- const all{{TABLE_SINGULAR}} = await main();
20
+ const {{TABLE_SINGULAR}} = await prisma.{{TABLE_PLURAL}}.findUnique({
21
+ where: { id: parseInt(req.params.id) }
22
+ });
23
+ res.status(200).json({{TABLE_SINGULAR}});
24
+ } catch (error) {
25
+ req.log?.error('Error fetching {{TABLE_SINGULAR}}:', error);
26
+ return res.status(500).json({ msg: 'Internal Server Error' });
27
+ }
28
+ };
18
29
 
19
- res.status(200);
20
- res.json(all{{TABLE_SINGULAR}});
30
+ export const search = async (req, res) => {
31
+ req.log?.info('search {{TABLE_PLURAL}} endpoint');
32
+ try {
33
+ const {{TABLE_PLURAL}} = await prisma.{{TABLE_PLURAL}}.findMany({
34
+ where: {
35
+ OR: [
36
+ { field1: { contains: req.query.q } },
37
+ { field2: { contains: req.query.q } }
38
+ ]
39
+ }
40
+ });
41
+ res.status(200).json({{TABLE_PLURAL}});
21
42
  } catch (error) {
22
- req.log?.error('Error fetching {{TABLE_PLURAL}}:', error);
43
+ req.log?.error('Error searching {{TABLE_PLURAL}}:', error);
23
44
  return res.status(500).json({ msg: 'Internal Server Error' });
24
45
  }
25
46
  };
26
47
 
27
- async function main() {
28
- const all{{TABLE_SINGULAR}} = await prisma.{{TABLE_PLURAL}}.findMany()
29
- console.log('All {{TABLE_PLURAL}}:', JSON.stringify(all{{TABLE_SINGULAR}}, null, 2))
30
- return all{{TABLE_SINGULAR}};
31
- }
48
+ export const create = async (req, res) => {
49
+ req.log?.info('create {{TABLE_SINGULAR}} endpoint');
50
+ try {
51
+ const {{TABLE_SINGULAR}} = await prisma.{{TABLE_PLURAL}}.create({
52
+ data: req.body
53
+ });
54
+ res.status(201).json({{TABLE_SINGULAR}});
55
+ } catch (error) {
56
+ req.log?.error('Error creating {{TABLE_SINGULAR}}:', error);
57
+ return res.status(500).json({ msg: 'Internal Server Error' });
58
+ }
59
+ };
60
+
61
+ export const update = async (req, res) => {
62
+ req.log?.info('update {{TABLE_SINGULAR}} endpoint');
63
+ try {
64
+ const {{TABLE_SINGULAR}} = await prisma.{{TABLE_PLURAL}}.update({
65
+ where: { id: parseInt(req.params.id) },
66
+ data: req.body
67
+ });
68
+ res.status(200).json({{TABLE_SINGULAR}});
69
+ } catch (error) {
70
+ req.log?.error('Error updating {{TABLE_SINGULAR}}:', error);
71
+ return res.status(500).json({ msg: 'Internal Server Error' });
72
+ }
73
+ };
74
+
75
+ export const remove = async (req, res) => {
76
+ req.log?.info('remove {{TABLE_SINGULAR}} endpoint');
77
+ try {
78
+ const {{TABLE_SINGULAR}} = await prisma.{{TABLE_PLURAL}}.delete({
79
+ where: { id: parseInt(req.params.id) }
80
+ });
81
+ res.status(200).json({{TABLE_SINGULAR}});
82
+ } catch (error) {
83
+ req.log?.error('Error deleting {{TABLE_SINGULAR}}:', error);
84
+ return res.status(500).json({ msg: 'Internal Server Error' });
85
+ }
86
+ };
@@ -0,0 +1,56 @@
1
+ import { getPrisma } from '../../services/prisma.service.js';
2
+
3
+ const prisma = getPrisma();
4
+
5
+ export const findAll = async () => {
6
+ const all = await prisma.{{TABLE_PLURAL}}.findMany()
7
+ console.log('All {{TABLE_PLURAL}}:', JSON.stringify(all, null, 2))
8
+ return all;
9
+ }
10
+
11
+ export const findById = async (id) => {
12
+ const {{TABLE_SINGULAR}} = await prisma.{{TABLE_PLURAL}}.findUnique({
13
+ where: { id: parseInt(id) }
14
+ })
15
+ return {{TABLE_SINGULAR}};
16
+ }
17
+
18
+ export const search = async (query) => {
19
+ const {{TABLE_PLURAL}} = await prisma.{{TABLE_PLURAL}}.findMany({
20
+ where: {
21
+ OR: [
22
+ { Texte: { contains: query } },
23
+ { Auteur: { contains: query } }
24
+ ]
25
+ }
26
+ })
27
+ return citations;
28
+ }
29
+
30
+ export const create = async (data) => {
31
+ const {{TABLE_SINGULAR}} = await prisma.{{TABLE_PLURAL}}.create({
32
+ data: {
33
+ Texte: data.Texte,
34
+ Auteur: data.Auteur
35
+ }
36
+ })
37
+ return {{TABLE_SINGULAR}};
38
+ }
39
+
40
+ export const update = async (id, data) => {
41
+ const {{TABLE_SINGULAR}} = await prisma.{{TABLE_PLURAL}}.update({
42
+ where: { id: parseInt(id) },
43
+ data: {
44
+ Texte: data.Texte,
45
+ Auteur: data.Auteur
46
+ }
47
+ })
48
+ return {{TABLE_SINGULAR}};
49
+ }
50
+
51
+ export const remove = async (id) => {
52
+ const {{TABLE_SINGULAR}} = await prisma.{{TABLE_PLURAL}}.delete({
53
+ where: { id: parseInt(id) }
54
+ })
55
+ return {{TABLE_SINGULAR}};
56
+ }
@@ -1,23 +1,105 @@
1
1
  import { Router } from 'express';
2
- import { all } from '../controllers/citations.controller.js';
2
+ import {
3
+ all,
4
+ getById,
5
+ search,
6
+ create,
7
+ update,
8
+ deleteItem
9
+ } from '../controllers/{{TABLE_SINGULAR}}.controller.js';
3
10
  import { authMiddleware } from '../middlewares/auth.middleware.js';
4
11
 
5
12
  const router = Router();
6
13
 
7
14
  /**
8
15
  * @openapi
9
- * /{TABLE_SINGULAR}/:
16
+ * /{{TABLE_PLURAL}}:
10
17
  * get:
11
- * summary: get all {TABLE_PLURAL}
18
+ * summary: Get all {{TABLE_PLURAL}}
12
19
  * responses:
13
20
  * 200:
14
21
  * description: OK
15
22
  * content:
16
23
  * application/json:
17
24
  * schema:
18
- * type: object
19
- *
25
+ * type: array
26
+ * post:
27
+ * summary: Create a new {{TABLE_SINGULAR}}
28
+ * requestBody:
29
+ * required: true
30
+ * content:
31
+ * application/json:
32
+ * schema:
33
+ * type: object
34
+ * responses:
35
+ * 201:
36
+ * description: {{TABLE_SINGULAR}} created
20
37
  */
21
38
  router.get('/', all);
39
+ router.post('/', create);
40
+
41
+ /**
42
+ * @openapi
43
+ * /{{TABLE_PLURAL}}/search:
44
+ * get:
45
+ * summary: Search {{TABLE_PLURAL}}
46
+ * parameters:
47
+ * - name: q
48
+ * in: query
49
+ * required: true
50
+ * schema:
51
+ * type: string
52
+ * responses:
53
+ * 200:
54
+ * description: OK
55
+ */
56
+ router.get('/search', search);
57
+
58
+ /**
59
+ * @openapi
60
+ * /{{TABLE_PLURAL}}/{id}:
61
+ * get:
62
+ * summary: Get {{TABLE_SINGULAR}} by ID
63
+ * parameters:
64
+ * - name: id
65
+ * in: path
66
+ * required: true
67
+ * schema:
68
+ * type: integer
69
+ * responses:
70
+ * 200:
71
+ * description: OK
72
+ * 404:
73
+ * description: {{TABLE_SINGULAR}} not found
74
+ * put:
75
+ * summary: Update a {{TABLE_SINGULAR}}
76
+ * parameters:
77
+ * - name: id
78
+ * in: path
79
+ * required: true
80
+ * schema:
81
+ * type: integer
82
+ * responses:
83
+ * 200:
84
+ * description: {{TABLE_SINGULAR}} updated
85
+ * 404:
86
+ * description: {{TABLE_SINGULAR}} not found
87
+ * delete:
88
+ * summary: Delete a {{TABLE_SINGULAR}}
89
+ * parameters:
90
+ * - name: id
91
+ * in: path
92
+ * required: true
93
+ * schema:
94
+ * type: integer
95
+ * responses:
96
+ * 200:
97
+ * description: {{TABLE_SINGULAR}} deleted
98
+ * 404:
99
+ * description: {{TABLE_SINGULAR}} not found
100
+ */
101
+ router.get('/:id', getById);
102
+ router.put('/:id', update);
103
+ router.delete('/:id', deleteItem);
22
104
 
23
105
  export default router;