Skip to content

Instantly share code, notes, and snippets.

@izelnakri
Last active July 10, 2021 12:51
Show Gist options
  • Select an option

  • Save izelnakri/2d8c20771d258fe50d09824d6cc1cdce to your computer and use it in GitHub Desktop.

Select an option

Save izelnakri/2d8c20771d258fe50d09824d6cc1cdce to your computer and use it in GitHub Desktop.
type RequireOnlyOne<T, Keys extends keyof T = keyof T> = Pick<T, Exclude<keyof T, Keys>> &
{
[K in Keys]-?: Required<Pick<T, K>> & Partial<Record<Exclude<Keys, K>, undefined>>;
}[Keys];
export interface ModelRefShape {
id?: number;
uuid?: string;
[propName: string]: any;
}
export type ModelRef = RequireOnlyOne<ModelRefShape, "id" | "uuid">;
export default class Model {
static Adapter = MemoryAdapter; // CAN BE: RESTAdapter, SQLAdapter, GraphQLAdapter, JSONAPIAdapter
static resetCache(fixtures?: Array<ModelRef>): Array<ModelRef> {
return this.Adapter.resetCache(this, fixtures);
}
static async resetRecords(targetState?: ModelRef[]): Promise<ModelRef[]> {
return await this.Adapter.resetRecords(this, targetState);
}
static peek(primaryKey: primaryKey | primaryKey[]): ModelRef[] | ModelRef | void {
return this.Adapter.peek(this, primaryKey);
}
static peekBy(queryObject: object): ModelRef | void {
return this.Adapter.peekBy(this, queryObject);
}
static peekAll(queryObject: object = {}): ModelRef[] | void {
return this.Adapter.peekAll(this, queryObject);
}
static async count(): Promise<number> {
return await this.Adapter.count(this);
}
static async find(primaryKey: primaryKey | primaryKey[]): Promise<ModelRef[] | ModelRef | void> {
return await this.Adapter.find(this, primaryKey);
}
static async findBy(queryObject: object): Promise<ModelRef | void> {
return await this.Adapter.findBy(this, queryObject);
}
static async findAll(queryObject: object = {}): Promise<ModelRef[] | void> {
return await this.Adapter.findAll(this, queryObject);
}
static async save(record: ModelRef): Promise<ModelRef> {
return await this.Adapter.save(this, record);
}
static async insert(record?: ModelRef): Promise<ModelRef> {
return await this.Adapter.insert(this, record);
}
static async update(record: ModelRef): Promise<ModelRef> {
return await this.Adapter.update(this, record);
}
static unload(record: ModelRef): ModelRef {
return this.Adapter.unload(this, record);
}
static async delete(record: ModelRef): Promise<ModelRef> {
return await this.Adapter.delete(this, record);
}
static async saveAll(records: ModelRef[]): Promise<ModelRef[]> {
return await this.Adapter.saveAll(this, records);
}
static async insertAll(records: ModelRef[]): Promise<ModelRef[]> {
return await this.Adapter.insertAll(this, records);
}
static async updateAll(records: ModelRef[]): Promise<ModelRef[]> {
return await this.Adapter.updateAll(this, records);
}
static unloadAll(records?: ModelRef[]): void {
return this.Adapter.unloadAll(this, records);
}
static async deleteAll(records: ModelRef[]): Promise<void> {
return await this.Adapter.deleteAll(this, records);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment