@riao/dbal 3.8.0 → 3.8.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 -3
- package/LICENSE.md +20 -20
- package/README.md +148 -148
- package/builder/builder.d.ts +7 -7
- package/builder/builder.js +19 -19
- package/builder/index.d.ts +2 -2
- package/builder/index.js +7 -7
- package/builder/sql-builder.d.ts +53 -53
- package/builder/sql-builder.js +157 -157
- package/builder/statement-builder.d.ts +12 -12
- package/builder/statement-builder.js +32 -32
- package/column/column-options.d.ts +156 -156
- package/column/column-options.js +2 -2
- package/column/column-type.d.ts +18 -18
- package/column/column-type.js +27 -27
- package/column/column-values.d.ts +34 -34
- package/column/column-values.js +2 -2
- package/column/index.d.ts +3 -3
- package/column/index.js +19 -19
- package/column-pack/date-time/create-timestamp-column.d.ts +2 -2
- package/column-pack/date-time/create-timestamp-column.js +10 -10
- package/column-pack/date-time/index.d.ts +2 -2
- package/column-pack/date-time/index.js +7 -7
- package/column-pack/date-time/update-timestamp-column.d.ts +2 -2
- package/column-pack/date-time/update-timestamp-column.js +14 -14
- package/column-pack/index.d.ts +6 -6
- package/column-pack/index.js +22 -22
- package/column-pack/keys/big-int-key-column.d.ts +2 -2
- package/column-pack/keys/big-int-key-column.js +10 -10
- package/column-pack/keys/index.d.ts +3 -3
- package/column-pack/keys/index.js +9 -9
- package/column-pack/keys/int-key-column.d.ts +2 -2
- package/column-pack/keys/int-key-column.js +10 -10
- package/column-pack/keys/uuid-key-column.d.ts +2 -2
- package/column-pack/keys/uuid-key-column.js +11 -11
- package/column-pack/person/date-of-birth-column.d.ts +2 -2
- package/column-pack/person/date-of-birth-column.js +8 -8
- package/column-pack/person/first-name-column.d.ts +2 -2
- package/column-pack/person/first-name-column.js +9 -9
- package/column-pack/person/index.d.ts +3 -3
- package/column-pack/person/index.js +9 -9
- package/column-pack/person/last-name-column.d.ts +2 -2
- package/column-pack/person/last-name-column.js +9 -9
- package/column-pack/resource/content-column.d.ts +2 -2
- package/column-pack/resource/content-column.js +9 -9
- package/column-pack/resource/description-column.d.ts +2 -2
- package/column-pack/resource/description-column.js +9 -9
- package/column-pack/resource/index.d.ts +6 -6
- package/column-pack/resource/index.js +15 -15
- package/column-pack/resource/is-active-column.d.ts +2 -2
- package/column-pack/resource/is-active-column.js +9 -9
- package/column-pack/resource/name-column.d.ts +2 -2
- package/column-pack/resource/name-column.js +10 -10
- package/column-pack/resource/path-column.d.ts +2 -2
- package/column-pack/resource/path-column.js +11 -11
- package/column-pack/resource/title-column.d.ts +2 -2
- package/column-pack/resource/title-column.js +10 -10
- package/column-pack/system/filename-column.d.ts +2 -2
- package/column-pack/system/filename-column.js +9 -9
- package/column-pack/system/filepath-column.d.ts +2 -2
- package/column-pack/system/filepath-column.js +9 -9
- package/column-pack/system/index.d.ts +4 -4
- package/column-pack/system/index.js +11 -11
- package/column-pack/system/ip-address-column.d.ts +2 -2
- package/column-pack/system/ip-address-column.js +9 -9
- package/column-pack/system/url-column.d.ts +3 -3
- package/column-pack/system/url-column.js +13 -13
- package/column-pack/user/email-column.d.ts +2 -2
- package/column-pack/user/email-column.js +11 -11
- package/column-pack/user/index.d.ts +4 -4
- package/column-pack/user/index.js +11 -11
- package/column-pack/user/password-column.d.ts +2 -2
- package/column-pack/user/password-column.js +10 -10
- package/column-pack/user/temp-email-column.d.ts +2 -2
- package/column-pack/user/temp-email-column.js +5 -5
- package/column-pack/user/username-column.d.ts +2 -2
- package/column-pack/user/username-column.js +11 -11
- package/comparison/comparison-aliases.d.ts +10 -10
- package/comparison/comparison-aliases.js +40 -40
- package/comparison/comparison-token.d.ts +17 -17
- package/comparison/comparison-token.js +15 -15
- package/comparison/comparison.d.ts +13 -13
- package/comparison/comparison.js +71 -71
- package/comparison/index.d.ts +4 -4
- package/comparison/index.js +24 -24
- package/comparison/is-comparison-token.d.ts +2 -2
- package/comparison/is-comparison-token.js +8 -8
- package/config/configure-db.d.ts +14 -14
- package/config/configure-db.js +18 -18
- package/config/database-env.d.ts +16 -16
- package/config/database-env.js +14 -14
- package/config/index.d.ts +2 -2
- package/config/index.js +7 -7
- package/database/connection-options.d.ts +7 -7
- package/database/connection-options.js +2 -2
- package/database/database.d.ts +204 -204
- package/database/database.js +330 -330
- package/database/driver-query.d.ts +10 -10
- package/database/driver-query.js +2 -2
- package/database/driver.d.ts +12 -12
- package/database/driver.js +47 -47
- package/database/get-database-path.d.ts +6 -6
- package/database/get-database-path.js +12 -12
- package/database/index.d.ts +7 -7
- package/database/index.js +26 -26
- package/database/load-database.d.ts +9 -9
- package/database/load-database.js +36 -36
- package/database/transaction.d.ts +8 -8
- package/database/transaction.js +2 -2
- package/ddl/alter-table.d.ts +26 -26
- package/ddl/alter-table.js +2 -2
- package/ddl/create-database.d.ts +3 -3
- package/ddl/create-database.js +2 -2
- package/ddl/create-index.d.ts +5 -5
- package/ddl/create-index.js +2 -2
- package/ddl/create-table.d.ts +8 -8
- package/ddl/create-table.js +2 -2
- package/ddl/create-user.d.ts +4 -4
- package/ddl/create-user.js +2 -2
- package/ddl/ddl-builder.d.ts +96 -96
- package/ddl/ddl-builder.js +565 -565
- package/ddl/ddl-repository.d.ts +135 -135
- package/ddl/ddl-repository.js +228 -228
- package/ddl/drop-database.d.ts +4 -4
- package/ddl/drop-database.js +2 -2
- package/ddl/drop-table.d.ts +4 -4
- package/ddl/drop-table.js +2 -2
- package/ddl/drop-user.d.ts +4 -4
- package/ddl/drop-user.js +2 -2
- package/ddl/foreign-key-constraint.d.ts +19 -19
- package/ddl/foreign-key-constraint.js +2 -2
- package/ddl/grant.d.ts +12 -12
- package/ddl/grant.js +2 -2
- package/ddl/index.d.ts +13 -13
- package/ddl/index.js +23 -23
- package/ddl/truncate.d.ts +3 -3
- package/ddl/truncate.js +2 -2
- package/dml/case-expression.d.ts +16 -16
- package/dml/case-expression.js +11 -11
- package/dml/delete.d.ts +8 -8
- package/dml/delete.js +2 -2
- package/dml/from.d.ts +4 -4
- package/dml/from.js +2 -2
- package/dml/group-by.d.ts +4 -4
- package/dml/group-by.js +2 -2
- package/dml/index.d.ts +11 -11
- package/dml/index.js +30 -30
- package/dml/insert.d.ts +14 -14
- package/dml/insert.js +2 -2
- package/dml/join.d.ts +7 -7
- package/dml/join.js +2 -2
- package/dml/order-by.d.ts +2 -2
- package/dml/order-by.js +2 -2
- package/dml/query-builder.d.ts +106 -106
- package/dml/query-builder.js +797 -793
- package/dml/query-builder.js.map +1 -1
- package/dml/query-repository.d.ts +84 -84
- package/dml/query-repository.js +239 -225
- package/dml/query-repository.js.map +1 -1
- package/dml/select.d.ts +29 -29
- package/dml/select.js +2 -2
- package/dml/set-options.d.ts +5 -5
- package/dml/set-options.js +2 -2
- package/dml/subquery.d.ts +5 -5
- package/dml/subquery.js +10 -10
- package/dml/update.d.ts +11 -11
- package/dml/update.js +2 -2
- package/expression/expression-token.d.ts +11 -11
- package/expression/expression-token.js +12 -12
- package/expression/expression.d.ts +6 -6
- package/expression/expression.js +2 -2
- package/expression/identifier.d.ts +7 -7
- package/expression/identifier.js +15 -15
- package/expression/index.d.ts +7 -7
- package/expression/index.js +25 -25
- package/expression/is-expression-token.d.ts +2 -2
- package/expression/is-expression-token.js +7 -7
- package/expression/key-val-expression.d.ts +7 -7
- package/expression/key-val-expression.js +2 -2
- package/expression/literal.d.ts +3 -3
- package/expression/literal.js +2 -2
- package/expression/logical.d.ts +18 -18
- package/expression/logical.js +30 -30
- package/expression/math.d.ts +17 -17
- package/expression/math.js +36 -36
- package/expression/raw.d.ts +8 -8
- package/expression/raw.js +16 -16
- package/expression/simple-expression.d.ts +8 -8
- package/expression/simple-expression.js +2 -2
- package/functions/function-aliases.d.ts +4 -4
- package/functions/function-aliases.js +12 -12
- package/functions/function-token.d.ts +29 -29
- package/functions/function-token.js +15 -15
- package/functions/functions.d.ts +19 -19
- package/functions/functions.js +94 -94
- package/functions/index.d.ts +4 -4
- package/functions/index.js +22 -22
- package/functions/is-database-function.d.ts +8 -8
- package/functions/is-database-function.js +14 -14
- package/functions/signatures/count.d.ts +14 -11
- package/functions/signatures/count.js +2 -2
- package/index.d.ts +16 -16
- package/index.js +32 -32
- package/migration/index.d.ts +3 -3
- package/migration/index.js +9 -9
- package/migration/migration-package.d.ts +14 -14
- package/migration/migration-package.js +10 -10
- package/migration/migration-record.d.ts +10 -10
- package/migration/migration-record.js +2 -2
- package/migration/migration-runner.d.ts +33 -33
- package/migration/migration-runner.js +180 -180
- package/migration/migration.d.ts +12 -12
- package/migration/migration.js +27 -27
- package/migration/migrations/001-create-migration-table.d.ts +8 -8
- package/migration/migrations/001-create-migration-table.js +54 -54
- package/migration/migrations/002-add-migration-package-column.d.ts +9 -9
- package/migration/migrations/002-add-migration-package-column.js +44 -44
- package/package.json +1 -1
- package/record/index.d.ts +1 -1
- package/record/index.js +17 -17
- package/record/record.d.ts +2 -2
- package/record/record.js +2 -2
- package/repository/index.d.ts +2 -2
- package/repository/index.js +5 -5
- package/repository/repository-options.d.ts +6 -6
- package/repository/repository-options.js +2 -2
- package/repository/repository.d.ts +29 -29
- package/repository/repository.js +68 -68
- package/schema/index.d.ts +3 -3
- package/schema/index.js +5 -5
- package/schema/schema-query-repository.d.ts +54 -54
- package/schema/schema-query-repository.js +159 -159
- package/schema/schema-table.d.ts +9 -9
- package/schema/schema-table.js +2 -2
- package/schema/schema.d.ts +4 -4
- package/schema/schema.js +2 -2
- package/seed/auto-seed.d.ts +9 -9
- package/seed/auto-seed.js +61 -61
- package/seed/create-seed-table.d.ts +8 -8
- package/seed/create-seed-table.js +64 -64
- package/seed/index.d.ts +4 -4
- package/seed/index.js +9 -9
- package/seed/seed-record.d.ts +11 -11
- package/seed/seed-record.js +2 -2
- package/seed/seed-runner.d.ts +20 -20
- package/seed/seed-runner.js +91 -91
- package/seed/seed.d.ts +12 -12
- package/seed/seed.js +27 -27
- package/tokens/index.d.ts +1 -1
- package/tokens/index.js +5 -5
- package/triggers/index.d.ts +2 -2
- package/triggers/index.js +18 -18
- package/triggers/prebuilt/index.d.ts +1 -1
- package/triggers/prebuilt/index.js +5 -5
- package/triggers/prebuilt/update-timestamp-trigger.d.ts +16 -16
- package/triggers/prebuilt/update-timestamp-trigger.js +24 -24
- package/triggers/trigger-options.d.ts +22 -22
- package/triggers/trigger-options.js +2 -2
- package/triggers/trigger.d.ts +19 -19
- package/triggers/trigger.js +23 -23
package/database/database.js
CHANGED
|
@@ -1,331 +1,331 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.Database = void 0;
|
|
13
|
-
const fs_1 = require("fs");
|
|
14
|
-
const path_1 = require("path");
|
|
15
|
-
const config_1 = require("../config");
|
|
16
|
-
const get_database_path_1 = require("./get-database-path");
|
|
17
|
-
const ddl_1 = require("../ddl");
|
|
18
|
-
const dml_1 = require("../dml");
|
|
19
|
-
const schema_query_repository_1 = require("../schema/schema-query-repository");
|
|
20
|
-
/**
|
|
21
|
-
* Represents a single database instance, including a driver,
|
|
22
|
-
* configuration, connection, etc.
|
|
23
|
-
*/
|
|
24
|
-
class Database {
|
|
25
|
-
constructor() {
|
|
26
|
-
/**
|
|
27
|
-
* Reconstructors will be used to re-initialize repositories
|
|
28
|
-
* after the database has been initialized.
|
|
29
|
-
*
|
|
30
|
-
* This allows repositories to be created in global scope
|
|
31
|
-
* before the database is init()'d
|
|
32
|
-
*/
|
|
33
|
-
this.queryRepoInitQueue = [];
|
|
34
|
-
this.ddlRepoInitQueue = [];
|
|
35
|
-
this.schemaRepoInitQueue = [];
|
|
36
|
-
this.isLoaded = false;
|
|
37
|
-
/**
|
|
38
|
-
* Migrations directory, relative to this database
|
|
39
|
-
* e.g. If the migrations are in `database/main/migrations`,
|
|
40
|
-
* set this to `migrations`
|
|
41
|
-
*/
|
|
42
|
-
this.migrations = 'migrations';
|
|
43
|
-
/**
|
|
44
|
-
* Seeds directory, relative to this database
|
|
45
|
-
* e.g. If the seeds are in `database/main/seeds`,
|
|
46
|
-
* set this to `seeds`
|
|
47
|
-
*/
|
|
48
|
-
this.seeds = 'seeds';
|
|
49
|
-
/**
|
|
50
|
-
* Use filesystem schema cache?
|
|
51
|
-
*/
|
|
52
|
-
this.useSchemaCache = true;
|
|
53
|
-
/**
|
|
54
|
-
* Schema storage directory, relative to this database
|
|
55
|
-
* e.g. If the schema is in `database/main/.schema`,
|
|
56
|
-
* set this to `.schema`
|
|
57
|
-
*/
|
|
58
|
-
this.schemaDirectory = '.schema';
|
|
59
|
-
/**
|
|
60
|
-
* Query builder class type used to create new query builders
|
|
61
|
-
*/
|
|
62
|
-
this.queryBuilderType = dml_1.DatabaseQueryBuilder;
|
|
63
|
-
/**
|
|
64
|
-
* Query repository class type used to create new repos
|
|
65
|
-
*/
|
|
66
|
-
this.queryRepositoryType = dml_1.QueryRepository;
|
|
67
|
-
/**
|
|
68
|
-
* DDL builder class type used to create new builders
|
|
69
|
-
*/
|
|
70
|
-
this.ddlBuilderType = ddl_1.DataDefinitionBuilder;
|
|
71
|
-
/**
|
|
72
|
-
* DDL repository class type used to create new repos
|
|
73
|
-
*/
|
|
74
|
-
this.ddlRepositoryType = ddl_1.DataDefinitionRepository;
|
|
75
|
-
/**
|
|
76
|
-
* Schema Query repository class type used to create new repos
|
|
77
|
-
*/
|
|
78
|
-
this.schemaQueryRepositoryType = schema_query_repository_1.SchemaQueryRepository;
|
|
79
|
-
}
|
|
80
|
-
/**
|
|
81
|
-
* Initialize the database
|
|
82
|
-
*
|
|
83
|
-
* @param options Database options
|
|
84
|
-
*/
|
|
85
|
-
init(options) {
|
|
86
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
87
|
-
if (this.isLoaded) {
|
|
88
|
-
return;
|
|
89
|
-
}
|
|
90
|
-
options = options !== null && options !== void 0 ? options : {};
|
|
91
|
-
if (!this.name) {
|
|
92
|
-
throw new Error('Cannot load database without a name');
|
|
93
|
-
}
|
|
94
|
-
if (!this.databasePath) {
|
|
95
|
-
this.databasePath = (0, get_database_path_1.getDatabasePath)();
|
|
96
|
-
}
|
|
97
|
-
if (!this.driver) {
|
|
98
|
-
this.driver = new this.driverType();
|
|
99
|
-
}
|
|
100
|
-
if (options.connectionOptions) {
|
|
101
|
-
this.env = options.connectionOptions;
|
|
102
|
-
}
|
|
103
|
-
else {
|
|
104
|
-
this.configureFromEnv();
|
|
105
|
-
}
|
|
106
|
-
if (options.useSchemaCache !== undefined) {
|
|
107
|
-
this.useSchemaCache = options.useSchemaCache;
|
|
108
|
-
}
|
|
109
|
-
yield this.connect();
|
|
110
|
-
this.isLoaded = true;
|
|
111
|
-
// Load schema query repos
|
|
112
|
-
this.schemaQuery = this.getSchemaQueryRepository();
|
|
113
|
-
for (const repo of this.schemaRepoInitQueue) {
|
|
114
|
-
repo.init({
|
|
115
|
-
database: this.env.database,
|
|
116
|
-
driver: this.driver,
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
|
-
yield this.loadSchema();
|
|
120
|
-
// Load DDL repositories
|
|
121
|
-
this.ddl = this.getDataDefinitionRepository();
|
|
122
|
-
for (const repo of this.ddlRepoInitQueue) {
|
|
123
|
-
repo.init({
|
|
124
|
-
driver: this.driver,
|
|
125
|
-
});
|
|
126
|
-
}
|
|
127
|
-
// Load query repositories
|
|
128
|
-
this.query = this.getQueryRepository();
|
|
129
|
-
for (const repo of this.queryRepoInitQueue) {
|
|
130
|
-
repo.init({
|
|
131
|
-
driver: this.driver,
|
|
132
|
-
schema: this.schema,
|
|
133
|
-
});
|
|
134
|
-
}
|
|
135
|
-
});
|
|
136
|
-
}
|
|
137
|
-
/**
|
|
138
|
-
* Load the configuration .env
|
|
139
|
-
*/
|
|
140
|
-
configureFromEnv() {
|
|
141
|
-
this.env = (0, config_1.configureDb)(this.envType, this.databasePath, this.name);
|
|
142
|
-
}
|
|
143
|
-
/**
|
|
144
|
-
* Connect to the database
|
|
145
|
-
*/
|
|
146
|
-
connect() {
|
|
147
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
148
|
-
if (!this.env) {
|
|
149
|
-
throw new Error(`Cannot connect() database "${this.name}" before init()`);
|
|
150
|
-
}
|
|
151
|
-
yield this.driver.connect(this.env);
|
|
152
|
-
});
|
|
153
|
-
}
|
|
154
|
-
/**
|
|
155
|
-
* Disconnect from the database
|
|
156
|
-
*/
|
|
157
|
-
disconnect() {
|
|
158
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
159
|
-
yield this.driver.disconnect();
|
|
160
|
-
});
|
|
161
|
-
}
|
|
162
|
-
/**
|
|
163
|
-
* Get the full relative path to this database's migrations folder
|
|
164
|
-
*
|
|
165
|
-
* @returns Returns the relative file path
|
|
166
|
-
*/
|
|
167
|
-
getMigrationsDirectory() {
|
|
168
|
-
return (0, path_1.join)(this.databasePath, this.name, this.migrations);
|
|
169
|
-
}
|
|
170
|
-
/**
|
|
171
|
-
* Get the full relative path to this database's seeds folder
|
|
172
|
-
*
|
|
173
|
-
* @returns Returns the relative file path
|
|
174
|
-
*/
|
|
175
|
-
getSeedsDirectory() {
|
|
176
|
-
return (0, path_1.join)(this.databasePath, this.name, this.seeds);
|
|
177
|
-
}
|
|
178
|
-
/**
|
|
179
|
-
* Get the full relative path to this database's schema folder
|
|
180
|
-
*
|
|
181
|
-
* @returns Returns the relative file path
|
|
182
|
-
*/
|
|
183
|
-
getSchemaDirectory() {
|
|
184
|
-
return (0, path_1.join)(this.databasePath, this.name, this.schemaDirectory);
|
|
185
|
-
}
|
|
186
|
-
/**
|
|
187
|
-
* Get a new DDL builder
|
|
188
|
-
*
|
|
189
|
-
* @returns DDL builder
|
|
190
|
-
*/
|
|
191
|
-
getDataDefinitionBuilder() {
|
|
192
|
-
return new this.ddlBuilderType();
|
|
193
|
-
}
|
|
194
|
-
/**
|
|
195
|
-
* Get a new DDL repository
|
|
196
|
-
*
|
|
197
|
-
* @param options Repository options
|
|
198
|
-
* @returns Returns the DDL repository
|
|
199
|
-
*/
|
|
200
|
-
getDataDefinitionRepository() {
|
|
201
|
-
const repo = new this.ddlRepositoryType({
|
|
202
|
-
ddlBuilderType: this.ddlBuilderType,
|
|
203
|
-
});
|
|
204
|
-
if (this.isLoaded) {
|
|
205
|
-
repo.init({ driver: this.driver });
|
|
206
|
-
}
|
|
207
|
-
else {
|
|
208
|
-
this.ddlRepoInitQueue.push(repo);
|
|
209
|
-
}
|
|
210
|
-
return repo;
|
|
211
|
-
}
|
|
212
|
-
/**
|
|
213
|
-
* Get a new query builder
|
|
214
|
-
*
|
|
215
|
-
* @returns Query builder
|
|
216
|
-
*/
|
|
217
|
-
getQueryBuilder() {
|
|
218
|
-
return new this.queryBuilderType();
|
|
219
|
-
}
|
|
220
|
-
/**
|
|
221
|
-
* Get a new query repository
|
|
222
|
-
*
|
|
223
|
-
* @param options Repository options
|
|
224
|
-
* @returns Returns the query repository
|
|
225
|
-
*/
|
|
226
|
-
getQueryRepository(options) {
|
|
227
|
-
const repo = new this.queryRepositoryType(Object.assign(Object.assign({}, (options !== null && options !== void 0 ? options : {})), { queryBuilderType: this.queryBuilderType }));
|
|
228
|
-
if (this.isLoaded) {
|
|
229
|
-
repo.init({
|
|
230
|
-
driver: this.driver,
|
|
231
|
-
schema: this.schema,
|
|
232
|
-
});
|
|
233
|
-
}
|
|
234
|
-
else {
|
|
235
|
-
this.queryRepoInitQueue.push(repo);
|
|
236
|
-
}
|
|
237
|
-
return repo;
|
|
238
|
-
}
|
|
239
|
-
/**
|
|
240
|
-
* Get a new schema query repository
|
|
241
|
-
*
|
|
242
|
-
* @returns Schema Query Repository
|
|
243
|
-
*/
|
|
244
|
-
getSchemaQueryRepository() {
|
|
245
|
-
const repo = new this.schemaQueryRepositoryType({
|
|
246
|
-
queryBuilderType: this.queryBuilderType,
|
|
247
|
-
});
|
|
248
|
-
if (this.isLoaded) {
|
|
249
|
-
repo.init({
|
|
250
|
-
database: this.env.database,
|
|
251
|
-
driver: this.driver,
|
|
252
|
-
});
|
|
253
|
-
}
|
|
254
|
-
else {
|
|
255
|
-
this.schemaRepoInitQueue.push(repo);
|
|
256
|
-
}
|
|
257
|
-
return repo;
|
|
258
|
-
}
|
|
259
|
-
/**
|
|
260
|
-
* Build & save the database schema
|
|
261
|
-
*/
|
|
262
|
-
buildSchema() {
|
|
263
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
264
|
-
this.schema = yield this.schemaQuery.getSchema();
|
|
265
|
-
yield this.saveSchema();
|
|
266
|
-
});
|
|
267
|
-
}
|
|
268
|
-
/**
|
|
269
|
-
* Save the database schema
|
|
270
|
-
*/
|
|
271
|
-
saveSchema() {
|
|
272
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
273
|
-
if (this.useSchemaCache) {
|
|
274
|
-
(0, fs_1.mkdirSync)(this.getSchemaDirectory(), { recursive: true });
|
|
275
|
-
const filepath = (0, path_1.join)(this.getSchemaDirectory(), 'schema.json');
|
|
276
|
-
const data = JSON.stringify(this.schema);
|
|
277
|
-
(0, fs_1.writeFileSync)(filepath, data);
|
|
278
|
-
}
|
|
279
|
-
});
|
|
280
|
-
}
|
|
281
|
-
/**
|
|
282
|
-
* Load the database schema
|
|
283
|
-
*/
|
|
284
|
-
loadSchema() {
|
|
285
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
286
|
-
const filepath = (0, path_1.join)(this.getSchemaDirectory(), 'schema.json');
|
|
287
|
-
if (this.useSchemaCache && (0, fs_1.existsSync)(filepath)) {
|
|
288
|
-
const data = (0, fs_1.readFileSync)(filepath).toString();
|
|
289
|
-
this.schema = JSON.parse(data);
|
|
290
|
-
}
|
|
291
|
-
else {
|
|
292
|
-
yield this.buildSchema();
|
|
293
|
-
}
|
|
294
|
-
});
|
|
295
|
-
}
|
|
296
|
-
/**
|
|
297
|
-
* Get schema metadata
|
|
298
|
-
*
|
|
299
|
-
* @returns Schema
|
|
300
|
-
*/
|
|
301
|
-
getSchema() {
|
|
302
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
303
|
-
if (!this.schema) {
|
|
304
|
-
yield this.loadSchema();
|
|
305
|
-
}
|
|
306
|
-
return this.schema;
|
|
307
|
-
});
|
|
308
|
-
}
|
|
309
|
-
/**
|
|
310
|
-
* Run a transaction
|
|
311
|
-
*
|
|
312
|
-
* @param fn Transaction callback
|
|
313
|
-
* @returns Passes the return from the transaction callback
|
|
314
|
-
*/
|
|
315
|
-
transaction(fn) {
|
|
316
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
317
|
-
const connectionDriver = new this.driverType();
|
|
318
|
-
const ddl = this.getDataDefinitionRepository();
|
|
319
|
-
ddl.init({ driver: connectionDriver });
|
|
320
|
-
const query = this.getQueryRepository();
|
|
321
|
-
query.init({ driver: connectionDriver, schema: this.schema });
|
|
322
|
-
return yield this.driver.transaction(fn, {
|
|
323
|
-
driver: connectionDriver,
|
|
324
|
-
ddl,
|
|
325
|
-
query,
|
|
326
|
-
});
|
|
327
|
-
});
|
|
328
|
-
}
|
|
329
|
-
}
|
|
330
|
-
exports.Database = Database;
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.Database = void 0;
|
|
13
|
+
const fs_1 = require("fs");
|
|
14
|
+
const path_1 = require("path");
|
|
15
|
+
const config_1 = require("../config");
|
|
16
|
+
const get_database_path_1 = require("./get-database-path");
|
|
17
|
+
const ddl_1 = require("../ddl");
|
|
18
|
+
const dml_1 = require("../dml");
|
|
19
|
+
const schema_query_repository_1 = require("../schema/schema-query-repository");
|
|
20
|
+
/**
|
|
21
|
+
* Represents a single database instance, including a driver,
|
|
22
|
+
* configuration, connection, etc.
|
|
23
|
+
*/
|
|
24
|
+
class Database {
|
|
25
|
+
constructor() {
|
|
26
|
+
/**
|
|
27
|
+
* Reconstructors will be used to re-initialize repositories
|
|
28
|
+
* after the database has been initialized.
|
|
29
|
+
*
|
|
30
|
+
* This allows repositories to be created in global scope
|
|
31
|
+
* before the database is init()'d
|
|
32
|
+
*/
|
|
33
|
+
this.queryRepoInitQueue = [];
|
|
34
|
+
this.ddlRepoInitQueue = [];
|
|
35
|
+
this.schemaRepoInitQueue = [];
|
|
36
|
+
this.isLoaded = false;
|
|
37
|
+
/**
|
|
38
|
+
* Migrations directory, relative to this database
|
|
39
|
+
* e.g. If the migrations are in `database/main/migrations`,
|
|
40
|
+
* set this to `migrations`
|
|
41
|
+
*/
|
|
42
|
+
this.migrations = 'migrations';
|
|
43
|
+
/**
|
|
44
|
+
* Seeds directory, relative to this database
|
|
45
|
+
* e.g. If the seeds are in `database/main/seeds`,
|
|
46
|
+
* set this to `seeds`
|
|
47
|
+
*/
|
|
48
|
+
this.seeds = 'seeds';
|
|
49
|
+
/**
|
|
50
|
+
* Use filesystem schema cache?
|
|
51
|
+
*/
|
|
52
|
+
this.useSchemaCache = true;
|
|
53
|
+
/**
|
|
54
|
+
* Schema storage directory, relative to this database
|
|
55
|
+
* e.g. If the schema is in `database/main/.schema`,
|
|
56
|
+
* set this to `.schema`
|
|
57
|
+
*/
|
|
58
|
+
this.schemaDirectory = '.schema';
|
|
59
|
+
/**
|
|
60
|
+
* Query builder class type used to create new query builders
|
|
61
|
+
*/
|
|
62
|
+
this.queryBuilderType = dml_1.DatabaseQueryBuilder;
|
|
63
|
+
/**
|
|
64
|
+
* Query repository class type used to create new repos
|
|
65
|
+
*/
|
|
66
|
+
this.queryRepositoryType = dml_1.QueryRepository;
|
|
67
|
+
/**
|
|
68
|
+
* DDL builder class type used to create new builders
|
|
69
|
+
*/
|
|
70
|
+
this.ddlBuilderType = ddl_1.DataDefinitionBuilder;
|
|
71
|
+
/**
|
|
72
|
+
* DDL repository class type used to create new repos
|
|
73
|
+
*/
|
|
74
|
+
this.ddlRepositoryType = ddl_1.DataDefinitionRepository;
|
|
75
|
+
/**
|
|
76
|
+
* Schema Query repository class type used to create new repos
|
|
77
|
+
*/
|
|
78
|
+
this.schemaQueryRepositoryType = schema_query_repository_1.SchemaQueryRepository;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Initialize the database
|
|
82
|
+
*
|
|
83
|
+
* @param options Database options
|
|
84
|
+
*/
|
|
85
|
+
init(options) {
|
|
86
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
87
|
+
if (this.isLoaded) {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
options = options !== null && options !== void 0 ? options : {};
|
|
91
|
+
if (!this.name) {
|
|
92
|
+
throw new Error('Cannot load database without a name');
|
|
93
|
+
}
|
|
94
|
+
if (!this.databasePath) {
|
|
95
|
+
this.databasePath = (0, get_database_path_1.getDatabasePath)();
|
|
96
|
+
}
|
|
97
|
+
if (!this.driver) {
|
|
98
|
+
this.driver = new this.driverType();
|
|
99
|
+
}
|
|
100
|
+
if (options.connectionOptions) {
|
|
101
|
+
this.env = options.connectionOptions;
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
this.configureFromEnv();
|
|
105
|
+
}
|
|
106
|
+
if (options.useSchemaCache !== undefined) {
|
|
107
|
+
this.useSchemaCache = options.useSchemaCache;
|
|
108
|
+
}
|
|
109
|
+
yield this.connect();
|
|
110
|
+
this.isLoaded = true;
|
|
111
|
+
// Load schema query repos
|
|
112
|
+
this.schemaQuery = this.getSchemaQueryRepository();
|
|
113
|
+
for (const repo of this.schemaRepoInitQueue) {
|
|
114
|
+
repo.init({
|
|
115
|
+
database: this.env.database,
|
|
116
|
+
driver: this.driver,
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
yield this.loadSchema();
|
|
120
|
+
// Load DDL repositories
|
|
121
|
+
this.ddl = this.getDataDefinitionRepository();
|
|
122
|
+
for (const repo of this.ddlRepoInitQueue) {
|
|
123
|
+
repo.init({
|
|
124
|
+
driver: this.driver,
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
// Load query repositories
|
|
128
|
+
this.query = this.getQueryRepository();
|
|
129
|
+
for (const repo of this.queryRepoInitQueue) {
|
|
130
|
+
repo.init({
|
|
131
|
+
driver: this.driver,
|
|
132
|
+
schema: this.schema,
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Load the configuration .env
|
|
139
|
+
*/
|
|
140
|
+
configureFromEnv() {
|
|
141
|
+
this.env = (0, config_1.configureDb)(this.envType, this.databasePath, this.name);
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Connect to the database
|
|
145
|
+
*/
|
|
146
|
+
connect() {
|
|
147
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
148
|
+
if (!this.env) {
|
|
149
|
+
throw new Error(`Cannot connect() database "${this.name}" before init()`);
|
|
150
|
+
}
|
|
151
|
+
yield this.driver.connect(this.env);
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Disconnect from the database
|
|
156
|
+
*/
|
|
157
|
+
disconnect() {
|
|
158
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
159
|
+
yield this.driver.disconnect();
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Get the full relative path to this database's migrations folder
|
|
164
|
+
*
|
|
165
|
+
* @returns Returns the relative file path
|
|
166
|
+
*/
|
|
167
|
+
getMigrationsDirectory() {
|
|
168
|
+
return (0, path_1.join)(this.databasePath, this.name, this.migrations);
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Get the full relative path to this database's seeds folder
|
|
172
|
+
*
|
|
173
|
+
* @returns Returns the relative file path
|
|
174
|
+
*/
|
|
175
|
+
getSeedsDirectory() {
|
|
176
|
+
return (0, path_1.join)(this.databasePath, this.name, this.seeds);
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Get the full relative path to this database's schema folder
|
|
180
|
+
*
|
|
181
|
+
* @returns Returns the relative file path
|
|
182
|
+
*/
|
|
183
|
+
getSchemaDirectory() {
|
|
184
|
+
return (0, path_1.join)(this.databasePath, this.name, this.schemaDirectory);
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Get a new DDL builder
|
|
188
|
+
*
|
|
189
|
+
* @returns DDL builder
|
|
190
|
+
*/
|
|
191
|
+
getDataDefinitionBuilder() {
|
|
192
|
+
return new this.ddlBuilderType();
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Get a new DDL repository
|
|
196
|
+
*
|
|
197
|
+
* @param options Repository options
|
|
198
|
+
* @returns Returns the DDL repository
|
|
199
|
+
*/
|
|
200
|
+
getDataDefinitionRepository() {
|
|
201
|
+
const repo = new this.ddlRepositoryType({
|
|
202
|
+
ddlBuilderType: this.ddlBuilderType,
|
|
203
|
+
});
|
|
204
|
+
if (this.isLoaded) {
|
|
205
|
+
repo.init({ driver: this.driver });
|
|
206
|
+
}
|
|
207
|
+
else {
|
|
208
|
+
this.ddlRepoInitQueue.push(repo);
|
|
209
|
+
}
|
|
210
|
+
return repo;
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Get a new query builder
|
|
214
|
+
*
|
|
215
|
+
* @returns Query builder
|
|
216
|
+
*/
|
|
217
|
+
getQueryBuilder() {
|
|
218
|
+
return new this.queryBuilderType();
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Get a new query repository
|
|
222
|
+
*
|
|
223
|
+
* @param options Repository options
|
|
224
|
+
* @returns Returns the query repository
|
|
225
|
+
*/
|
|
226
|
+
getQueryRepository(options) {
|
|
227
|
+
const repo = new this.queryRepositoryType(Object.assign(Object.assign({}, (options !== null && options !== void 0 ? options : {})), { queryBuilderType: this.queryBuilderType }));
|
|
228
|
+
if (this.isLoaded) {
|
|
229
|
+
repo.init({
|
|
230
|
+
driver: this.driver,
|
|
231
|
+
schema: this.schema,
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
else {
|
|
235
|
+
this.queryRepoInitQueue.push(repo);
|
|
236
|
+
}
|
|
237
|
+
return repo;
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Get a new schema query repository
|
|
241
|
+
*
|
|
242
|
+
* @returns Schema Query Repository
|
|
243
|
+
*/
|
|
244
|
+
getSchemaQueryRepository() {
|
|
245
|
+
const repo = new this.schemaQueryRepositoryType({
|
|
246
|
+
queryBuilderType: this.queryBuilderType,
|
|
247
|
+
});
|
|
248
|
+
if (this.isLoaded) {
|
|
249
|
+
repo.init({
|
|
250
|
+
database: this.env.database,
|
|
251
|
+
driver: this.driver,
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
else {
|
|
255
|
+
this.schemaRepoInitQueue.push(repo);
|
|
256
|
+
}
|
|
257
|
+
return repo;
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Build & save the database schema
|
|
261
|
+
*/
|
|
262
|
+
buildSchema() {
|
|
263
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
264
|
+
this.schema = yield this.schemaQuery.getSchema();
|
|
265
|
+
yield this.saveSchema();
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* Save the database schema
|
|
270
|
+
*/
|
|
271
|
+
saveSchema() {
|
|
272
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
273
|
+
if (this.useSchemaCache) {
|
|
274
|
+
(0, fs_1.mkdirSync)(this.getSchemaDirectory(), { recursive: true });
|
|
275
|
+
const filepath = (0, path_1.join)(this.getSchemaDirectory(), 'schema.json');
|
|
276
|
+
const data = JSON.stringify(this.schema);
|
|
277
|
+
(0, fs_1.writeFileSync)(filepath, data);
|
|
278
|
+
}
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Load the database schema
|
|
283
|
+
*/
|
|
284
|
+
loadSchema() {
|
|
285
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
286
|
+
const filepath = (0, path_1.join)(this.getSchemaDirectory(), 'schema.json');
|
|
287
|
+
if (this.useSchemaCache && (0, fs_1.existsSync)(filepath)) {
|
|
288
|
+
const data = (0, fs_1.readFileSync)(filepath).toString();
|
|
289
|
+
this.schema = JSON.parse(data);
|
|
290
|
+
}
|
|
291
|
+
else {
|
|
292
|
+
yield this.buildSchema();
|
|
293
|
+
}
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
297
|
+
* Get schema metadata
|
|
298
|
+
*
|
|
299
|
+
* @returns Schema
|
|
300
|
+
*/
|
|
301
|
+
getSchema() {
|
|
302
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
303
|
+
if (!this.schema) {
|
|
304
|
+
yield this.loadSchema();
|
|
305
|
+
}
|
|
306
|
+
return this.schema;
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* Run a transaction
|
|
311
|
+
*
|
|
312
|
+
* @param fn Transaction callback
|
|
313
|
+
* @returns Passes the return from the transaction callback
|
|
314
|
+
*/
|
|
315
|
+
transaction(fn) {
|
|
316
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
317
|
+
const connectionDriver = new this.driverType();
|
|
318
|
+
const ddl = this.getDataDefinitionRepository();
|
|
319
|
+
ddl.init({ driver: connectionDriver });
|
|
320
|
+
const query = this.getQueryRepository();
|
|
321
|
+
query.init({ driver: connectionDriver, schema: this.schema });
|
|
322
|
+
return yield this.driver.transaction(fn, {
|
|
323
|
+
driver: connectionDriver,
|
|
324
|
+
ddl,
|
|
325
|
+
query,
|
|
326
|
+
});
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
exports.Database = Database;
|
|
331
331
|
//# sourceMappingURL=database.js.map
|