sedentary 0.0.16 → 0.0.20

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/minidb.js CHANGED
@@ -16,45 +16,53 @@ class MiniDB extends db_1.DB {
16
16
  this.body = JSON.parse((await readFile(this.file)).toString());
17
17
  }
18
18
  catch (e) {
19
- if (e.code !== "ENOENT")
19
+ const err = e;
20
+ if (err.code !== "ENOENT")
20
21
  throw e;
21
22
  }
22
23
  }
23
24
  async dropConstraints(table) {
24
- const { constraints } = this.body.tables[table.tableName];
25
+ const { constraints } = this.body.tables[table.tableName] || { constraints: { f: {}, u: {} } };
25
26
  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];
27
+ const arr = table.constraints.filter(({ constraintName, type }) => constraintName === constraint && type === "f");
28
+ const dbOptions = arr.length ? arr[0].attribute.foreignKey.options : { onDelete: "delete", onUpdate: "delete" };
29
+ const inOptions = constraints.f[constraint].options;
30
+ if (dbOptions.onDelete !== inOptions.onDelete || dbOptions.onUpdate !== inOptions.onUpdate) {
31
+ this.syncLog(`'${table.tableName}': Removing foreign key: '${constraint}'`);
32
+ if (this.sync)
33
+ delete constraints.f[constraint];
29
34
  }
30
35
  }
31
36
  for (const constraint of Object.keys(constraints.u).sort()) {
32
37
  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];
38
+ this.syncLog(`'${table.tableName}': Removing unique constraint from field: '${constraints.u[constraint].on}'`);
39
+ if (this.sync)
40
+ delete constraints.u[constraint];
35
41
  }
36
42
  }
37
43
  await this.save();
38
44
  return [];
39
45
  }
40
46
  async dropFields(table) {
41
- const { fields } = this.body.tables[table.tableName];
42
- for (const attribute in fields) {
47
+ const { fields } = this.body.tables[table.tableName] || { fields: {} };
48
+ for (const attribute of Object.keys(fields).sort()) {
43
49
  if (!table.findField(attribute)) {
44
- this.log(`'${table.tableName}': Removing field: '${attribute}'`);
45
- delete fields[attribute];
50
+ this.syncLog(`'${table.tableName}': Removing field: '${attribute}'`);
51
+ if (this.sync)
52
+ delete fields[attribute];
46
53
  }
47
54
  }
48
55
  await this.save();
49
56
  }
50
57
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
51
58
  async dropIndexes(table, constraintIndexes) {
52
- const { indexes } = this.body.tables[table.tableName];
53
- for (const name in indexes) {
59
+ const { indexes } = this.body.tables[table.tableName] || { indexes: {} };
60
+ for (const name of Object.keys(indexes).sort()) {
54
61
  const index = table.indexes.filter(_ => _.indexName === name);
55
62
  if (index.length === 0 || !this.indexesEq(indexes[name], index[0])) {
56
- this.log(`'${table.tableName}': Removing index: '${name}'`);
57
- delete indexes[name];
63
+ this.syncLog(`'${table.tableName}': Removing index: '${name}'`);
64
+ if (this.sync)
65
+ delete indexes[name];
58
66
  }
59
67
  }
60
68
  await this.save();
@@ -64,72 +72,84 @@ class MiniDB extends db_1.DB {
64
72
  await writeFile(this.file, JSON.stringify(this.body));
65
73
  }
66
74
  async syncConstraints(table) {
67
- const { constraints } = this.body.tables[table.tableName];
75
+ const { constraints } = this.body.tables[table.tableName] || { constraints: { f: {}, u: {} } };
68
76
  for (const constraint of table.constraints) {
69
- const { constraintName, type } = constraint;
70
- const { fieldName, foreignKey } = constraint.attribute;
77
+ const { attribute, constraintName, type } = constraint;
71
78
  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;
79
+ if (type === "f") {
80
+ const { fieldName, options, tableName } = attribute.foreignKey;
81
+ const onDelete = options.onDelete !== "no action" ? ` on delete ${options.onDelete}` : "";
82
+ const onUpdate = options.onUpdate !== "no action" ? ` on update ${options.onUpdate}` : "";
83
+ this.syncLog(`'${table.tableName}': Adding foreign key '${constraint.constraintName}' on field: '${attribute.fieldName}' references '${tableName}(${fieldName})'${onDelete}${onUpdate}`);
84
+ if (this.sync)
85
+ constraints[type][constraintName] = { on: attribute.fieldName, options, fieldName, tableName };
86
+ }
87
+ else {
88
+ this.syncLog(`'${table.tableName}': Adding unique constraint on field: '${attribute.fieldName}'`);
89
+ if (this.sync)
90
+ constraints[type][constraintName] = { on: attribute.fieldName };
81
91
  }
82
92
  }
83
93
  }
84
94
  await this.save();
85
95
  }
86
96
  async syncIndexes(table) {
87
- const { indexes } = this.body.tables[table.tableName];
97
+ const { indexes } = this.body.tables[table.tableName] || { indexes: {} };
88
98
  for (const index of table.indexes) {
89
99
  const { indexName } = index;
90
100
  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;
101
+ this.syncLog(`'${table.tableName}': Adding index: '${indexName}' on (${index.fields.map(_ => `'${_}'`).join(", ")}) type '${index.type}'${index.unique ? " unique" : ""}`);
102
+ if (this.sync)
103
+ indexes[indexName] = index;
93
104
  }
94
105
  }
95
106
  await this.save();
96
107
  }
97
108
  async syncFields(table) {
98
109
  for (const attribute of table.attributes) {
99
- const { fields } = this.body.tables[table.tableName];
110
+ const { fields } = this.body.tables[table.tableName] || { fields: {} };
100
111
  const { defaultValue, fieldName, notNull, size, type } = attribute;
101
112
  let field = fields[fieldName];
102
113
  if (!field) {
103
- this.log(`'${table.tableName}': Adding field: '${fieldName}' '${type}' '${size || ""}'`);
104
- field = fields[fieldName] = { size, type };
114
+ this.syncLog(`'${table.tableName}': Adding field: '${fieldName}' '${type}' '${size || ""}'`);
115
+ if (this.sync)
116
+ field = fields[fieldName] = { size, type };
117
+ else
118
+ field = {};
105
119
  }
106
120
  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 });
121
+ this.syncLog(`'${table.tableName}': Changing field type: '${fieldName}' '${type}' '${size || ""}'`);
122
+ if (this.sync)
123
+ field = fields[fieldName] = Object.assign(Object.assign({}, field), { size, type });
109
124
  }
110
125
  if (field.default) {
111
126
  if (!defaultValue) {
112
- this.log(`'${table.tableName}': Dropping default value for field: '${fieldName}'`);
113
- delete field.default;
127
+ this.syncLog(`'${table.tableName}': Dropping default value for field: '${fieldName}'`);
128
+ if (this.sync)
129
+ delete field.default;
114
130
  }
115
131
  else if (field.default !== defaultValue) {
116
- this.log(`'${table.tableName}': Changing default value to '${defaultValue}' for field: '${fieldName}'`);
117
- field.default = defaultValue;
132
+ this.syncLog(`'${table.tableName}': Changing default value to '${defaultValue}' for field: '${fieldName}'`);
133
+ if (this.sync)
134
+ field.default = defaultValue;
118
135
  }
119
136
  }
120
137
  else if (defaultValue) {
121
- this.log(`'${table.tableName}': Setting default value '${defaultValue instanceof Date ? defaultValue.toISOString() : defaultValue}' for field: '${fieldName}'`);
122
- field.default = defaultValue;
138
+ this.syncLog(`'${table.tableName}': Setting default value '${defaultValue instanceof Date ? defaultValue.toISOString() : defaultValue}' for field: '${fieldName}'`);
139
+ if (this.sync)
140
+ field.default = defaultValue;
123
141
  }
124
142
  if (field.notNull) {
125
143
  if (!notNull) {
126
- this.log(`'${table.tableName}': Dropping not null for field: '${fieldName}'`);
127
- delete field.notNull;
144
+ this.syncLog(`'${table.tableName}': Dropping not null for field: '${fieldName}'`);
145
+ if (this.sync)
146
+ delete field.notNull;
128
147
  }
129
148
  }
130
149
  else if (notNull) {
131
- this.log(`'${table.tableName}': Setting not null for field: '${fieldName}'`);
132
- field.notNull = true;
150
+ this.syncLog(`'${table.tableName}': Setting not null for field: '${fieldName}'`);
151
+ if (this.sync)
152
+ field.notNull = true;
133
153
  }
134
154
  }
135
155
  await this.save();
@@ -145,20 +165,24 @@ class MiniDB extends db_1.DB {
145
165
  }
146
166
  else if (!this.body.tables[table.tableName].parent)
147
167
  return;
148
- this.log(`Removing table: '${table.tableName}'`);
149
- delete this.body.tables[table.tableName];
168
+ this.syncLog(`Removing table: '${table.tableName}'`);
169
+ if (this.sync)
170
+ delete this.body.tables[table.tableName];
150
171
  })();
151
172
  }
152
173
  if (!this.body.tables[table.tableName]) {
153
- this.log(`Adding table: '${table.tableName}'`);
154
- this.body.tables[table.tableName] = { constraints: { f: {}, u: {} }, fields: {}, indexes: {} };
174
+ this.syncLog(`Adding table: '${table.tableName}'`);
175
+ if (this.sync)
176
+ this.body.tables[table.tableName] = { constraints: { f: {}, u: {} }, fields: {}, indexes: {} };
155
177
  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;
178
+ this.syncLog(`Setting parent: '${table.parent.tableName}' - to table: '${table.tableName}'`);
179
+ if (this.sync)
180
+ this.body.tables[table.tableName].parent = table.parent.tableName;
158
181
  }
159
182
  if (table.autoIncrement && !this.body.next[table.tableName]) {
160
- this.log(`Setting auto increment: '${table.tableName}'`);
161
- this.body.next[table.tableName] = 1;
183
+ this.syncLog(`Setting auto increment: '${table.tableName}'`);
184
+ if (this.sync)
185
+ this.body.next[table.tableName] = 1;
162
186
  }
163
187
  }
164
188
  await this.save();
@@ -0,0 +1,2 @@
1
+ export declare class Transaction {
2
+ }
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Transaction = void 0;
4
+ class Transaction {
5
+ }
6
+ exports.Transaction = Transaction;
package/package.json CHANGED
@@ -5,16 +5,17 @@
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.10",
8
+ "@types/node": "17.0.5",
9
9
  "@types/yamljs": "0.2.31",
10
- "@typescript-eslint/eslint-plugin": "5.4.0",
11
- "@typescript-eslint/parser": "5.4.0",
12
- "eslint": "8.3.0",
10
+ "@typescript-eslint/eslint-plugin": "5.8.1",
11
+ "@typescript-eslint/parser": "5.8.1",
12
+ "eslint": "8.5.0",
13
13
  "mocha": "9.1.3",
14
14
  "nyc": "15.1.0",
15
- "prettier": "2.5.0",
15
+ "prettier": "2.5.1",
16
16
  "ts-node": "10.4.0",
17
- "typescript": "4.5.2",
17
+ "tsd": "0.19.0",
18
+ "typescript": "4.5.4",
18
19
  "yamljs": "0.3.0"
19
20
  },
20
21
  "engines": {
@@ -55,6 +56,23 @@
55
56
  "tsc": "tsc --declaration",
56
57
  "version": "node -r ts-node/register utils.ts version"
57
58
  },
59
+ "tsd": {
60
+ "compilerOptions": {
61
+ "alwaysStrict": true,
62
+ "declaration": true,
63
+ "esModuleInterop": true,
64
+ "module": "commonjs",
65
+ "noImplicitAny": true,
66
+ "noImplicitReturns": true,
67
+ "noImplicitThis": true,
68
+ "strict": true,
69
+ "strictBindCallApply": true,
70
+ "strictFunctionTypes": true,
71
+ "strictNullChecks": true,
72
+ "strictPropertyInitialization": true,
73
+ "target": "es2017"
74
+ }
75
+ },
58
76
  "types": "index.d.ts",
59
- "version": "0.0.16"
77
+ "version": "0.0.20"
60
78
  }
package/utils.d.ts ADDED
@@ -0,0 +1 @@
1
+ export {};
package/utils.js ADDED
@@ -0,0 +1,130 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const yamljs_1 = __importDefault(require("yamljs"));
7
+ const fs_1 = require("fs");
8
+ const { readFile, writeFile } = fs_1.promises;
9
+ const { VERSION, npm_package_name } = process.env;
10
+ const common = ["*.tgz", "coverage", "node_modules", "test.json", ""];
11
+ const git = [".gitignore", ".npmignore", ".nyc_output", "docs/build", "docs/__pycache__", "index.d.ts", "index.js", "lib/*.d.ts", "lib/*.js"];
12
+ const npm = [".*", "Makefile", "docs", "index.ts", "lib/db.ts", "lib/minidb.ts", "lib/transaction.ts", "sedentary-*", "test", "tsconfig.json", "utils.ts"];
13
+ const descriptions = { sedentary: "", "sedentary-mysql": " - MySQL", "sedentary-pg": " - PostgreSQL", "sedentary-sqlite": " - SQLite" };
14
+ const urls = { sedentary: "", "sedentary-mysql": "-mysql", "sedentary-pg": "-pg", "sedentary-sqlite": "-sqlite" };
15
+ const deps = { sedentary: {}, "sedentary-mysql": {}, "sedentary-pg": { pg: "8.7.1", "pg-format": "1.0.4" }, "sedentary-sqlite": {} };
16
+ const devd = { sedentary: {}, "sedentary-mysql": {}, "sedentary-pg": { "@types/pg": "8.6.3", "@types/pg-format": "1.0.2" }, "sedentary-sqlite": {} };
17
+ const packagejson = {
18
+ author: "Daniele Ricci <daniele.icc@gmail.com> (https://github.com/iccicci)",
19
+ dependencies: {},
20
+ devDependencies: {
21
+ "@types/mocha": "9.0.0",
22
+ "@types/node": "17.0.5",
23
+ "@types/yamljs": "0.2.31",
24
+ "@typescript-eslint/eslint-plugin": "5.8.1",
25
+ "@typescript-eslint/parser": "5.8.1",
26
+ eslint: "8.5.0",
27
+ mocha: "9.1.3",
28
+ prettier: "2.5.1",
29
+ nyc: "15.1.0",
30
+ "ts-node": "10.4.0",
31
+ tsd: "0.19.0",
32
+ typescript: "4.5.4",
33
+ yamljs: "0.3.0"
34
+ },
35
+ engines: { node: ">=12.0" },
36
+ funding: { url: "https://blockchain.info/address/1Md9WFAHrXTb3yPBwQWmUfv2RmzrtbHioB" },
37
+ keywords: ["DB", "ORM", "database", "migration", "mysql", "postgresql", "sqlite"],
38
+ license: "MIT",
39
+ prettier: {
40
+ arrowParens: "avoid",
41
+ endOfLine: "lf",
42
+ jsxBracketSameLine: true,
43
+ printWidth: 200,
44
+ trailingComma: "none",
45
+ useTabs: false
46
+ },
47
+ readmeFilename: "README.md",
48
+ scripts: {
49
+ coverage: "nyc -r lcov -r text -r text-summary -r html mocha -r ts-node/register test/*ts",
50
+ gitignore: "node -r ts-node/register utils.ts gitignore",
51
+ npmignore: "node -r ts-node/register utils.ts npmignore",
52
+ packagejson: "node -r ts-node/register utils.ts packagejson",
53
+ test: "mocha -r ts-node/register test/*ts",
54
+ travis: "node -r ts-node/register utils.ts travis",
55
+ tsc: "tsc --declaration",
56
+ version: "node -r ts-node/register utils.ts version"
57
+ },
58
+ tsd: { compilerOptions: {} },
59
+ types: "index.d.ts"
60
+ };
61
+ const before_script_common = [
62
+ "curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter",
63
+ "chmod +x ./cc-test-reporter",
64
+ "./cc-test-reporter before-build"
65
+ ];
66
+ const conditions = { sedentary: "", "sedentary-pg": "&& $PG_VERSION == 14 " };
67
+ const travis = {
68
+ common: {
69
+ after_script: [`if [[ \`node --version\` =~ ^v16 ${conditions[npm_package_name]}]] ; then ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT ; fi`],
70
+ before_script: before_script_common,
71
+ language: "node_js",
72
+ node_js: ["16", "14", "12"],
73
+ script: "npm run coverage",
74
+ sudo: "required"
75
+ },
76
+ sedentary: { env: { global: ["CC_TEST_REPORTER_ID=1aa1f737e7bf7d2859a2c7d9a0d9634a0d9aa89e3a19476d576faa7d02a1d46f"] } },
77
+ "sedentary-pg": {
78
+ before_install: ["sudo service postgresql stop", "sudo service postgresql restart $PG_VERSION"],
79
+ before_script: [
80
+ ...before_script_common,
81
+ 'psql -c "CREATE DATABASE sedentary;" -U postgres',
82
+ "psql -c \"ALTER DATABASE sedentary SET timezone TO 'GMT';\" -U postgres",
83
+ 'export SPG=\'{"database":"sedentary","password":"postgres","user":"postgres"}\''
84
+ ],
85
+ env: {
86
+ global: ["CC_TEST_REPORTER_ID=c7519657dfea145349c1b7a98f7134f033c25f598b40ad5b077744eb4beb7c66"],
87
+ matrix: ["PG_VERSION=14", "PG_VERSION=13", "PG_VERSION=12", "PG_VERSION=11", "PG_VERSION=10"]
88
+ }
89
+ }
90
+ };
91
+ function sort(obj) {
92
+ const ret = {};
93
+ if (obj instanceof Array || !(obj instanceof Object))
94
+ return obj;
95
+ Object.entries(obj)
96
+ .sort(([a], [b]) => (a < b ? -1 : 1))
97
+ .forEach(([k, v]) => (ret[k] = sort(v)));
98
+ return ret;
99
+ }
100
+ (async function () {
101
+ if (process.argv[2] === "gitignore")
102
+ writeFile(".gitignore", [...git, ...common].join("\n"), "utf-8");
103
+ if (process.argv[2] === "npmignore")
104
+ writeFile(".npmignore", [...npm, ...common].join("\n"), "utf-8");
105
+ if (process.argv[2] === "travis")
106
+ writeFile(".travis.yml", yamljs_1.default.stringify(sort(Object.assign(Object.assign({}, travis.common), travis[npm_package_name])), 4, 2), "utf-8");
107
+ if (process.argv[2] === "packagejson") {
108
+ const bugs = `https://github.com/iccicci/sedentary${urls[npm_package_name]}/issues`;
109
+ const description = "The ORM which never needs to migrate" + descriptions[npm_package_name];
110
+ const homepage = `https://github.com/iccicci/sedentary${urls[npm_package_name]}#readme`;
111
+ const repository = `https://github.com/iccicci/sedentary${urls[npm_package_name]}`;
112
+ const pkg = JSON.parse(await readFile("package.json", "utf-8"));
113
+ const { compilerOptions } = JSON.parse(await readFile("tsconfig.json", "utf-8"));
114
+ const { name, version } = pkg;
115
+ const tsd = { compilerOptions };
116
+ let { dependencies, devDependencies } = pkg;
117
+ try {
118
+ const { version } = JSON.parse(await readFile("../package.json", "utf-8"));
119
+ dependencies = Object.assign(Object.assign({}, deps[npm_package_name]), { sedentary: version });
120
+ devDependencies = Object.assign(Object.assign({}, devDependencies), devd[npm_package_name]);
121
+ }
122
+ catch (e) { }
123
+ await writeFile("package.json", JSON.stringify(sort(Object.assign(Object.assign({}, packagejson), { bugs, dependencies, devDependencies, description, homepage, name, repository, tsd, version })), null, 2), "utf-8");
124
+ }
125
+ if (process.argv[2] === "version") {
126
+ const pkg = JSON.parse(await readFile("package.json", "utf-8"));
127
+ pkg.version = VERSION;
128
+ await writeFile("package.json", JSON.stringify(pkg, null, 2), "utf-8");
129
+ }
130
+ })();
package/lib/log.d.ts DELETED
@@ -1,4 +0,0 @@
1
- export declare function createLogger(log: ((message: string) => void) | null | undefined): {
2
- (...data: any[]): void;
3
- (message?: any, ...optionalParams: any[]): void;
4
- };
package/lib/log.js DELETED
@@ -1,20 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createLogger = void 0;
4
- const console_1 = require("console");
5
- const stream_1 = require("stream");
6
- class Logger extends stream_1.Writable {
7
- constructor(log) {
8
- super();
9
- this.log = log;
10
- }
11
- _write(chunk, encoding, callback) {
12
- this.log(chunk.toString());
13
- callback();
14
- }
15
- }
16
- function createLogger(log) {
17
- // eslint-disable-next-line no-console
18
- return log ? new console_1.Console(new Logger(log)).log : log === null ? () => { } : console.log;
19
- }
20
- exports.createLogger = createLogger;
package/lib/log.ts DELETED
@@ -1,22 +0,0 @@
1
- import { Console } from "console";
2
- import { Writable } from "stream";
3
-
4
- class Logger extends Writable {
5
- private log: (message: string) => void;
6
-
7
- constructor(log: (message: string) => void) {
8
- super();
9
-
10
- this.log = log;
11
- }
12
-
13
- _write(chunk: Buffer, encoding: string, callback: () => void) {
14
- this.log(chunk.toString());
15
- callback();
16
- }
17
- }
18
-
19
- export function createLogger(log: ((message: string) => void) | null | undefined) {
20
- // eslint-disable-next-line no-console
21
- return log ? new Console(new Logger(log)).log : log === null ? () => {} : console.log;
22
- }