Skip to content

Commit

Permalink
feat: add basic table and spreadsheet type interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Th1nkK1D committed Apr 25, 2024
1 parent 4786e52 commit c6ae100
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
20 changes: 20 additions & 0 deletions examples/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Type } from '@sinclair/typebox';
import { Spreadsheet, Table } from '../src';

const userTable = Table('users', {
id: Type.Number(),
value: Type.String(),
});

const itemTable = Table('items', {
id: Type.Number(),
name: Type.String(),
});

const sheet = Spreadsheet('1SbX2kgAGsslbhGuB-EI_YdSAnIt3reU1_OEtWmDVOVk', [
userTable,
itemTable,
]);

const users = sheet.get('users');
const items = sheet.get('items');
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './spreadsheet';
export * from './table';
25 changes: 25 additions & 0 deletions src/spreadsheet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {
Type,
type Static,
type TKeyOf,
type TIndexFromPropertyKey,
type Intersect,
} from '@sinclair/typebox';
import type { TableDefition } from './table';

export function Spreadsheet<T extends TableDefition<any, any>[]>(
sheetsId: string,
tables: [...T]
) {
const tablesSchema = Type.Intersect(tables);

return {
get<N extends Static<TKeyOf<typeof tablesSchema>>>(
tableName: N
): Static<TIndexFromPropertyKey<Intersect<T>, N>> {
const itemSchema = Type.Index(tablesSchema, [tableName]);

return {} as Static<typeof itemSchema>;
},
};
}
19 changes: 19 additions & 0 deletions src/table.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {
Type,
type TProperties,
type TObject,
type TConst,
type TRecordOrObject,
} from '@sinclair/typebox';

export type TableDefition<
N extends string,
S extends TProperties
> = TRecordOrObject<TConst<N>, TObject<S>>;

export function Table<N extends string, S extends TProperties>(
name: N,
schema: S
) {
return Type.Record(Type.Const(name), Type.Object(schema));
}

0 comments on commit c6ae100

Please sign in to comment.