mango-orm 1.0.0
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/LICENSE +15 -0
- package/README.md +1244 -0
- package/dist/src/mango.d.ts +276 -0
- package/dist/src/mango.js +713 -0
- package/package.json +49 -0
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
import * as sql from "mysql";
|
|
2
|
+
/**
|
|
3
|
+
* MangoType - Schema builder for defining table column types
|
|
4
|
+
* Provides chainable methods to build SQL column definitions
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* const type = new MangoType();
|
|
8
|
+
* type.varchar(255).notNull().unique();
|
|
9
|
+
*/
|
|
10
|
+
declare class MangoType {
|
|
11
|
+
private query;
|
|
12
|
+
constructor();
|
|
13
|
+
/** Define an INT column */
|
|
14
|
+
int(): this;
|
|
15
|
+
/** Define a BIGINT column */
|
|
16
|
+
bigInt(): this;
|
|
17
|
+
/** Define a FLOAT column */
|
|
18
|
+
float(): this;
|
|
19
|
+
/**
|
|
20
|
+
* Define a CHAR column with fixed length
|
|
21
|
+
* @param length - The fixed character length
|
|
22
|
+
*/
|
|
23
|
+
char(length: number): this;
|
|
24
|
+
/** Define a TEXT column for large text data */
|
|
25
|
+
text(): this;
|
|
26
|
+
/** Define a DATE column (YYYY-MM-DD) */
|
|
27
|
+
date(): this;
|
|
28
|
+
/** Define a DATETIME column (YYYY-MM-DD HH:MM:SS) */
|
|
29
|
+
dateTime(): this;
|
|
30
|
+
/** Define a TIMESTAMP column (auto-updated on changes) */
|
|
31
|
+
timeStamp(): this;
|
|
32
|
+
/** Define a BOOLEAN column (stored as TINYINT(1)) */
|
|
33
|
+
boolean(): this;
|
|
34
|
+
/**
|
|
35
|
+
* Define a TINYINT column
|
|
36
|
+
* @param length - Display width (e.g., TINYINT(1))
|
|
37
|
+
*/
|
|
38
|
+
tinyInt(length: number): this;
|
|
39
|
+
/** Make column auto-incrementing (use with INT/BIGINT) */
|
|
40
|
+
autoIncrement(): this;
|
|
41
|
+
/** Mark column as primary key */
|
|
42
|
+
primaryKey(): this;
|
|
43
|
+
/**
|
|
44
|
+
* Define a VARCHAR column with variable length
|
|
45
|
+
* @param length - Maximum character length (e.g., 255)
|
|
46
|
+
*/
|
|
47
|
+
varchar(length: number): this;
|
|
48
|
+
/** Make column NOT NULL (required field) */
|
|
49
|
+
notNull(): this;
|
|
50
|
+
/** Add UNIQUE constraint (no duplicate values) */
|
|
51
|
+
unique(): this;
|
|
52
|
+
/** Get the built SQL column definition */
|
|
53
|
+
getQuery(): string;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* MangoQuery - Internal query executor
|
|
57
|
+
* Handles SQL query execution and prepared statements
|
|
58
|
+
* Automatically resets state after execution to prevent query contamination
|
|
59
|
+
*/
|
|
60
|
+
declare class MangoQuery {
|
|
61
|
+
private db;
|
|
62
|
+
query: string;
|
|
63
|
+
supplies: any;
|
|
64
|
+
/**
|
|
65
|
+
* Configure the database connection pool
|
|
66
|
+
* @param db - MySQL connection pool instance
|
|
67
|
+
*/
|
|
68
|
+
config(db: sql.Pool): void;
|
|
69
|
+
/**
|
|
70
|
+
* Execute the built query with prepared statements
|
|
71
|
+
* Automatically resets query and supplies after execution
|
|
72
|
+
* @returns Promise resolving to query results
|
|
73
|
+
*/
|
|
74
|
+
execute<T>(): Promise<T>;
|
|
75
|
+
/**
|
|
76
|
+
* Execute a custom SQL query with parameters
|
|
77
|
+
* Use for complex queries not covered by the query builder
|
|
78
|
+
* @param query - Raw SQL query string
|
|
79
|
+
* @param supplies - Array of parameter values
|
|
80
|
+
* @returns Promise resolving to query results
|
|
81
|
+
*/
|
|
82
|
+
customQuery<T>(query: string, supplies: any[]): Promise<T>;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* MangoTable<T> - Main query builder class for table operations
|
|
86
|
+
* Provides chainable methods for building SQL queries
|
|
87
|
+
* Generic type T represents the shape of table rows
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* const users = new MangoTable(db, 'users', ['id', 'name', 'email']);
|
|
91
|
+
* const result = await users.selectAll().where('id', '=', 1).execute();
|
|
92
|
+
*/
|
|
93
|
+
declare class MangoTable<T> {
|
|
94
|
+
private db;
|
|
95
|
+
private tableName;
|
|
96
|
+
private tableFields;
|
|
97
|
+
private query;
|
|
98
|
+
/**
|
|
99
|
+
* Create a new table instance
|
|
100
|
+
* @param db - MySQL connection pool
|
|
101
|
+
* @param name - Table name (no spaces allowed)
|
|
102
|
+
* @param fields - Array of column names in the table
|
|
103
|
+
*/
|
|
104
|
+
constructor(db: sql.Pool, name: string, fields?: string[]);
|
|
105
|
+
/**
|
|
106
|
+
* Add new columns to an existing table
|
|
107
|
+
* Updates internal field list for validation
|
|
108
|
+
* @param fields - Object mapping column names to MangoType definitions
|
|
109
|
+
* @returns this for method chaining
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* table.addColumns({
|
|
113
|
+
* age: mango.types().int().notNull(),
|
|
114
|
+
* status: mango.types().varchar(50)
|
|
115
|
+
* }).execute();
|
|
116
|
+
*/
|
|
117
|
+
addColumns(fields: Record<string, MangoType>): this;
|
|
118
|
+
/**
|
|
119
|
+
* Remove columns from the table
|
|
120
|
+
* Validates columns exist before removal
|
|
121
|
+
* @param fields - Array of column names to remove
|
|
122
|
+
* @returns this for method chaining
|
|
123
|
+
*
|
|
124
|
+
* @example
|
|
125
|
+
* table.removeColumns(['old_field', 'deprecated_col']).execute();
|
|
126
|
+
*/
|
|
127
|
+
removeColumns(fields: string[]): this;
|
|
128
|
+
/**
|
|
129
|
+
* Select all columns from the table
|
|
130
|
+
* @returns this for method chaining
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* await table.selectAll().where('active', '=', true).execute();
|
|
134
|
+
*/
|
|
135
|
+
selectAll(): this;
|
|
136
|
+
/**
|
|
137
|
+
* Select specific columns from the table
|
|
138
|
+
* Validates all columns exist before building query
|
|
139
|
+
* @param columns - Array of column names to select
|
|
140
|
+
* @returns this for method chaining
|
|
141
|
+
*
|
|
142
|
+
* @example
|
|
143
|
+
* await table.selectColumns(['id', 'name', 'email']).execute();
|
|
144
|
+
*/
|
|
145
|
+
selectColumns(columns: string[]): this;
|
|
146
|
+
selectDistinctColumns(columns: string[]): this;
|
|
147
|
+
orderBy(columnName: string): this;
|
|
148
|
+
sort(sort?: number): this;
|
|
149
|
+
limit(length: number): this;
|
|
150
|
+
offset(length: number): this;
|
|
151
|
+
insertOne(data: Record<string, any>): this;
|
|
152
|
+
insertMany(fields: string[], data?: any[][]): this;
|
|
153
|
+
getFields(): string[];
|
|
154
|
+
getName(): string;
|
|
155
|
+
truncate(): this;
|
|
156
|
+
/**
|
|
157
|
+
* Add WHERE clause to filter query results
|
|
158
|
+
* Use with comparison operators to filter rows
|
|
159
|
+
* Supports falsy values (0, false, empty string)
|
|
160
|
+
* @param field - Column name to filter on
|
|
161
|
+
* @param operator - SQL comparison operator (=, !=, <, >, LIKE, etc.)
|
|
162
|
+
* @param value - Value to compare against (any type)
|
|
163
|
+
* @returns this for method chaining
|
|
164
|
+
*
|
|
165
|
+
* @example
|
|
166
|
+
* table.selectAll().where('age', '>=', 18).execute();
|
|
167
|
+
* table.selectAll().where('active', '=', false).execute(); // Works with false!
|
|
168
|
+
* table.selectAll().where('count', '=', 0).execute(); // Works with 0!
|
|
169
|
+
*/
|
|
170
|
+
where(field: string, operator: string, value: any): this;
|
|
171
|
+
/**
|
|
172
|
+
* Add AND condition to existing WHERE clause
|
|
173
|
+
* Chain multiple conditions together
|
|
174
|
+
* @param field - Column name
|
|
175
|
+
* @param operator - SQL comparison operator
|
|
176
|
+
* @param value - Value to compare (any type including 0, false)
|
|
177
|
+
* @returns this for method chaining
|
|
178
|
+
*
|
|
179
|
+
* @example
|
|
180
|
+
* table.selectAll()
|
|
181
|
+
* .where('age', '>=', 18)
|
|
182
|
+
* .and('active', '=', true)
|
|
183
|
+
* .execute();
|
|
184
|
+
*/
|
|
185
|
+
and(field: string, operator: string, value: any): this;
|
|
186
|
+
/**
|
|
187
|
+
* Add OR condition to existing WHERE clause
|
|
188
|
+
* Combine alternative conditions
|
|
189
|
+
* @param field - Column name
|
|
190
|
+
* @param operator - SQL comparison operator
|
|
191
|
+
* @param value - Value to compare (any type including 0, false)
|
|
192
|
+
* @returns this for method chaining
|
|
193
|
+
*
|
|
194
|
+
* @example
|
|
195
|
+
* table.selectAll()
|
|
196
|
+
* .where('role', '=', 'admin')
|
|
197
|
+
* .or('role', '=', 'moderator')
|
|
198
|
+
* .execute();
|
|
199
|
+
*/
|
|
200
|
+
or(field: string, operator: string, value: any): this;
|
|
201
|
+
update(data: Record<string, any>): this;
|
|
202
|
+
join(type: 'INNER' | 'LEFT' | 'RIGHT' | 'FULL', table: string, condition: {
|
|
203
|
+
left: string;
|
|
204
|
+
operator: string;
|
|
205
|
+
right: string;
|
|
206
|
+
}): this;
|
|
207
|
+
delete(): this;
|
|
208
|
+
whereIn(field: string, values: any[]): this;
|
|
209
|
+
whereNotIn(field: string, values: any[]): this;
|
|
210
|
+
getQuery(): MangoQuery;
|
|
211
|
+
execute(): Promise<T[]>;
|
|
212
|
+
customQuery<Type>(query: string, supplies: any[]): Promise<Type[]>;
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Mango - Main ORM class for MySQL database operations
|
|
216
|
+
* Manages connection pool and provides table instances
|
|
217
|
+
* Auto-discovers existing tables on connection
|
|
218
|
+
*
|
|
219
|
+
* @example
|
|
220
|
+
* const mango = new Mango();
|
|
221
|
+
* await mango.connect({
|
|
222
|
+
* host: 'localhost',
|
|
223
|
+
* user: 'root',
|
|
224
|
+
* password: 'pass',
|
|
225
|
+
* database: 'mydb'
|
|
226
|
+
* });
|
|
227
|
+
*
|
|
228
|
+
* const users = mango.selectTable('users');
|
|
229
|
+
* const results = await users.selectAll().execute();
|
|
230
|
+
*/
|
|
231
|
+
declare class Mango {
|
|
232
|
+
private db;
|
|
233
|
+
private tables;
|
|
234
|
+
private query;
|
|
235
|
+
connect({ host, user, password, database, connectionLimit, waitForConnection, queueLimit, connectTimeout, charset, }: {
|
|
236
|
+
host: any;
|
|
237
|
+
user: any;
|
|
238
|
+
password: any;
|
|
239
|
+
database: any;
|
|
240
|
+
connectionLimit?: number;
|
|
241
|
+
waitForConnection?: boolean;
|
|
242
|
+
queueLimit?: number;
|
|
243
|
+
connectTimeout?: number;
|
|
244
|
+
charset?: string;
|
|
245
|
+
}): Promise<this>;
|
|
246
|
+
disconnect(): Promise<void>;
|
|
247
|
+
/**
|
|
248
|
+
* Get a new MangoType instance for schema building
|
|
249
|
+
* Use when creating or modifying table columns
|
|
250
|
+
* @returns New MangoType instance for chaining
|
|
251
|
+
*
|
|
252
|
+
* @example
|
|
253
|
+
* const idField = mango.types().int().autoIncrement().primaryKey();
|
|
254
|
+
*/
|
|
255
|
+
types(): MangoType;
|
|
256
|
+
/**
|
|
257
|
+
* Get a table instance by name
|
|
258
|
+
* Returns strongly-typed table if generic provided
|
|
259
|
+
* @param name - Name of the table
|
|
260
|
+
* @returns MangoTable instance for building queries
|
|
261
|
+
*
|
|
262
|
+
* @example
|
|
263
|
+
* interface User { id: number; name: string; email: string }
|
|
264
|
+
* const users = mango.selectTable<User>('users');
|
|
265
|
+
*/
|
|
266
|
+
selectTable<T = any>(name: string): MangoTable<T>;
|
|
267
|
+
/**
|
|
268
|
+
* Get all discovered table instances
|
|
269
|
+
* @returns Array of all MangoTable instances
|
|
270
|
+
*/
|
|
271
|
+
getTables(): MangoTable<any>[];
|
|
272
|
+
createTable<T>(name: string, fields: Record<string, MangoType>): Promise<MangoTable<T>>;
|
|
273
|
+
dropTable(name: string): Promise<void>;
|
|
274
|
+
customQuery<Type>(query: string, supplies: any[]): Promise<Type>;
|
|
275
|
+
}
|
|
276
|
+
export { Mango, MangoType, MangoTable };
|