tspace-mysql 1.0.5 → 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -15
- package/dist/cli/index.js +1 -3
- package/dist/cli/migrate/make.js +4 -17
- package/dist/cli/tables/make.d.ts +1 -1
- package/dist/cli/tables/make.js +0 -5
- package/dist/lib/config/env.js +17 -6
- package/dist/lib/tspace/DB.js +1 -0
- package/dist/lib/tspace/Database.d.ts +1 -1
- package/dist/lib/tspace/Model.js +17 -17
- package/package.json +5 -1
package/README.md
CHANGED
|
@@ -131,12 +131,14 @@ import Post from '../Post'
|
|
|
131
131
|
import Comment from '../Comment'
|
|
132
132
|
import User from '../User'
|
|
133
133
|
|
|
134
|
+
/**
|
|
134
135
|
Folder directory example
|
|
135
136
|
- App
|
|
136
137
|
- Model
|
|
137
138
|
Post.ts
|
|
138
139
|
User.ts
|
|
139
140
|
Comment.ts
|
|
141
|
+
*/
|
|
140
142
|
|
|
141
143
|
(async () => {
|
|
142
144
|
const users = await new User()
|
|
@@ -147,9 +149,11 @@ Folder directory example
|
|
|
147
149
|
|
|
148
150
|
console.log(users)
|
|
149
151
|
})()
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
*
|
|
155
|
+
* User.ts
|
|
156
|
+
*/
|
|
153
157
|
import { Model } from 'tspace-mysql'
|
|
154
158
|
class User extends Model {
|
|
155
159
|
constructor(){
|
|
@@ -159,10 +163,10 @@ class User extends Model {
|
|
|
159
163
|
}
|
|
160
164
|
}
|
|
161
165
|
export default User
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
*Post.ts
|
|
165
|
-
|
|
166
|
+
/**
|
|
167
|
+
*
|
|
168
|
+
* Post.ts
|
|
169
|
+
*/
|
|
166
170
|
import { Model } from 'tspace-mysql'
|
|
167
171
|
|
|
168
172
|
class Post extends Model {
|
|
@@ -173,10 +177,12 @@ class Post extends Model {
|
|
|
173
177
|
}
|
|
174
178
|
}
|
|
175
179
|
export default Post
|
|
176
|
-
```
|
|
177
180
|
|
|
178
|
-
|
|
179
|
-
|
|
181
|
+
/**
|
|
182
|
+
*
|
|
183
|
+
* Comment.ts
|
|
184
|
+
*/
|
|
185
|
+
|
|
180
186
|
import { Model } from 'tspace-mysql'
|
|
181
187
|
|
|
182
188
|
class Comment extends Model {
|
|
@@ -209,7 +215,7 @@ select(column1 ,column2 ,...N)
|
|
|
209
215
|
except(column1 ,column2 ,...N)
|
|
210
216
|
only(column1 ,column2 ,...N)
|
|
211
217
|
hidden(column1 ,column2 ,...N)
|
|
212
|
-
join
|
|
218
|
+
join(primary key , table.foreign key)
|
|
213
219
|
rightJoin (primary key , table.foreign key)
|
|
214
220
|
leftJoin (primary key , table.foreign key)
|
|
215
221
|
limit (limit)
|
|
@@ -219,13 +225,10 @@ latest (column)
|
|
|
219
225
|
oldest (column)
|
|
220
226
|
groupBy (column)
|
|
221
227
|
|
|
222
|
-
insert(objects)
|
|
223
228
|
create(objects)
|
|
224
229
|
createMultiple(array objects)
|
|
225
230
|
update (objects)
|
|
226
|
-
insertNotExists(objects)
|
|
227
231
|
createNotExists(objects)
|
|
228
|
-
updateOrInsert (objects)
|
|
229
232
|
updateOrCreate (objects)
|
|
230
233
|
/**
|
|
231
234
|
* relationship
|
|
@@ -336,7 +339,7 @@ import { Schema , Blueprint , DB } from 'tspace-mysql'
|
|
|
336
339
|
*/
|
|
337
340
|
})()
|
|
338
341
|
```
|
|
339
|
-
tspace-mysql migrate
|
|
342
|
+
tspace-mysql migrate --dir=App/Models/Migrations
|
|
340
343
|
/* migrate all table in folder into database */
|
|
341
344
|
```js
|
|
342
345
|
* Blueprint method
|
package/dist/cli/index.js
CHANGED
|
@@ -19,8 +19,6 @@ try {
|
|
|
19
19
|
var name = (_c = (_b = (_a = process.argv.slice(2)) === null || _a === void 0 ? void 0 : _a.find(function (data) { return data === null || data === void 0 ? void 0 : data.includes('--name='); })) === null || _b === void 0 ? void 0 : _b.replace('--name=', '')) !== null && _c !== void 0 ? _c : null;
|
|
20
20
|
var migrate = (_e = (_d = process.argv.slice(2)) === null || _d === void 0 ? void 0 : _d.includes('--m')) !== null && _e !== void 0 ? _e : false;
|
|
21
21
|
var dir = (_h = (_g = (_f = process.argv.slice(2)) === null || _f === void 0 ? void 0 : _f.find(function (data) { return data === null || data === void 0 ? void 0 : data.includes('--dir='); })) === null || _g === void 0 ? void 0 : _g.replace('--dir=', '/')) !== null && _h !== void 0 ? _h : null;
|
|
22
|
-
// const migrateFolder = process.argv.slice(2)?.find(data => data?.includes('--f='))?.replace('--f=','/') ?? null
|
|
23
|
-
// const type = process.argv.slice(2)?.includes('--js') ?? false ? '.js' : '.ts'
|
|
24
22
|
var type = (_l = (_k = (_j = process.argv.slice(2)) === null || _j === void 0 ? void 0 : _j.find(function (data) { return data === null || data === void 0 ? void 0 : data.includes('--type='); })) === null || _k === void 0 ? void 0 : _k.replace('--type=', '.')) !== null && _l !== void 0 ? _l : '.ts';
|
|
25
23
|
type = ['.js', '.ts'].includes(type) ? type : '.ts';
|
|
26
24
|
var file = (_o = (_m = process.argv.slice(3)) === null || _m === void 0 ? void 0 : _m.shift()) !== null && _o !== void 0 ? _o : '';
|
|
@@ -37,5 +35,5 @@ try {
|
|
|
37
35
|
commands[process.argv[2]](cmd);
|
|
38
36
|
}
|
|
39
37
|
catch (err) {
|
|
40
|
-
console.log("
|
|
38
|
+
console.log("readme https://www.npmjs.com/package/tspace-mysql");
|
|
41
39
|
}
|
package/dist/cli/migrate/make.js
CHANGED
|
@@ -15,25 +15,15 @@ var child_process_1 = require("child_process");
|
|
|
15
15
|
exports.default = (function (_ref) {
|
|
16
16
|
var e_1, _a;
|
|
17
17
|
var _b, _c, _d;
|
|
18
|
-
var
|
|
19
|
-
var folder = file;
|
|
18
|
+
var type = _ref.type, dir = _ref.dir, cwd = _ref.cwd, fs = _ref.fs;
|
|
20
19
|
try {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
}
|
|
25
|
-
catch (e) {
|
|
26
|
-
fs.mkdirSync(cwd + "/".concat(folder), {
|
|
27
|
-
recursive: true
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
try {
|
|
31
|
-
var path = dir ? "".concat(cwd, "/").concat(dir, "/").concat(folder) : "".concat(cwd, "/").concat(folder);
|
|
20
|
+
if (dir == null)
|
|
21
|
+
throw new Error('Not found directory');
|
|
22
|
+
var path = "".concat(cwd, "/").concat(dir);
|
|
32
23
|
var files = (_b = fs.readdirSync(path)) !== null && _b !== void 0 ? _b : [];
|
|
33
24
|
if (!(files === null || files === void 0 ? void 0 : files.length))
|
|
34
25
|
console.log('this folder is empty');
|
|
35
26
|
var cmd = type === '.js' ? 'node' : 'ts-node';
|
|
36
|
-
var sum = 0;
|
|
37
27
|
try {
|
|
38
28
|
for (var files_1 = __values(files), files_1_1 = files_1.next(); !files_1_1.done; files_1_1 = files_1.next()) {
|
|
39
29
|
var _file = files_1_1.value;
|
|
@@ -46,7 +36,6 @@ exports.default = (function (_ref) {
|
|
|
46
36
|
if (err)
|
|
47
37
|
console.error(err);
|
|
48
38
|
});
|
|
49
|
-
sum++;
|
|
50
39
|
}
|
|
51
40
|
}
|
|
52
41
|
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
@@ -56,8 +45,6 @@ exports.default = (function (_ref) {
|
|
|
56
45
|
}
|
|
57
46
|
finally { if (e_1) throw e_1.error; }
|
|
58
47
|
}
|
|
59
|
-
if (sum === file.length)
|
|
60
|
-
process.exit(0);
|
|
61
48
|
}
|
|
62
49
|
catch (err) {
|
|
63
50
|
console.log(err.message);
|
package/dist/cli/tables/make.js
CHANGED
|
@@ -6,10 +6,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
var table_1 = __importDefault(require("./table"));
|
|
7
7
|
exports.default = (function (_ref) {
|
|
8
8
|
var file = _ref.file, type = _ref.type, cwd = _ref.cwd, dir = _ref.dir, fs = _ref.fs, npm = _ref.npm;
|
|
9
|
-
// if(name == null) {
|
|
10
|
-
// console.log(`use ${npm} make:migration <NAME TABLE> --dir=<DIRECTORY>`)
|
|
11
|
-
// process.exit(0);
|
|
12
|
-
// }
|
|
13
9
|
try {
|
|
14
10
|
fs.accessSync(cwd + "/".concat(file), fs.F_OK, {
|
|
15
11
|
recursive: true
|
|
@@ -27,5 +23,4 @@ exports.default = (function (_ref) {
|
|
|
27
23
|
console.log(err.message);
|
|
28
24
|
});
|
|
29
25
|
console.log("Migration : ".concat(file, " created successfully"));
|
|
30
|
-
process.exit(0);
|
|
31
26
|
});
|
package/dist/lib/config/env.js
CHANGED
|
@@ -2,15 +2,26 @@
|
|
|
2
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
|
-
var _a;
|
|
6
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
6
|
var dotenv_1 = __importDefault(require("dotenv"));
|
|
8
7
|
var path_1 = __importDefault(require("path"));
|
|
9
|
-
var
|
|
10
|
-
var
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
8
|
+
var fs_1 = __importDefault(require("fs"));
|
|
9
|
+
var environment = function () {
|
|
10
|
+
var _a;
|
|
11
|
+
var NODE_ENV = (_a = process.env) === null || _a === void 0 ? void 0 : _a.NODE_ENV;
|
|
12
|
+
var dotenvWithoutEnviroment = path_1.default.join(path_1.default.resolve(), ".env");
|
|
13
|
+
var dotenWithEnviroment = path_1.default.join(path_1.default.resolve(), ".env.".concat(NODE_ENV));
|
|
14
|
+
if (NODE_ENV == null) {
|
|
15
|
+
if (fs_1.default.existsSync(dotenvWithoutEnviroment))
|
|
16
|
+
return dotenvWithoutEnviroment;
|
|
17
|
+
if (fs_1.default.existsSync(dotenWithEnviroment))
|
|
18
|
+
return dotenWithEnviroment;
|
|
19
|
+
}
|
|
20
|
+
if (fs_1.default.existsSync(dotenWithEnviroment))
|
|
21
|
+
return dotenWithEnviroment;
|
|
22
|
+
return dotenvWithoutEnviroment;
|
|
23
|
+
};
|
|
24
|
+
dotenv_1.default.config({ path: environment() });
|
|
14
25
|
var env = Object.freeze({
|
|
15
26
|
DB_HOST: process.env.DB_HOST,
|
|
16
27
|
DB_PORT: process.env.DB_PORT,
|
package/dist/lib/tspace/DB.js
CHANGED
|
@@ -5,7 +5,7 @@ declare class Database extends AbstractDatabase {
|
|
|
5
5
|
distinct(column?: string): this;
|
|
6
6
|
select(...params: string[]): this;
|
|
7
7
|
chunk(chunk: number): this;
|
|
8
|
-
when(value: string | number | undefined | null, cb: Function): this;
|
|
8
|
+
when(value: string | number | undefined | null | Boolean, cb: Function): this;
|
|
9
9
|
whereRaw(query: string): this;
|
|
10
10
|
where(column: string, operator?: any, value?: any): this;
|
|
11
11
|
whereId(id: number): this;
|
package/dist/lib/tspace/Model.js
CHANGED
|
@@ -1171,19 +1171,18 @@ var Model = /** @class */ (function (_super) {
|
|
|
1171
1171
|
}
|
|
1172
1172
|
return dataParents;
|
|
1173
1173
|
};
|
|
1174
|
-
Model.prototype._relation = function (
|
|
1174
|
+
Model.prototype._relation = function (parents, relation) {
|
|
1175
1175
|
return __awaiter(this, void 0, void 0, function () {
|
|
1176
|
-
var _a, pk, fk, pkId, dataPerentId, query,
|
|
1176
|
+
var _a, pk, fk, pkId, dataPerentId, query, childs;
|
|
1177
1177
|
return __generator(this, function (_b) {
|
|
1178
1178
|
switch (_b.label) {
|
|
1179
1179
|
case 0:
|
|
1180
1180
|
if (!Object.keys(relation).length)
|
|
1181
1181
|
return [2 /*return*/, []];
|
|
1182
1182
|
_a = this._valueInRelation(relation), pk = _a.pk, fk = _a.fk;
|
|
1183
|
-
pkId =
|
|
1183
|
+
pkId = parents.map(function (parent) { return parent[pk]; }).filter(function (data) { return data != null; });
|
|
1184
1184
|
dataPerentId = Array.from(new Set(pkId)) || [];
|
|
1185
|
-
if
|
|
1186
|
-
throw new Error("can't relationship without primary or foreign key");
|
|
1185
|
+
// if(!dataPerentId.length && !this.$db.get('WITH_EXISTS')) throw new Error("can't relationship without primary or foreign key")
|
|
1187
1186
|
if (!dataPerentId.length && this.$db.get('WITH_EXISTS'))
|
|
1188
1187
|
return [2 /*return*/, []];
|
|
1189
1188
|
return [4 /*yield*/, relation.query];
|
|
@@ -1193,8 +1192,8 @@ var Model = /** @class */ (function (_super) {
|
|
|
1193
1192
|
throw new Error("unknow callback queries in [relation : ".concat(relation.name, "]"));
|
|
1194
1193
|
return [4 /*yield*/, query.whereIn(fk, dataPerentId).debug(this.$db.get('DEBUG')).get()];
|
|
1195
1194
|
case 2:
|
|
1196
|
-
|
|
1197
|
-
return [2 /*return*/,
|
|
1195
|
+
childs = _b.sent();
|
|
1196
|
+
return [2 /*return*/, childs];
|
|
1198
1197
|
}
|
|
1199
1198
|
});
|
|
1200
1199
|
});
|
|
@@ -1226,7 +1225,7 @@ var Model = /** @class */ (function (_super) {
|
|
|
1226
1225
|
dataChilds_1 = _e.sent();
|
|
1227
1226
|
otherId = dataChilds_1.map(function (sub) { return sub[otherFk_1]; }).filter(function (data) { return data != null; });
|
|
1228
1227
|
otherArrId = Array.from(new Set(otherId)) || [];
|
|
1229
|
-
return [4 /*yield*/, modelOther.whereIn(otherPk_1, otherArrId).
|
|
1228
|
+
return [4 /*yield*/, this._queryStatementModel(modelOther.whereIn(otherPk_1, otherArrId).toSQL())];
|
|
1230
1229
|
case 2:
|
|
1231
1230
|
otherdataChilds_1 = _e.sent();
|
|
1232
1231
|
dataChilds_1.forEach(function (sub) {
|
|
@@ -1313,14 +1312,14 @@ var Model = /** @class */ (function (_super) {
|
|
|
1313
1312
|
return __generator(this, function (_b) {
|
|
1314
1313
|
switch (_b.label) {
|
|
1315
1314
|
case 0:
|
|
1316
|
-
currentPage = this.$db.get('PAGE');
|
|
1315
|
+
currentPage = +(this.$db.get('PAGE'));
|
|
1317
1316
|
this.select("".concat(this.$utils().constants('COUNT'), "(*) ").concat(this.$utils().constants('AS'), " total"));
|
|
1318
1317
|
sql = this._getSQLModel();
|
|
1319
1318
|
return [4 /*yield*/, this._queryStatementModel(sql)];
|
|
1320
1319
|
case 1:
|
|
1321
1320
|
res = _b.sent();
|
|
1322
1321
|
total = (res === null || res === void 0 ? void 0 : res.shift().total) || 0;
|
|
1323
|
-
limit = this.$db.get('PER_PAGE');
|
|
1322
|
+
limit = +(this.$db.get('PER_PAGE'));
|
|
1324
1323
|
lastPage = Math.ceil(total / limit) || 0;
|
|
1325
1324
|
lastPage = lastPage > 1 ? lastPage : 1;
|
|
1326
1325
|
nextPage = currentPage + 1;
|
|
@@ -1897,7 +1896,7 @@ var Model = /** @class */ (function (_super) {
|
|
|
1897
1896
|
var _a;
|
|
1898
1897
|
if (transaction === void 0) { transaction = { query: [{ table: '', id: '' }] }; }
|
|
1899
1898
|
return __awaiter(this, void 0, void 0, function () {
|
|
1900
|
-
var attributes,
|
|
1899
|
+
var attributes, query, query, _b;
|
|
1901
1900
|
return __generator(this, function (_c) {
|
|
1902
1901
|
switch (_c.label) {
|
|
1903
1902
|
case 0:
|
|
@@ -1905,14 +1904,15 @@ var Model = /** @class */ (function (_super) {
|
|
|
1905
1904
|
attributes = this.$attributes;
|
|
1906
1905
|
if ((_a = Object.keys(attributes)) === null || _a === void 0 ? void 0 : _a.length) {
|
|
1907
1906
|
if (this.$db.get('WHERE')) {
|
|
1908
|
-
|
|
1909
|
-
this.$db.set('UPDATE', "".concat(this.$utils().constants('UPDATE'), " ").concat(this.$db.get('TABLE_NAME'), " ").concat(
|
|
1907
|
+
query = this._queryUpdateModel(attributes);
|
|
1908
|
+
this.$db.set('UPDATE', "".concat(this.$utils().constants('UPDATE'), " ").concat(this.$db.get('TABLE_NAME'), " ").concat(query));
|
|
1910
1909
|
this.$db.set('SAVE', 'UPDATE');
|
|
1911
|
-
return [2 /*return*/];
|
|
1912
1910
|
}
|
|
1913
|
-
|
|
1914
|
-
|
|
1915
|
-
|
|
1911
|
+
else {
|
|
1912
|
+
query = this._queryInsertModel(attributes);
|
|
1913
|
+
this.$db.set('INSERT', "".concat(this.$utils().constants('INSERT'), " ").concat(this.$db.get('TABLE_NAME'), " ").concat(query));
|
|
1914
|
+
this.$db.set('SAVE', 'INSERT');
|
|
1915
|
+
}
|
|
1916
1916
|
}
|
|
1917
1917
|
_b = this.$db.get('SAVE');
|
|
1918
1918
|
switch (_b) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tspace-mysql",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.6",
|
|
4
4
|
"description": "mysql query builder object relational mapping",
|
|
5
5
|
"main": "dist/lib/index.js",
|
|
6
6
|
"types": "dist/lib/index.d.ts",
|
|
@@ -38,6 +38,10 @@
|
|
|
38
38
|
},
|
|
39
39
|
"homepage": "https://github.com/thanathip41",
|
|
40
40
|
"scripts": {
|
|
41
|
+
"start" : "ts-node ./test/index.ts",
|
|
42
|
+
"prod" : "set NODE_ENV=production&& ts-node ./test/index.ts",
|
|
43
|
+
"dev" : "set NODE_ENV=development&& ts-node ./test/index.ts",
|
|
44
|
+
"test" : "set NODE_ENV=test&& ts-node ./test/index.ts",
|
|
41
45
|
"build": "tsc",
|
|
42
46
|
"prepare": "npm run build"
|
|
43
47
|
},
|