mirror of
https://github.com/Paris-est-Ludique/intranet.git
synced 2025-06-09 00:54:21 +02:00
Makes all gSheet calls sequential
This commit is contained in:
parent
bf62510d0a
commit
db3dba7a40
@ -3,6 +3,10 @@ import _ from "lodash"
|
||||
import { promises as fs } from "fs"
|
||||
import { GoogleSpreadsheet, GoogleSpreadsheetWorksheet } from "google-spreadsheet"
|
||||
|
||||
import sequentialDBOperations from "./sequentialDBOperations"
|
||||
|
||||
const addDBOperation = sequentialDBOperations()
|
||||
|
||||
const SCOPES = ["https://www.googleapis.com/auth/spreadsheets"]
|
||||
const CRED_PATH = path.resolve(process.cwd(), "access/gsheets.json")
|
||||
|
||||
@ -13,6 +17,7 @@ export async function listGet<Element extends ElementWithId>(
|
||||
specimen: Element
|
||||
): Promise<Element[]> {
|
||||
type StringifiedElement = Record<keyof Element, string>
|
||||
return addDBOperation(async () => {
|
||||
const sheet = await getGSheet(sheetName)
|
||||
|
||||
// Load sheet into an array of objects
|
||||
@ -35,6 +40,7 @@ export async function listGet<Element extends ElementWithId>(
|
||||
})
|
||||
|
||||
return elements
|
||||
})
|
||||
}
|
||||
|
||||
export async function get<Element extends ElementWithId>(
|
||||
@ -42,6 +48,7 @@ export async function get<Element extends ElementWithId>(
|
||||
membreId: number,
|
||||
specimen: Element
|
||||
): Promise<Element | undefined> {
|
||||
// No need to addDBOperation here, since listGet does it already
|
||||
const list = await listGet<Element>(sheetName, specimen)
|
||||
return list.find((element) => element.id === membreId)
|
||||
}
|
||||
@ -50,6 +57,7 @@ export async function setList<Element extends ElementWithId>(
|
||||
sheetName: string,
|
||||
elements: Element[]
|
||||
): Promise<true | undefined> {
|
||||
return addDBOperation(async () => {
|
||||
const sheet = await getGSheet(sheetName)
|
||||
|
||||
// Load sheet into an array of objects
|
||||
@ -57,7 +65,10 @@ export async function setList<Element extends ElementWithId>(
|
||||
if (!rows[0]) {
|
||||
throw new Error(`No column types defined in sheet ${sheetName}`)
|
||||
}
|
||||
const types = _.pick(rows[0], Object.keys(elements[0] || {})) as Record<keyof Element, string>
|
||||
const types = _.pick(rows[0], Object.keys(elements[0] || {})) as Record<
|
||||
keyof Element,
|
||||
string
|
||||
>
|
||||
|
||||
// Update received rows
|
||||
let rowid = 1
|
||||
@ -96,6 +107,7 @@ export async function setList<Element extends ElementWithId>(
|
||||
}
|
||||
|
||||
return true
|
||||
})
|
||||
}
|
||||
|
||||
export async function set<Element extends ElementWithId>(
|
||||
@ -105,6 +117,7 @@ export async function set<Element extends ElementWithId>(
|
||||
if (!element) {
|
||||
return undefined
|
||||
}
|
||||
return addDBOperation(async () => {
|
||||
const sheet = await getGSheet(sheetName)
|
||||
|
||||
// Load sheet into an array of objects
|
||||
@ -124,6 +137,7 @@ export async function set<Element extends ElementWithId>(
|
||||
Object.assign(row, stringifiedRow)
|
||||
await row.save()
|
||||
return element
|
||||
})
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/ban-types
|
||||
@ -134,6 +148,7 @@ export async function add<ElementNoId extends object, Element extends ElementNoI
|
||||
if (!partialElement) {
|
||||
return undefined
|
||||
}
|
||||
return addDBOperation(async () => {
|
||||
const sheet = await getGSheet(sheetName)
|
||||
|
||||
// Load sheet into an array of objects
|
||||
@ -159,6 +174,7 @@ export async function add<ElementNoId extends object, Element extends ElementNoI
|
||||
await sheet.addRow(stringifiedRow)
|
||||
|
||||
return element
|
||||
})
|
||||
}
|
||||
|
||||
async function getGSheet(sheetName: string): Promise<GoogleSpreadsheetWorksheet> {
|
||||
|
30
src/gsheets/sequentialDBOperations.ts
Normal file
30
src/gsheets/sequentialDBOperations.ts
Normal file
@ -0,0 +1,30 @@
|
||||
export default function sequentialDBOperations<OperationReturn>(): any {
|
||||
interface Operation {
|
||||
task: () => Promise<OperationReturn>
|
||||
resolve: (value: OperationReturn) => void
|
||||
reject: (reason: unknown) => void
|
||||
}
|
||||
|
||||
const operations: Operation[] = []
|
||||
|
||||
async function addDBOperation(task: () => Promise<OperationReturn>) {
|
||||
return new Promise(
|
||||
(resolve: (value: OperationReturn) => void, reject: (reason: unknown) => void) => {
|
||||
operations.push({ task, resolve, reject })
|
||||
if (operations.length === 1) {
|
||||
task().then(resolve).catch(reject).finally(runNextDBOperation)
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
function runNextDBOperation(): void {
|
||||
operations.shift()
|
||||
if (operations[0]) {
|
||||
const { task, resolve, reject } = operations[0]
|
||||
task().then(resolve).catch(reject).finally(runNextDBOperation)
|
||||
}
|
||||
}
|
||||
|
||||
return addDBOperation
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user