-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): add models setup and improve composables
closes #2 BREAKING CHANGE: composable are now dedicated objects and should not be object-spread in your definition anymore. Instead of doing `{ ...publishable }` you must use `{ publishable }`.
- Loading branch information
1 parent
67c3301
commit 82cde5a
Showing
13 changed files
with
170 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { ModelComposable } from '@foscia/core/model/types'; | ||
import { SYMBOL_MODEL_COMPOSABLE } from '@foscia/core/symbols'; | ||
import { isFosciaType } from '@foscia/shared'; | ||
|
||
export default function isComposable( | ||
value: unknown, | ||
): value is ModelComposable<any> { | ||
return isFosciaType(value, SYMBOL_MODEL_COMPOSABLE); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,27 @@ | ||
import makeDefinition from '@foscia/core/model/makeDefinition'; | ||
import { ModelInstance, ModelParsedDefinition } from '@foscia/core/model/types'; | ||
import makeModelSetup from '@foscia/core/model/makeModelSetup'; | ||
import { | ||
ModelComposable, | ||
ModelFlattenDefinition, | ||
ModelInstance, | ||
ModelParsedDefinition, | ||
ModelRawSetup, | ||
} from '@foscia/core/model/types'; | ||
import { SYMBOL_MODEL_COMPOSABLE } from '@foscia/core/symbols'; | ||
|
||
/** | ||
* Create a composable definition which will be used by a model factory. | ||
* | ||
* @param rawDefinition | ||
* @param rawSetup | ||
*/ | ||
export default function makeComposable<D extends {} = {}>( | ||
rawDefinition?: D & ThisType<ModelInstance<ModelParsedDefinition<D>>>, | ||
rawDefinition?: D & ThisType<ModelInstance<ModelFlattenDefinition<D>>>, | ||
rawSetup?: ModelRawSetup<D>, | ||
) { | ||
return makeDefinition(rawDefinition) as ModelParsedDefinition<D>; | ||
return { | ||
$FOSCIA_TYPE: SYMBOL_MODEL_COMPOSABLE, | ||
$definition: makeDefinition(rawDefinition), | ||
$setup: makeModelSetup(rawSetup), | ||
} as ModelComposable<ModelParsedDefinition<D>>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,44 @@ | ||
import makeModelClass from '@foscia/core/model/makeModelClass'; | ||
import makeModelSetup from '@foscia/core/model/makeModelSetup'; | ||
import { | ||
ExtendableModel, | ||
ModelConfig, | ||
ModelFlattenDefinition, | ||
ModelInstance, | ||
ModelParsedDefinition, | ||
ModelRawSetup, | ||
} from '@foscia/core/model/types'; | ||
|
||
export default function makeModelFactory<ND extends {} = {}>( | ||
baseConfig?: ModelConfig, | ||
baseRawDefinition?: ND & ThisType<ModelInstance<ModelParsedDefinition<ND>>>, | ||
// eslint-disable-next-line max-len | ||
baseRawDefinition?: ND & ThisType<ModelInstance<ModelFlattenDefinition<ModelParsedDefinition<ND>>>>, | ||
baseRawSetup?: ModelRawSetup<ModelFlattenDefinition<ModelParsedDefinition<ND>>>, | ||
) { | ||
const baseSetup = makeModelSetup(baseRawSetup); | ||
|
||
return <D extends {} = {}>( | ||
rawConfig: string | (ModelConfig & { type: string; }), | ||
rawDefinition?: D & ThisType<ModelInstance<ModelParsedDefinition<ND & D>>>, | ||
// eslint-disable-next-line max-len | ||
rawDefinition?: D & ThisType<ModelInstance<ModelFlattenDefinition<ModelParsedDefinition<ND & D>>>>, | ||
rawSetup?: ModelRawSetup<ModelFlattenDefinition<ModelParsedDefinition<ND & D>>>, | ||
) => { | ||
const setup = makeModelSetup(rawSetup); | ||
|
||
const { type, ...config } = typeof rawConfig === 'string' | ||
? { type: rawConfig } | ||
: rawConfig; | ||
|
||
return makeModelClass(type, { | ||
...baseConfig, | ||
...config, | ||
}, { | ||
boot: [...baseSetup.boot, ...setup.boot], | ||
init: [...baseSetup.init, ...setup.init], | ||
}).extend({ | ||
...baseRawDefinition, | ||
...rawDefinition, | ||
// eslint-disable-next-line max-len | ||
}) as ExtendableModel<ModelParsedDefinition<ND & D>, ModelInstance<ModelParsedDefinition<ND & D>>>; | ||
}) as ExtendableModel<ModelFlattenDefinition<ModelParsedDefinition<ND & D>>, ModelInstance<ModelFlattenDefinition<ModelParsedDefinition<ND & D>>>>; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { ModelRawSetup, ModelSetup } from '@foscia/core/model/types'; | ||
import { wrap } from '@foscia/shared'; | ||
|
||
export default function makeModelSetup<D extends {} = {}>(rawSetup?: ModelRawSetup<D>) { | ||
return { | ||
boot: [...wrap(rawSetup?.boot)], | ||
init: [...wrap(rawSetup?.init)], | ||
} as ModelSetup<D>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { hasMany, makeComposable } from '@foscia/core'; | ||
import CommentMock from '../models/comment.mock'; | ||
|
||
export default makeComposable({ | ||
comments: hasMany(() => CommentMock), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
import { attr, hasMany, makeModel, toDateTime } from '@foscia/core'; | ||
import CommentMock from './comment.mock'; | ||
import { attr, makeModel, toDateTime } from '@foscia/core'; | ||
import commentable from '../composables/commentable'; | ||
|
||
export default class PostMock extends makeModel('posts', { | ||
commentable, | ||
title: attr<string>(), | ||
body: attr<string | null>(), | ||
comments: hasMany(() => CommentMock), | ||
publishedAt: attr(toDateTime()).nullable().readOnly(), | ||
}) { | ||
} |
Oops, something went wrong.