Path: blob/master/src/packages/sync/editor/db/sync.ts
1450 views
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { SyncDoc, SyncOpts0, SyncOpts } from "../generic/sync-doc";6import { from_str, DBDocument } from "./doc";7import { Document, DocType } from "../generic/types";89export interface SyncDBOpts0 extends SyncOpts0 {10primary_keys: string[];11string_cols: string[];12}1314export interface SyncDBOpts extends SyncDBOpts0 {15from_str: (str: string) => Document;16doctype: DocType;17}1819export class SyncDB extends SyncDoc {20constructor(opts: SyncDBOpts0) {21// Typescript question -- What is the right way to do this?22const opts1: SyncDBOpts = opts as unknown as SyncDBOpts;23if (opts1.primary_keys == null || opts1.primary_keys.length <= 0) {24throw Error("primary_keys must have length at least 1");25}26opts1.from_str = (str) =>27from_str(str, opts1.primary_keys, opts1.string_cols);28opts1.doctype = {29type: "db",30patch_format: 1,31opts: {32primary_keys: opts1.primary_keys,33string_cols: opts1.string_cols,34},35};36super(opts1 as SyncOpts);37}3839get_one(arg?) : any {40// I know it is really of type DBDocument.41return (this.get_doc() as DBDocument).get_one(arg);42}43}444546