|
@@ -1,14 +1,12 @@ |
|
|
import {readFile, writeFile} from 'fs/promises'; |
|
|
|
|
|
import {join} from 'path'; |
|
|
|
|
|
|
|
|
import { readFile, writeFile } from 'fs/promises'; |
|
|
|
|
|
import { join } from 'path'; |
|
|
import { Resource, validation as v } from '@modal-sh/yasumi'; |
|
|
import { Resource, validation as v } from '@modal-sh/yasumi'; |
|
|
import { DataSource, ResourceIdConfig } from '@modal-sh/yasumi/backend'; |
|
|
import { DataSource, ResourceIdConfig } from '@modal-sh/yasumi/backend'; |
|
|
import assert from 'assert'; |
|
|
import assert from 'assert'; |
|
|
|
|
|
|
|
|
class ResourceNotPreparedError extends Error {} |
|
|
|
|
|
|
|
|
export class ResourceNotPreparedError extends Error {} |
|
|
|
|
|
|
|
|
const resourceNotPreparedError = new ResourceNotPreparedError(); |
|
|
|
|
|
|
|
|
|
|
|
class ResourceIdNotDesignatedError extends Error {} |
|
|
|
|
|
|
|
|
export class ResourceIdNotDesignatedError extends Error {} |
|
|
|
|
|
|
|
|
export class JsonLinesDataSource< |
|
|
export class JsonLinesDataSource< |
|
|
Schema extends v.BaseSchema = v.BaseSchema, |
|
|
Schema extends v.BaseSchema = v.BaseSchema, |
|
@@ -38,7 +36,7 @@ export class JsonLinesDataSource< |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
async initialize() { |
|
|
async initialize() { |
|
|
assert(typeof this.path === 'string', resourceNotPreparedError); |
|
|
|
|
|
|
|
|
assert(typeof this.path === 'string', new ResourceNotPreparedError()); |
|
|
|
|
|
|
|
|
try { |
|
|
try { |
|
|
const fileContents = await readFile(this.path, 'utf-8'); |
|
|
const fileContents = await readFile(this.path, 'utf-8'); |
|
@@ -59,14 +57,14 @@ export class JsonLinesDataSource< |
|
|
|
|
|
|
|
|
async newId() { |
|
|
async newId() { |
|
|
const idConfig = this.resource?.state.shared.get('idConfig') as ResourceIdConfig<any>; |
|
|
const idConfig = this.resource?.state.shared.get('idConfig') as ResourceIdConfig<any>; |
|
|
assert(typeof idConfig !== 'undefined', resourceNotPreparedError); |
|
|
|
|
|
|
|
|
assert(typeof idConfig !== 'undefined', new ResourceNotPreparedError()); |
|
|
const theNewId = await idConfig.generationStrategy(this); |
|
|
const theNewId = await idConfig.generationStrategy(this); |
|
|
return theNewId as string; |
|
|
return theNewId as string; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
async getById(idSerialized: string) { |
|
|
async getById(idSerialized: string) { |
|
|
assert(typeof this.resource !== 'undefined', resourceNotPreparedError); |
|
|
|
|
|
assert(typeof this.path === 'string', resourceNotPreparedError); |
|
|
|
|
|
|
|
|
assert(typeof this.resource !== 'undefined', new ResourceNotPreparedError()); |
|
|
|
|
|
assert(typeof this.path === 'string', new ResourceNotPreparedError()); |
|
|
|
|
|
|
|
|
const idAttr = this.resource.state.shared.get('idAttr'); |
|
|
const idAttr = this.resource.state.shared.get('idAttr'); |
|
|
assert(typeof idAttr === 'string', new ResourceIdNotDesignatedError()); |
|
|
assert(typeof idAttr === 'string', new ResourceIdNotDesignatedError()); |
|
@@ -87,8 +85,8 @@ export class JsonLinesDataSource< |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
async create(data: Data) { |
|
|
async create(data: Data) { |
|
|
assert(typeof this.resource !== 'undefined', resourceNotPreparedError); |
|
|
|
|
|
assert(typeof this.path === 'string', resourceNotPreparedError); |
|
|
|
|
|
|
|
|
assert(typeof this.resource !== 'undefined', new ResourceNotPreparedError()); |
|
|
|
|
|
assert(typeof this.path === 'string', new ResourceNotPreparedError()); |
|
|
|
|
|
|
|
|
const idAttr = this.resource.state.shared.get('idAttr'); |
|
|
const idAttr = this.resource.state.shared.get('idAttr'); |
|
|
assert(typeof idAttr === 'string', new ResourceIdNotDesignatedError()); |
|
|
assert(typeof idAttr === 'string', new ResourceIdNotDesignatedError()); |
|
@@ -115,8 +113,8 @@ export class JsonLinesDataSource< |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
async delete(idSerialized: string) { |
|
|
async delete(idSerialized: string) { |
|
|
assert(typeof this.resource !== 'undefined', resourceNotPreparedError); |
|
|
|
|
|
assert(typeof this.path === 'string', resourceNotPreparedError); |
|
|
|
|
|
|
|
|
assert(typeof this.resource !== 'undefined', new ResourceNotPreparedError()); |
|
|
|
|
|
assert(typeof this.path === 'string', new ResourceNotPreparedError()); |
|
|
|
|
|
|
|
|
const idAttr = this.resource.state.shared.get('idAttr'); |
|
|
const idAttr = this.resource.state.shared.get('idAttr'); |
|
|
assert(typeof idAttr === 'string', new ResourceIdNotDesignatedError()); |
|
|
assert(typeof idAttr === 'string', new ResourceIdNotDesignatedError()); |
|
@@ -135,8 +133,8 @@ export class JsonLinesDataSource< |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
async emplace(idSerialized: string, dataWithId: Data) { |
|
|
async emplace(idSerialized: string, dataWithId: Data) { |
|
|
assert(typeof this.resource !== 'undefined', resourceNotPreparedError); |
|
|
|
|
|
assert(typeof this.path === 'string', resourceNotPreparedError); |
|
|
|
|
|
|
|
|
assert(typeof this.resource !== 'undefined', new ResourceNotPreparedError()); |
|
|
|
|
|
assert(typeof this.path === 'string', new ResourceNotPreparedError()); |
|
|
|
|
|
|
|
|
const idAttr = this.resource.state.shared.get('idAttr'); |
|
|
const idAttr = this.resource.state.shared.get('idAttr'); |
|
|
assert(typeof idAttr === 'string', new ResourceIdNotDesignatedError()); |
|
|
assert(typeof idAttr === 'string', new ResourceIdNotDesignatedError()); |
|
@@ -171,8 +169,8 @@ export class JsonLinesDataSource< |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
async patch(idSerialized: string, data: Partial<Data>) { |
|
|
async patch(idSerialized: string, data: Partial<Data>) { |
|
|
assert(typeof this.resource !== 'undefined', resourceNotPreparedError); |
|
|
|
|
|
assert(typeof this.path === 'string', resourceNotPreparedError); |
|
|
|
|
|
|
|
|
assert(typeof this.resource !== 'undefined', new ResourceNotPreparedError()); |
|
|
|
|
|
assert(typeof this.path === 'string', new ResourceNotPreparedError()); |
|
|
|
|
|
|
|
|
const idAttr = this.resource.state.shared.get('idAttr'); |
|
|
const idAttr = this.resource.state.shared.get('idAttr'); |
|
|
assert(typeof idAttr === 'string', new ResourceIdNotDesignatedError()); |
|
|
assert(typeof idAttr === 'string', new ResourceIdNotDesignatedError()); |
|
|