@riao/dbal 1.0.1
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/CHANGELOG.md +3 -0
- package/LICENSE.md +20 -0
- package/README.md +241 -0
- package/builder/builder.d.ts +41 -0
- package/builder/builder.js +73 -0
- package/builder/builder.js.map +1 -0
- package/builder/index.d.ts +1 -0
- package/builder/index.js +6 -0
- package/builder/index.js.map +1 -0
- package/column/column-options.d.ts +141 -0
- package/column/column-options.js +3 -0
- package/column/column-options.js.map +1 -0
- package/column/column-type.d.ts +17 -0
- package/column/column-type.js +26 -0
- package/column/column-type.js.map +1 -0
- package/column/column-values.d.ts +29 -0
- package/column/column-values.js +3 -0
- package/column/column-values.js.map +1 -0
- package/column/index.d.ts +3 -0
- package/column/index.js +20 -0
- package/column/index.js.map +1 -0
- package/config/configure-db.d.ts +14 -0
- package/config/configure-db.js +19 -0
- package/config/configure-db.js.map +1 -0
- package/config/database-env.d.ts +16 -0
- package/config/database-env.js +15 -0
- package/config/database-env.js.map +1 -0
- package/config/index.d.ts +2 -0
- package/config/index.js +8 -0
- package/config/index.js.map +1 -0
- package/database/connection-options.d.ts +7 -0
- package/database/connection-options.js +3 -0
- package/database/connection-options.js.map +1 -0
- package/database/database.d.ts +185 -0
- package/database/database.js +257 -0
- package/database/database.js.map +1 -0
- package/database/driver-query.d.ts +10 -0
- package/database/driver-query.js +3 -0
- package/database/driver-query.js.map +1 -0
- package/database/driver.d.ts +9 -0
- package/database/driver.js +43 -0
- package/database/driver.js.map +1 -0
- package/database/get-database-path.d.ts +6 -0
- package/database/get-database-path.js +13 -0
- package/database/get-database-path.js.map +1 -0
- package/database/index.d.ts +6 -0
- package/database/index.js +27 -0
- package/database/index.js.map +1 -0
- package/database/load-database.d.ts +9 -0
- package/database/load-database.js +37 -0
- package/database/load-database.js.map +1 -0
- package/ddl/alter-table.d.ts +26 -0
- package/ddl/alter-table.js +3 -0
- package/ddl/alter-table.js.map +1 -0
- package/ddl/create-database.d.ts +3 -0
- package/ddl/create-database.js +3 -0
- package/ddl/create-database.js.map +1 -0
- package/ddl/create-table.d.ts +8 -0
- package/ddl/create-table.js +3 -0
- package/ddl/create-table.js.map +1 -0
- package/ddl/create-user.d.ts +4 -0
- package/ddl/create-user.js +3 -0
- package/ddl/create-user.js.map +1 -0
- package/ddl/ddl-builder.d.ts +68 -0
- package/ddl/ddl-builder.js +358 -0
- package/ddl/ddl-builder.js.map +1 -0
- package/ddl/ddl-repository.d.ts +104 -0
- package/ddl/ddl-repository.js +176 -0
- package/ddl/ddl-repository.js.map +1 -0
- package/ddl/drop-database.d.ts +4 -0
- package/ddl/drop-database.js +3 -0
- package/ddl/drop-database.js.map +1 -0
- package/ddl/drop-table.d.ts +4 -0
- package/ddl/drop-table.js +3 -0
- package/ddl/drop-table.js.map +1 -0
- package/ddl/drop-user.d.ts +4 -0
- package/ddl/drop-user.js +3 -0
- package/ddl/drop-user.js.map +1 -0
- package/ddl/foreign-key-constraint.d.ts +9 -0
- package/ddl/foreign-key-constraint.js +3 -0
- package/ddl/foreign-key-constraint.js.map +1 -0
- package/ddl/grant.d.ts +12 -0
- package/ddl/grant.js +3 -0
- package/ddl/grant.js.map +1 -0
- package/ddl/index.d.ts +12 -0
- package/ddl/index.js +24 -0
- package/ddl/index.js.map +1 -0
- package/ddl/truncate.d.ts +3 -0
- package/ddl/truncate.js +3 -0
- package/ddl/truncate.js.map +1 -0
- package/dml/column-name.d.ts +3 -0
- package/dml/column-name.js +12 -0
- package/dml/column-name.js.map +1 -0
- package/dml/delete.d.ts +8 -0
- package/dml/delete.js +3 -0
- package/dml/delete.js.map +1 -0
- package/dml/index.d.ts +10 -0
- package/dml/index.js +30 -0
- package/dml/index.js.map +1 -0
- package/dml/insert.d.ts +11 -0
- package/dml/insert.js +3 -0
- package/dml/insert.js.map +1 -0
- package/dml/join.d.ts +15 -0
- package/dml/join.js +3 -0
- package/dml/join.js.map +1 -0
- package/dml/order-by.d.ts +2 -0
- package/dml/order-by.js +3 -0
- package/dml/order-by.js.map +1 -0
- package/dml/query-builder.d.ts +53 -0
- package/dml/query-builder.js +398 -0
- package/dml/query-builder.js.map +1 -0
- package/dml/query-repository.d.ts +67 -0
- package/dml/query-repository.js +140 -0
- package/dml/query-repository.js.map +1 -0
- package/dml/select.d.ts +23 -0
- package/dml/select.js +3 -0
- package/dml/select.js.map +1 -0
- package/dml/update.d.ts +9 -0
- package/dml/update.js +3 -0
- package/dml/update.js.map +1 -0
- package/dml/where.d.ts +26 -0
- package/dml/where.js +67 -0
- package/dml/where.js.map +1 -0
- package/functions/function-interface.d.ts +18 -0
- package/functions/function-interface.js +8 -0
- package/functions/function-interface.js.map +1 -0
- package/functions/functions.d.ts +5 -0
- package/functions/functions.js +18 -0
- package/functions/functions.js.map +1 -0
- package/functions/index.d.ts +1 -0
- package/functions/index.js +18 -0
- package/functions/index.js.map +1 -0
- package/index.d.ts +9 -0
- package/index.js +26 -0
- package/index.js.map +1 -0
- package/migration/create-migration-table.d.ts +8 -0
- package/migration/create-migration-table.js +54 -0
- package/migration/create-migration-table.js.map +1 -0
- package/migration/index.d.ts +2 -0
- package/migration/index.js +8 -0
- package/migration/index.js.map +1 -0
- package/migration/migration-record.d.ts +9 -0
- package/migration/migration-record.js +3 -0
- package/migration/migration-record.js.map +1 -0
- package/migration/migration-runner.d.ts +19 -0
- package/migration/migration-runner.js +114 -0
- package/migration/migration-runner.js.map +1 -0
- package/migration/migration.d.ts +11 -0
- package/migration/migration.js +27 -0
- package/migration/migration.js.map +1 -0
- package/package.json +28 -0
- package/record/index.d.ts +1 -0
- package/record/index.js +18 -0
- package/record/index.js.map +1 -0
- package/record/record.d.ts +2 -0
- package/record/record.js +3 -0
- package/record/record.js.map +1 -0
- package/repository/index.d.ts +2 -0
- package/repository/index.js +6 -0
- package/repository/index.js.map +1 -0
- package/repository/repository-options.d.ts +4 -0
- package/repository/repository-options.js +3 -0
- package/repository/repository-options.js.map +1 -0
- package/repository/repository.d.ts +6 -0
- package/repository/repository.js +10 -0
- package/repository/repository.js.map +1 -0
- package/schema/index.d.ts +3 -0
- package/schema/index.js +6 -0
- package/schema/index.js.map +1 -0
- package/schema/schema-query-repository.d.ts +45 -0
- package/schema/schema-query-repository.js +154 -0
- package/schema/schema-query-repository.js.map +1 -0
- package/schema/schema-table.d.ts +9 -0
- package/schema/schema-table.js +3 -0
- package/schema/schema-table.js.map +1 -0
- package/schema/schema.d.ts +4 -0
- package/schema/schema.js +3 -0
- package/schema/schema.js.map +1 -0
- package/seed/auto-seed.d.ts +8 -0
- package/seed/auto-seed.js +58 -0
- package/seed/auto-seed.js.map +1 -0
- package/seed/create-seed-table.d.ts +8 -0
- package/seed/create-seed-table.js +64 -0
- package/seed/create-seed-table.js.map +1 -0
- package/seed/index.d.ts +4 -0
- package/seed/index.js +10 -0
- package/seed/index.js.map +1 -0
- package/seed/seed-record.d.ts +11 -0
- package/seed/seed-record.js +3 -0
- package/seed/seed-record.js.map +1 -0
- package/seed/seed-runner.d.ts +20 -0
- package/seed/seed-runner.js +91 -0
- package/seed/seed-runner.js.map +1 -0
- package/seed/seed.d.ts +12 -0
- package/seed/seed.js +28 -0
- package/seed/seed.js.map +1 -0
package/CHANGELOG.md
ADDED
package/LICENSE.md
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Copyright (c) 2022 riao-dbal and others
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
4
|
+
a copy of this software and associated documentation files (the
|
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
9
|
+
the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be
|
|
12
|
+
included in all copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
# riao-dbal - Readme
|
|
2
|
+
|
|
3
|
+
## Installation
|
|
4
|
+
|
|
5
|
+
`npm i riao-dbal`
|
|
6
|
+
|
|
7
|
+
You'll also need to install a database driver, i.e. `npm i riao-driver-mysql`
|
|
8
|
+
|
|
9
|
+
## Project Setup
|
|
10
|
+
|
|
11
|
+
Create a folder in the root of your project named `database`. Your database(s) will go in here.
|
|
12
|
+
|
|
13
|
+
### Create a database
|
|
14
|
+
|
|
15
|
+
In your new database folder, create a folder called `main`. This will be your main database.
|
|
16
|
+
|
|
17
|
+
In this folder, you'll need two files: a `main.db.env` file to configure your database, and an `index.ts` file to export your database.
|
|
18
|
+
|
|
19
|
+
You're folder structure should now look like this:
|
|
20
|
+
|
|
21
|
+
```
|
|
22
|
+
|- database
|
|
23
|
+
|- main
|
|
24
|
+
|- index.ts
|
|
25
|
+
|- main.db.env
|
|
26
|
+
|- src
|
|
27
|
+
|- package.json
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Let's create the index file first. This file create a database class for our database, named `MainDatabase`. It extends `DatabaseMySql8` because that's the driver we want to use.
|
|
31
|
+
|
|
32
|
+
`database/main/index.ts`
|
|
33
|
+
```typescript
|
|
34
|
+
import { DatabaseMySql8 } from 'riao-driver-mysql';
|
|
35
|
+
|
|
36
|
+
export default class MainDatabase extends DatabaseMySql8 {}
|
|
37
|
+
|
|
38
|
+
export const maindb = new MainDatabase();
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Now we can setup a mysql database, and provide the connection configuration in our env file:
|
|
42
|
+
|
|
43
|
+
`database/main/main.db.env`
|
|
44
|
+
```
|
|
45
|
+
port=3307
|
|
46
|
+
username=riao
|
|
47
|
+
password=password1234
|
|
48
|
+
database=riao
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Don't forget to add the `database/` folder to your tsconfig:
|
|
52
|
+
|
|
53
|
+
`tsconfig.json`
|
|
54
|
+
```json
|
|
55
|
+
{
|
|
56
|
+
...
|
|
57
|
+
"include": ["./database", "./src" ...]
|
|
58
|
+
}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Using the database
|
|
62
|
+
|
|
63
|
+
To use our new database, we can import from the index file, init() it, and use it!
|
|
64
|
+
|
|
65
|
+
`src/main.ts`
|
|
66
|
+
```typescript
|
|
67
|
+
import { maindb } from '../database/main';
|
|
68
|
+
|
|
69
|
+
export async function main() {
|
|
70
|
+
await maindb.init(); // Make sure to `init()` the database first!
|
|
71
|
+
|
|
72
|
+
await maindb.query.insert({
|
|
73
|
+
table: 'user',
|
|
74
|
+
records: [
|
|
75
|
+
{
|
|
76
|
+
id: 23,
|
|
77
|
+
email: 'tom@tester.com',
|
|
78
|
+
password: 'asdfa2342',
|
|
79
|
+
},
|
|
80
|
+
],
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
const users = await maindb.query.find({
|
|
84
|
+
table: 'user',
|
|
85
|
+
where: {
|
|
86
|
+
email: 'tom@tester.com',
|
|
87
|
+
},
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
console.log(users);
|
|
91
|
+
// [ { id: 23, email: 'tom@tester.com', password: 'asdfa2342', } ]
|
|
92
|
+
|
|
93
|
+
await maindb.query.update({
|
|
94
|
+
table: 'user',
|
|
95
|
+
set: {
|
|
96
|
+
password: 'password1234',
|
|
97
|
+
},
|
|
98
|
+
where: {
|
|
99
|
+
email: 'tom@tester.com',
|
|
100
|
+
},
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
const user = await maindb.query.findOneOrFail({
|
|
104
|
+
table: 'user',
|
|
105
|
+
where: { id: 23 },
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
console.log(user);
|
|
109
|
+
// { id: 23, email: 'tom@tester.com', password: 'password1234' }
|
|
110
|
+
|
|
111
|
+
await maindb.query.delete({
|
|
112
|
+
table: 'user',
|
|
113
|
+
where: {
|
|
114
|
+
id: user.id,
|
|
115
|
+
},
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## Custom Config (No database folder/files required)
|
|
121
|
+
|
|
122
|
+
**WARNING:** Using this method will not let you take advantage of migrations, seeds, or riao's `.env` loading!
|
|
123
|
+
|
|
124
|
+
If you don't want to create a database folder and files, you can load & configure a database manually:
|
|
125
|
+
|
|
126
|
+
```typescript
|
|
127
|
+
import { ColumnType } from 'riao-dbal';
|
|
128
|
+
import { DatabaseMySql8 } from 'riao-driver-mysql';
|
|
129
|
+
|
|
130
|
+
const db = new DatabaseMySql8();
|
|
131
|
+
|
|
132
|
+
await db.init({
|
|
133
|
+
host: 'localhost',
|
|
134
|
+
port: 3306,
|
|
135
|
+
username: 'riao',
|
|
136
|
+
password: 'password1234',
|
|
137
|
+
database: 'riao',
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
await db.ddl.createTable({
|
|
141
|
+
name: 'user',
|
|
142
|
+
ifNotExists: true,
|
|
143
|
+
columns: [
|
|
144
|
+
{
|
|
145
|
+
name: 'id',
|
|
146
|
+
type: ColumnType.BIGINT,
|
|
147
|
+
primaryKey: true,
|
|
148
|
+
autoIncrement: true,
|
|
149
|
+
},
|
|
150
|
+
{
|
|
151
|
+
name: 'email',
|
|
152
|
+
type: ColumnType.VARCHAR,
|
|
153
|
+
length: 255,
|
|
154
|
+
},
|
|
155
|
+
{
|
|
156
|
+
name: 'password',
|
|
157
|
+
type: ColumnType.VARCHAR,
|
|
158
|
+
length: 255,
|
|
159
|
+
},
|
|
160
|
+
],
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
await db.query.insert({
|
|
164
|
+
table: 'user',
|
|
165
|
+
records: [
|
|
166
|
+
{
|
|
167
|
+
email: 'test@example.com',
|
|
168
|
+
password: 'password1234',
|
|
169
|
+
},
|
|
170
|
+
],
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
const users = await db.query.find({
|
|
174
|
+
table: 'user',
|
|
175
|
+
where: { email: 'test@example.com' },
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
console.log(users);
|
|
179
|
+
// [ { id: 1, email: 'test@example.com', password: 'password1234' } ]
|
|
180
|
+
|
|
181
|
+
const user = await db.query.findOneOrFail({
|
|
182
|
+
table: 'user',
|
|
183
|
+
where: { email: 'test@example.com' },
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
console.log(user);
|
|
187
|
+
// { id: 1, email: 'test@example.com', password: 'password1234' }
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
## Hierarchy
|
|
191
|
+
|
|
192
|
+
### Builder
|
|
193
|
+
|
|
194
|
+
A builder generates sql queries to be run against a database.
|
|
195
|
+
|
|
196
|
+
#### DDL Builder
|
|
197
|
+
|
|
198
|
+
The DDL (Data definition language) Builder builds queries for creating, updating, and truncating databases, tables, etc.
|
|
199
|
+
|
|
200
|
+
#### Query (DML) Builder
|
|
201
|
+
|
|
202
|
+
The Query (DML - Data manipulation language) Builder builds queries for creating, querying, updating, and deleting records.
|
|
203
|
+
|
|
204
|
+
```typescript
|
|
205
|
+
const query = db
|
|
206
|
+
.getQueryBuilder()
|
|
207
|
+
.select({
|
|
208
|
+
table: 'user',
|
|
209
|
+
where: {
|
|
210
|
+
id: 2,
|
|
211
|
+
},
|
|
212
|
+
})
|
|
213
|
+
.toDatabaseQuery();
|
|
214
|
+
|
|
215
|
+
console.log(query);
|
|
216
|
+
// { sql: 'SELECT * FROM user WHERE (id = ?)', params: [ 2 ] }
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
### Repository
|
|
220
|
+
|
|
221
|
+
Repositories are functions to build queries (using the builders) and run them against a database.
|
|
222
|
+
|
|
223
|
+
#### DDL Repository
|
|
224
|
+
|
|
225
|
+
The DDL Builder contains functions to use generate DDL queries and run them against a database.
|
|
226
|
+
|
|
227
|
+
#### Query Repository
|
|
228
|
+
|
|
229
|
+
The Query Builder contains functions to use generate DML queries and run them against a database.
|
|
230
|
+
|
|
231
|
+
### Database Drivers
|
|
232
|
+
|
|
233
|
+
Drivers contain functions to interact with the underlying database.
|
|
234
|
+
|
|
235
|
+
### Database
|
|
236
|
+
|
|
237
|
+
A database has a driver, connection options, builders, and repositories. Once you create a database and connect to it, you can use it's repositories or builders to query that database.
|
|
238
|
+
|
|
239
|
+
## Contributing & Development
|
|
240
|
+
|
|
241
|
+
See [contributing.md](docs/contributing/contributing.md) for information on how to develop or contribute to this project!
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { DatabaseFunctionToken } from '../functions/function-interface';
|
|
2
|
+
import { DatabaseQueryOptions } from '../database/driver-query';
|
|
3
|
+
export declare abstract class Builder {
|
|
4
|
+
protected sql: string;
|
|
5
|
+
protected params: any[];
|
|
6
|
+
protected operators: {
|
|
7
|
+
closeParens: string;
|
|
8
|
+
openParens: string;
|
|
9
|
+
equals: string;
|
|
10
|
+
like: string;
|
|
11
|
+
notEqual: string;
|
|
12
|
+
negate: string;
|
|
13
|
+
lt: string;
|
|
14
|
+
lte: string;
|
|
15
|
+
gt: string;
|
|
16
|
+
gte: string;
|
|
17
|
+
and: string;
|
|
18
|
+
or: string;
|
|
19
|
+
null: string;
|
|
20
|
+
is: string;
|
|
21
|
+
in: string;
|
|
22
|
+
};
|
|
23
|
+
commaSeparate(strings: string[]): this;
|
|
24
|
+
openParens(): this;
|
|
25
|
+
closeParens(): this;
|
|
26
|
+
trimEnd(s: string): this;
|
|
27
|
+
toDatabaseQuery(): DatabaseQueryOptions;
|
|
28
|
+
/**
|
|
29
|
+
* Check if a value is a database function token
|
|
30
|
+
*
|
|
31
|
+
* @param fn Value to check
|
|
32
|
+
* @returns Returns true if the value is a database function
|
|
33
|
+
*/
|
|
34
|
+
isDatabaseFunction(fn: any): boolean;
|
|
35
|
+
/**
|
|
36
|
+
* Append a database function the query
|
|
37
|
+
*
|
|
38
|
+
* @param fn Database function token
|
|
39
|
+
*/
|
|
40
|
+
databaseFunction(fn: DatabaseFunctionToken): void;
|
|
41
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Builder = void 0;
|
|
4
|
+
class Builder {
|
|
5
|
+
constructor() {
|
|
6
|
+
this.sql = '';
|
|
7
|
+
this.params = [];
|
|
8
|
+
this.operators = {
|
|
9
|
+
closeParens: ')',
|
|
10
|
+
openParens: '(',
|
|
11
|
+
equals: '=',
|
|
12
|
+
like: 'LIKE',
|
|
13
|
+
notEqual: '!=',
|
|
14
|
+
negate: '!',
|
|
15
|
+
lt: '<',
|
|
16
|
+
lte: '<=',
|
|
17
|
+
gt: '>',
|
|
18
|
+
gte: '>=',
|
|
19
|
+
and: 'AND',
|
|
20
|
+
or: 'OR',
|
|
21
|
+
null: 'NULL',
|
|
22
|
+
is: 'IS',
|
|
23
|
+
in: 'IN',
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
commaSeparate(strings) {
|
|
27
|
+
this.sql += strings.join(', ');
|
|
28
|
+
return this;
|
|
29
|
+
}
|
|
30
|
+
openParens() {
|
|
31
|
+
this.sql += this.operators.openParens;
|
|
32
|
+
return this;
|
|
33
|
+
}
|
|
34
|
+
closeParens() {
|
|
35
|
+
this.sql = this.sql.trimEnd();
|
|
36
|
+
this.sql += this.operators.closeParens + ' ';
|
|
37
|
+
return this;
|
|
38
|
+
}
|
|
39
|
+
trimEnd(s) {
|
|
40
|
+
s = s.trimEnd();
|
|
41
|
+
this.sql = this.sql.trimEnd();
|
|
42
|
+
this.sql = this.sql.substring(0, this.sql.length - s.length);
|
|
43
|
+
return this;
|
|
44
|
+
}
|
|
45
|
+
toDatabaseQuery() {
|
|
46
|
+
return {
|
|
47
|
+
sql: this.sql.trim(),
|
|
48
|
+
params: this.params,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
// ------------------------------------------------------------------------
|
|
52
|
+
// Database Functions
|
|
53
|
+
// ------------------------------------------------------------------------
|
|
54
|
+
/**
|
|
55
|
+
* Check if a value is a database function token
|
|
56
|
+
*
|
|
57
|
+
* @param fn Value to check
|
|
58
|
+
* @returns Returns true if the value is a database function
|
|
59
|
+
*/
|
|
60
|
+
isDatabaseFunction(fn) {
|
|
61
|
+
return typeof fn === 'object' && 'riao_dbfn' in fn;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Append a database function the query
|
|
65
|
+
*
|
|
66
|
+
* @param fn Database function token
|
|
67
|
+
*/
|
|
68
|
+
databaseFunction(fn) {
|
|
69
|
+
this.sql += fn.riao_dbfn;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
exports.Builder = Builder;
|
|
73
|
+
//# sourceMappingURL=builder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"builder.js","sourceRoot":"","sources":["../../../src/builder/builder.ts"],"names":[],"mappings":";;;AAGA,MAAsB,OAAO;IAA7B;QACW,QAAG,GAAG,EAAE,CAAC;QACT,WAAM,GAAU,EAAE,CAAC;QAEnB,cAAS,GAAG;YACrB,WAAW,EAAE,GAAG;YAChB,UAAU,EAAE,GAAG;YACf,MAAM,EAAE,GAAG;YACX,IAAI,EAAE,MAAM;YACZ,QAAQ,EAAE,IAAI;YACd,MAAM,EAAE,GAAG;YACX,EAAE,EAAE,GAAG;YACP,GAAG,EAAE,IAAI;YACT,EAAE,EAAE,GAAG;YACP,GAAG,EAAE,IAAI;YACT,GAAG,EAAE,KAAK;YACV,EAAE,EAAE,IAAI;YACR,IAAI,EAAE,MAAM;YACZ,EAAE,EAAE,IAAI;YACR,EAAE,EAAE,IAAI;SACR,CAAC;IA0DH,CAAC;IAxDO,aAAa,CAAC,OAAiB;QACrC,IAAI,CAAC,GAAG,IAAI,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAE/B,OAAO,IAAI,CAAC;IACb,CAAC;IAEM,UAAU;QAChB,IAAI,CAAC,GAAG,IAAI,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC;QAEtC,OAAO,IAAI,CAAC;IACb,CAAC;IAEM,WAAW;QACjB,IAAI,CAAC,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,OAAO,EAAE,CAAC;QAC9B,IAAI,CAAC,GAAG,IAAI,IAAI,CAAC,SAAS,CAAC,WAAW,GAAG,GAAG,CAAC;QAE7C,OAAO,IAAI,CAAC;IACb,CAAC;IAEM,OAAO,CAAC,CAAS;QACvB,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,CAAC;QAChB,IAAI,CAAC,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,OAAO,EAAE,CAAC;QAC9B,IAAI,CAAC,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,MAAM,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC;QAE7D,OAAO,IAAI,CAAC;IACb,CAAC;IAEM,eAAe;QACrB,OAAO;YACN,GAAG,EAAE,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE;YACpB,MAAM,EAAE,IAAI,CAAC,MAAM;SACnB,CAAC;IACH,CAAC;IAED,2EAA2E;IAC3E,qBAAqB;IACrB,2EAA2E;IAE3E;;;;;OAKG;IACI,kBAAkB,CAAC,EAAO;QAChC,OAAO,OAAO,EAAE,KAAK,QAAQ,IAAI,WAAW,IAAI,EAAE,CAAC;IACpD,CAAC;IAED;;;;OAIG;IACI,gBAAgB,CAAC,EAAyB;QAChD,IAAI,CAAC,GAAG,IAAI,EAAE,CAAC,SAAS,CAAC;IAC1B,CAAC;CACD;AA9ED,0BA8EC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { Builder } from './builder';
|
package/builder/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Builder = void 0;
|
|
4
|
+
var builder_1 = require("./builder");
|
|
5
|
+
Object.defineProperty(exports, "Builder", { enumerable: true, get: function () { return builder_1.Builder; } });
|
|
6
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/builder/index.ts"],"names":[],"mappings":";;;AAAA,qCAAoC;AAA3B,kGAAA,OAAO,OAAA"}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { DatabaseFunctionToken } from '../functions/function-interface';
|
|
2
|
+
import { ColumnType } from './column-type';
|
|
3
|
+
import * as DataTypes from './column-values';
|
|
4
|
+
/**
|
|
5
|
+
* Base interface for column options
|
|
6
|
+
*/
|
|
7
|
+
export interface BaseColumnOptions {
|
|
8
|
+
name: string;
|
|
9
|
+
type: ColumnType;
|
|
10
|
+
default?: string | DataTypes.ColumnValue | DatabaseFunctionToken;
|
|
11
|
+
primaryKey?: boolean;
|
|
12
|
+
required?: boolean;
|
|
13
|
+
isUnique?: boolean;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Bool column options
|
|
17
|
+
*/
|
|
18
|
+
export interface BoolColumnOptions extends BaseColumnOptions {
|
|
19
|
+
default?: DataTypes.BoolValue | DatabaseFunctionToken<ColumnType.BOOL>;
|
|
20
|
+
type: ColumnType.BOOL;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Base class for integer column options
|
|
24
|
+
*/
|
|
25
|
+
export interface BaseIntColumnOptions extends BaseColumnOptions {
|
|
26
|
+
default?: DataTypes.IntegerValue | DatabaseFunctionToken<ColumnType.INT>;
|
|
27
|
+
autoIncrement?: boolean;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Tiny int column options
|
|
31
|
+
*/
|
|
32
|
+
export interface TinyIntColumnOptions extends BaseIntColumnOptions {
|
|
33
|
+
type: ColumnType.TINYINT;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Small int column options
|
|
37
|
+
*/
|
|
38
|
+
export interface SmallIntColumnOptions extends BaseIntColumnOptions {
|
|
39
|
+
type: ColumnType.SMALLINT;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Int column options
|
|
43
|
+
*/
|
|
44
|
+
export interface IntColumnOptions extends BaseIntColumnOptions {
|
|
45
|
+
type: ColumnType.INT;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Big int column options
|
|
49
|
+
*/
|
|
50
|
+
export interface BigIntColumnOptions extends BaseIntColumnOptions {
|
|
51
|
+
type: ColumnType.BIGINT;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Base interface for decimal types
|
|
55
|
+
*/
|
|
56
|
+
export interface BaseDecimalColumnOptions extends BaseColumnOptions {
|
|
57
|
+
default?: DataTypes.DecimalValue | DatabaseFunctionToken<ColumnType.DECIMAL>;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Decimal column options
|
|
61
|
+
*/
|
|
62
|
+
export interface DecimalColumnOptions extends BaseDecimalColumnOptions {
|
|
63
|
+
significant: number;
|
|
64
|
+
decimal: number;
|
|
65
|
+
type: ColumnType.DECIMAL;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Float column options
|
|
69
|
+
*/
|
|
70
|
+
export interface FloatColumnOptions extends BaseDecimalColumnOptions {
|
|
71
|
+
type: ColumnType.FLOAT;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Double column options
|
|
75
|
+
*/
|
|
76
|
+
export interface DoubleColumnOptions extends BaseDecimalColumnOptions {
|
|
77
|
+
type: ColumnType.DOUBLE;
|
|
78
|
+
}
|
|
79
|
+
export type DateDefaultValues = string | DataTypes.DateValue;
|
|
80
|
+
/**
|
|
81
|
+
* Date column options
|
|
82
|
+
*/
|
|
83
|
+
export interface DateColumnOptions extends BaseColumnOptions {
|
|
84
|
+
default?: DateDefaultValues | DatabaseFunctionToken<ColumnType.DATE>;
|
|
85
|
+
type: ColumnType.DATE;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Time column options
|
|
89
|
+
*/
|
|
90
|
+
export interface TimeColumnOptions extends BaseColumnOptions {
|
|
91
|
+
default?: DateDefaultValues | DatabaseFunctionToken<ColumnType.TIME>;
|
|
92
|
+
type: ColumnType.TIME;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Timestamp column options
|
|
96
|
+
*/
|
|
97
|
+
export interface TimestampColumnOptions extends BaseColumnOptions {
|
|
98
|
+
default?: DateDefaultValues | DatabaseFunctionToken<ColumnType.TIMESTAMP>;
|
|
99
|
+
type: ColumnType.TIMESTAMP;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Base interface for text column options
|
|
103
|
+
*/
|
|
104
|
+
export interface BaseTextColumnOptions extends BaseColumnOptions {
|
|
105
|
+
default?: DataTypes.TextValue | DatabaseFunctionToken<ColumnType.CHAR> | DatabaseFunctionToken<ColumnType.VARCHAR> | DatabaseFunctionToken<ColumnType.TEXT>;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Char column options
|
|
109
|
+
*/
|
|
110
|
+
export interface CharColumnOptions extends BaseTextColumnOptions {
|
|
111
|
+
type: ColumnType.CHAR;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* VarChar column options
|
|
115
|
+
*/
|
|
116
|
+
export interface VarCharColumnOptions extends BaseTextColumnOptions {
|
|
117
|
+
length: number;
|
|
118
|
+
type: ColumnType.VARCHAR;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Text column options
|
|
122
|
+
*/
|
|
123
|
+
export interface TextColumOptions extends BaseTextColumnOptions {
|
|
124
|
+
type: ColumnType.TEXT;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Base interface for blob column options
|
|
128
|
+
*/
|
|
129
|
+
export interface BaseBlobColumnOptions extends BaseColumnOptions {
|
|
130
|
+
default?: DataTypes.BlobValue | DatabaseFunctionToken<ColumnType.BLOB>;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Blob column options
|
|
134
|
+
*/
|
|
135
|
+
export interface BlobColumnOptions extends BaseBlobColumnOptions {
|
|
136
|
+
type: ColumnType.BLOB;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Column options
|
|
140
|
+
*/
|
|
141
|
+
export type ColumnOptions = BoolColumnOptions | TinyIntColumnOptions | SmallIntColumnOptions | IntColumnOptions | BigIntColumnOptions | DecimalColumnOptions | FloatColumnOptions | DoubleColumnOptions | DateColumnOptions | TimeColumnOptions | TimestampColumnOptions | CharColumnOptions | VarCharColumnOptions | TextColumOptions | BlobColumnOptions;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"column-options.js","sourceRoot":"","sources":["../../../src/column/column-options.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export declare enum ColumnType {
|
|
2
|
+
BOOL = "BOOL",
|
|
3
|
+
TINYINT = "TINYINT",
|
|
4
|
+
SMALLINT = "SMALLINT",
|
|
5
|
+
INT = "INT",
|
|
6
|
+
BIGINT = "BIGINT",
|
|
7
|
+
DECIMAL = "DECIMAL",
|
|
8
|
+
FLOAT = "FLOAT",
|
|
9
|
+
DOUBLE = "DOUBLE",
|
|
10
|
+
DATE = "DATE",
|
|
11
|
+
TIME = "TIME",
|
|
12
|
+
TIMESTAMP = "TIMESTAMP",
|
|
13
|
+
CHAR = "CHAR",
|
|
14
|
+
VARCHAR = "VARCHAR",
|
|
15
|
+
TEXT = "TEXT",
|
|
16
|
+
BLOB = "BLOB"
|
|
17
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ColumnType = void 0;
|
|
4
|
+
var ColumnType;
|
|
5
|
+
(function (ColumnType) {
|
|
6
|
+
// Numbers
|
|
7
|
+
ColumnType["BOOL"] = "BOOL";
|
|
8
|
+
ColumnType["TINYINT"] = "TINYINT";
|
|
9
|
+
ColumnType["SMALLINT"] = "SMALLINT";
|
|
10
|
+
ColumnType["INT"] = "INT";
|
|
11
|
+
ColumnType["BIGINT"] = "BIGINT";
|
|
12
|
+
ColumnType["DECIMAL"] = "DECIMAL";
|
|
13
|
+
ColumnType["FLOAT"] = "FLOAT";
|
|
14
|
+
ColumnType["DOUBLE"] = "DOUBLE";
|
|
15
|
+
// Dates/Times
|
|
16
|
+
ColumnType["DATE"] = "DATE";
|
|
17
|
+
ColumnType["TIME"] = "TIME";
|
|
18
|
+
ColumnType["TIMESTAMP"] = "TIMESTAMP";
|
|
19
|
+
// Text
|
|
20
|
+
ColumnType["CHAR"] = "CHAR";
|
|
21
|
+
ColumnType["VARCHAR"] = "VARCHAR";
|
|
22
|
+
ColumnType["TEXT"] = "TEXT";
|
|
23
|
+
// Binary
|
|
24
|
+
ColumnType["BLOB"] = "BLOB";
|
|
25
|
+
})(ColumnType = exports.ColumnType || (exports.ColumnType = {}));
|
|
26
|
+
//# sourceMappingURL=column-type.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"column-type.js","sourceRoot":"","sources":["../../../src/column/column-type.ts"],"names":[],"mappings":";;;AAAA,IAAY,UAuBX;AAvBD,WAAY,UAAU;IACrB,UAAU;IACV,2BAAa,CAAA;IACb,iCAAmB,CAAA;IACnB,mCAAqB,CAAA;IACrB,yBAAW,CAAA;IACX,+BAAiB,CAAA;IACjB,iCAAmB,CAAA;IACnB,6BAAe,CAAA;IACf,+BAAiB,CAAA;IAEjB,cAAc;IACd,2BAAa,CAAA;IACb,2BAAa,CAAA;IACb,qCAAuB,CAAA;IAEvB,OAAO;IACP,2BAAa,CAAA;IACb,iCAAmB,CAAA;IACnB,2BAAa,CAAA;IAEb,SAAS;IACT,2BAAa,CAAA;AACd,CAAC,EAvBW,UAAU,GAAV,kBAAU,KAAV,kBAAU,QAuBrB"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/**
|
|
3
|
+
* Type for a boolean property
|
|
4
|
+
*/
|
|
5
|
+
export type BoolValue = boolean;
|
|
6
|
+
/**
|
|
7
|
+
* Type for a integer property
|
|
8
|
+
*/
|
|
9
|
+
export type IntegerValue = number | bigint;
|
|
10
|
+
/**
|
|
11
|
+
* Type for a decimal/float property
|
|
12
|
+
*/
|
|
13
|
+
export type DecimalValue = number;
|
|
14
|
+
/**
|
|
15
|
+
* Type for a date/time property
|
|
16
|
+
*/
|
|
17
|
+
export type DateValue = Date;
|
|
18
|
+
/**
|
|
19
|
+
* Type for a text property
|
|
20
|
+
*/
|
|
21
|
+
export type TextValue = string;
|
|
22
|
+
/**
|
|
23
|
+
* Type for a blob property
|
|
24
|
+
*/
|
|
25
|
+
export type BlobValue = string | Buffer;
|
|
26
|
+
/**
|
|
27
|
+
* Type for a property
|
|
28
|
+
*/
|
|
29
|
+
export type ColumnValue = BoolValue | IntegerValue | DecimalValue | DateValue | TextValue | BlobValue;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"column-values.js","sourceRoot":"","sources":["../../../src/column/column-values.ts"],"names":[],"mappings":""}
|
package/column/index.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./column-options"), exports);
|
|
18
|
+
__exportStar(require("./column-type"), exports);
|
|
19
|
+
__exportStar(require("./column-values"), exports);
|
|
20
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/column/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,mDAAiC;AACjC,gDAA8B;AAC9B,kDAAgC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { ConfigurationOptions } from 'ts-appconfig';
|
|
2
|
+
import { DatabaseEnv } from './database-env';
|
|
3
|
+
/**
|
|
4
|
+
* Load the database's .env
|
|
5
|
+
*
|
|
6
|
+
* @param env Environment Type (must extend DatabaseEnv)
|
|
7
|
+
* @param path Path to env file
|
|
8
|
+
* @param name Database name
|
|
9
|
+
* @param options ts-appconfig options
|
|
10
|
+
* @returns Returns the loaded environment variables
|
|
11
|
+
*/
|
|
12
|
+
export declare function configureDb<T extends DatabaseEnv>(env: {
|
|
13
|
+
new (): T;
|
|
14
|
+
}, path: string, name: string, options?: ConfigurationOptions): T;
|