velocious 1.0.7 → 1.0.9

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/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "velocious": "bin/velocious.js"
4
4
  },
5
5
  "name": "velocious",
6
- "version": "1.0.7",
6
+ "version": "1.0.9",
7
7
  "main": "index.js",
8
8
  "scripts": {
9
9
  "test": "jasmine",
@@ -15,7 +15,9 @@ const configurationResolver = async (args) => {
15
15
  configuration = configurationImport.default
16
16
  } catch (error) {
17
17
  // This might happen during an "init" CLI command where we copy a sample configuration file.
18
- if (error.code != "ERR_MODULE_NOT_FOUND") throw error
18
+ if (!error.message.match(/^Cannot find module '(.+)\/configuration\.js'/)) {
19
+ throw error
20
+ }
19
21
 
20
22
  configuration = new Configuration(args)
21
23
  }
@@ -69,6 +69,15 @@ export default class VelociousDatabaseDriversBase {
69
69
  this.idSeq = id
70
70
  }
71
71
 
72
+ async tableExists(tableName) {
73
+ const tables = await this.getTables()
74
+ const table = tables.find((table) => table.getName() == tableName)
75
+
76
+ if (table) return true
77
+
78
+ return false
79
+ }
80
+
72
81
  async update(...args) {
73
82
  const sql = this.updateSql(...args)
74
83
 
@@ -1,5 +1,6 @@
1
1
  import {digg} from "diggerize"
2
2
 
3
+ import AlterTable from "../sqlite/sql/alter-table.js"
3
4
  import Base from "../base.js"
4
5
  import CreateIndex from "../sqlite/sql/create-index.js"
5
6
  import CreateTable from "../sqlite/sql/create-table.js"
@@ -12,6 +13,13 @@ import Table from "./table"
12
13
  import Update from "../sqlite/sql/update.js"
13
14
 
14
15
  export default class VelociousDatabaseDriversSqliteBase extends Base {
16
+ alterTableSql(columnData) {
17
+ const createArgs = Object.assign({driver: this}, columnData)
18
+ const alterTable = new AlterTable(createArgs)
19
+
20
+ return alterTable.toSqls()
21
+ }
22
+
15
23
  createIndexSql(indexData) {
16
24
  const createArgs = Object.assign({driver: this}, indexData)
17
25
  const createIndex = new CreateIndex(createArgs)
@@ -0,0 +1,4 @@
1
+ import AlterTableBase from "../../../query/alter-table-base.js"
2
+
3
+ export default class VelociousDatabaseConnectionDriversMysqlSqlAlterTable extends AlterTableBase {
4
+ }
@@ -5,12 +5,11 @@ import Migrator from "./migrator"
5
5
  export default class VelociousDatabaseMigrateFromRequireContext {
6
6
  constructor(configuration) {
7
7
  this.configuration = configuration || Configuration.current()
8
+ this.migrator = new Migrator({configuration: this.configuration})
8
9
  }
9
10
 
10
11
  async execute(requireContext) {
11
- const migrator = new Migrator({configuration: this.configuration})
12
-
13
- await migrator.prepare()
12
+ await this.migrator.prepare()
14
13
 
15
14
  const files = requireContext.keys()
16
15
  .map((file) => {
@@ -20,7 +19,7 @@ export default class VelociousDatabaseMigrateFromRequireContext {
20
19
 
21
20
  const date = parseInt(match[1])
22
21
  const migrationName = match[2]
23
- const migrationClassName = inflection.camelize(migrationName)
22
+ const migrationClassName = inflection.camelize(migrationName.replaceAll("-", "_"))
24
23
 
25
24
  return {
26
25
  file,
@@ -32,7 +31,7 @@ export default class VelociousDatabaseMigrateFromRequireContext {
32
31
  .sort((migration1, migration2) => migration1.date - migration2.date)
33
32
 
34
33
  for (const migration of files) {
35
- if (!migrator.hasRunMigrationVersion(migration.date)) {
34
+ if (!this.migrator.hasRunMigrationVersion(migration.date)) {
36
35
  await this.runMigrationFile(migration, requireContext)
37
36
  }
38
37
  }
@@ -1,10 +1,22 @@
1
- import TableData from "../table-data/index.js"
1
+ import TableData, {TableColumn} from "../table-data/index.js"
2
2
 
3
3
  export default class VelociousDatabaseMigration {
4
4
  constructor({configuration}) {
5
5
  this.configuration = configuration
6
6
  }
7
7
 
8
+ async addColumn(tableName, columnName, args) {
9
+ const databasePool = this.configuration.getDatabasePool()
10
+ const sqls = databasePool.alterTableSql({
11
+ columns: [new TableColumn(columnName, args)],
12
+ tableName
13
+ })
14
+
15
+ for (const sql of sqls) {
16
+ await databasePool.query(sql)
17
+ }
18
+ }
19
+
8
20
  async addIndex(tableName, columns, args) {
9
21
  const databasePool = this.configuration.getDatabasePool()
10
22
  const createIndexArgs = Object.assign(
@@ -35,6 +35,8 @@ class VelociousDatabasePoolBase {
35
35
  }
36
36
 
37
37
  const forwardMethods = [
38
+ "alterTable",
39
+ "alterTableSql",
38
40
  "createIndex",
39
41
  "createIndexSql",
40
42
  "createTable",
@@ -0,0 +1,26 @@
1
+ import QueryBase from "./base.js"
2
+ import restArgsError from "../../utils/rest-args-error.js"
3
+
4
+ export default class VelociousDatabaseQueryAlterTableBase extends QueryBase {
5
+ constructor({columns, driver, tableName, ...restArgs}) {
6
+ restArgsError(restArgs)
7
+
8
+ super({driver})
9
+ this.columns = columns
10
+ this.tableName = tableName
11
+ }
12
+
13
+ toSqls() {
14
+ const sqls = []
15
+
16
+ for (const column of this.columns) {
17
+ let sql = `ALTER TABLE ${this.driver.quoteTable(this.tableName)} ADD `
18
+
19
+ sql += this.driver.quoteColumn(column.getName())
20
+
21
+ sqls.push(sql)
22
+ }
23
+
24
+ return sqls
25
+ }
26
+ }
@@ -77,6 +77,12 @@ export default class VelociousDatabaseRecord {
77
77
  return relationship
78
78
  }
79
79
 
80
+ static getRelationships() {
81
+ if (this._relationships) return Object.values(this._relationships)
82
+
83
+ return []
84
+ }
85
+
80
86
  getRelationshipByName(relationshipName) {
81
87
  if (!this._instanceRelationships) this._instanceRelationships = {}
82
88
 
@@ -308,7 +314,7 @@ export default class VelociousDatabaseRecord {
308
314
 
309
315
  const model = instanceRelationship.loaded()
310
316
 
311
- if (model.isChanged()) {
317
+ if (model?.isChanged()) {
312
318
  await model.save()
313
319
 
314
320
  const foreignKey = instanceRelationship.getForeignKey()
@@ -569,6 +575,13 @@ export default class VelociousDatabaseRecord {
569
575
 
570
576
  await this._reloadWithId(id)
571
577
  this.setIsNewRecord(false)
578
+
579
+ // Mark all relationships as preloaded, since we don't expect anything to have magically appeared since we created the record.
580
+ for (const relationship of this.constructor.getRelationships()) {
581
+ const instanceRelationship = this.getRelationshipByName(relationship.getRelationshipName())
582
+
583
+ instanceRelationship.setPreloaded(true)
584
+ }
572
585
  }
573
586
 
574
587
  async _updateRecordWithChanges() {
@@ -39,9 +39,5 @@ export default class VelociousDatabaseRecordHasManyInstanceRelationship extends
39
39
  this._loaded = models
40
40
  }
41
41
 
42
- setPreloaded(preloadedValue) {
43
- this._preloaded = preloadedValue
44
- }
45
-
46
42
  getTargetModelClass = () => this.relationship.getTargetModelClass()
47
43
  }
@@ -3,6 +3,8 @@ class TableColumn {
3
3
  this.args = args
4
4
  this.name = name
5
5
  }
6
+
7
+ getName = () => this.name
6
8
  }
7
9
 
8
10
  class TableIndex {
@@ -23,6 +25,8 @@ class TableReference {
23
25
  }
24
26
  }
25
27
 
28
+ export {TableColumn}
29
+
26
30
  export default class TableData {
27
31
  _columns = []
28
32
  _indexes = []