diff --git a/packages/common/src/db/schema/Table.ts b/packages/common/src/db/schema/Table.ts index 5f91afaf..f5e29115 100644 --- a/packages/common/src/db/schema/Table.ts +++ b/packages/common/src/db/schema/Table.ts @@ -238,6 +238,12 @@ export class Table { } validate() { + if (this.options.name === '') { + throw new Error( + "It appears you are trying to create a new Schema with an array instead of an object. Passing in an object instead of an array into 'new Schema()' may resolve your issue." + ); + } + if (InvalidSQLCharacters.test(this.name)) { throw new Error(`Invalid characters in table name: ${this.name}`); } diff --git a/packages/common/tests/db/schema/Schema.test.ts b/packages/common/tests/db/schema/Schema.test.ts index 031dc80a..465700eb 100644 --- a/packages/common/tests/db/schema/Schema.test.ts +++ b/packages/common/tests/db/schema/Schema.test.ts @@ -2,14 +2,27 @@ import { describe, it, expect } from 'vitest'; import { Schema } from '../../../src/db/schema/Schema'; import { Table } from '../../../src/db/schema/Table'; import { column, ColumnType } from '../../../src/db/schema/Column'; +import { Column } from '../../../lib'; describe('Schema', () => { + it('should fail if array is passed and not a table object', () => { + const table1 = new Table({ name: column.text }); + const table2 = new Table({ age: { type: ColumnType.INTEGER } }); + const schema = new Schema([table1, table2]); + expect(() => schema.validate()).toThrow(); + }); + it('should create a schema with an array of tables', () => { - const tables = [ - new Table({ name: column.text, }), - new Table({ age: { type: ColumnType.INTEGER } }) - ]; - const schema = new Schema(tables); + const table1 = new Table({ + name: 'table1', + columns: [new Column({ name: 'name', type: ColumnType.TEXT })] + }); + const table2 = new Table({ + name: 'table2', + columns: [new Column({ name: 'age', type: ColumnType.INTEGER })] + }); + const schema = new Schema([table1, table2]); + expect(() => schema.validate()).not.toThrow(); expect(schema.tables).toHaveLength(2); expect(schema.tables[0].columns[0].name).toBe('name');