@rws-framework/db 2.4.6 → 3.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/.bin/add-v.sh +9 -9
- package/.bin/emerge.sh +10 -10
- package/.eslintrc.json +53 -53
- package/README.md +343 -405
- package/dist/decorators/IdType.d.ts +8 -0
- package/dist/decorators/IdType.js +10 -0
- package/dist/decorators/InverseRelation.d.ts +2 -2
- package/dist/decorators/InverseRelation.js +5 -1
- package/dist/decorators/InverseTimeSeries.d.ts +0 -0
- package/dist/decorators/InverseTimeSeries.js +0 -0
- package/dist/decorators/RWSCollection.d.ts +7 -0
- package/dist/decorators/RWSCollection.js +6 -0
- package/dist/decorators/Relation.d.ts +8 -6
- package/dist/decorators/Relation.js +8 -1
- package/dist/decorators/TrackType.d.ts +2 -14
- package/dist/decorators/TrackType.js +7 -0
- package/dist/decorators/TypeFunctions.d.ts +44 -0
- package/dist/decorators/TypeFunctions.js +174 -0
- package/dist/decorators/index.d.ts +4 -2
- package/dist/decorators/index.js +3 -1
- package/dist/helper/DbHelper.d.ts +76 -5
- package/dist/helper/DbHelper.js +93 -154
- package/dist/helper/FieldsHelper.d.ts +0 -0
- package/dist/helper/FieldsHelper.js +0 -0
- package/dist/helper/db/index.d.ts +9 -0
- package/dist/helper/db/index.js +18 -0
- package/dist/helper/db/relation-manager.d.ts +45 -0
- package/dist/helper/db/relation-manager.js +105 -0
- package/dist/helper/db/schema-generator.d.ts +37 -0
- package/dist/helper/db/schema-generator.js +255 -0
- package/dist/helper/db/type-converter.d.ts +22 -0
- package/dist/helper/db/type-converter.js +106 -0
- package/dist/helper/db/utils.d.ts +24 -0
- package/dist/helper/db/utils.js +99 -0
- package/dist/index.d.ts +3 -3
- package/dist/index.js +2 -1
- package/dist/models/TimeSeriesModel.d.ts +7 -7
- package/dist/models/TimeSeriesModel.js +33 -33
- package/dist/models/_model.d.ts +2 -2
- package/dist/models/_model.js +0 -0
- package/dist/models/core/RWSModel.d.ts +5 -2
- package/dist/models/core/RWSModel.js +8 -5
- package/dist/models/core/TimeSeriesModel.d.ts +0 -0
- package/dist/models/core/TimeSeriesModel.js +0 -0
- package/dist/models/index.d.ts +2 -2
- package/dist/models/index.js +0 -0
- package/dist/models/interfaces/IDbOpts.d.ts +17 -0
- package/dist/models/interfaces/IDbOpts.js +2 -0
- package/dist/models/interfaces/IIdOpts.d.ts +0 -0
- package/dist/models/interfaces/IIdOpts.js +1 -0
- package/dist/models/interfaces/IIdTypeOpts.d.ts +3 -0
- package/dist/models/interfaces/IIdTypeOpts.js +2 -0
- package/dist/models/interfaces/IModel.d.ts +0 -0
- package/dist/models/interfaces/IModel.js +0 -0
- package/dist/models/interfaces/IRWSModelServices.d.ts +0 -0
- package/dist/models/interfaces/IRWSModelServices.js +0 -0
- package/dist/models/interfaces/ITrackerOpts.d.ts +12 -0
- package/dist/models/interfaces/ITrackerOpts.js +2 -0
- package/dist/models/interfaces/OpModelType.d.ts +4 -1
- package/dist/models/interfaces/OpModelType.js +0 -0
- package/dist/models/types/RelationTypes.d.ts +0 -0
- package/dist/models/types/RelationTypes.js +0 -0
- package/dist/models/utils/ModelUtils.d.ts +0 -0
- package/dist/models/utils/ModelUtils.js +0 -0
- package/dist/models/utils/PaginationUtils.d.ts +0 -0
- package/dist/models/utils/PaginationUtils.js +0 -0
- package/dist/models/utils/RelationUtils.d.ts +1 -1
- package/dist/models/utils/RelationUtils.js +2 -2
- package/dist/models/utils/TimeSeriesUtils.d.ts +0 -0
- package/dist/models/utils/TimeSeriesUtils.js +0 -0
- package/dist/services/DBService.d.ts +0 -0
- package/dist/services/DBService.js +0 -0
- package/dist/types/DbConfigHandler.d.ts +1 -1
- package/dist/types/DbConfigHandler.js +0 -0
- package/dist/types/FindParams.d.ts +0 -0
- package/dist/types/FindParams.js +0 -0
- package/dist/types/IRWSModel.d.ts +1 -1
- package/dist/types/IRWSModel.js +0 -0
- package/dist/types/ITimeSeries.d.ts +0 -0
- package/dist/types/ITimeSeries.js +0 -0
- package/exec/console.js +110 -110
- package/exec/db.rws.webpack.config.js +168 -168
- package/exec/src/cli.ts +73 -75
- package/exec/tsconfig.json +32 -32
- package/exec/webpackFilters.js +17 -17
- package/package.json +36 -36
- package/src/decorators/IdType.ts +17 -0
- package/src/decorators/InverseRelation.ts +41 -37
- package/src/decorators/InverseTimeSeries.ts +21 -21
- package/src/decorators/RWSCollection.ts +45 -27
- package/src/decorators/Relation.ts +61 -48
- package/src/decorators/TrackType.ts +65 -69
- package/src/decorators/index.ts +16 -8
- package/src/empty.js +0 -0
- package/src/helper/DbHelper.ts +133 -223
- package/src/helper/FieldsHelper.ts +34 -34
- package/src/helper/db/index.ts +10 -0
- package/src/helper/db/relation-manager.ts +119 -0
- package/src/helper/db/schema-generator.ts +318 -0
- package/src/helper/db/type-converter.ts +119 -0
- package/src/helper/db/utils.ts +120 -0
- package/src/index.ts +47 -38
- package/src/models/_model.ts +29 -29
- package/src/models/core/RWSModel.ts +529 -520
- package/src/models/core/TimeSeriesModel.ts +19 -19
- package/src/models/index.ts +20 -20
- package/src/models/interfaces/IDbOpts.ts +17 -0
- package/src/models/interfaces/IIdTypeOpts.ts +4 -0
- package/src/models/interfaces/IModel.ts +12 -12
- package/src/models/interfaces/IRWSModelServices.ts +7 -7
- package/src/models/interfaces/ITrackerOpts.ts +13 -0
- package/src/models/interfaces/OpModelType.ts +52 -49
- package/src/models/types/RelationTypes.ts +25 -25
- package/src/models/utils/ModelUtils.ts +65 -65
- package/src/models/utils/PaginationUtils.ts +42 -42
- package/src/models/utils/RelationUtils.ts +76 -76
- package/src/models/utils/TimeSeriesUtils.ts +38 -38
- package/src/services/DBService.ts +277 -277
- package/src/types/DbConfigHandler.ts +17 -17
- package/src/types/FindParams.ts +13 -13
- package/src/types/IRWSModel.ts +2 -2
- package/src/types/ITimeSeries.ts +5 -5
- package/tsconfig.json +22 -22
|
@@ -3,12 +3,15 @@ import { IRWSModelServices } from '../interfaces/IRWSModelServices';
|
|
|
3
3
|
import { OpModelType } from '../interfaces/OpModelType';
|
|
4
4
|
import { FindByType, IPaginationParams } from '../../types/FindParams';
|
|
5
5
|
import { DBService } from '../../services/DBService';
|
|
6
|
+
import { ISuperTagData } from '../../decorators/RWSCollection';
|
|
6
7
|
declare class RWSModel<T> implements IModel {
|
|
7
8
|
static services: IRWSModelServices;
|
|
8
9
|
[key: string]: any;
|
|
9
10
|
id: string | number;
|
|
10
11
|
static _collection: string;
|
|
11
12
|
static _RELATIONS: {};
|
|
13
|
+
static _NO_ID: boolean;
|
|
14
|
+
static _SUPER_TAGS: ISuperTagData[];
|
|
12
15
|
static _BANNED_KEYS: string[];
|
|
13
16
|
static allModels: OpModelType<any>[];
|
|
14
17
|
static _CUT_KEYS: string[];
|
|
@@ -51,14 +54,14 @@ declare class RWSModel<T> implements IModel {
|
|
|
51
54
|
sanitizeDBData(data: any): any;
|
|
52
55
|
static watchCollection<T extends RWSModel<T>>(this: OpModelType<T>, preRun: () => void): Promise<any>;
|
|
53
56
|
static findOneBy<T extends RWSModel<T>>(this: OpModelType<T>, findParams?: FindByType): Promise<T | null>;
|
|
54
|
-
static find<T extends RWSModel<T>>(this: OpModelType<T>, id: string, findParams?: Omit<FindByType, 'conditions'>): Promise<T | null>;
|
|
57
|
+
static find<T extends RWSModel<T>>(this: OpModelType<T>, id: string | number, findParams?: Omit<FindByType, 'conditions'>): Promise<T | null>;
|
|
55
58
|
static findBy<T extends RWSModel<T>>(this: OpModelType<T>, findParams?: FindByType): Promise<T[]>;
|
|
56
59
|
static delete<T extends RWSModel<T>>(this: OpModelType<T>, conditions: any): Promise<void>;
|
|
57
60
|
delete<T extends RWSModel<T>>(): Promise<void>;
|
|
58
61
|
static create<T extends RWSModel<T>>(this: new () => T, data: any): Promise<T>;
|
|
59
62
|
static loadModels(): OpModelType<any>[];
|
|
60
63
|
loadModels(): OpModelType<any>[];
|
|
61
|
-
private
|
|
64
|
+
private checkRelDisabled;
|
|
62
65
|
static setServices(services: IRWSModelServices): void;
|
|
63
66
|
getDb(): DBService;
|
|
64
67
|
static getDb(): DBService;
|
|
@@ -94,7 +94,7 @@ class RWSModel {
|
|
|
94
94
|
continue;
|
|
95
95
|
}
|
|
96
96
|
const relMeta = relManyData[key];
|
|
97
|
-
const relationEnabled = RelationUtils_1.RelationUtils.
|
|
97
|
+
const relationEnabled = !RelationUtils_1.RelationUtils.checkRelDisabled(this, relMeta.key);
|
|
98
98
|
if (relationEnabled) {
|
|
99
99
|
this[relMeta.key] = await relMeta.inversionModel.findBy({
|
|
100
100
|
conditions: {
|
|
@@ -110,7 +110,7 @@ class RWSModel {
|
|
|
110
110
|
continue;
|
|
111
111
|
}
|
|
112
112
|
const relMeta = relOneData[key];
|
|
113
|
-
const relationEnabled = RelationUtils_1.RelationUtils.
|
|
113
|
+
const relationEnabled = !RelationUtils_1.RelationUtils.checkRelDisabled(this, relMeta.key);
|
|
114
114
|
if (!data[relMeta.hydrationField] && relMeta.required) {
|
|
115
115
|
throw new Error(`Relation field "${relMeta.hydrationField}" is required in model ${this.constructor.name}.`);
|
|
116
116
|
}
|
|
@@ -122,7 +122,8 @@ class RWSModel {
|
|
|
122
122
|
this[relMeta.key] = await newRelModel.save();
|
|
123
123
|
}
|
|
124
124
|
const cutKeys = this.constructor._CUT_KEYS;
|
|
125
|
-
|
|
125
|
+
const trackedField = Object.keys((await ModelUtils_1.ModelUtils.getModelAnnotations(this.constructor))).includes(relMeta.hydrationField);
|
|
126
|
+
if (!cutKeys.includes(relMeta.hydrationField) && !trackedField) {
|
|
126
127
|
cutKeys.push(relMeta.hydrationField);
|
|
127
128
|
}
|
|
128
129
|
}
|
|
@@ -373,8 +374,8 @@ class RWSModel {
|
|
|
373
374
|
loadModels() {
|
|
374
375
|
return RWSModel.loadModels();
|
|
375
376
|
}
|
|
376
|
-
|
|
377
|
-
return RelationUtils_1.RelationUtils.
|
|
377
|
+
checkRelDisabled(key) {
|
|
378
|
+
return RelationUtils_1.RelationUtils.checkRelDisabled(this, key);
|
|
378
379
|
}
|
|
379
380
|
static setServices(services) {
|
|
380
381
|
this.allModels = services.configService.get('db_models');
|
|
@@ -391,6 +392,8 @@ exports.RWSModel = RWSModel;
|
|
|
391
392
|
RWSModel.services = {};
|
|
392
393
|
RWSModel._collection = null;
|
|
393
394
|
RWSModel._RELATIONS = {};
|
|
395
|
+
RWSModel._NO_ID = false;
|
|
396
|
+
RWSModel._SUPER_TAGS = [];
|
|
394
397
|
RWSModel._BANNED_KEYS = ['_collection'];
|
|
395
398
|
RWSModel.allModels = [];
|
|
396
399
|
RWSModel._CUT_KEYS = [];
|
|
File without changes
|
|
File without changes
|
package/dist/models/index.d.ts
CHANGED
|
@@ -3,5 +3,5 @@ import { IRWSModelServices } from './interfaces/IRWSModelServices';
|
|
|
3
3
|
import { OpModelType } from './interfaces/OpModelType';
|
|
4
4
|
import { RelationBindType, RelOneMetaType, RelManyMetaType } from './types/RelationTypes';
|
|
5
5
|
import { RWSModel } from './core/RWSModel';
|
|
6
|
-
import { TrackType,
|
|
7
|
-
export { IModel, IRWSModelServices, OpModelType, RelationBindType, RelOneMetaType, RelManyMetaType, RWSModel, TrackType,
|
|
6
|
+
import { TrackType, ITrackerMetaOpts } from '../decorators';
|
|
7
|
+
export { IModel, IRWSModelServices, OpModelType, RelationBindType, RelOneMetaType, RelManyMetaType, RWSModel, TrackType, ITrackerMetaOpts };
|
package/dist/models/index.js
CHANGED
|
File without changes
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export interface IDbOpts {
|
|
2
|
+
dbOptions?: {
|
|
3
|
+
mysql?: {
|
|
4
|
+
useType?: string;
|
|
5
|
+
useText?: boolean;
|
|
6
|
+
maxLength?: number;
|
|
7
|
+
useUuid?: boolean;
|
|
8
|
+
};
|
|
9
|
+
postgres?: {
|
|
10
|
+
useText?: boolean;
|
|
11
|
+
useUuid?: boolean;
|
|
12
|
+
};
|
|
13
|
+
mongodb?: {
|
|
14
|
+
customType?: string;
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { IDbOpts } from "./IDbOpts";
|
|
2
|
+
import { OpModelType } from "./OpModelType";
|
|
3
|
+
export interface ITrackerOpts extends IDbOpts {
|
|
4
|
+
required?: boolean;
|
|
5
|
+
unique?: boolean | string;
|
|
6
|
+
isArray?: boolean;
|
|
7
|
+
relationField?: string;
|
|
8
|
+
relatedToField?: string;
|
|
9
|
+
relatedTo?: OpModelType<any>;
|
|
10
|
+
inversionModel?: OpModelType<any>;
|
|
11
|
+
relationName?: string;
|
|
12
|
+
}
|
|
@@ -4,11 +4,14 @@ import { IRWSModelServices } from './IRWSModelServices';
|
|
|
4
4
|
import { RelOneMetaType, RelManyMetaType } from '../types/RelationTypes';
|
|
5
5
|
import { DBService } from '../../services/DBService';
|
|
6
6
|
import type { RWSModel } from '../core/RWSModel';
|
|
7
|
+
import { ISuperTagData } from '../../decorators/RWSCollection';
|
|
7
8
|
export interface OpModelType<T> {
|
|
8
9
|
new (data?: any | null): T;
|
|
9
10
|
services: IRWSModelServices;
|
|
10
11
|
name: string;
|
|
11
12
|
_collection: string;
|
|
13
|
+
_NO_ID: boolean;
|
|
14
|
+
_SUPER_TAGS: ISuperTagData[];
|
|
12
15
|
_RELATIONS: {
|
|
13
16
|
[key: string]: boolean;
|
|
14
17
|
};
|
|
@@ -18,7 +21,7 @@ export interface OpModelType<T> {
|
|
|
18
21
|
checkForInclusionWithThrow: (className: string) => void;
|
|
19
22
|
checkForInclusion: (className: string) => boolean;
|
|
20
23
|
findOneBy<T extends RWSModel<T>>(this: OpModelType<T>, findParams?: FindByType): Promise<T | null>;
|
|
21
|
-
find<T extends RWSModel<T>>(this: OpModelType<T>, id: string, findParams?: Omit<FindByType, 'conditions'>): Promise<T | null>;
|
|
24
|
+
find<T extends RWSModel<T>>(this: OpModelType<T>, id: string | number, findParams?: Omit<FindByType, 'conditions'>): Promise<T | null>;
|
|
22
25
|
findBy<T extends RWSModel<T>>(this: OpModelType<T>, findParams?: FindByType): Promise<T[]>;
|
|
23
26
|
paginate<T extends RWSModel<T>>(this: OpModelType<T>, paginateParams?: IPaginationParams, findParams?: FindByType): Promise<T[]>;
|
|
24
27
|
delete<T extends RWSModel<T>>(this: OpModelType<T>, conditions: any): Promise<void>;
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -57,9 +57,9 @@ class RelationUtils {
|
|
|
57
57
|
// Check if the property exists and is an object with an id property
|
|
58
58
|
return !!model[key] && typeof model[key] === 'object' && model[key] !== null && 'id' in model[key];
|
|
59
59
|
}
|
|
60
|
-
static
|
|
60
|
+
static checkRelDisabled(model, key) {
|
|
61
61
|
return Object.keys(model.constructor._RELATIONS).includes(key) &&
|
|
62
|
-
model.constructor._RELATIONS[key] ===
|
|
62
|
+
model.constructor._RELATIONS[key] === false;
|
|
63
63
|
}
|
|
64
64
|
}
|
|
65
65
|
exports.RelationUtils = RelationUtils;
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -2,7 +2,7 @@ import { OpModelType } from "../models/interfaces/OpModelType";
|
|
|
2
2
|
export interface IDbConfigParams {
|
|
3
3
|
db_url?: string;
|
|
4
4
|
db_name?: string;
|
|
5
|
-
db_type?: 'mongodb' | 'mysql' | 'sqlite';
|
|
5
|
+
db_type?: 'mongodb' | 'mysql' | 'sqlite' | 'postgresql' | 'postgres';
|
|
6
6
|
db_models?: OpModelType<any>[];
|
|
7
7
|
}
|
|
8
8
|
export interface IdGeneratorOptions {
|
|
File without changes
|
|
File without changes
|
package/dist/types/FindParams.js
CHANGED
|
File without changes
|
package/dist/types/IRWSModel.js
CHANGED
|
File without changes
|
|
File without changes
|
|
File without changes
|
package/exec/console.js
CHANGED
|
@@ -1,111 +1,111 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
const { rwsShell, rwsPath } = require('@rws-framework/console');
|
|
4
|
-
const chalk = require('chalk');
|
|
5
|
-
const crypto = require('crypto');
|
|
6
|
-
const path = require('path');
|
|
7
|
-
const fs = require('fs');
|
|
8
|
-
|
|
9
|
-
const params = process.argv.splice(2);
|
|
10
|
-
let paramsString = params.length ? (' ' + params.join(' ')) : '';
|
|
11
|
-
|
|
12
|
-
const appRoot = process.cwd();
|
|
13
|
-
const rwsCliConfigDir = path.resolve(appRoot, 'node_modules', '.rws', 'cli');
|
|
14
|
-
const getCachedPath = (key) => path.resolve(rwsCliConfigDir, key);
|
|
15
|
-
|
|
16
|
-
const currentCwd = path.resolve(__dirname);
|
|
17
|
-
|
|
18
|
-
console.log({params})
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
const commandString = `npx webpack --config db.rws.webpack.config.js --output-path ./build ${process.cwd()} ${params[2]}`;
|
|
22
|
-
function needsCacheWarming(){
|
|
23
|
-
|
|
24
|
-
if(!fs.existsSync(getCachedPath('paths')) || !fs.existsSync(getCachedPath('checksum'))){
|
|
25
|
-
return true;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
const fileList = fs.readFileSync(getCachedPath('paths'), 'utf-8').split('\n');
|
|
29
|
-
|
|
30
|
-
if(fileList.length){
|
|
31
|
-
const fileContents = [];
|
|
32
|
-
for(const filePath of fileList){
|
|
33
|
-
if(fs.existsSync(filePath)){
|
|
34
|
-
fileContents.push(fs.readFileSync(filePath, 'utf-8'));
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
const finalMD5 = crypto.createHash('md5').update(fileContents.join('\n')).digest('hex');
|
|
38
|
-
const cachedMD5 = fs.readFileSync(getCachedPath('checksum'), 'utf-8');
|
|
39
|
-
|
|
40
|
-
if(finalMD5 === cachedMD5){
|
|
41
|
-
return false;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
return true;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
async function main()
|
|
49
|
-
{
|
|
50
|
-
const hasRebuild = paramsString.split(' ').pop().indexOf('--rebuild') > -1;
|
|
51
|
-
const doWarmCache = needsCacheWarming() || hasRebuild;
|
|
52
|
-
|
|
53
|
-
if(!((params[0] || false) && (params[1] || false) && (params[2] || false))){
|
|
54
|
-
throw new Error(`CLI command MUST have 3 parameters: {DB_URL, DB_NAME, MODELS_DIR}`);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
if(doWarmCache){
|
|
58
|
-
console.log(chalk.yellow('[RWS DB CLI] Building CLI client...'));
|
|
59
|
-
|
|
60
|
-
const cacheTypes = ['paths', 'checksum'];
|
|
61
|
-
|
|
62
|
-
for(const type of cacheTypes){
|
|
63
|
-
if(fs.existsSync(getCachedPath(type))){
|
|
64
|
-
fs.unlinkSync(getCachedPath(type));
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
await tsc();
|
|
69
|
-
|
|
70
|
-
await rwsShell.runCommand(commandString, currentCwd);
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
}else{
|
|
74
|
-
console.log(chalk.blue('[RWS CLI CACHE] Starting command from built CLI client.'));
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
let startSlice = hasRebuild ? -1 : paramsString.split(' ').length;
|
|
78
|
-
let endSlice = hasRebuild ? -1 : null ;
|
|
79
|
-
|
|
80
|
-
paramsString = [
|
|
81
|
-
...paramsString.split(' ').slice(0, startSlice),
|
|
82
|
-
currentCwd,
|
|
83
|
-
endSlice ?
|
|
84
|
-
paramsString.split(' ').at(endSlice)
|
|
85
|
-
: null
|
|
86
|
-
].filter((item) => item !== null).join(' ');
|
|
87
|
-
|
|
88
|
-
await rwsShell.runCommand(`node ${path.join(currentCwd, 'build', 'main.cli.rws.js')}${paramsString}`, process.cwd());
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
async function tsc (){
|
|
92
|
-
const tempConfigContent = {
|
|
93
|
-
"extends": "./tsconfig.json",
|
|
94
|
-
"include": [
|
|
95
|
-
path.join(process.cwd(), params[2], 'index.ts')
|
|
96
|
-
],
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
const tempConfigPath = path.join(currentCwd, '.tmp.gitignore.json');
|
|
100
|
-
fs.writeFileSync(tempConfigPath, JSON.stringify(tempConfigContent, null, 2));
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
await rwsShell.runCommand(`tsc -p ${tempConfigPath}`, currentCwd);
|
|
104
|
-
fs.unlinkSync(tempConfigPath);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
main().then((data) => {
|
|
108
|
-
console.log(chalk.green('[RWS DB CLI] Command complete.'));
|
|
109
|
-
}).catch((e) => {
|
|
110
|
-
console.error(e.message);
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const { rwsShell, rwsPath } = require('@rws-framework/console');
|
|
4
|
+
const chalk = require('chalk');
|
|
5
|
+
const crypto = require('crypto');
|
|
6
|
+
const path = require('path');
|
|
7
|
+
const fs = require('fs');
|
|
8
|
+
|
|
9
|
+
const params = process.argv.splice(2);
|
|
10
|
+
let paramsString = params.length ? (' ' + params.join(' ')) : '';
|
|
11
|
+
|
|
12
|
+
const appRoot = process.cwd();
|
|
13
|
+
const rwsCliConfigDir = path.resolve(appRoot, 'node_modules', '.rws', 'cli');
|
|
14
|
+
const getCachedPath = (key) => path.resolve(rwsCliConfigDir, key);
|
|
15
|
+
|
|
16
|
+
const currentCwd = path.resolve(__dirname);
|
|
17
|
+
|
|
18
|
+
console.log({params})
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
const commandString = `npx webpack --config db.rws.webpack.config.js --output-path ./build ${process.cwd()} ${params[2]}`;
|
|
22
|
+
function needsCacheWarming(){
|
|
23
|
+
|
|
24
|
+
if(!fs.existsSync(getCachedPath('paths')) || !fs.existsSync(getCachedPath('checksum'))){
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const fileList = fs.readFileSync(getCachedPath('paths'), 'utf-8').split('\n');
|
|
29
|
+
|
|
30
|
+
if(fileList.length){
|
|
31
|
+
const fileContents = [];
|
|
32
|
+
for(const filePath of fileList){
|
|
33
|
+
if(fs.existsSync(filePath)){
|
|
34
|
+
fileContents.push(fs.readFileSync(filePath, 'utf-8'));
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
const finalMD5 = crypto.createHash('md5').update(fileContents.join('\n')).digest('hex');
|
|
38
|
+
const cachedMD5 = fs.readFileSync(getCachedPath('checksum'), 'utf-8');
|
|
39
|
+
|
|
40
|
+
if(finalMD5 === cachedMD5){
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async function main()
|
|
49
|
+
{
|
|
50
|
+
const hasRebuild = paramsString.split(' ').pop().indexOf('--rebuild') > -1;
|
|
51
|
+
const doWarmCache = needsCacheWarming() || hasRebuild;
|
|
52
|
+
|
|
53
|
+
if(!((params[0] || false) && (params[1] || false) && (params[2] || false))){
|
|
54
|
+
throw new Error(`CLI command MUST have 3 parameters: {DB_URL, DB_NAME, MODELS_DIR}`);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if(doWarmCache){
|
|
58
|
+
console.log(chalk.yellow('[RWS DB CLI] Building CLI client...'));
|
|
59
|
+
|
|
60
|
+
const cacheTypes = ['paths', 'checksum'];
|
|
61
|
+
|
|
62
|
+
for(const type of cacheTypes){
|
|
63
|
+
if(fs.existsSync(getCachedPath(type))){
|
|
64
|
+
fs.unlinkSync(getCachedPath(type));
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
await tsc();
|
|
69
|
+
|
|
70
|
+
await rwsShell.runCommand(commandString, currentCwd);
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
}else{
|
|
74
|
+
console.log(chalk.blue('[RWS CLI CACHE] Starting command from built CLI client.'));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
let startSlice = hasRebuild ? -1 : paramsString.split(' ').length;
|
|
78
|
+
let endSlice = hasRebuild ? -1 : null ;
|
|
79
|
+
|
|
80
|
+
paramsString = [
|
|
81
|
+
...paramsString.split(' ').slice(0, startSlice),
|
|
82
|
+
currentCwd,
|
|
83
|
+
endSlice ?
|
|
84
|
+
paramsString.split(' ').at(endSlice)
|
|
85
|
+
: null
|
|
86
|
+
].filter((item) => item !== null).join(' ');
|
|
87
|
+
|
|
88
|
+
await rwsShell.runCommand(`node ${path.join(currentCwd, 'build', 'main.cli.rws.js')}${paramsString}`, process.cwd());
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
async function tsc (){
|
|
92
|
+
const tempConfigContent = {
|
|
93
|
+
"extends": "./tsconfig.json",
|
|
94
|
+
"include": [
|
|
95
|
+
path.join(process.cwd(), params[2], 'index.ts')
|
|
96
|
+
],
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
const tempConfigPath = path.join(currentCwd, '.tmp.gitignore.json');
|
|
100
|
+
fs.writeFileSync(tempConfigPath, JSON.stringify(tempConfigContent, null, 2));
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
await rwsShell.runCommand(`tsc -p ${tempConfigPath}`, currentCwd);
|
|
104
|
+
fs.unlinkSync(tempConfigPath);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
main().then((data) => {
|
|
108
|
+
console.log(chalk.green('[RWS DB CLI] Command complete.'));
|
|
109
|
+
}).catch((e) => {
|
|
110
|
+
console.error(e.message);
|
|
111
111
|
});
|