Skip to content

Commit

Permalink
Collection for Schema relations
Browse files Browse the repository at this point in the history
Closes: #327

PR-URL: #347
  • Loading branch information
tshemsedinov committed Jun 29, 2021
1 parent 1073d24 commit 5ee46d8
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## [Unreleased][unreleased]

- Schema projection
- Schema.prototype.relations: Set<{ to: string, type: string }>

## [1.3.1][] - 2021-06-26

Expand Down
10 changes: 9 additions & 1 deletion lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ class Schema {
this.fields = {};
this.indexes = {};
this.references = new Set();
this.relations = new Set();
this.validate = defs.validate || null;
this.format = defs.format || null;
this.parse = defs.parse || null;
Expand Down Expand Up @@ -172,6 +173,7 @@ class Schema {
const schema = Schema.from(entry);
this.fields[key] = schema;
this.references = new Set([...this.references, ...schema.references]);
this.relations = new Set([...this.relations, ...schema.relations]);
}
continue;
}
Expand All @@ -184,6 +186,9 @@ class Schema {
if (!Reflect.has(def, 'required')) def.required = required;
if (def.length) def.length = formatLength(def.length);
this.references.add(type);
if (isFirstUpper(type)) {
this.relations.add({ to: type, type: 'many-to-one' });
}
this.fields[key] = def;
}
}
Expand All @@ -192,7 +197,10 @@ class Schema {
const { index, primary, unique, many } = def;
const isIndex = many || Array.isArray(index || primary || unique);
if (isIndex) this.indexes[key] = def;
if (many) this.references.add(many);
if (many) {
this.references.add(many);
this.relations.add({ to: many, type: 'one-to-many' });
}
return isIndex;
}

Expand Down
2 changes: 2 additions & 0 deletions test/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ metatests.test('Schema: database', (test) => {
},
indexes: {},
references: new Set(['Country']),
relations: new Set([]),
validate: null,
format: null,
parse: null,
Expand All @@ -58,6 +59,7 @@ metatests.test('Schema: database', (test) => {
altKey: { unique: ['name', 'street'] },
},
references: new Set(['Country', 'Person']),
relations: new Set([]),
validate: null,
format: null,
parse: null,
Expand Down
1 change: 1 addition & 0 deletions test/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ metatests.test('Model: from struct', (test) => {
fields: { name: { type: 'string', unique: true, required: true } },
indexes: { addresses: { many: 'Address' } },
references: new Set(['Address']),
relations: new Set([]),
validate: null,
format: null,
parse: null,
Expand Down
12 changes: 12 additions & 0 deletions types/metaschema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ type Kind =
| 'struct'
| 'scalar';

type Cardinality =
| 'one-to-one'
| 'one-to-many'
| 'many-to-one'
| 'many-to-many';

interface Relation {
to: string;
type: Cardinality;
}

export class Schema {
name: string;
namespaces: Set<Model>;
Expand All @@ -22,6 +33,7 @@ export class Schema {
fields: object;
indexes: object;
references: Set<string>;
relations: Set<Relation>;
validate: Function | null;
format: Function | null;
parse: Function | null;
Expand Down

0 comments on commit 5ee46d8

Please sign in to comment.