Skip to content
This repository was archived by the owner on Aug 18, 2022. It is now read-only.

Commit cf9d634

Browse files
committed
Fixed FK resolution bug
1 parent d81684c commit cf9d634

6 files changed

Lines changed: 11 additions & 9 deletions

File tree

src/lib/fixture.mjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import set from 'lodash/set.js'
2+
import { OPTIONS } from '../symbols.mjs'
23

34
/**
45
* An instance of a single fixture which represents a single
@@ -44,8 +45,9 @@ class Fixture {
4445
set (model, relations, insertMap) {
4546
for (const [key, pointer] of Object.entries(relations)) {
4647
const relation = insertMap.get(pointer)
48+
const { col } = relation[OPTIONS].table.pk
4749

48-
set(model, key, relation[relation._options.table.pk])
50+
set(model, key, relation[col])
4951
}
5052
}
5153
}

src/lib/model.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { uuid } from '../utils.mjs'
2+
import { OPTIONS } from '../symbols.mjs'
23

34
export const NOW = new Date().toISOString()
45

@@ -20,7 +21,7 @@ class Model {
2021

2122
this.#configure(table)
2223

23-
this[options.keys.options] = {
24+
this[OPTIONS] = {
2425
...options,
2526
table
2627
}

src/lib/resolver.mjs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import omit from 'lodash/omit.js'
21
import invokeMap from 'lodash/invokeMap.js'
32
import { filterValues } from '../utils.mjs'
3+
import { OPTIONS } from '../symbols.mjs'
44
import Model from './model.mjs'
55
import Table from './table.mjs'
66

@@ -76,8 +76,7 @@ class Resolver {
7676
* @returns {object} - Collection of relations to resolve later.
7777
*/
7878
relations (model) {
79-
const json = omit(model, [this.options.keys.options])
80-
const relations = filterValues(json, v => v?.toString().match(/^@[\w]+/g))
79+
const relations = filterValues(model, v => v?.toString().match(/^@[\w]+/g))
8180

8281
this.applyPolymorphism(model, relations)
8382

@@ -97,7 +96,7 @@ class Resolver {
9796
table: { columns },
9897
suffixes: { type, id },
9998
fixtures
100-
} = model._options
99+
} = model[OPTIONS]
101100

102101
for (const [polyName, relationKey] of entries(relations)) {
103102
const polyType = polyName + type

src/symbols.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const OPTIONS = Symbol('options')

src/utils.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import isPlainObject from 'lodash/isPlainObject.js'
22
import isArray from 'lodash/isArray.js'
33
import debug from 'debug'
44
import * as uuidLib from 'uuid'
5+
import { OPTIONS } from './symbols.mjs'
56

67
const {
78
keys,
@@ -25,7 +26,7 @@ export const uuid = uuidLib.default?.v4 ?? uuidLib.v4
2526
*/
2627
export function toJson (instance) {
2728
const modelMap = keys(instance)
28-
.filter(key => !!instance._options.table.columns[key])
29+
.filter(key => !!instance[OPTIONS].table.columns[key])
2930
.map(key => {
3031
const cell = instance[key]
3132
const isJson = isPlainObject(cell) || isArray(cell)

src/zero.mjs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import Resolver from './lib/resolver.mjs'
1414
* @property {string} [type] - Primary key column type.
1515
* @property {string} directory - Path to fixture files.
1616
* @property {object} keys - Configurable keys for storing options relating to this program.
17-
* @property {string} keys.options - Key to use on Model instances for options.
1817
* @property {string} keys.model - Key to use for model options within the fixtures.
1918
* @property {object} suffixes - Polymorphic column suffixes.
2019
* @property {string} suffixes.type - Suffix used for the polymorphic type column.
@@ -31,7 +30,6 @@ export const DEFAULT_OPTIONS = {
3130
type: 'uuid'
3231
},
3332
keys: {
34-
options: '_options',
3533
model: '_model'
3634
},
3735
suffixes: {

0 commit comments

Comments
 (0)