sedentary 0.0.14 → 0.0.18

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/lib/db.js CHANGED
@@ -29,33 +29,23 @@ exports.Attribute = Attribute;
29
29
  function autoImplement() {
30
30
  return class {
31
31
  constructor(defaults) {
32
- Object.assign(this, defaults || {});
32
+ Object.assign(this, defaults);
33
33
  }
34
34
  };
35
35
  }
36
36
  class Table extends autoImplement() {
37
- constructor(defaults) {
38
- super(defaults);
39
- if (!this.primaryKey) {
40
- if (this.parent)
41
- this.primaryKey = this.parent.primaryKey;
42
- else {
43
- this.primaryKey = "id";
44
- this.autoIncrement = true;
45
- }
46
- }
37
+ findField(name) {
38
+ return this.attributes.filter(_ => _.fieldName === name)[0];
47
39
  }
48
40
  }
49
41
  exports.Table = Table;
50
42
  class DB {
51
43
  constructor(log) {
52
- this.tables = {};
53
- this.tablesArr = [];
44
+ this.tables = [];
54
45
  this.log = log;
55
46
  }
56
- addTable(table) {
57
- this.tables[table.tableName] = table;
58
- this.tablesArr.push(table);
47
+ findTable(name) {
48
+ return this.tables.filter(_ => _.tableName === name)[0];
59
49
  }
60
50
  indexesEq(a, b) {
61
51
  if (a.fields.length !== b.fields.length)
@@ -69,9 +59,9 @@ class DB {
69
59
  return false;
70
60
  return true;
71
61
  }
72
- async sync() {
73
- for (const i in this.tablesArr) {
74
- const table = this.tablesArr[i];
62
+ async syncDataBase() {
63
+ for (const table of this.tables) {
64
+ this.sync = table.sync;
75
65
  await this.syncTable(table);
76
66
  const indexes = await this.dropConstraints(table);
77
67
  await this.dropIndexes(table, indexes);
@@ -82,5 +72,9 @@ class DB {
82
72
  await this.syncIndexes(table);
83
73
  }
84
74
  }
75
+ syncLog(...data) {
76
+ const args = this.sync ? data : ["NOT SYNCING:", ...data];
77
+ this.log(...args);
78
+ }
85
79
  }
86
80
  exports.DB = DB;
package/lib/minidb.js CHANGED
@@ -21,40 +21,47 @@ class MiniDB extends db_1.DB {
21
21
  }
22
22
  }
23
23
  async dropConstraints(table) {
24
- const { constraints } = this.body.tables[table.tableName];
24
+ const { constraints } = this.body.tables[table.tableName] || { constraints: { f: {}, u: {} } };
25
25
  for (const constraint of Object.keys(constraints.f).sort()) {
26
- if (!table.constraints.filter(({ constraintName, type }) => constraintName === constraint && type === "f").length) {
27
- this.log(`'${table.tableName}': Removing foreign key: '${constraint}'`);
28
- delete constraints.f[constraint];
26
+ const arr = table.constraints.filter(({ constraintName, type }) => constraintName === constraint && type === "f");
27
+ const dbOptions = arr.length ? arr[0].attribute.foreignKey.options : { onDelete: "delete", onUpdate: "delete" };
28
+ const inOptions = constraints.f[constraint].options;
29
+ if (dbOptions.onDelete !== inOptions.onDelete || dbOptions.onUpdate !== inOptions.onUpdate) {
30
+ this.syncLog(`'${table.tableName}': Removing foreign key: '${constraint}'`);
31
+ if (this.sync)
32
+ delete constraints.f[constraint];
29
33
  }
30
34
  }
31
35
  for (const constraint of Object.keys(constraints.u).sort()) {
32
36
  if (!table.constraints.filter(({ constraintName, type }) => constraintName === constraint && type === "u").length) {
33
- this.log(`'${table.tableName}': Removing unique constraint from field: '${constraints.u[constraint].fieldName}'`);
34
- delete constraints.u[constraint];
37
+ this.syncLog(`'${table.tableName}': Removing unique constraint from field: '${constraints.u[constraint].on}'`);
38
+ if (this.sync)
39
+ delete constraints.u[constraint];
35
40
  }
36
41
  }
37
42
  await this.save();
38
43
  return [];
39
44
  }
40
45
  async dropFields(table) {
41
- const { fields } = this.body.tables[table.tableName];
42
- for (const attribute in fields) {
43
- if (table.attributes.filter(f => f.fieldName === attribute).length === 0) {
44
- this.log(`'${table.tableName}': Removing field: '${attribute}'`);
45
- delete fields[attribute];
46
+ const { fields } = this.body.tables[table.tableName] || { fields: {} };
47
+ for (const attribute of Object.keys(fields).sort()) {
48
+ if (!table.findField(attribute)) {
49
+ this.syncLog(`'${table.tableName}': Removing field: '${attribute}'`);
50
+ if (this.sync)
51
+ delete fields[attribute];
46
52
  }
47
53
  }
48
54
  await this.save();
49
55
  }
50
56
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
51
57
  async dropIndexes(table, constraintIndexes) {
52
- const { indexes } = this.body.tables[table.tableName];
53
- for (const name in indexes) {
58
+ const { indexes } = this.body.tables[table.tableName] || { indexes: {} };
59
+ for (const name of Object.keys(indexes).sort()) {
54
60
  const index = table.indexes.filter(_ => _.indexName === name);
55
61
  if (index.length === 0 || !this.indexesEq(indexes[name], index[0])) {
56
- this.log(`'${table.tableName}': Removing index: '${name}'`);
57
- delete indexes[name];
62
+ this.syncLog(`'${table.tableName}': Removing index: '${name}'`);
63
+ if (this.sync)
64
+ delete indexes[name];
58
65
  }
59
66
  }
60
67
  await this.save();
@@ -64,72 +71,84 @@ class MiniDB extends db_1.DB {
64
71
  await writeFile(this.file, JSON.stringify(this.body));
65
72
  }
66
73
  async syncConstraints(table) {
67
- const { constraints } = this.body.tables[table.tableName];
74
+ const { constraints } = this.body.tables[table.tableName] || { constraints: { f: {}, u: {} } };
68
75
  for (const constraint of table.constraints) {
69
- const { constraintName, type } = constraint;
70
- const { fieldName, foreignKey } = constraint.attribute;
76
+ const { attribute, constraintName, type } = constraint;
71
77
  if (!constraints[type][constraintName]) {
72
- switch (type) {
73
- case "f":
74
- this.log(`'${table.tableName}': Adding foreign key '${constraint.constraintName}' on field: '${fieldName}' references '${foreignKey.tableName}(${foreignKey.fieldName})'`);
75
- constraints[type][constraintName] = { fieldName, toField: foreignKey.fieldName, toTable: foreignKey.tableName };
76
- break;
77
- case "u":
78
- this.log(`'${table.tableName}': Adding unique constraint on field: '${fieldName}'`);
79
- constraints[type][constraintName] = { fieldName };
80
- break;
78
+ if (type === "f") {
79
+ const { fieldName, options, tableName } = attribute.foreignKey;
80
+ const onDelete = options.onDelete !== "no action" ? ` on delete ${options.onDelete}` : "";
81
+ const onUpdate = options.onUpdate !== "no action" ? ` on update ${options.onUpdate}` : "";
82
+ this.syncLog(`'${table.tableName}': Adding foreign key '${constraint.constraintName}' on field: '${attribute.fieldName}' references '${tableName}(${fieldName})'${onDelete}${onUpdate}`);
83
+ if (this.sync)
84
+ constraints[type][constraintName] = { on: attribute.fieldName, options, fieldName, tableName };
85
+ }
86
+ else {
87
+ this.syncLog(`'${table.tableName}': Adding unique constraint on field: '${attribute.fieldName}'`);
88
+ if (this.sync)
89
+ constraints[type][constraintName] = { on: attribute.fieldName };
81
90
  }
82
91
  }
83
92
  }
84
93
  await this.save();
85
94
  }
86
95
  async syncIndexes(table) {
87
- const { indexes } = this.body.tables[table.tableName];
96
+ const { indexes } = this.body.tables[table.tableName] || { indexes: {} };
88
97
  for (const index of table.indexes) {
89
98
  const { indexName } = index;
90
99
  if (!(indexName in indexes)) {
91
- this.log(`'${table.tableName}': Adding index: '${indexName}' on (${index.fields.map(_ => `'${_}'`).join(", ")}) type '${index.type}'${index.unique ? " unique" : ""}`);
92
- indexes[indexName] = index;
100
+ this.syncLog(`'${table.tableName}': Adding index: '${indexName}' on (${index.fields.map(_ => `'${_}'`).join(", ")}) type '${index.type}'${index.unique ? " unique" : ""}`);
101
+ if (this.sync)
102
+ indexes[indexName] = index;
93
103
  }
94
104
  }
95
105
  await this.save();
96
106
  }
97
107
  async syncFields(table) {
98
108
  for (const attribute of table.attributes) {
99
- const { fields } = this.body.tables[table.tableName];
109
+ const { fields } = this.body.tables[table.tableName] || { fields: {} };
100
110
  const { defaultValue, fieldName, notNull, size, type } = attribute;
101
111
  let field = fields[fieldName];
102
112
  if (!field) {
103
- this.log(`'${table.tableName}': Adding field: '${fieldName}' '${type}' '${size || ""}'`);
104
- field = fields[fieldName] = { size, type };
113
+ this.syncLog(`'${table.tableName}': Adding field: '${fieldName}' '${type}' '${size || ""}'`);
114
+ if (this.sync)
115
+ field = fields[fieldName] = { size, type };
116
+ else
117
+ field = {};
105
118
  }
106
119
  if (field.size !== size || field.type !== type) {
107
- this.log(`'${table.tableName}': Changing field type: '${fieldName}' '${type}' '${size || ""}'`);
108
- field = fields[fieldName] = Object.assign(Object.assign({}, field), { size, type });
120
+ this.syncLog(`'${table.tableName}': Changing field type: '${fieldName}' '${type}' '${size || ""}'`);
121
+ if (this.sync)
122
+ field = fields[fieldName] = Object.assign(Object.assign({}, field), { size, type });
109
123
  }
110
124
  if (field.default) {
111
125
  if (!defaultValue) {
112
- this.log(`'${table.tableName}': Dropping default value for field: '${fieldName}'`);
113
- delete field.default;
126
+ this.syncLog(`'${table.tableName}': Dropping default value for field: '${fieldName}'`);
127
+ if (this.sync)
128
+ delete field.default;
114
129
  }
115
130
  else if (field.default !== defaultValue) {
116
- this.log(`'${table.tableName}': Changing default value to '${defaultValue}' for field: '${fieldName}'`);
117
- field.default = defaultValue;
131
+ this.syncLog(`'${table.tableName}': Changing default value to '${defaultValue}' for field: '${fieldName}'`);
132
+ if (this.sync)
133
+ field.default = defaultValue;
118
134
  }
119
135
  }
120
136
  else if (defaultValue) {
121
- this.log(`'${table.tableName}': Setting default value '${defaultValue instanceof Date ? defaultValue.toISOString() : defaultValue}' for field: '${fieldName}'`);
122
- field.default = defaultValue;
137
+ this.syncLog(`'${table.tableName}': Setting default value '${defaultValue instanceof Date ? defaultValue.toISOString() : defaultValue}' for field: '${fieldName}'`);
138
+ if (this.sync)
139
+ field.default = defaultValue;
123
140
  }
124
141
  if (field.notNull) {
125
142
  if (!notNull) {
126
- this.log(`'${table.tableName}': Dropping not null for field: '${fieldName}'`);
127
- delete field.notNull;
143
+ this.syncLog(`'${table.tableName}': Dropping not null for field: '${fieldName}'`);
144
+ if (this.sync)
145
+ delete field.notNull;
128
146
  }
129
147
  }
130
148
  else if (notNull) {
131
- this.log(`'${table.tableName}': Setting not null for field: '${fieldName}'`);
132
- field.notNull = true;
149
+ this.syncLog(`'${table.tableName}': Setting not null for field: '${fieldName}'`);
150
+ if (this.sync)
151
+ field.notNull = true;
133
152
  }
134
153
  }
135
154
  await this.save();
@@ -145,20 +164,24 @@ class MiniDB extends db_1.DB {
145
164
  }
146
165
  else if (!this.body.tables[table.tableName].parent)
147
166
  return;
148
- this.log(`Removing table: '${table.tableName}'`);
149
- delete this.body.tables[table.tableName];
167
+ this.syncLog(`Removing table: '${table.tableName}'`);
168
+ if (this.sync)
169
+ delete this.body.tables[table.tableName];
150
170
  })();
151
171
  }
152
172
  if (!this.body.tables[table.tableName]) {
153
- this.log(`Adding table: '${table.tableName}'`);
154
- this.body.tables[table.tableName] = { constraints: { f: {}, u: {} }, fields: {}, indexes: {} };
173
+ this.syncLog(`Adding table: '${table.tableName}'`);
174
+ if (this.sync)
175
+ this.body.tables[table.tableName] = { constraints: { f: {}, u: {} }, fields: {}, indexes: {} };
155
176
  if (table.parent) {
156
- this.log(`Setting parent: '${table.parent.tableName}' - to table: '${table.tableName}'`);
157
- this.body.tables[table.tableName].parent = table.parent.tableName;
177
+ this.syncLog(`Setting parent: '${table.parent.tableName}' - to table: '${table.tableName}'`);
178
+ if (this.sync)
179
+ this.body.tables[table.tableName].parent = table.parent.tableName;
158
180
  }
159
181
  if (table.autoIncrement && !this.body.next[table.tableName]) {
160
- this.log(`Setting auto increment: '${table.tableName}'`);
161
- this.body.next[table.tableName] = 1;
182
+ this.syncLog(`Setting auto increment: '${table.tableName}'`);
183
+ if (this.sync)
184
+ this.body.next[table.tableName] = 1;
162
185
  }
163
186
  }
164
187
  await this.save();
package/package.json CHANGED
@@ -5,21 +5,24 @@
5
5
  "description": "The ORM which never needs to migrate",
6
6
  "devDependencies": {
7
7
  "@types/mocha": "9.0.0",
8
- "@types/node": "16.11.7",
8
+ "@types/node": "16.11.10",
9
9
  "@types/yamljs": "0.2.31",
10
- "@typescript-eslint/eslint-plugin": "5.3.1",
11
- "@typescript-eslint/parser": "5.3.1",
12
- "eslint": "8.2.0",
10
+ "@typescript-eslint/eslint-plugin": "5.4.0",
11
+ "@typescript-eslint/parser": "5.4.0",
12
+ "eslint": "8.3.0",
13
13
  "mocha": "9.1.3",
14
14
  "nyc": "15.1.0",
15
- "prettier": "2.4.1",
15
+ "prettier": "2.5.0",
16
16
  "ts-node": "10.4.0",
17
- "typescript": "4.4.4",
17
+ "typescript": "4.5.2",
18
18
  "yamljs": "0.3.0"
19
19
  },
20
20
  "engines": {
21
21
  "node": ">=12.0"
22
22
  },
23
+ "funding": {
24
+ "url": "https://blockchain.info/address/1Md9WFAHrXTb3yPBwQWmUfv2RmzrtbHioB"
25
+ },
23
26
  "homepage": "https://github.com/iccicci/sedentary#readme",
24
27
  "keywords": [
25
28
  "DB",
@@ -53,5 +56,5 @@
53
56
  "version": "node -r ts-node/register utils.ts version"
54
57
  },
55
58
  "types": "index.d.ts",
56
- "version": "0.0.14"
59
+ "version": "0.0.18"
57
60
  }
@@ -0,0 +1,2 @@
1
+ sphinx
2
+ sphinx_rtd_theme
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2017 Daniele Ricci
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
@@ -1,32 +0,0 @@
1
- # sedentary-pg
2
-
3
- [![Build Status][travis-badge]][travis-url]
4
- [![Code Climate][code-badge]][code-url]
5
- [![Test Coverage][cover-badge]][code-url]
6
-
7
- [![NPM version][npm-badge]][npm-url]
8
- [![NPM downloads][npm-downloads-badge]][npm-url]
9
- [![Stars][stars-badge]][github-url]
10
-
11
- [![Types][types-badge]][npm-url]
12
- [![Dependents][deps-badge]][npm-url]
13
- [![Donate][donate-badge]][donate-url]
14
-
15
- [code-badge]: https://codeclimate.com/github/iccicci/sedentary-pg/badges/gpa.svg
16
- [code-url]: https://codeclimate.com/github/iccicci/sedentary-pg
17
- [cover-badge]: https://codeclimate.com/github/iccicci/sedentary-pg/badges/coverage.svg
18
- [deps-badge]: https://badgen.net/npm/dependents/sedentary-pg?icon=npm&cache=300
19
- [donate-badge]: https://badgen.net/badge/donate/bitcoin?icon=bitcoin&cache=300
20
- [donate-url]: https://blockchain.info/address/1Md9WFAHrXTb3yPBwQWmUfv2RmzrtbHioB
21
- [github-url]: https://github.com/iccicci/sedentary-pg
22
- [npm-downloads-badge]: https://badgen.net/npm/dw/sedentary-pg?icon=npm&cache=300
23
- [npm-badge]: https://badgen.net/npm/v/sedentary-pg?color=green&icon=npm&cache=300
24
- [npm-url]: https://www.npmjs.com/package/sedentary-pg
25
- [stars-badge]: https://badgen.net/github/stars/iccicci/sedentary-pg?icon=github&cache=300
26
- [travis-badge]: https://badgen.net/travis/iccicci/sedentary-pg?icon=travis&cache=300
27
- [travis-url]: https://app.travis-ci.com/github/iccicci/sedentary-pg
28
- [types-badge]: https://badgen.net/npm/types/sedentary-pg?color=green&icon=typescript&cache=300
29
-
30
- # under development
31
-
32
- https://bucardo.org/postgres_all_versions.html