oktopios-orm 0.2.0__tar.gz → 0.6.0__tar.gz

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.
@@ -0,0 +1,222 @@
1
+ Metadata-Version: 2.4
2
+ Name: oktopios-orm
3
+ Version: 0.6.0
4
+ Summary: ORM pour le langage Oktopios — modeles decores, migrations, query-builder (facon Doctrine / Django ORM)
5
+ Author: Mouanwiya Ali Soule
6
+ License: MIT License
7
+
8
+ Copyright (c) 2025 Mouanwiya Ali Soule
9
+
10
+ Permission is hereby granted, free of charge, to any person obtaining a copy
11
+ of this software and associated documentation files (the "Software"), to deal
12
+ in the Software without restriction, including without limitation the rights
13
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
+ copies of the Software, and to permit persons to whom the Software is
15
+ furnished to do so, subject to the following conditions:
16
+
17
+ The above copyright notice and this permission notice shall be included in all
18
+ copies or substantial portions of the Software.
19
+
20
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
+ SOFTWARE.
27
+
28
+ Project-URL: Homepage, https://github.com/ALISOULEMOUANWIYA/oktopios-orm
29
+ Project-URL: Language, https://pypi.org/project/oktopios/
30
+ Keywords: oktopios,orm,database,sqlite,migrations,okp
31
+ Requires-Python: >=3.8
32
+ Description-Content-Type: text/markdown
33
+ License-File: LICENSE
34
+ Requires-Dist: oktopios>=0.16.0
35
+ Dynamic: license-file
36
+
37
+ # oktopios-orm 🐙🗄️
38
+
39
+ ORM pour le langage **[Oktopios](https://pypi.org/project/oktopios/)** — dans
40
+ l'esprit de **Doctrine** (Symfony) et de l'**ORM de Django** : modèles déclarés
41
+ par décorateurs, migrations automatiques, dépôt générique et query-builder.
42
+
43
+ Deuxième couche de l'écosystème :
44
+ **langage `oktopios` → `oktopios-orm` → `oktopios-framework`**.
45
+
46
+ ## Installation
47
+
48
+ ```bash
49
+ pip install oktopios-orm
50
+ ```
51
+
52
+ Cela installe aussi le langage `oktopios` (dépendance). Comme Django a besoin de
53
+ Python et Doctrine de PHP, `oktopios-orm` a besoin de l'interpréteur `oktopios`.
54
+
55
+ ## Utilisation
56
+
57
+ Dans un fichier `.okp` :
58
+
59
+ ```okp
60
+ use "oktopios_orm/orm.okp" // résolu depuis le paquet installé
61
+ inject Type
62
+
63
+ @Entity("articles")
64
+ class Article {
65
+ @Id
66
+ var id: int
67
+ @Column("titre")
68
+ var title: string
69
+ var vues: int
70
+ fun __construct(title: string, vues: int) {
71
+ this.title = title
72
+ this.vues = vues
73
+ }
74
+ }
75
+
76
+ var schema = new Schema()
77
+ var sample = new Article("", 0)
78
+ schema.migrate("blog.db", sample) // CREATE TABLE articles (id INTEGER PRIMARY KEY, titre TEXT, vues INTEGER)
79
+
80
+ var repo = new Repo("blog.db", sample)
81
+ var a = new Article("Bonjour", 100)
82
+ Type.set(a, "id", 1)
83
+ repo.save(a)
84
+
85
+ for (x in repo.all()) { print(x.title) }
86
+
87
+ var q = new Query("blog.db", "articles")
88
+ print(q.select("id, titre, vues").where("vues", ">", 50).orderBy("vues", "DESC").get())
89
+ ```
90
+
91
+ Exécuter : `okp examples/blog.okp`
92
+
93
+ ## Contenu (`oktopios_orm/orm.okp`)
94
+
95
+ - **`Schema`** — migrations : génère le schéma SQL depuis les modèles décorés
96
+ (`@Entity`, `@Id`, `@Column`) ; mapping des types (`int→INTEGER`, `string→TEXT`,
97
+ `float→REAL`, `bool→INTEGER`).
98
+ - **`Repo`** — dépôt générique : `save`, `all`, `findBy`, `remove`, hydratation
99
+ ligne → objet typé (par réflexion).
100
+ - **`Query`** — constructeur de requêtes fluide : `select().where().orderBy().get()`,
101
+ paramétré (anti-injection).
102
+
103
+ ## Relations `@ManyToOne` 🔗
104
+
105
+ ```okp
106
+ @Entity("auteurs")
107
+ class Auteur { @Id var id: int @Column("nom") var nom: string }
108
+
109
+ @Entity("articles")
110
+ class Article {
111
+ @Id var id: int
112
+ @Column("titre") var titre: string
113
+ @ManyToOne("Auteur") var auteur: any // colonne FK : auteur_id
114
+ }
115
+
116
+ var repoA = new Repo(db, new Auteur())
117
+ var a = new Auteur(); a.nom = "Awa"; repoA.save(a) // save réaffecte a.id
118
+
119
+ var repoArt = new Repo(db, new Article())
120
+ var art = new Article(); art.titre = "Hello"; art.auteur = a
121
+ repoArt.save(art) // écrit auteur_id = a.id
122
+
123
+ var trouve = repoArt.find(1)
124
+ print(trouve.auteur.nom) // -> "Awa" (objet lié chargé)
125
+ ```
126
+
127
+ `@ManyToOne("Classe")` crée une colonne de clé étrangère `<champ>_id` (ou le 2ᵉ
128
+ argument), écrite par `save` et chargée par `find`/`findBy` (un niveau).
129
+
130
+ ### Collections : `@OneToMany` et `@ManyToMany`
131
+
132
+ ```okp
133
+ @Entity("auteurs")
134
+ class Auteur {
135
+ @Id var id: int
136
+ @Column("nom") var nom: string
137
+ @OneToMany("Article", "auteur_id") var articles: any // inverse de @ManyToOne
138
+ }
139
+
140
+ @Entity("articles")
141
+ class Article {
142
+ @Id var id: int
143
+ @Column("titre") var titre: string
144
+ @ManyToOne("Auteur") var auteur: any
145
+ @ManyToMany("Tag", "article_tag", "article_id", "tag_id") var tags: any
146
+ }
147
+
148
+ var auteur = repoAuteur.find(1)
149
+ for (a in auteur.articles) { print(a.titre) } // @OneToMany chargé
150
+
151
+ repoArt.attach(article, "tags", tag) // lie via la table de jointure
152
+ var art = repoArt.find(1)
153
+ for (t in art.tags) { print(t.libelle) } // @ManyToMany chargé
154
+ ```
155
+
156
+ `@ManyToMany` crée la **table de jointure** à la migration ; `Repo.attach` y
157
+ insère les liens. Les collections sont chargées sur un niveau (pas de récursion).
158
+
159
+ **Inverse automatique** : `@OneToMany("Article")` (sans colonne) infère la clé
160
+ étrangère `<classe>_id`. **Cascade** : `repo.saveCascade(auteur)` enregistre
161
+ l'auteur et ses `articles`. **Lazy** : `repo.findLazy(id)` charge sans relations.
162
+
163
+ ## Pagination 📄
164
+
165
+ ```okp
166
+ print(repoArt.count()) // nombre total de lignes
167
+ for (x in repoArt.page(10, 2)) { // page 2, 10 par page (entités hydratées)
168
+ print(x.titre)
169
+ }
170
+ // Bas niveau : Query.limit(n).offset(d) + Query.count()
171
+ ```
172
+
173
+ ## Query enrichi 🔎
174
+
175
+ ```okp
176
+ // constructeur fluide, puis hydratation en entités
177
+ var actifs = repo.hydrateRows(
178
+ repo.query()
179
+ .where("age", ">", 18)
180
+ .orWhere("role", "=", "admin")
181
+ .orderBy("nom", "ASC")
182
+ .limit(20)
183
+ .get()
184
+ )
185
+
186
+ repo.query().whereIn("id", [1, 2, 3]).count() // IN (…)
187
+ repo.query().like("titre", "%okp%").first() // LIKE, 1re ligne | null
188
+ repo.query().join("auteurs a", "a.id = articles.auteur_id").get()
189
+ ```
190
+
191
+ ## Migrations incrémentales (façon Alembic) 🧬
192
+
193
+ Suit les migrations déjà appliquées (table `oktopios_migrations`) et n'exécute
194
+ que les nouvelles, dans l'ordre — **idempotent** :
195
+
196
+ ```okp
197
+ var mig = new Migrator("app.db")
198
+ var n = mig.migrer([
199
+ { "nom": "001_users", "sql": "CREATE TABLE users (id INTEGER PRIMARY KEY, nom TEXT)", "down": "DROP TABLE users" },
200
+ { "nom": "002_ville", "sql": "ALTER TABLE users ADD COLUMN ville TEXT" }
201
+ ])
202
+ print(n) // nb réellement appliquées (0 au 2e lancement)
203
+ print(mig.appliquees()) // ["001_users", "002_ville"]
204
+
205
+ mig.rollbackLast() // annule "002_ville" (via son "down" s'il existe)
206
+ mig.rollback("001_users") // exécute "DROP TABLE users" et retire du suivi
207
+ ```
208
+
209
+ ## Décorateurs reconnus
210
+
211
+ | Décorateur | Cible | Effet |
212
+ |---|---|---|
213
+ | `@Entity("table")` | classe | nom de la table |
214
+ | `@Column("colonne")` | champ | nom de la colonne |
215
+ | `@Id` | champ | clé primaire (auto-générée, réaffectée par `save`) |
216
+ | `@ManyToOne("Classe", "fk"?)` | champ | relation N-1 (colonne FK ici) |
217
+ | `@OneToMany("Enfant", "fk")` | champ | collection inverse (1-N) |
218
+ | `@ManyToMany("Autre", "jointure", "fk_ici", "fk_autre")` | champ | relation N-N (table de jointure) |
219
+
220
+ ## Licence
221
+
222
+ MIT © Mouanwiya Ali Soule
@@ -0,0 +1,186 @@
1
+ # oktopios-orm 🐙🗄️
2
+
3
+ ORM pour le langage **[Oktopios](https://pypi.org/project/oktopios/)** — dans
4
+ l'esprit de **Doctrine** (Symfony) et de l'**ORM de Django** : modèles déclarés
5
+ par décorateurs, migrations automatiques, dépôt générique et query-builder.
6
+
7
+ Deuxième couche de l'écosystème :
8
+ **langage `oktopios` → `oktopios-orm` → `oktopios-framework`**.
9
+
10
+ ## Installation
11
+
12
+ ```bash
13
+ pip install oktopios-orm
14
+ ```
15
+
16
+ Cela installe aussi le langage `oktopios` (dépendance). Comme Django a besoin de
17
+ Python et Doctrine de PHP, `oktopios-orm` a besoin de l'interpréteur `oktopios`.
18
+
19
+ ## Utilisation
20
+
21
+ Dans un fichier `.okp` :
22
+
23
+ ```okp
24
+ use "oktopios_orm/orm.okp" // résolu depuis le paquet installé
25
+ inject Type
26
+
27
+ @Entity("articles")
28
+ class Article {
29
+ @Id
30
+ var id: int
31
+ @Column("titre")
32
+ var title: string
33
+ var vues: int
34
+ fun __construct(title: string, vues: int) {
35
+ this.title = title
36
+ this.vues = vues
37
+ }
38
+ }
39
+
40
+ var schema = new Schema()
41
+ var sample = new Article("", 0)
42
+ schema.migrate("blog.db", sample) // CREATE TABLE articles (id INTEGER PRIMARY KEY, titre TEXT, vues INTEGER)
43
+
44
+ var repo = new Repo("blog.db", sample)
45
+ var a = new Article("Bonjour", 100)
46
+ Type.set(a, "id", 1)
47
+ repo.save(a)
48
+
49
+ for (x in repo.all()) { print(x.title) }
50
+
51
+ var q = new Query("blog.db", "articles")
52
+ print(q.select("id, titre, vues").where("vues", ">", 50).orderBy("vues", "DESC").get())
53
+ ```
54
+
55
+ Exécuter : `okp examples/blog.okp`
56
+
57
+ ## Contenu (`oktopios_orm/orm.okp`)
58
+
59
+ - **`Schema`** — migrations : génère le schéma SQL depuis les modèles décorés
60
+ (`@Entity`, `@Id`, `@Column`) ; mapping des types (`int→INTEGER`, `string→TEXT`,
61
+ `float→REAL`, `bool→INTEGER`).
62
+ - **`Repo`** — dépôt générique : `save`, `all`, `findBy`, `remove`, hydratation
63
+ ligne → objet typé (par réflexion).
64
+ - **`Query`** — constructeur de requêtes fluide : `select().where().orderBy().get()`,
65
+ paramétré (anti-injection).
66
+
67
+ ## Relations `@ManyToOne` 🔗
68
+
69
+ ```okp
70
+ @Entity("auteurs")
71
+ class Auteur { @Id var id: int @Column("nom") var nom: string }
72
+
73
+ @Entity("articles")
74
+ class Article {
75
+ @Id var id: int
76
+ @Column("titre") var titre: string
77
+ @ManyToOne("Auteur") var auteur: any // colonne FK : auteur_id
78
+ }
79
+
80
+ var repoA = new Repo(db, new Auteur())
81
+ var a = new Auteur(); a.nom = "Awa"; repoA.save(a) // save réaffecte a.id
82
+
83
+ var repoArt = new Repo(db, new Article())
84
+ var art = new Article(); art.titre = "Hello"; art.auteur = a
85
+ repoArt.save(art) // écrit auteur_id = a.id
86
+
87
+ var trouve = repoArt.find(1)
88
+ print(trouve.auteur.nom) // -> "Awa" (objet lié chargé)
89
+ ```
90
+
91
+ `@ManyToOne("Classe")` crée une colonne de clé étrangère `<champ>_id` (ou le 2ᵉ
92
+ argument), écrite par `save` et chargée par `find`/`findBy` (un niveau).
93
+
94
+ ### Collections : `@OneToMany` et `@ManyToMany`
95
+
96
+ ```okp
97
+ @Entity("auteurs")
98
+ class Auteur {
99
+ @Id var id: int
100
+ @Column("nom") var nom: string
101
+ @OneToMany("Article", "auteur_id") var articles: any // inverse de @ManyToOne
102
+ }
103
+
104
+ @Entity("articles")
105
+ class Article {
106
+ @Id var id: int
107
+ @Column("titre") var titre: string
108
+ @ManyToOne("Auteur") var auteur: any
109
+ @ManyToMany("Tag", "article_tag", "article_id", "tag_id") var tags: any
110
+ }
111
+
112
+ var auteur = repoAuteur.find(1)
113
+ for (a in auteur.articles) { print(a.titre) } // @OneToMany chargé
114
+
115
+ repoArt.attach(article, "tags", tag) // lie via la table de jointure
116
+ var art = repoArt.find(1)
117
+ for (t in art.tags) { print(t.libelle) } // @ManyToMany chargé
118
+ ```
119
+
120
+ `@ManyToMany` crée la **table de jointure** à la migration ; `Repo.attach` y
121
+ insère les liens. Les collections sont chargées sur un niveau (pas de récursion).
122
+
123
+ **Inverse automatique** : `@OneToMany("Article")` (sans colonne) infère la clé
124
+ étrangère `<classe>_id`. **Cascade** : `repo.saveCascade(auteur)` enregistre
125
+ l'auteur et ses `articles`. **Lazy** : `repo.findLazy(id)` charge sans relations.
126
+
127
+ ## Pagination 📄
128
+
129
+ ```okp
130
+ print(repoArt.count()) // nombre total de lignes
131
+ for (x in repoArt.page(10, 2)) { // page 2, 10 par page (entités hydratées)
132
+ print(x.titre)
133
+ }
134
+ // Bas niveau : Query.limit(n).offset(d) + Query.count()
135
+ ```
136
+
137
+ ## Query enrichi 🔎
138
+
139
+ ```okp
140
+ // constructeur fluide, puis hydratation en entités
141
+ var actifs = repo.hydrateRows(
142
+ repo.query()
143
+ .where("age", ">", 18)
144
+ .orWhere("role", "=", "admin")
145
+ .orderBy("nom", "ASC")
146
+ .limit(20)
147
+ .get()
148
+ )
149
+
150
+ repo.query().whereIn("id", [1, 2, 3]).count() // IN (…)
151
+ repo.query().like("titre", "%okp%").first() // LIKE, 1re ligne | null
152
+ repo.query().join("auteurs a", "a.id = articles.auteur_id").get()
153
+ ```
154
+
155
+ ## Migrations incrémentales (façon Alembic) 🧬
156
+
157
+ Suit les migrations déjà appliquées (table `oktopios_migrations`) et n'exécute
158
+ que les nouvelles, dans l'ordre — **idempotent** :
159
+
160
+ ```okp
161
+ var mig = new Migrator("app.db")
162
+ var n = mig.migrer([
163
+ { "nom": "001_users", "sql": "CREATE TABLE users (id INTEGER PRIMARY KEY, nom TEXT)", "down": "DROP TABLE users" },
164
+ { "nom": "002_ville", "sql": "ALTER TABLE users ADD COLUMN ville TEXT" }
165
+ ])
166
+ print(n) // nb réellement appliquées (0 au 2e lancement)
167
+ print(mig.appliquees()) // ["001_users", "002_ville"]
168
+
169
+ mig.rollbackLast() // annule "002_ville" (via son "down" s'il existe)
170
+ mig.rollback("001_users") // exécute "DROP TABLE users" et retire du suivi
171
+ ```
172
+
173
+ ## Décorateurs reconnus
174
+
175
+ | Décorateur | Cible | Effet |
176
+ |---|---|---|
177
+ | `@Entity("table")` | classe | nom de la table |
178
+ | `@Column("colonne")` | champ | nom de la colonne |
179
+ | `@Id` | champ | clé primaire (auto-générée, réaffectée par `save`) |
180
+ | `@ManyToOne("Classe", "fk"?)` | champ | relation N-1 (colonne FK ici) |
181
+ | `@OneToMany("Enfant", "fk")` | champ | collection inverse (1-N) |
182
+ | `@ManyToMany("Autre", "jointure", "fk_ici", "fk_autre")` | champ | relation N-N (table de jointure) |
183
+
184
+ ## Licence
185
+
186
+ MIT © Mouanwiya Ali Soule
@@ -1,2 +1,2 @@
1
1
  """oktopios-orm — ORM pour le langage Oktopios."""
2
- __version__ = "0.2.0"
2
+ __version__ = "0.6.0"