Merge branch 'master' into chore/move-table-creation-to-sdk

This commit is contained in:
Adria Navarro 2024-10-09 13:49:45 +02:00 committed by GitHub
commit 761d48f4d5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 31 additions and 11 deletions

View File

@ -1,6 +1,6 @@
{ {
"$schema": "node_modules/lerna/schemas/lerna-schema.json", "$schema": "node_modules/lerna/schemas/lerna-schema.json",
"version": "2.32.12", "version": "2.32.13",
"npmClient": "yarn", "npmClient": "yarn",
"packages": [ "packages": [
"packages/*", "packages/*",

View File

@ -213,17 +213,21 @@ export class DatabaseImpl implements Database {
async getMultiple<T extends Document>( async getMultiple<T extends Document>(
ids: string[], ids: string[],
opts?: { allowMissing?: boolean } opts?: { allowMissing?: boolean; excludeDocs?: boolean }
): Promise<T[]> { ): Promise<T[]> {
// get unique // get unique
ids = [...new Set(ids)] ids = [...new Set(ids)]
const includeDocs = !opts?.excludeDocs
const response = await this.allDocs<T>({ const response = await this.allDocs<T>({
keys: ids, keys: ids,
include_docs: true, include_docs: includeDocs,
}) })
const rowUnavailable = (row: RowResponse<T>) => { const rowUnavailable = (row: RowResponse<T>) => {
// row is deleted - key lookup can return this // row is deleted - key lookup can return this
if (row.doc == null || ("deleted" in row.value && row.value.deleted)) { if (
(includeDocs && row.doc == null) ||
(row.value && "deleted" in row.value && row.value.deleted)
) {
return true return true
} }
return row.error === "not_found" return row.error === "not_found"
@ -237,7 +241,7 @@ export class DatabaseImpl implements Database {
const missingIds = missing.map(row => row.key).join(", ") const missingIds = missing.map(row => row.key).join(", ")
throw new Error(`Unable to get documents: ${missingIds}`) throw new Error(`Unable to get documents: ${missingIds}`)
} }
return rows.map(row => row.doc!) return rows.map(row => (includeDocs ? row.doc! : row.value))
} }
async remove(idOrDoc: string | Document, rev?: string) { async remove(idOrDoc: string | Document, rev?: string) {

View File

@ -1846,7 +1846,7 @@ describe.each([
}) })
describe("exportRows", () => { describe("exportRows", () => {
beforeAll(async () => { beforeEach(async () => {
table = await config.api.table.save(defaultTable()) table = await config.api.table.save(defaultTable())
}) })
@ -1883,6 +1883,16 @@ describe.each([
}) })
}) })
it("should allow exporting without filtering", async () => {
const existing = await config.api.row.save(table._id!, {})
const res = await config.api.row.exportRows(table._id!)
const results = JSON.parse(res)
expect(results.length).toEqual(1)
const row = results[0]
expect(row._id).toEqual(existing._id)
})
it("should allow exporting only certain columns", async () => { it("should allow exporting only certain columns", async () => {
const existing = await config.api.row.save(table._id!, {}) const existing = await config.api.row.save(table._id!, {})
const res = await config.api.row.exportRows(table._id!, { const res = await config.api.row.exportRows(table._id!, {

View File

@ -221,9 +221,15 @@ class LinkController {
link.id !== row._id && link.fieldName === linkedSchema.name link.id !== row._id && link.fieldName === linkedSchema.name
) )
// check all the related rows exist
const foundRecords = await this._db.getMultiple(
links.map(l => l.id),
{ allowMissing: true, excludeDocs: true }
)
// The 1 side of 1:N is already related to something else // The 1 side of 1:N is already related to something else
// You must remove the existing relationship // You must remove the existing relationship
if (links.length > 0) { if (foundRecords.length > 0) {
throw new Error( throw new Error(
`1:N Relationship Error: Record already linked to another.` `1:N Relationship Error: Record already linked to another.`
) )

View File

@ -62,10 +62,10 @@ export async function exportRows(
).rows.map(row => row.doc!) ).rows.map(row => row.doc!)
result = await outputProcessing(table, response) result = await outputProcessing(table, response)
} else if (query) { } else {
let searchResponse = await sdk.rows.search({ let searchResponse = await sdk.rows.search({
tableId, tableId,
query, query: query || {},
sort, sort,
sortOrder, sortOrder,
}) })

View File

@ -105,7 +105,7 @@ export class RowAPI extends TestAPI {
exportRows = async ( exportRows = async (
tableId: string, tableId: string,
body: ExportRowsRequest, body?: ExportRowsRequest,
format: RowExportFormat = RowExportFormat.JSON, format: RowExportFormat = RowExportFormat.JSON,
expectations?: Expectations expectations?: Expectations
) => { ) => {

View File

@ -133,7 +133,7 @@ export interface Database {
exists(docId: string): Promise<boolean> exists(docId: string): Promise<boolean>
getMultiple<T extends Document>( getMultiple<T extends Document>(
ids: string[], ids: string[],
opts?: { allowMissing?: boolean } opts?: { allowMissing?: boolean; excludeDocs?: boolean }
): Promise<T[]> ): Promise<T[]>
remove(idOrDoc: Document): Promise<Nano.DocumentDestroyResponse> remove(idOrDoc: Document): Promise<Nano.DocumentDestroyResponse>
remove(idOrDoc: string, rev?: string): Promise<Nano.DocumentDestroyResponse> remove(idOrDoc: string, rev?: string): Promise<Nano.DocumentDestroyResponse>