Skip to content

Commit

Permalink
refactor: return instance from Schema async constructor
Browse files Browse the repository at this point in the history
PR-URL: #41
  • Loading branch information
tshemsedinov committed Sep 15, 2020
1 parent 7591f47 commit 4d0702f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
20 changes: 12 additions & 8 deletions lib/schema-db.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand All @@ -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];
Expand All @@ -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`);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/schema-pg.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 };
Expand Down
3 changes: 2 additions & 1 deletion lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 4d0702f

Please sign in to comment.