Skip to content

Commit

Permalink
refactor put requests and make schema generation platform aware
Browse files Browse the repository at this point in the history
  • Loading branch information
kldavis4 committed Apr 15, 2022
1 parent 903c6ff commit 558cc43
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 75 deletions.
6 changes: 6 additions & 0 deletions .changeset/little-dryers-smoke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@tinacms/cli': patch
'@tinacms/graphql': patch
---

Make schema init platform-aware and refactor database put requests
4 changes: 2 additions & 2 deletions packages/@tinacms/cli/src/cmds/compile/defaultSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

export const defaultSchema = `
export const defaultSchema = (sep: string) => `
import { defineSchema, defineConfig } from "tinacms";
export default defineSchema({
collections: [
{
label: "Blog Posts",
name: "posts",
path: "content/posts",
path: "content${sep}posts",
fields: [
{
type: "string",
Expand Down
4 changes: 1 addition & 3 deletions packages/@tinacms/cli/src/cmds/compile/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ import path from 'path'
import fs from 'fs-extra'
import { build } from 'esbuild'
import type { Loader } from 'esbuild'
import * as _ from 'lodash'
import type { TinaCloudSchema } from '@tinacms/graphql'
import { TinaSchemaValidationError } from '@tinacms/schema-tools'
import { dangerText, logText } from '../../utils/theme'
import { defaultSchema } from './defaultSchema'
import { logger } from '../../logger'
Expand Down Expand Up @@ -91,7 +89,7 @@ export const compile = async (
// Ensure there is a .tina/schema.ts file
await fs.ensureFile(file)
// Write a basic schema to it
await fs.writeFile(file, defaultSchema)
await fs.writeFile(file, defaultSchema(path.sep))
}

// Turns the schema into JS files so they can be run
Expand Down
112 changes: 50 additions & 62 deletions packages/@tinacms/graphql/src/database/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,43 @@ export class Database {
this.store = config.store
}

private collectionForPath = async (
filepath: string
): Promise<
| CollectionFieldsWithNamespace<true>
| CollectionTemplatesWithNamespace<true>
| undefined
> => {
const tinaSchema = await this.getSchema()
const collection = tinaSchema.schema.collections.find((collection) =>
filepath.startsWith(collection.path)
)
return collection
}

private async partitionPathsByCollection(documentPaths: string[]) {
const pathsByCollection: Record<string, string[]> = {}
const nonCollectionPaths: string[] = []
const collections: Record<
string,
| CollectionFieldsWithNamespace<true>
| CollectionTemplatesWithNamespace<true>
> = {}
for (const documentPath of documentPaths) {
const collection = await this.collectionForPath(documentPath)
if (collection) {
if (!pathsByCollection[collection.name]) {
pathsByCollection[collection.name] = []
}
collections[collection.name] = collection
pathsByCollection[collection.name].push(documentPath)
} else {
nonCollectionPaths.push(documentPath)
}
}
return { pathsByCollection, nonCollectionPaths, collections }
}

public get = async <T extends object>(filepath: string): Promise<T> => {
if (SYSTEM_FILES.includes(filepath)) {
throw new Error(`Unexpected get for config file ${filepath}`)
Expand Down Expand Up @@ -140,10 +177,7 @@ export class Database {
) => {
const { stringifiedFile, payload, keepTemplateKey } =
await this.stringifyFile(filepath, data)
const tinaSchema = await this.getSchema()
const collection = tinaSchema.schema.collections.find((collection) =>
filepath.startsWith(collection.path)
)
const collection = await this.collectionForPath(filepath)
let collectionIndexDefinitions
if (collection) {
const indexDefinitions = await this.getIndexDefinitions()
Expand All @@ -154,27 +188,23 @@ export class Database {
}
await this.store.put(filepath, payload, {
keepTemplateKey,
collection: collection.name,
collection: collection?.name,
indexDefinitions: collectionIndexDefinitions,
})
}

public put = async (filepath: string, data: { [key: string]: unknown }) => {
console.log('Database.put', filepath, data)
public put = async (
filepath: string,
data: { [key: string]: unknown },
collection?: string
) => {
if (SYSTEM_FILES.includes(filepath)) {
throw new Error(`Unexpected put for config file ${filepath}`)
} else {
const tinaSchema = await this.getSchema()
console.log('collections', tinaSchema.schema.collections)
const collection = tinaSchema.schema.collections.find((collection) => {
console.log(filepath, collection.path)
return filepath.startsWith(collection.path)
})
console.log('Database.put', collection)
let collectionIndexDefinitions
if (collection) {
const indexDefinitions = await this.getIndexDefinitions()
collectionIndexDefinitions = indexDefinitions?.[collection.name]
collectionIndexDefinitions = indexDefinitions?.[collection]
}

const { stringifiedFile, payload, keepTemplateKey } =
Expand All @@ -184,7 +214,7 @@ export class Database {
}
await this.store.put(filepath, payload, {
keepTemplateKey,
collection: collection?.name,
collection: collection,
indexDefinitions: collectionIndexDefinitions,
})
}
Expand Down Expand Up @@ -477,29 +507,8 @@ export class Database {
}

public deleteContentByPaths = async (documentPaths: string[]) => {
const pathsByCollection: Record<string, string[]> = {}
const nonCollectionPaths: string[] = []
const collections: Record<
string,
| CollectionFieldsWithNamespace<true>
| CollectionTemplatesWithNamespace<true>
> = {}
const tinaSchema = await this.getSchema()
for (const documentPath of documentPaths) {
const collection = tinaSchema.schema.collections.find((collection) =>
documentPath.startsWith(collection.path)
)

if (collection) {
if (!pathsByCollection[collection.name]) {
pathsByCollection[collection.name] = []
}
collections[collection.name] = collection
pathsByCollection[collection.name].push(documentPath)
} else {
nonCollectionPaths.push(documentPath)
}
}
const { pathsByCollection, nonCollectionPaths, collections } =
await this.partitionPathsByCollection(documentPaths)

for (const collection of Object.keys(pathsByCollection)) {
await _deleteIndexContent(
Expand All @@ -513,29 +522,8 @@ export class Database {
}

public indexContentByPaths = async (documentPaths: string[]) => {
const pathsByCollection: Record<string, string[]> = {}
const nonCollectionPaths: string[] = []
const collections: Record<
string,
| CollectionFieldsWithNamespace<true>
| CollectionTemplatesWithNamespace<true>
> = {}
const tinaSchema = await this.getSchema()
for (const documentPath of documentPaths) {
const collection = tinaSchema.schema.collections.find((collection) =>
documentPath.startsWith(collection.path)
)

if (collection) {
if (!pathsByCollection[collection.name]) {
pathsByCollection[collection.name] = []
}
collections[collection.name] = collection
pathsByCollection[collection.name].push(documentPath)
} else {
nonCollectionPaths.push(documentPath)
}
}
const { pathsByCollection, nonCollectionPaths, collections } =
await this.partitionPathsByCollection(documentPaths)

for (const collection of Object.keys(pathsByCollection)) {
await _indexContent(
Expand Down
12 changes: 4 additions & 8 deletions packages/@tinacms/graphql/src/resolver/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,9 +368,8 @@ export class Resolver {
collection
)

console.log('calling put3', realPath)
// @ts-ignore
await this.database.put(realPath, params)
await this.database.put(realPath, params, collection.name)
return this.getDocument(realPath)
}

Expand Down Expand Up @@ -402,8 +401,7 @@ export class Resolver {
params,
templateInfo.template
)
console.log('calling put', realPath)
await this.database.put(realPath, values)
await this.database.put(realPath, values, collection.name)
}
break
case 'union':
Expand All @@ -421,8 +419,7 @@ export class Resolver {
...this.buildFieldMutations(templateParams, template),
_template: lastItem(template.namespace),
}
console.log('calling put4', realPath)
await this.database.put(realPath, values)
await this.database.put(realPath, values, collection.name)
}
})
}
Expand All @@ -434,9 +431,8 @@ export class Resolver {
isCollectionSpecific ? args.params : args.params[collection.name],
collection
)
console.log('calling put2', realPath)
//@ts-ignore
await this.database.put(realPath, params)
await this.database.put(realPath, params, collection.name)
return this.getDocument(realPath)
}

Expand Down

0 comments on commit 558cc43

Please sign in to comment.