mirror: use `SchemaInfo` in `_initialize` (#858)
Summary: This simplifies and clarifies the code with no observable change. Test Plan: Existing unit tests suffice; run `yarn unit`. wchargin-branch: mirror-use-schemainfo
This commit is contained in:
parent
e69ff57c58
commit
85efa811e0
|
@ -223,56 +223,27 @@ export class Mirror {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Then, create primitive-data tables, which depend on the schema.
|
// Then, create primitive-data tables, which depend on the schema.
|
||||||
const schema = this._schema;
|
// We only create tables for object types, as union types have no
|
||||||
for (const typename of Object.keys(schema)) {
|
// physical representation; they exist only at the type level.
|
||||||
const nodeType = schema[typename];
|
for (const typename of Object.keys(this._schemaInfo.objectTypes)) {
|
||||||
switch (nodeType.type) {
|
const type = this._schemaInfo.objectTypes[typename];
|
||||||
case "UNION":
|
if (!isSqlSafe(typename)) {
|
||||||
// Unions exist at the type level only; they have no physical
|
throw new Error(
|
||||||
// representation.
|
"invalid object type name: " + JSON.stringify(typename)
|
||||||
break;
|
);
|
||||||
case "OBJECT": {
|
|
||||||
if (!isSqlSafe(typename)) {
|
|
||||||
throw new Error(
|
|
||||||
"invalid object type name: " + JSON.stringify(typename)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
const primitiveFieldNames: Schema.Fieldname[] = [];
|
|
||||||
for (const fieldname of Object.keys(nodeType.fields)) {
|
|
||||||
const field = nodeType.fields[fieldname];
|
|
||||||
switch (field.type) {
|
|
||||||
case "ID": // handled separately
|
|
||||||
break;
|
|
||||||
case "NODE": // goes in `links` table
|
|
||||||
break;
|
|
||||||
case "CONNECTION": // goes in `connections` table
|
|
||||||
break;
|
|
||||||
case "PRIMITIVE":
|
|
||||||
if (!isSqlSafe(fieldname)) {
|
|
||||||
throw new Error(
|
|
||||||
"invalid field name: " + JSON.stringify(fieldname)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
primitiveFieldNames.push(fieldname);
|
|
||||||
break;
|
|
||||||
// istanbul ignore next
|
|
||||||
default:
|
|
||||||
throw new Error((field.type: empty));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
const tableName = primitivesTableName(typename);
|
|
||||||
const tableSpec = [
|
|
||||||
"id TEXT NOT NULL PRIMARY KEY",
|
|
||||||
...primitiveFieldNames.map((fieldname) => `"${fieldname}"`),
|
|
||||||
"FOREIGN KEY(id) REFERENCES objects(id)",
|
|
||||||
].join(", ");
|
|
||||||
db.prepare(`CREATE TABLE ${tableName} (${tableSpec})`).run();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
// istanbul ignore next
|
|
||||||
default:
|
|
||||||
throw new Error((nodeType.type: empty));
|
|
||||||
}
|
}
|
||||||
|
for (const fieldname of type.primitiveFieldNames) {
|
||||||
|
if (!isSqlSafe(fieldname)) {
|
||||||
|
throw new Error("invalid field name: " + JSON.stringify(fieldname));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const tableName = primitivesTableName(typename);
|
||||||
|
const tableSpec = [
|
||||||
|
"id TEXT NOT NULL PRIMARY KEY",
|
||||||
|
...type.primitiveFieldNames.map((fieldname) => `"${fieldname}"`),
|
||||||
|
"FOREIGN KEY(id) REFERENCES objects(id)",
|
||||||
|
].join(", ");
|
||||||
|
db.prepare(`CREATE TABLE ${tableName} (${tableSpec})`).run();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue