rake-db 1.3.2 → 2.0.0

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.
Files changed (72) hide show
  1. package/.env +3 -0
  2. package/.env.local +1 -0
  3. package/README.md +1 -545
  4. package/db.ts +16 -0
  5. package/dist/index.d.ts +94 -0
  6. package/dist/index.esm.js +190 -0
  7. package/dist/index.esm.js.map +1 -0
  8. package/dist/index.js +201 -0
  9. package/dist/index.js.map +1 -0
  10. package/jest-setup.ts +3 -0
  11. package/migrations/20221009210157_first.ts +8 -0
  12. package/migrations/20221009210200_second.ts +5 -0
  13. package/package.json +55 -41
  14. package/rollup.config.js +3 -0
  15. package/src/commands/createOrDrop.test.ts +145 -0
  16. package/src/commands/createOrDrop.ts +107 -0
  17. package/src/commands/generate.test.ts +133 -0
  18. package/src/commands/generate.ts +85 -0
  19. package/src/commands/migrateOrRollback.test.ts +118 -0
  20. package/src/commands/migrateOrRollback.ts +108 -0
  21. package/src/common.test.ts +281 -0
  22. package/src/common.ts +224 -0
  23. package/src/index.ts +2 -0
  24. package/src/migration/change.ts +20 -0
  25. package/src/migration/changeTable.test.ts +417 -0
  26. package/src/migration/changeTable.ts +375 -0
  27. package/src/migration/createTable.test.ts +269 -0
  28. package/src/migration/createTable.ts +169 -0
  29. package/src/migration/migration.test.ts +341 -0
  30. package/src/migration/migration.ts +296 -0
  31. package/src/migration/migrationUtils.ts +281 -0
  32. package/src/rakeDb.ts +29 -0
  33. package/src/test-utils.ts +45 -0
  34. package/tsconfig.json +12 -0
  35. package/dist/lib/createAndDrop.d.ts +0 -2
  36. package/dist/lib/createAndDrop.js +0 -63
  37. package/dist/lib/defaults.d.ts +0 -2
  38. package/dist/lib/defaults.js +0 -5
  39. package/dist/lib/errorCodes.d.ts +0 -4
  40. package/dist/lib/errorCodes.js +0 -7
  41. package/dist/lib/generate.d.ts +0 -1
  42. package/dist/lib/generate.js +0 -99
  43. package/dist/lib/help.d.ts +0 -2
  44. package/dist/lib/help.js +0 -24
  45. package/dist/lib/init.d.ts +0 -2
  46. package/dist/lib/init.js +0 -276
  47. package/dist/lib/migrate.d.ts +0 -4
  48. package/dist/lib/migrate.js +0 -189
  49. package/dist/lib/migration.d.ts +0 -37
  50. package/dist/lib/migration.js +0 -159
  51. package/dist/lib/schema/changeTable.d.ts +0 -23
  52. package/dist/lib/schema/changeTable.js +0 -109
  53. package/dist/lib/schema/column.d.ts +0 -31
  54. package/dist/lib/schema/column.js +0 -201
  55. package/dist/lib/schema/createTable.d.ts +0 -10
  56. package/dist/lib/schema/createTable.js +0 -53
  57. package/dist/lib/schema/foreignKey.d.ts +0 -11
  58. package/dist/lib/schema/foreignKey.js +0 -53
  59. package/dist/lib/schema/index.d.ts +0 -3
  60. package/dist/lib/schema/index.js +0 -54
  61. package/dist/lib/schema/primaryKey.d.ts +0 -9
  62. package/dist/lib/schema/primaryKey.js +0 -24
  63. package/dist/lib/schema/table.d.ts +0 -43
  64. package/dist/lib/schema/table.js +0 -110
  65. package/dist/lib/schema/timestamps.d.ts +0 -3
  66. package/dist/lib/schema/timestamps.js +0 -9
  67. package/dist/lib/utils.d.ts +0 -26
  68. package/dist/lib/utils.js +0 -114
  69. package/dist/rake-db.d.ts +0 -2
  70. package/dist/rake-db.js +0 -34
  71. package/dist/types.d.ts +0 -94
  72. package/dist/types.js +0 -40
package/dist/lib/utils.js DELETED
@@ -1,114 +0,0 @@
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
- exports.createSchemaMigrations = exports.askAdminCreds = exports.getConfig = exports.mkdirRecursive = exports.join = exports.noop = void 0;
7
- const fs_1 = __importDefault(require("fs"));
8
- const pg_adapter_1 = require("pg-adapter");
9
- const dotenv_1 = require("dotenv");
10
- const path_1 = __importDefault(require("path"));
11
- const defaults_1 = require("./defaults");
12
- const { Snippet } = require('enquirer');
13
- let camelCase = false;
14
- const noop = () => {
15
- // noop
16
- };
17
- exports.noop = noop;
18
- const join = (...args) => {
19
- if (camelCase)
20
- return (args[0] +
21
- args
22
- .slice(1)
23
- .map((word) => word[0].toUpperCase() + word.slice(1))
24
- .join(''));
25
- else
26
- return args.map((word) => word.toLowerCase()).join('_');
27
- };
28
- exports.join = join;
29
- const mkdirRecursive = (inputPath) => {
30
- if (fs_1.default.existsSync(inputPath))
31
- return;
32
- const basePath = path_1.default.dirname(inputPath);
33
- if (!fs_1.default.existsSync(basePath))
34
- exports.mkdirRecursive(basePath);
35
- fs_1.default.mkdirSync(inputPath);
36
- };
37
- exports.mkdirRecursive = mkdirRecursive;
38
- const getConfig = () => {
39
- dotenv_1.config();
40
- const camelCaseEnv = process.env.DATABASE_CAMEL_CASE;
41
- camelCase =
42
- camelCaseEnv === 'true'
43
- ? true
44
- : camelCaseEnv === 'false'
45
- ? false
46
- : defaults_1.defaultCamelCase;
47
- const migrationsPath = process.env.MIGRATIONS_PATH || defaults_1.defaultMigrationsPath;
48
- const databasesString = process.env.DATABASES;
49
- const databaseURL = process.env.DATABASE_URL;
50
- let databasesURLs;
51
- if (databasesString) {
52
- const databases = databasesString.split(',');
53
- const urls = databases.map((key) => process.env[key]);
54
- const absentIndex = urls.findIndex((url) => !url);
55
- if (absentIndex !== -1)
56
- throw new Error(`DATABASES env variable contains ${databases[absentIndex]}, but ${databases[absentIndex]} variable is not set`);
57
- databasesURLs = urls;
58
- }
59
- else if (databaseURL) {
60
- databasesURLs = [databaseURL];
61
- }
62
- else {
63
- throw new Error('One of DATABASES or DATABASE_URL env variable must be set');
64
- }
65
- return {
66
- camelCase,
67
- migrationsPath,
68
- configs: databasesURLs.map(pg_adapter_1.parseUrl),
69
- };
70
- };
71
- exports.getConfig = getConfig;
72
- const askAdminCreds = async ({ user, password } = {
73
- user: 'postgres',
74
- password: '',
75
- }) => {
76
- const adminCredsPrompt = new Snippet({
77
- message: `What are postgres admin login and password?`,
78
- fields: [
79
- {
80
- name: 'user',
81
- required: true,
82
- },
83
- {
84
- name: 'password',
85
- },
86
- ],
87
- values: {
88
- user,
89
- password,
90
- },
91
- template: 'Admin user: {{user}}\nAdmin password: {{password}}',
92
- });
93
- const { values } = await adminCredsPrompt.run();
94
- if (!values.password)
95
- values.password = '';
96
- return { user, password };
97
- };
98
- exports.askAdminCreds = askAdminCreds;
99
- const createSchemaMigrations = async (db) => {
100
- try {
101
- await db.connect();
102
- await db.exec('CREATE TABLE IF NOT EXISTS "schemaMigrations" ( version TEXT NOT NULL )');
103
- console.log('Created versions table');
104
- }
105
- catch (err) {
106
- if (err.code === '42P07') {
107
- console.log('Versions table exists');
108
- }
109
- else {
110
- throw err;
111
- }
112
- }
113
- };
114
- exports.createSchemaMigrations = createSchemaMigrations;
package/dist/rake-db.d.ts DELETED
@@ -1,2 +0,0 @@
1
- #!/usr/bin/env node
2
- export * from './types';
package/dist/rake-db.js DELETED
@@ -1,34 +0,0 @@
1
- #!/usr/bin/env node
2
- "use strict";
3
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
- if (k2 === undefined) k2 = k;
5
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
6
- }) : (function(o, m, k, k2) {
7
- if (k2 === undefined) k2 = k;
8
- o[k2] = m[k];
9
- }));
10
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
11
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
12
- };
13
- Object.defineProperty(exports, "__esModule", { value: true });
14
- __exportStar(require("./types"), exports);
15
- const args = process.argv.slice(2);
16
- let firstArg = args.shift();
17
- if (!firstArg)
18
- firstArg = 'help';
19
- const commandArray = firstArg.split(':');
20
- const command = commandArray[0];
21
- if (command === 'init')
22
- require('./lib/init').default();
23
- else if (command === 'create')
24
- require('./lib/createAndDrop').createDb(args);
25
- else if (command === 'drop')
26
- require('./lib/createAndDrop').dropDb(args);
27
- else if (command === 'generate' || command === 'g')
28
- require('./lib/generate').generate(...args);
29
- else if (command === 'migrate')
30
- require('./lib/migrate').migrate(args);
31
- else if (command === 'rollback')
32
- require('./lib/migrate').rollback(args);
33
- else
34
- require('./lib/help').default();
package/dist/types.d.ts DELETED
@@ -1,94 +0,0 @@
1
- import Migration from './lib/migration';
2
- import Table from './lib/schema/table';
3
- import { Value } from 'pg-adapter/dist/lib/quote';
4
- import { Column } from './lib/schema/column';
5
- import { ForeignKey } from './lib/schema/foreignKey';
6
- import { CreateTable } from './lib/schema/createTable';
7
- export { Migration, Table };
8
- export declare type TableOptions = {
9
- id?: boolean;
10
- comment?: string;
11
- };
12
- export declare type JoinTableOptions = TableOptions & {
13
- tableName?: string;
14
- unique?: boolean;
15
- references?: boolean;
16
- columnOptions?: ColumnOptions;
17
- options?: TableOptions;
18
- };
19
- export declare type ForeignKeyOptions = {
20
- name?: string;
21
- table: string;
22
- column: string | string[];
23
- references: string | string[];
24
- onUpdate?: OnUpdateOrDeleteAction;
25
- onDelete?: OnUpdateOrDeleteAction;
26
- index?: true | IndexOptions;
27
- };
28
- export declare type ForeignKeyFunction = (params: ForeignKeyOptions) => ForeignKey;
29
- export declare type ColumnOptions = {
30
- primaryKey?: boolean;
31
- type?: string;
32
- default?: Value;
33
- null?: boolean;
34
- index?: boolean | IndexOptions;
35
- comment?: string;
36
- mode?: string;
37
- unique?: boolean;
38
- length?: number | string;
39
- precision?: number | string;
40
- scale?: number | string;
41
- collate?: string;
42
- using?: string;
43
- references?: {
44
- table: string;
45
- column?: string;
46
- onUpdate?: OnUpdateOrDeleteAction;
47
- onDelete?: OnUpdateOrDeleteAction;
48
- };
49
- };
50
- export declare type TableCallback = (t: CreateTable) => void;
51
- export declare type IndexOptions = {
52
- name?: string;
53
- unique?: boolean;
54
- expression?: number | string;
55
- order?: string;
56
- using?: string;
57
- including?: string | string[];
58
- with?: string;
59
- tablespace?: string;
60
- where?: string;
61
- mode?: string;
62
- };
63
- export declare type AddIndexFunction = (name: string, options?: true | IndexOptions) => void;
64
- export declare type ColumnFunction = (name: string, type: string, options?: ColumnOptions) => Column;
65
- export declare const ColumnTypes: {
66
- readonly bigint: "bigint";
67
- readonly bigserial: "bigserial";
68
- readonly boolean: "boolean";
69
- readonly date: "date";
70
- readonly decimal: "decimal";
71
- readonly float: "float8";
72
- readonly integer: "integer";
73
- readonly varchar: "varchar";
74
- readonly text: "text";
75
- readonly smallint: "smallint";
76
- readonly smallserial: "smallserial";
77
- readonly string: "text";
78
- readonly time: "time";
79
- readonly timestamp: "timestamp";
80
- readonly timestamptz: "timestamptz";
81
- readonly binary: "bytea";
82
- readonly serial: "serial";
83
- readonly json: "json";
84
- readonly jsonb: "jsonb";
85
- };
86
- export declare enum OnCallback {
87
- noAction = "NO ACTION",
88
- restrict = "RESTRICT",
89
- cascade = "CASCADE",
90
- setNull = "SET NULL",
91
- nullify = "SET NULL",
92
- setDefault = "SET DEFAULT"
93
- }
94
- export declare type OnUpdateOrDeleteAction = 'NO ACTION' | 'no action' | 'RESTRICT' | 'restrict' | 'CASCADE' | 'cascade' | 'SET NULL' | 'set null' | 'SET DEFAULT' | 'set default';
package/dist/types.js DELETED
@@ -1,40 +0,0 @@
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
- exports.OnCallback = exports.ColumnTypes = exports.Table = exports.Migration = void 0;
7
- const migration_1 = __importDefault(require("./lib/migration"));
8
- exports.Migration = migration_1.default;
9
- const table_1 = __importDefault(require("./lib/schema/table"));
10
- exports.Table = table_1.default;
11
- exports.ColumnTypes = {
12
- bigint: 'bigint',
13
- bigserial: 'bigserial',
14
- boolean: 'boolean',
15
- date: 'date',
16
- decimal: 'decimal',
17
- float: 'float8',
18
- integer: 'integer',
19
- varchar: 'varchar',
20
- text: 'text',
21
- smallint: 'smallint',
22
- smallserial: 'smallserial',
23
- string: 'text',
24
- time: 'time',
25
- timestamp: 'timestamp',
26
- timestamptz: 'timestamptz',
27
- binary: 'bytea',
28
- serial: 'serial',
29
- json: 'json',
30
- jsonb: 'jsonb',
31
- };
32
- var OnCallback;
33
- (function (OnCallback) {
34
- OnCallback["noAction"] = "NO ACTION";
35
- OnCallback["restrict"] = "RESTRICT";
36
- OnCallback["cascade"] = "CASCADE";
37
- OnCallback["setNull"] = "SET NULL";
38
- OnCallback["nullify"] = "SET NULL";
39
- OnCallback["setDefault"] = "SET DEFAULT";
40
- })(OnCallback = exports.OnCallback || (exports.OnCallback = {}));