masterrecord 0.0.23 → 0.0.25
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/DeleteManager.js +51 -0
- package/Entity/EntityModel.js +192 -120
- package/Entity/EntityModelBuilder.js +41 -63
- package/Entity/EntityTrackerModel.js +222 -42
- package/InsertManager.js +138 -0
- package/MYSQLEngine.js +409 -0
- package/Masterrecord.js +233 -179
- package/Migrations/cli.js +106 -105
- package/Migrations/migrationTemplate.js +63 -63
- package/Migrations/migrations.js +65 -22
- package/Migrations/schema.js +42 -42
- package/QueryLanguage/queryManager.js +66 -0
- package/QueryLanguage/queryMethods.js +171 -0
- package/QueryLanguage/queryScript.js +331 -0
- package/SQLLiteEngine.js +409 -0
- package/Tools.js +118 -55
- package/package.json +23 -27
- package/QueryLanguage/_Expression.js +0 -322
- package/QueryLanguage/_LogicalQuery.js +0 -23
- package/QueryLanguage/_OperatorList.js +0 -88
- package/QueryLanguage/_QueryModel.js +0 -442
- package/QueryLanguage/_Tokenization.js +0 -173
- package/QueryLanguage/__Query.js +0 -386
- package/QueryLanguage/_simpleQuery.js +0 -184
- package/QueryLanguage/queryBuilder.js +0 -52
- package/SQLEngine.js +0 -52
|
@@ -1,64 +1,64 @@
|
|
|
1
|
-
var Schema = require('./schema');
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
// https://channel9.msdn.com/Blogs/EF/Migrations-Under-the-Hood
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
// migration Logic
|
|
8
|
-
// using the command line run the command - "add migration 'name' 'context file location' ";
|
|
9
|
-
// this will call the context which will return on object of entities
|
|
10
|
-
// then call the previous migration and pass that model object and the context object to the EDMModelDiffer
|
|
11
|
-
// the EDMModelDiffer function to calculate database changes
|
|
12
|
-
// EDMModelDiffer will return only database changes model that have not been implemented already
|
|
13
|
-
// we then create the miration using function 'migrationCodeGenerator' based on the model that was provided by EDMModelDiffer
|
|
14
|
-
// js date stamp Date.now()
|
|
15
|
-
|
|
16
|
-
class Migration extends Schema {
|
|
17
|
-
|
|
18
|
-
constructor() {
|
|
19
|
-
super();
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
static up(){
|
|
23
|
-
|
|
24
|
-
this.createTable("user", {
|
|
25
|
-
id : {
|
|
26
|
-
type : "integer",
|
|
27
|
-
primary : true,
|
|
28
|
-
unique : true,
|
|
29
|
-
nullable : false
|
|
30
|
-
},
|
|
31
|
-
user_id : {
|
|
32
|
-
type : "integer",
|
|
33
|
-
required : true, // SQL Data Constraints
|
|
34
|
-
foreignKey : "user" // SQL Data Constraints
|
|
35
|
-
},
|
|
36
|
-
website_url : {
|
|
37
|
-
type : "string",
|
|
38
|
-
required : true,
|
|
39
|
-
maxLength : 60
|
|
40
|
-
},
|
|
41
|
-
website_type : {
|
|
42
|
-
type : "string",
|
|
43
|
-
required : true
|
|
44
|
-
}
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
this.addColumn("blog", "url", {
|
|
48
|
-
type : "string",
|
|
49
|
-
required : true
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
this.done();
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
static down(){
|
|
56
|
-
this.dropTable("user");
|
|
57
|
-
|
|
58
|
-
this.dropColumn("blog", "url");
|
|
59
|
-
|
|
60
|
-
this.done();
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
1
|
+
var Schema = require('./schema');
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
// https://channel9.msdn.com/Blogs/EF/Migrations-Under-the-Hood
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
// migration Logic
|
|
8
|
+
// using the command line run the command - "add migration 'name' 'context file location' ";
|
|
9
|
+
// this will call the context which will return on object of entities
|
|
10
|
+
// then call the previous migration and pass that model object and the context object to the EDMModelDiffer
|
|
11
|
+
// the EDMModelDiffer function to calculate database changes
|
|
12
|
+
// EDMModelDiffer will return only database changes model that have not been implemented already
|
|
13
|
+
// we then create the miration using function 'migrationCodeGenerator' based on the model that was provided by EDMModelDiffer
|
|
14
|
+
// js date stamp Date.now()
|
|
15
|
+
|
|
16
|
+
class Migration extends Schema {
|
|
17
|
+
|
|
18
|
+
constructor() {
|
|
19
|
+
super();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
static up(){
|
|
23
|
+
|
|
24
|
+
this.createTable("user", {
|
|
25
|
+
id : {
|
|
26
|
+
type : "integer",
|
|
27
|
+
primary : true,
|
|
28
|
+
unique : true,
|
|
29
|
+
nullable : false
|
|
30
|
+
},
|
|
31
|
+
user_id : {
|
|
32
|
+
type : "integer",
|
|
33
|
+
required : true, // SQL Data Constraints
|
|
34
|
+
foreignKey : "user" // SQL Data Constraints
|
|
35
|
+
},
|
|
36
|
+
website_url : {
|
|
37
|
+
type : "string",
|
|
38
|
+
required : true,
|
|
39
|
+
maxLength : 60
|
|
40
|
+
},
|
|
41
|
+
website_type : {
|
|
42
|
+
type : "string",
|
|
43
|
+
required : true
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
this.addColumn("blog", "url", {
|
|
48
|
+
type : "string",
|
|
49
|
+
required : true
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
this.done();
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
static down(){
|
|
56
|
+
this.dropTable("user");
|
|
57
|
+
|
|
58
|
+
this.dropColumn("blog", "url");
|
|
59
|
+
|
|
60
|
+
this.done();
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
64
|
module.exports = Migration;
|
package/Migrations/migrations.js
CHANGED
|
@@ -1,23 +1,66 @@
|
|
|
1
|
-
// version 1
|
|
2
|
-
var fs = require('fs');
|
|
3
|
-
// https://blog.tekspace.io/code-first-multiple-db-context-migration/
|
|
4
|
-
|
|
5
|
-
// node masterrecord add-migration josh C:\Users\rbatista\Downloads\kollege\freshmen\app\models\context
|
|
6
|
-
class Migrations{
|
|
7
|
-
|
|
8
|
-
EDMModelDiffer(
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
//
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
1
|
+
// version 1
|
|
2
|
+
var fs = require('fs');
|
|
3
|
+
// https://blog.tekspace.io/code-first-multiple-db-context-migration/
|
|
4
|
+
|
|
5
|
+
// node masterrecord add-migration josh C:\Users\rbatista\Downloads\kollege\freshmen\app\models\context
|
|
6
|
+
class Migrations{
|
|
7
|
+
|
|
8
|
+
EDMModelDiffer(snapShots, tableList){
|
|
9
|
+
new schemaList = []
|
|
10
|
+
const tableKeyList = Object.keys(tableList);
|
|
11
|
+
// loop through tables
|
|
12
|
+
for (const tableKey of tableKeyList) {
|
|
13
|
+
// check if table has already been migrated
|
|
14
|
+
var tableSnapShot = snapShots[tableKey];
|
|
15
|
+
if(tableSnapShot){
|
|
16
|
+
// check if we have the column in the sceama
|
|
17
|
+
const columnKeyList = Object.keys(tableKey);
|
|
18
|
+
// loop through tables
|
|
19
|
+
for (const columnKey of columnKeyList) {
|
|
20
|
+
var newTable = {};
|
|
21
|
+
if(tableSnapShot[columnKey]){
|
|
22
|
+
|
|
23
|
+
/*
|
|
24
|
+
id : {
|
|
25
|
+
nullabale : true,
|
|
26
|
+
type : string
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
id : {
|
|
30
|
+
nullabale : true,
|
|
31
|
+
type : int,
|
|
32
|
+
unique : true
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
*/
|
|
36
|
+
// if we then check if we have all the same types
|
|
37
|
+
// loop through schema list
|
|
38
|
+
// check if we have the same in the context
|
|
39
|
+
// if the same then check if if value is diffrent
|
|
40
|
+
// then delete from context
|
|
41
|
+
// after loop complete wahtever left in conext just push to object
|
|
42
|
+
}
|
|
43
|
+
else{
|
|
44
|
+
// if we dont have it then add it to the
|
|
45
|
+
newTable[columnKey] = tableKey[columnKey];
|
|
46
|
+
schemaList.push(newTable);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}else{
|
|
50
|
+
schemaList.push(contextKeys[key]);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
migrationCodeGenerator(name, column, migrationDate){
|
|
58
|
+
// will create migration file with data needed
|
|
59
|
+
// using the migration template
|
|
60
|
+
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
}
|
|
65
|
+
|
|
23
66
|
module.exports = Migrations;
|
package/Migrations/schema.js
CHANGED
|
@@ -1,43 +1,43 @@
|
|
|
1
|
-
// version 1
|
|
2
|
-
var fs = require('fs');
|
|
3
|
-
|
|
4
|
-
class Schema{
|
|
5
|
-
|
|
6
|
-
// create obj to convert into create sql
|
|
7
|
-
addColumn(tableName, columnName, ){
|
|
8
|
-
|
|
9
|
-
// add column to database
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
dropColumn(tableName, columnName){
|
|
13
|
-
// drop column
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
createTable(name, columns){
|
|
18
|
-
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
dropTable(name){
|
|
22
|
-
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
// will get the data and create the file
|
|
26
|
-
done(){
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
/*
|
|
34
|
-
up and down function..
|
|
35
|
-
on commmand line call of run migrations with folder location of context. it will
|
|
36
|
-
load context and all the objects.
|
|
37
|
-
it will then match objects with migration data.
|
|
38
|
-
if it's a new item it will generate a new migration dependent on what comes from migration.
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
*/
|
|
42
|
-
|
|
1
|
+
// version 1
|
|
2
|
+
var fs = require('fs');
|
|
3
|
+
|
|
4
|
+
class Schema{
|
|
5
|
+
|
|
6
|
+
// create obj to convert into create sql
|
|
7
|
+
addColumn(tableName, columnName, ){
|
|
8
|
+
|
|
9
|
+
// add column to database
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
dropColumn(tableName, columnName){
|
|
13
|
+
// drop column
|
|
14
|
+
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
createTable(name, columns){
|
|
18
|
+
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
dropTable(name){
|
|
22
|
+
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// will get the data and create the file
|
|
26
|
+
done(){
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
/*
|
|
34
|
+
up and down function..
|
|
35
|
+
on commmand line call of run migrations with folder location of context. it will
|
|
36
|
+
load context and all the objects.
|
|
37
|
+
it will then match objects with migration data.
|
|
38
|
+
if it's a new item it will generate a new migration dependent on what comes from migration.
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
*/
|
|
42
|
+
|
|
43
43
|
module.exports = Schema;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
// ALL THIS SHOULD DO IS BUILD A SQL QUERY
|
|
2
|
+
// version 1.0.1
|
|
3
|
+
|
|
4
|
+
class queryManager{
|
|
5
|
+
constructor( query) {
|
|
6
|
+
|
|
7
|
+
/*
|
|
8
|
+
queryObject
|
|
9
|
+
{
|
|
10
|
+
"where": where user.id = 1
|
|
11
|
+
"raw": "select * from tablename where user.id = 2" OR false,
|
|
12
|
+
"builder": builderObject,
|
|
13
|
+
"select" : "*",
|
|
14
|
+
"from" : from tablename
|
|
15
|
+
}
|
|
16
|
+
*/
|
|
17
|
+
this._query = query;
|
|
18
|
+
this._queryBuilder = query.builder;
|
|
19
|
+
this._context = query.builder.__context;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
single(){
|
|
23
|
+
var entityValue = this._context._SQLEngine.get(this._query.getScript());
|
|
24
|
+
var sing = this._queryBuilder.__execute(entityValue, this._queryBuilder);
|
|
25
|
+
return sing;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
//
|
|
29
|
+
select(query){
|
|
30
|
+
// this will come after the where
|
|
31
|
+
this._query.select = "select " + query;
|
|
32
|
+
return this;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
count(){
|
|
36
|
+
// trying to match string select and relace with select Count(*);
|
|
37
|
+
var res = this._query.select.replace("select *", "select Count(*)");
|
|
38
|
+
var entityValue = this._context._SQLEngine.get(res);
|
|
39
|
+
var val = entityValue[Object.keys(entityValue)[0]];
|
|
40
|
+
return val;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
toList(){
|
|
44
|
+
var entityValue = this._context._SQLEngine.all(this._query);
|
|
45
|
+
var toLi = this._queryBuilder.__executeList(entityValue, this._queryBuilder);
|
|
46
|
+
return toLi;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
module.exports = queryManager;
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
/*
|
|
55
|
+
|
|
56
|
+
LINQ Extension Methods
|
|
57
|
+
First()
|
|
58
|
+
FirstOrDefault()
|
|
59
|
+
SingleOrDefault()
|
|
60
|
+
Count()
|
|
61
|
+
Min()
|
|
62
|
+
Max()
|
|
63
|
+
Last()
|
|
64
|
+
LastOrDefault()
|
|
65
|
+
Average()
|
|
66
|
+
*/
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
// ALL THIS SHOULD DO IS BUILD A SQL QUERY
|
|
2
|
+
// version 1.0.222
|
|
3
|
+
// TODO: change name of queryManager to select manager;
|
|
4
|
+
var entityTrackerModel = require('masterrecord/Entity/EntityTrackerModel');
|
|
5
|
+
var tools = require('masterrecord/Tools');
|
|
6
|
+
var queryScript = require('masterrecord/QueryLanguage/queryScript');
|
|
7
|
+
|
|
8
|
+
class queryMethods{
|
|
9
|
+
|
|
10
|
+
constructor(entity, context) {
|
|
11
|
+
this.__entity = entity;
|
|
12
|
+
this.__context = context;
|
|
13
|
+
this.__queryObject = new queryScript();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// build a single entity
|
|
17
|
+
__singleEntityBuilder(dataModel){
|
|
18
|
+
var $that = this;
|
|
19
|
+
if(dataModel){
|
|
20
|
+
var ent = new entityTrackerModel();
|
|
21
|
+
var mod = ent.build(dataModel, $that.__entity, $that.__context);
|
|
22
|
+
mod.__state = "track";
|
|
23
|
+
$that.__context.__track(mod);
|
|
24
|
+
return mod;
|
|
25
|
+
}else{
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// build multiple entities
|
|
31
|
+
__multipleEntityBuilder(entityValue){
|
|
32
|
+
var $that = this;
|
|
33
|
+
var listArray = [];
|
|
34
|
+
if(entityValue){
|
|
35
|
+
for(let i = 0; i < entityValue.length; i++){
|
|
36
|
+
listArray.push($that.__singleEntityBuilder(entityValue[i]));
|
|
37
|
+
}
|
|
38
|
+
return listArray;
|
|
39
|
+
}else{
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
__reset(){
|
|
45
|
+
this.__queryObject.reset();
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
raw(query){
|
|
49
|
+
|
|
50
|
+
this.__queryObject.raw(query);
|
|
51
|
+
return this;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
where(query, ...args){
|
|
55
|
+
var str = query.toString();
|
|
56
|
+
if(args){
|
|
57
|
+
for(let argument in args){
|
|
58
|
+
var item = args[argument];
|
|
59
|
+
str = str.replace("$$", item);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
this.__queryObject.where(str, this.__entity.__name);
|
|
63
|
+
return this;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// when you dont want to use lazy loading and want it called at that moment
|
|
67
|
+
//Eagerly loading
|
|
68
|
+
include(query, ...args){
|
|
69
|
+
var str = query.toString();
|
|
70
|
+
if(args){
|
|
71
|
+
for(let argument in args){
|
|
72
|
+
var item = args[argument];
|
|
73
|
+
str = str.replace("$$", item);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
this.__queryObject.include(str, this.__entity.__name);
|
|
77
|
+
return this;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// only takes a array of selected items
|
|
81
|
+
select(query, ...args){
|
|
82
|
+
var str = query.toString();
|
|
83
|
+
if(args){
|
|
84
|
+
for(let argument in args){
|
|
85
|
+
var item = args[argument];
|
|
86
|
+
str = str.replace("$$", item);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
this.__queryObject.select(str, this.__entity.__name);
|
|
90
|
+
return this;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
// do join on two tables = inner join
|
|
96
|
+
join(){
|
|
97
|
+
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
skip(){
|
|
101
|
+
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
limit(){
|
|
105
|
+
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
oderBy(){
|
|
109
|
+
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
groupBy(){
|
|
113
|
+
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
contains(){
|
|
117
|
+
// https://entityframework.net/knowledge-base/3491721/linq-to-entities---where-in-clause-in-query
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
count(){
|
|
121
|
+
// trying to match string select and relace with select Count(*);
|
|
122
|
+
var entityValue = this.__context._SQLEngine.getCount(this.__queryObject, this.__entity, this.__context);
|
|
123
|
+
var val = entityValue[Object.keys(entityValue)[0]];
|
|
124
|
+
this.__reset();
|
|
125
|
+
return val;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
single(){
|
|
129
|
+
var entityValue = this.__context._SQLEngine.get(this.__queryObject.script, this.__entity, this.__context);
|
|
130
|
+
var sing = this.__singleEntityBuilder(entityValue, this._queryBuilder);
|
|
131
|
+
this.__reset();
|
|
132
|
+
return sing;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
toList(){
|
|
136
|
+
var entityValue = this.__context._SQLEngine.all(this.__queryObject.script, this.__entity, this.__context);
|
|
137
|
+
var toLi = this.__multipleEntityBuilder(entityValue, this._queryBuilder);
|
|
138
|
+
this.__reset();
|
|
139
|
+
return toLi;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
asQueryable(){
|
|
143
|
+
// returns the sql created and does not make a call to DB
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
add(entityValue){
|
|
147
|
+
// This will call context API to REMOVE entity to update list
|
|
148
|
+
tools.clearAllProto(entityValue);
|
|
149
|
+
entityValue.__state = "insert";
|
|
150
|
+
entityValue.__entity = this.__entity;
|
|
151
|
+
entityValue.__context = this.__context;
|
|
152
|
+
this.__context.__track(entityValue);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
remove(entityValue){
|
|
156
|
+
entityValue.__state = "delete";
|
|
157
|
+
entityValue.__entity = this.__entity;
|
|
158
|
+
entityValue.__context = this.__context;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
track(entityValue){
|
|
162
|
+
entityValue.__state = "track";
|
|
163
|
+
tools.clearAllProto(entityValue);
|
|
164
|
+
entityValue.__entity = this.__entity;
|
|
165
|
+
entityValue.__context = this.__context;
|
|
166
|
+
this.__context.__track(entityValue);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
module.exports = queryMethods;
|
|
171
|
+
|