Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

40 add support for additional table creation parameters #41

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,25 @@ To create a new table, use the `createTable` command:

```typescript
createTable('firstTable', { // inferred table name and entry
id: 'int', // only allows for known data types ('int', 'char', 'blob')
job: 'char',
name: 'char',
sex: 'char',
hasReadTheReadme: 'bool'
id: {
autoincrement: true,
type: 'INTEGER', // only allows for known data types ('int', 'char', 'blob')
nullable: false,
primary: true,
unique: true,
},
job: {
type: 'char',
},
name: {
type: 'char',
},
sex: {
type: 'char',
},
hasReadTheReadme: {
type: 'bool',
},
})
```

Expand Down
26 changes: 20 additions & 6 deletions playground/src/sibyl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,26 @@ const db = new SQL.Database()
const { createTable, Insert, All, Select, Create, Update } = await Sibyl<Tables>(db)

createTable('orders', {
currency: 'char',
product: 'char',
id: 'int',
price: 'char',
booleanTest: 'bool',
status: 'varchar',
currency: {
type: 'char',
},
product: {
type: 'char',
},
id: {
autoincrement: true,
type: 'INTEGER',
primary: true,
},
price: {
type: 'char',
},
booleanTest: {
type: 'bool',
},
status: {
type: 'varchar',
},
})

const insertions: Order[] = []
Expand Down
12 changes: 6 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import type { Database } from 'sql.js'
import { buildSelectQuery, buildUpdateQuery, convertBooleanValues, convertCreateTableStatement, convertToObjects, formatInsertStatement, objectToWhereClause } from './sibylLib'
import type { DeleteArgs, SelectArgs, UpdateArgs } from './types'
import type { DBBlob, DBBoolean, DBDate, DBEntry, DBNumber, DBString, DBValue, DeleteArgs, SelectArgs, UpdateArgs } from './types'

export default async function Sibyl<T extends Record<string, any>>(db: Database) {
type MappedTable<T> = {
[Key in keyof T]:
T[Key] extends boolean ? 'bool' :
T[Key] extends number ? 'int' | 'real' :
T[Key] extends string ? 'varchar' | 'char' :
T[Key] extends Date ? 'text' | 'int' | 'real' :
T[Key] extends Blob ? 'blob' :
T[Key] extends boolean ? DBValue<DBBoolean> :
T[Key] extends number ? DBValue<DBNumber> :
T[Key] extends string ? DBValue<DBString> :
T[Key] extends Date ? DBValue<DBDate> :
T[Key] extends Blob ? DBValue<DBBlob> :
null
}

Expand Down
24 changes: 21 additions & 3 deletions src/sibylLib.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { DataStructure, SelectArgs, SibylResponse, UpdateArgs } from './types'
import type { DBEntry, DataStructure, SelectArgs, SibylResponse, UpdateArgs } from './types'

export function formatInsertStatement<T extends Record<string, any>>(table: string, structs: T[]) {
const sortedStructs = sortKeys(structs)
Expand Down Expand Up @@ -121,8 +121,26 @@ export function buildUpdateQuery<T, K extends string | number | symbol = 'id'>(t
}
export function convertCreateTableStatement<T extends Record<string, any>>(obj: T): string {
let result = ''
for (const [columnName, columnType] of Object.entries(sortKeys([obj])[0]))
result += `${columnName} ${columnType}, `
for (const [columnName, columnType] of Object.entries<DBEntry<any>>(sortKeys([obj])[0])) {
result += columnName

if (columnType.type)
result += ` ${columnType.type}`

if (columnType.primary)
result += ' PRIMARY KEY'

if (columnType.autoincrement)
result += ' AUTOINCREMENT'

if (columnType.nullable === false)
result += ' NOT NULL'

if (columnType.unique)
result += ' UNIQUE'

result += ', '
}

result = result.slice(0, -2)
return result
Expand Down
22 changes: 15 additions & 7 deletions src/tests/convertCreateTableStatement.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
import { describe, expect, it } from 'vitest'
import { convertCreateTableStatement } from '../sibylLib'
import type { DBNumber, DBString, DBValue } from '../types'

interface TableRow {
id: 'int'
location: 'char'
name: 'char'
id: DBValue<DBNumber>
name: DBValue<DBString>
}

describe('convertCreateTableStatement tests', () => {
it('converts a table object to a statement', async () => {
const actual = convertCreateTableStatement<TableRow>({
name: 'char',
id: 'int',
location: 'char',
id: {
autoincrement: true,
type: 'INTEGER',
nullable: false,
primary: true,
unique: true,
},
name: {
type: 'char',
nullable: false,
},
})

const expectation = 'id int, location char, name char'
const expectation = 'id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE, name char NOT NULL'
expect(actual).toStrictEqual(expectation)
})
})
19 changes: 15 additions & 4 deletions src/tests/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,21 @@ describe('create tests', () => {
const { createTable, Create } = await Sibyl<Tables>(db)

createTable('first', {
id: 'int',
location: 'char',
name: 'char',
booleanTest: 'bool',
id: {
autoincrement: true,
type: 'INTEGER',
primary: true,
unique: true,
},
location: {
type: 'char',
},
name: {
type: 'char',
},
booleanTest: {
type: 'bool',
},
})
const actual = Create('first', {
name: 'Craig',
Expand Down
26 changes: 18 additions & 8 deletions src/tests/delete.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,19 @@ describe('delete tests', () => {
const { createTable, Insert, All, Delete } = await Sibyl<Tables>(db)

createTable('first', {
id: 'int',
location: 'char',
name: 'char',
id: {
autoincrement: true,
type: 'INTEGER',
nullable: false,
primary: true,
unique: true,
},
location: {
type: 'char',
},
name: {
type: 'char',
},
})
Insert('first', [
{
Expand All @@ -43,16 +53,16 @@ describe('delete tests', () => {
let actual = All('first')

let expectation = [
{
name: 'Craig',
id: 2344,
location: 'Brighton',
},
{
id: 1,
name: 'Bob',
location: 'Cornwall',
},
{
name: 'Craig',
id: 2344,
location: 'Brighton',
},
]
expect(actual).toStrictEqual(expectation)

Expand Down
Loading
Loading