Skip to content

Commit

Permalink
fix(datastore-storage-adapter): don't enforce required model fields i…
Browse files Browse the repository at this point in the history
…n sql (#9166)
  • Loading branch information
iartemiev committed Nov 5, 2021
1 parent 873941c commit fbe1f2e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 8 deletions.
54 changes: 52 additions & 2 deletions packages/datastore-storage-adapter/__tests__/SQLiteUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ const INTERNAL_TEST_SCHEMA_STATEMENTS = [
const INTERNAL_TEST_SCHEMA_MANY_TO_MANY_STATEMENT =
'CREATE TABLE IF NOT EXISTS "PostEditor" ("id" PRIMARY KEY NOT NULL, "post" TEXT, "postID" TEXT NOT NULL, "editor" TEXT, "editorID" TEXT NOT NULL, "createdAt" TEXT, "updatedAt" TEXT, "_version" INTEGER, "_lastChangedAt" INTEGER, "_deleted" INTEGER);';

const INTERNAL_TEST_SCHEMA_ONE_TO_MANY_STATEMENT =
'CREATE TABLE IF NOT EXISTS "Post" ("id" PRIMARY KEY NOT NULL, "title" TEXT NOT NULL, "comments" TEXT, "_version" INTEGER, "_lastChangedAt" INTEGER, "_deleted" INTEGER);';

describe('SQLiteUtils tests', () => {
let Model: PersistentModelConstructor<Model>;

Expand All @@ -59,17 +62,23 @@ describe('SQLiteUtils tests', () => {
});

describe('modelCreateTableStatement', () => {
it('should generate valid CREATE TABLE statement from a M:N join table model with implicit FKs', () => {
it('should generate a valid CREATE TABLE statement from a M:N join table model with implicit FKs', () => {
expect(modelCreateTableStatement(postEditorImplicit, true)).toEqual(
INTERNAL_TEST_SCHEMA_MANY_TO_MANY_STATEMENT
);
});

it('should generate valid CREATE TABLE statement from a M:N join table model with explicit FKs', () => {
it('should generate a valid CREATE TABLE statement from a M:N join table model with explicit FKs', () => {
expect(modelCreateTableStatement(postEditorExplicit, true)).toEqual(
INTERNAL_TEST_SCHEMA_MANY_TO_MANY_STATEMENT
);
});

it('should generate a valid CREATE TABLE statement from a 1:M join table model', () => {
expect(modelCreateTableStatement(postWithRequiredComments, true)).toEqual(
INTERNAL_TEST_SCHEMA_ONE_TO_MANY_STATEMENT
);
});
});

describe('implicitAuthFieldsForModel', () => {
Expand Down Expand Up @@ -656,3 +665,44 @@ const groupsAuthExplicit: SchemaModel = {
},
],
};

const postWithRequiredComments: SchemaModel = {
name: 'Post',
pluralName: 'Posts',
fields: {
id: {
name: 'id',
isArray: false,
type: 'ID',
isRequired: true,
attributes: [],
},
title: {
name: 'title',
isArray: false,
type: 'String',
isRequired: true,
attributes: [],
},
comments: {
name: 'comments',
isArray: true,
type: {
model: 'Comment',
},
isRequired: true,
attributes: [],
isArrayNullable: true,
association: {
connectionType: 'HAS_MANY',
associatedWith: 'post',
},
},
},
attributes: [
{
type: 'model',
properties: {},
},
],
};
Original file line number Diff line number Diff line change
Expand Up @@ -133,23 +133,25 @@ export function modelCreateTableStatement(
}

if (isModelFieldType(field.type)) {
let columnParam = `"${field.name}" TEXT`;

// add targetName as well as field name for BELONGS_TO relations
if (isTargetNameAssociation(field.association)) {
const required = field.isRequired ? ' NOT NULL' : '';

let columnParam = `"${field.name}" TEXT`;
// check if this field has been explicitly defined in the model
const fkDefinedInModel = Object.values(model.fields).find(
(f: ModelField) => f.name === field.association.targetName
);

// only add auto-generate it if not
// if the FK is not explicitly defined in the model, we have to add it here
if (!fkDefinedInModel) {
const required = field.isRequired ? ' NOT NULL' : '';
columnParam += `, "${field.association.targetName}" TEXT${required}`;
}

return acc + `, ${columnParam}`;
}

// ignore isRequired param for model fields, since they will not contain
// the related data locally
return acc + `, ${columnParam}`;
}

// default to TEXT
Expand Down

0 comments on commit fbe1f2e

Please sign in to comment.