@mlagie/sql-connector 1.0.2 → 1.0.3-beta

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/README.md CHANGED
@@ -42,25 +42,66 @@ await connect(config);
42
42
  await logout();
43
43
  ```
44
44
 
45
+ ### Interface `SchemaField`
46
+
47
+ Décrit les propriétés d'un champ dans le schéma d'une table SQL. Chaque clé du dictionnaire passé au constructeur de `Schema` doit respecter cette interface.
48
+
49
+ | Propriété | Type | Description |
50
+ |------------------|--------------------------------------|-----------------------------------------------------------------------------|
51
+ | type | `SqlType` ou `{ name: SqlType }` | Type du champ (String, Number, Boolean, etc.) |
52
+ | length | `number` | Longueur maximale (pour VARCHAR ou INT) |
53
+ | required | `boolean` | Si le champ est obligatoire (NOT NULL) |
54
+ | default | `any` | Valeur par défaut |
55
+ | unique | `boolean` | Si le champ doit être unique |
56
+ | auto_increment | `boolean` | Si le champ est auto-incrémenté |
57
+ | foreignKey | `string` | Clé étrangère (ex: "otherTable(column)") |
58
+ | enum | `string[]` | Liste de valeurs pour un champ ENUM |
59
+ | primary_key | `boolean` | Si le champ est une clé primaire |
60
+ | customize | `string` | Ajout d'options SQL personnalisées |
61
+
62
+ #### Exemple d'utilisation
63
+
64
+ ```javascript
65
+ const userSchema = new Schema({
66
+ status: {
67
+ type: String,
68
+ enum: ["active", "inactive", "pending"], // champ ENUM
69
+ required: true,
70
+ default: "pending"
71
+ },
72
+ id: {
73
+ type: Number,
74
+ auto_increment: true,
75
+ primary_key: true
76
+ },
77
+ email: {
78
+ type: String,
79
+ length: 255,
80
+ unique: true,
81
+ required: true
82
+ }
83
+ });
84
+ ```
85
+
45
86
  ### Classes `Schema`
46
87
 
47
88
  Représente un schéma de base de données.
48
89
  * Constructeur:
49
- * Schema(schemaDict) : Crée une instance de Schema.
50
- * schemaDict (Object) : Un dictionnaire définissant le schéma.
90
+ * Schema(schemaDict) : Crée une instance de Schema.
91
+ * schemaDict (Object) : Un dictionnaire définissant le schéma.
51
92
 
52
93
  * Exemple
53
94
 
54
95
  ```javascript
55
96
  const transferSchema = new Schema({
56
- token: {
57
- type: String,
58
- length: 50
59
- },
60
- mdp: {
61
- type: String,
62
- length: 15
63
- }
97
+ token: {
98
+ type: String,
99
+ length: 50
100
+ },
101
+ mdp: {
102
+ type: String,
103
+ length: 15
104
+ }
64
105
  });
65
106
  ```
66
107
  ## Class Model
package/index.d.ts CHANGED
@@ -21,6 +21,8 @@ export interface SchemaField {
21
21
  unique?: boolean;
22
22
  auto_increment?: boolean;
23
23
  foreignKey?: string;
24
+ enum?: string[];
25
+ primary_key?: boolean;
24
26
  customize?: string;
25
27
  }
26
28
 
package/index.js CHANGED
@@ -338,7 +338,13 @@ class Model {
338
338
  if (field.type && typeof field == "object") {
339
339
  if (!sqlTypeMap[fieldType]) throw new Error(`Field ${fieldName} has unsupported type ${fieldType}.`);
340
340
 
341
- let columnDefinition = `${fieldName} ${sqlTypeMap[fieldType]}${sqlTypeMap[fieldType] == "VARCHAR" || sqlTypeMap[fieldType] == "INT" ? `(${field.length > 0 ? field.length : lengthDefault})` : ""}`;
341
+ let columnDefinition = "";
342
+ if (Array.isArray(field.enum) && field.enum.length > 0) {
343
+ const enumValues = field.enum.map(v => `'${v.replace(/'/g, "''")}'`).join(", ");
344
+ columnDefinition = `${fieldName} ENUM(${enumValues})`;
345
+ } else {
346
+ columnDefinition = `${fieldName} ${sqlTypeMap[fieldType]}${sqlTypeMap[fieldType] == "VARCHAR" || sqlTypeMap[fieldType] == "INT" ? `(${field.length > 0 ? field.length : lengthDefault})` : ""}`;
347
+ }
342
348
 
343
349
  if (field.required) columnDefinition += ' NOT NULL';
344
350
  if (field.default !== undefined && field.default != null) columnDefinition += ` DEFAULT "${field.default}"`;
@@ -346,6 +352,7 @@ class Model {
346
352
  if (field.unique) columnDefinition += ' UNIQUE';
347
353
  if (field.auto_increment) columnDefinition += ' AUTO_INCREMENT';
348
354
  if (field.foreignKey) foreignKey.push(`FOREIGN KEY (${fieldName}) REFERENCES ${field.foreignKey}`);
355
+ if (field.primary_key) columnDefinition += ' PRIMARY KEY';
349
356
  if (typeof field.customize === 'string' && field.customize.length != 0) columnDefinition += ` ${field.customize}`;
350
357
  return columnDefinition;
351
358
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mlagie/sql-connector",
3
- "version": "1.0.2",
3
+ "version": "1.0.3-beta",
4
4
  "description": "Le module sql-connector permet de gérer les connexions à une base de données MySQL, de définir des schémas de tables, et d'interagir avec les données de manière simple et efficace.",
5
5
  "main": "index.js",
6
6
  "scripts": {