[fabric/sqlite-store] Refactor collection references to use new model definitions
This commit is contained in:
parent
3c3ce276c0
commit
c38f74414b
@ -1,5 +1,5 @@
|
|||||||
import {
|
import {
|
||||||
defineCollection,
|
defineModel,
|
||||||
Field,
|
Field,
|
||||||
isGreaterOrEqualTo,
|
isGreaterOrEqualTo,
|
||||||
isGreaterThan,
|
isGreaterThan,
|
||||||
@ -13,7 +13,7 @@ import { describe, expect, test } from "@fabric/testing";
|
|||||||
import { filterToParams, filterToSQL } from "./filter-to-sql.ts";
|
import { filterToParams, filterToSQL } from "./filter-to-sql.ts";
|
||||||
|
|
||||||
describe("SQL where clause from filter options", () => {
|
describe("SQL where clause from filter options", () => {
|
||||||
const col = defineCollection("users", {
|
const col = defineModel("users", {
|
||||||
name: Field.string(),
|
name: Field.string(),
|
||||||
age: Field.integer(),
|
age: Field.integer(),
|
||||||
status: Field.string(),
|
status: Field.string(),
|
||||||
|
|||||||
@ -1,12 +1,12 @@
|
|||||||
// deno-lint-ignore-file no-explicit-any
|
// deno-lint-ignore-file no-explicit-any
|
||||||
import {
|
import {
|
||||||
Collection,
|
|
||||||
FieldDefinition,
|
FieldDefinition,
|
||||||
FILTER_OPTION_OPERATOR_SYMBOL,
|
FILTER_OPTION_OPERATOR_SYMBOL,
|
||||||
FILTER_OPTION_TYPE_SYMBOL,
|
FILTER_OPTION_TYPE_SYMBOL,
|
||||||
FILTER_OPTION_VALUE_SYMBOL,
|
FILTER_OPTION_VALUE_SYMBOL,
|
||||||
FilterOptions,
|
FilterOptions,
|
||||||
FilterValue,
|
FilterValue,
|
||||||
|
Model,
|
||||||
MultiFilterOption,
|
MultiFilterOption,
|
||||||
SingleFilterOption,
|
SingleFilterOption,
|
||||||
} from "@fabric/domain";
|
} from "@fabric/domain";
|
||||||
@ -24,7 +24,7 @@ export function filterToSQL(filterOptions?: FilterOptions) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function filterToParams(
|
export function filterToParams(
|
||||||
collection: Collection,
|
collection: Model,
|
||||||
filterOptions?: FilterOptions,
|
filterOptions?: FilterOptions,
|
||||||
) {
|
) {
|
||||||
if (!filterOptions) return {};
|
if (!filterOptions) return {};
|
||||||
@ -100,7 +100,7 @@ function getWhereFromKeyValue(
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getParamsFromMultiFilterOption(
|
function getParamsFromMultiFilterOption(
|
||||||
collection: Collection,
|
collection: Model,
|
||||||
filterOptions: MultiFilterOption,
|
filterOptions: MultiFilterOption,
|
||||||
) {
|
) {
|
||||||
return filterOptions.reduce(
|
return filterOptions.reduce(
|
||||||
@ -115,7 +115,7 @@ function getParamsFromMultiFilterOption(
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getParamsFromSingleFilterOption(
|
function getParamsFromSingleFilterOption(
|
||||||
collection: Collection,
|
collection: Model,
|
||||||
filterOptions: SingleFilterOption,
|
filterOptions: SingleFilterOption,
|
||||||
opts: { postfix?: string } = {},
|
opts: { postfix?: string } = {},
|
||||||
) {
|
) {
|
||||||
|
|||||||
@ -1,9 +1,9 @@
|
|||||||
import { defineCollection, Field } from "@fabric/domain";
|
import { defineModel, Field } from "@fabric/domain";
|
||||||
import { describe, expect, test } from "@fabric/testing";
|
import { describe, expect, test } from "@fabric/testing";
|
||||||
import { modelToSql } from "./model-to-sql.ts";
|
import { modelToSql } from "./model-to-sql.ts";
|
||||||
|
|
||||||
describe("ModelToSQL", () => {
|
describe("ModelToSQL", () => {
|
||||||
const model = defineCollection("something", {
|
const model = defineModel("something", {
|
||||||
id: Field.uuid({ isPrimaryKey: true }),
|
id: Field.uuid({ isPrimaryKey: true }),
|
||||||
name: Field.string(),
|
name: Field.string(),
|
||||||
age: Field.integer(),
|
age: Field.integer(),
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
// deno-lint-ignore-file no-explicit-any
|
// deno-lint-ignore-file no-explicit-any
|
||||||
import { Variant, VariantTag } from "@fabric/core";
|
import { Variant, VariantTag } from "@fabric/core";
|
||||||
import { Collection, FieldDefinition, getTargetKey } from "@fabric/domain";
|
import { FieldDefinition, getTargetKey, Model } from "@fabric/domain";
|
||||||
|
|
||||||
type FieldSQLDefinitionMap = {
|
type FieldSQLDefinitionMap = {
|
||||||
[K in FieldDefinition[VariantTag]]: (
|
[K in FieldDefinition[VariantTag]]: (
|
||||||
@ -61,7 +61,7 @@ function modifiersFromOpts(field: FieldDefinition) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function modelToSql(
|
export function modelToSql(
|
||||||
model: Collection<string, Record<string, FieldDefinition>>,
|
model: Model<string, Record<string, FieldDefinition>>,
|
||||||
) {
|
) {
|
||||||
const fields = Object.entries(model.fields)
|
const fields = Object.entries(model.fields)
|
||||||
.map(([name, type]) => fieldDefinitionToSQL(name, type))
|
.map(([name, type]) => fieldDefinitionToSQL(name, type))
|
||||||
|
|||||||
@ -23,7 +23,10 @@ export function recordToSQLKeyParams(record: Record<string, any>) {
|
|||||||
/**
|
/**
|
||||||
* Unfold a record into a string of it's keys separated by commas.
|
* Unfold a record into a string of it's keys separated by commas.
|
||||||
*/
|
*/
|
||||||
export function recordToSQLParams(model: Model, record: Record<string, any>) {
|
export function recordToSQLParams(
|
||||||
|
model: Model,
|
||||||
|
record: Record<string, any>,
|
||||||
|
) {
|
||||||
return Object.keys(record).reduce(
|
return Object.keys(record).reduce(
|
||||||
(acc, key) => ({
|
(acc, key) => ({
|
||||||
...acc,
|
...acc,
|
||||||
|
|||||||
@ -1,8 +1,8 @@
|
|||||||
// deno-lint-ignore-file no-explicit-any
|
// deno-lint-ignore-file no-explicit-any
|
||||||
import { PosixDate, VariantTag } from "@fabric/core";
|
import { PosixDate, VariantTag } from "@fabric/core";
|
||||||
import { Collection, FieldDefinition, FieldToType } from "@fabric/domain";
|
import { FieldDefinition, FieldToType, Model } from "@fabric/domain";
|
||||||
|
|
||||||
export function transformRow(model: Collection) {
|
export function transformRow(model: Model) {
|
||||||
return (row: Record<string, any>) => {
|
return (row: Record<string, any>) => {
|
||||||
const result: Record<string, any> = {};
|
const result: Record<string, any> = {};
|
||||||
for (const key in row) {
|
for (const key in row) {
|
||||||
|
|||||||
@ -1,8 +1,8 @@
|
|||||||
// deno-lint-ignore-file no-explicit-any
|
// deno-lint-ignore-file no-explicit-any
|
||||||
import { AsyncResult, Keyof, Optional } from "@fabric/core";
|
import { AsyncResult, Keyof, Optional } from "@fabric/core";
|
||||||
import {
|
import {
|
||||||
Collection,
|
|
||||||
FilterOptions,
|
FilterOptions,
|
||||||
|
Model,
|
||||||
ModelSchema,
|
ModelSchema,
|
||||||
OrderByOptions,
|
OrderByOptions,
|
||||||
SelectableQuery,
|
SelectableQuery,
|
||||||
@ -128,7 +128,7 @@ export class QueryBuilder<T> implements StoreQuery<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function getSelectStatement(
|
export function getSelectStatement(
|
||||||
collection: Collection,
|
collection: Model,
|
||||||
query: StoreQueryDefinition,
|
query: StoreQueryDefinition,
|
||||||
): [string, Record<string, any>] {
|
): [string, Record<string, any>] {
|
||||||
const selectFields = query.keys ? query.keys.join(", ") : "*";
|
const selectFields = query.keys ? query.keys.join(", ") : "*";
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
import { PosixDate, Run } from "@fabric/core";
|
import { PosixDate, Run } from "@fabric/core";
|
||||||
import { defineModel, Field, isLike, UUID } from "@fabric/domain";
|
import { defineAggregateModel, Field, isLike, UUID } from "@fabric/domain";
|
||||||
import { UUIDGeneratorMock } from "@fabric/domain/mocks";
|
import { UUIDGeneratorMock } from "@fabric/domain/mocks";
|
||||||
import {
|
import {
|
||||||
afterEach,
|
afterEach,
|
||||||
@ -13,11 +13,11 @@ import { SQLiteStateStore } from "./state-store.ts";
|
|||||||
|
|
||||||
describe("State Store", () => {
|
describe("State Store", () => {
|
||||||
const models = [
|
const models = [
|
||||||
defineModel("demo", {
|
defineAggregateModel("demo", {
|
||||||
value: Field.float(),
|
value: Field.float(),
|
||||||
owner: Field.reference({ targetModel: "users" }),
|
owner: Field.reference({ targetModel: "users" }),
|
||||||
}),
|
}),
|
||||||
defineModel("users", {
|
defineAggregateModel("users", {
|
||||||
name: Field.string(),
|
name: Field.string(),
|
||||||
}),
|
}),
|
||||||
];
|
];
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
import { AsyncResult, UnexpectedError } from "@fabric/core";
|
import { AsyncResult, UnexpectedError } from "@fabric/core";
|
||||||
import {
|
import {
|
||||||
Model,
|
type Model,
|
||||||
ModelSchemaFromModels,
|
ModelSchemaFromModels,
|
||||||
ModelToType,
|
ModelToType,
|
||||||
StoreQuery,
|
StoreQuery,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user