diff --git a/lib/schema-db.js b/lib/schema-db.js index 3c7b907..7703f7e 100644 --- a/lib/schema-db.js +++ b/lib/schema-db.js @@ -18,11 +18,15 @@ class DatabaseSchema { static async load(schemaPath) { const files = await fs.readdir(schemaPath, { withFileTypes: true }); const dbPath = path.join(schemaPath, '.database.js'); - const database = await new Schema(dbPath); + const dbSchema = await new Schema(dbPath); + if (!dbSchema) throw new Error(`File is not found: ${dbPath}`); + const { entity: database } = dbSchema; const typesPath = path.join(schemaPath, '.types.js'); - const types = await new Schema(typesPath); + const typesSchema = await new Schema(typesPath); + if (!typesSchema) console.log(`File is not found: ${typesPath}`); + const { entity: types } = typesSchema || { entity: {} }; const SchemaClass = DatabaseSchema.implementations[database.driver]; - const dbSchema = new SchemaClass(schemaPath, database, types); + const databaseSchema = new SchemaClass(schemaPath, database, types); for (const file of files) { if (file.isDirectory()) continue; if (!file.name.endsWith('.js')) continue; @@ -31,10 +35,10 @@ class DatabaseSchema { const schema = await new Schema(absPath); if (!schema) continue; const schemaName = path.basename(file.name, '.js'); - dbSchema.entities.set(schemaName, schema); + databaseSchema.entities.set(schemaName, schema); } - await dbSchema.preprocess(); - return dbSchema; + await databaseSchema.preprocess(); + return databaseSchema; } async preprocess() { @@ -54,7 +58,7 @@ class DatabaseSchema { } async reorderEntity(name) { - const entity = this.entities.get(name); + const { entity } = this.entities.get(name); const fields = Object.keys(entity); for (const field of fields) { const { type } = entity[field]; @@ -68,7 +72,7 @@ class DatabaseSchema { async validate() { console.log('Validating metaschema'); for (const name of this.order) { - const entity = this.entities.get(name); + const { entity } = this.entities.get(name); const fields = Object.keys(entity); if (entity) console.log(` ${name}: ${fields.length} fields`); } diff --git a/lib/schema-pg.js b/lib/schema-pg.js index 3493ff3..250dae1 100644 --- a/lib/schema-pg.js +++ b/lib/schema-pg.js @@ -78,7 +78,7 @@ const primaryKey = entityName => { class PgSchema extends DatabaseSchema { async preprocessEntity(name) { - const entity = this.entities.get(name); + const { entity } = this.entities.get(name); const fields = Object.keys(entity); const types = { ...PG_TYPES, ...this.types }; for (const fieldName of fields) { @@ -102,7 +102,7 @@ class PgSchema extends DatabaseSchema { } async createEntity(name) { - const entity = this.entities.get(name); + const { entity } = this.entities.get(name); const sql = []; const idx = []; const types = { ...PG_TYPES, ...this.types }; diff --git a/lib/schema.js b/lib/schema.js index adb1c99..b9c41bd 100644 --- a/lib/schema.js +++ b/lib/schema.js @@ -17,11 +17,12 @@ class Schema { const src = await fs.readFile(this.path, 'utf8'); if (!src) return null; const script = new vm.Script(src, { filename: this.path }); - return script.runInThisContext(SCRIPT_OPTIONS); + this.entity = script.runInThisContext(SCRIPT_OPTIONS); } catch (err) { if (err.code !== 'ENOENT') { console.error(err.stack); } + return null; } return this; }