Skip to content

Commit eb9668f

Browse files
committed
Update to orbit 0.17
1 parent 64198d2 commit eb9668f

10 files changed

+1744
-1151
lines changed

package.json

+17-17
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
"repository": "https://github.com/tchak/orbit-sql",
1616
"license": "MIT",
1717
"dependencies": {
18-
"@orbit/data": "^0.16.5",
19-
"inflected": "^2.0.4",
20-
"knex": "^0.21.1",
21-
"objection": "^2.1.3"
18+
"@orbit/records": "^0.17.0-beta.12",
19+
"inflected": "^2.1.0",
20+
"knex": "^0.21.17",
21+
"objection": "^2.2.14"
2222
},
2323
"scripts": {
2424
"build": "rm -rf ./dist && tsc",
@@ -29,19 +29,19 @@
2929
},
3030
"devDependencies": {
3131
"@types/inflected": "^1.1.29",
32-
"@types/node": "^13.13.4",
33-
"@types/qunit": "^2.9.1",
34-
"@typescript-eslint/eslint-plugin": "^2.30.0",
35-
"@typescript-eslint/parser": "^2.30.0",
36-
"eslint": "^6.8.0",
37-
"eslint-config-prettier": "^6.11.0",
38-
"eslint-plugin-prettier": "^3.1.3",
39-
"prettier": "^2.0.5",
40-
"qunit": "^2.9.3",
41-
"release-it": "^13.5.7",
42-
"sqlite3": "^4.2.0",
43-
"ts-node": "^8.9.1",
44-
"typescript": "^3.8.3"
32+
"@types/node": "^14.14.31",
33+
"@types/qunit": "^2.11.1",
34+
"@typescript-eslint/eslint-plugin": "^4.15.1",
35+
"@typescript-eslint/parser": "^4.15.1",
36+
"eslint": "^7.20.0",
37+
"eslint-config-prettier": "^7.2.0",
38+
"eslint-plugin-prettier": "^3.3.1",
39+
"prettier": "^2.2.1",
40+
"qunit": "^2.14.0",
41+
"release-it": "^14.4.1",
42+
"sqlite3": "^5.0.2",
43+
"ts-node": "^9.1.1",
44+
"typescript": "^4.1.5"
4545
},
4646
"publishConfig": {
4747
"access": "public",

src/build-models.ts

+60-66
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,25 @@ import {
55
snakeCaseMappers,
66
} from 'objection';
77
import {
8-
Schema,
8+
RecordSchema,
99
RecordNotFoundException,
1010
RecordRelationship,
1111
Record as OrbitRecord,
12-
} from '@orbit/data';
12+
} from '@orbit/records';
1313
import { foreignKey, tableize } from 'inflected';
1414

1515
import { tableizeJoinTable, castAttributeValue } from './utils';
1616

17-
export class BaseModel extends Model {
17+
export abstract class BaseModel extends Model {
1818
id: string;
1919
createdAt: string;
2020
updatedAt: string;
2121

2222
static get virtualAttributes() {
2323
return ['orbitSchema', 'orbitType'];
2424
}
25-
orbitSchema: Schema;
26-
orbitType: string;
25+
abstract get orbitSchema(): RecordSchema;
26+
abstract get orbitType(): string;
2727

2828
$beforeInsert() {
2929
this.createdAt = new Date().toISOString();
@@ -57,22 +57,22 @@ export class BaseModel extends Model {
5757

5858
schema.eachAttribute(type, (property, attribute) => {
5959
if (result[property] != null) {
60-
(attributes as Record<string, unknown>)[property] = castAttributeValue(
60+
attributes[property] = castAttributeValue(
6161
result[property],
6262
attribute.type
6363
);
6464
record.attributes = attributes;
6565
}
6666
});
6767

68-
schema.eachRelationship(type, (property, { type: kind, model: type }) => {
68+
schema.eachRelationship(type, (property, { kind, type }) => {
6969
if (kind === 'hasOne') {
70-
const id = result[`${property}Id`];
70+
const id = result[`${property}Id`] as string | undefined;
7171
if (id) {
72-
(relationships as Record<string, unknown>)[property] = {
72+
relationships[property] = {
7373
data: {
7474
type: type as string,
75-
id: id as string,
75+
id: id,
7676
},
7777
};
7878
record.relationships = relationships;
@@ -85,7 +85,7 @@ export class BaseModel extends Model {
8585
}
8686

8787
export function buildModels(
88-
schema: Schema
88+
schema: RecordSchema
8989
): Record<string, ModelClass<BaseModel>> {
9090
const models: Record<string, ModelClass<BaseModel>> = {};
9191

@@ -97,7 +97,7 @@ export function buildModels(
9797
}
9898

9999
export function buildModel(
100-
schema: Schema,
100+
schema: RecordSchema,
101101
type: string,
102102
models: Record<string, ModelClass<BaseModel>>
103103
): ModelClass<BaseModel> {
@@ -118,72 +118,66 @@ export function buildModel(
118118

119119
static get relationMappings() {
120120
const relationMappings: Record<string, RelationMapping<BaseModel>> = {};
121-
schema.eachRelationship(
122-
type,
123-
(property, { type: kind, model: type, inverse }) => {
124-
if (!inverse || !type) {
125-
throw new Error(
126-
`SQLSource: "type" and "inverse" are required on a relationship`
127-
);
128-
}
121+
schema.eachRelationship(type, (property, { kind, type, inverse }) => {
122+
if (!inverse || !type) {
123+
throw new Error(
124+
`SQLSource: "type" and "inverse" are required on a relationship`
125+
);
126+
}
129127

130-
if (Array.isArray(type)) {
131-
throw new Error(
132-
`SQLSource: polymorphic types are not supported yet`
133-
);
134-
}
128+
if (Array.isArray(type)) {
129+
throw new Error(
130+
`SQLSource: polymorphic types are not supported yet`
131+
);
132+
}
135133

136-
const relationColumnName = foreignKey(property);
137-
const inverseColumnName = foreignKey(inverse);
138-
const relationTableName = tableize(type);
139-
const relationModel = buildModel(schema, type, models);
140-
let relationMapping: RelationMapping<BaseModel>;
134+
const relationColumnName = foreignKey(property);
135+
const inverseColumnName = foreignKey(inverse);
136+
const relationTableName = tableize(type);
137+
const relationModel = buildModel(schema, type, models);
138+
let relationMapping: RelationMapping<BaseModel>;
139+
140+
if (kind === 'hasOne') {
141+
relationMapping = {
142+
relation: Model.BelongsToOneRelation,
143+
modelClass: relationModel,
144+
join: {
145+
from: `${tableName}.${relationColumnName}`,
146+
to: `${relationTableName}.id`,
147+
},
148+
};
149+
} else {
150+
const relDef = schema.getRelationship(type, inverse);
151+
152+
if (relDef?.kind === 'hasMany') {
153+
const joinTableName = tableizeJoinTable(property, inverse);
141154

142-
if (kind === 'hasOne') {
143155
relationMapping = {
144-
relation: Model.BelongsToOneRelation,
156+
relation: Model.ManyToManyRelation,
145157
modelClass: relationModel,
146158
join: {
147-
from: `${tableName}.${relationColumnName}`,
159+
from: `${tableName}.id`,
160+
through: {
161+
from: `${joinTableName}.${relationColumnName}`,
162+
to: `${joinTableName}.${inverseColumnName}`,
163+
},
148164
to: `${relationTableName}.id`,
149165
},
150166
};
151167
} else {
152-
const { type: inverseKind } = schema.getRelationship(
153-
type,
154-
inverse
155-
);
156-
157-
if (inverseKind === 'hasMany') {
158-
const joinTableName = tableizeJoinTable(property, inverse);
159-
160-
relationMapping = {
161-
relation: Model.ManyToManyRelation,
162-
modelClass: relationModel,
163-
join: {
164-
from: `${tableName}.id`,
165-
through: {
166-
from: `${joinTableName}.${relationColumnName}`,
167-
to: `${joinTableName}.${inverseColumnName}`,
168-
},
169-
to: `${relationTableName}.id`,
170-
},
171-
};
172-
} else {
173-
relationMapping = {
174-
relation: Model.HasManyRelation,
175-
modelClass: relationModel,
176-
join: {
177-
from: `${tableName}.id`,
178-
to: `${relationTableName}.${inverseColumnName}`,
179-
},
180-
};
181-
}
168+
relationMapping = {
169+
relation: Model.HasManyRelation,
170+
modelClass: relationModel,
171+
join: {
172+
from: `${tableName}.id`,
173+
to: `${relationTableName}.${inverseColumnName}`,
174+
},
175+
};
182176
}
183-
184-
relationMappings[property] = relationMapping;
185177
}
186-
);
178+
179+
relationMappings[property] = relationMapping;
180+
});
187181

188182
return relationMappings;
189183
}

src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { default, SQLSourceSettings } from './sql-source';
1+
export { SQLSource as default, SQLSourceSettings } from './sql-source';

src/migrate-models.ts

+27-28
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1-
import { Schema } from '@orbit/data';
1+
import { RecordSchema } from '@orbit/records';
22
import Knex from 'knex';
33
import { underscore, foreignKey, tableize } from 'inflected';
44

55
import { tableizeJoinTable } from './utils';
66

7-
export async function migrateModels(db: Knex, schema: Schema) {
7+
export async function migrateModels(db: Knex, schema: RecordSchema) {
88
for (let type in schema.models) {
99
await migrateModel(db, schema, type);
1010
}
1111
}
1212

13-
export async function migrateModel(db: Knex, schema: Schema, type: string) {
13+
export async function migrateModel(
14+
db: Knex,
15+
schema: RecordSchema,
16+
type: string
17+
) {
1418
const tableName = tableize(type);
1519
const joinTables: Record<string, [string, string]> = {};
1620
const hasTable = await db.schema.hasTable(tableName);
@@ -46,36 +50,31 @@ export async function migrateModel(db: Knex, schema: Schema, type: string) {
4650
}
4751
});
4852

49-
schema.eachRelationship(
50-
type,
51-
(property, { type: kind, model: type, inverse }) => {
52-
const columnName = foreignKey(property);
53-
if (kind === 'hasOne') {
54-
table.uuid(columnName);
55-
} else {
56-
if (!inverse || !type) {
57-
throw new Error(
58-
`SQLSource: "type" and "inverse" are required on a relationship`
59-
);
60-
}
53+
schema.eachRelationship(type, (property, { kind, type, inverse }) => {
54+
const columnName = foreignKey(property);
55+
if (kind === 'hasOne') {
56+
table.uuid(columnName);
57+
} else {
58+
if (!inverse || !type) {
59+
throw new Error(
60+
`SQLSource: "type" and "inverse" are required on a relationship`
61+
);
62+
}
6163

62-
if (Array.isArray(type)) {
63-
throw new Error(
64-
`SQLSource: polymorphic types are not supported yet`
65-
);
66-
}
64+
if (Array.isArray(type)) {
65+
throw new Error(`SQLSource: polymorphic types are not supported yet`);
66+
}
6767

68-
let { type: inverseKind } = schema.getRelationship(type, inverse);
68+
let relDef = schema.getRelationship(type, inverse);
6969

70-
if (inverseKind === 'hasMany') {
71-
joinTables[tableizeJoinTable(property, inverse)] = [
72-
columnName,
73-
foreignKey(inverse),
74-
];
75-
}
70+
if (relDef?.kind === 'hasMany') {
71+
joinTables[tableizeJoinTable(property, inverse)] = [
72+
columnName,
73+
foreignKey(inverse),
74+
];
7675
}
7776
}
78-
);
77+
});
7978
});
8079

8180
for (let joinTableName in joinTables) {

0 commit comments

Comments
 (0)