diff --git a/README.md b/README.md index be77f82..80ede83 100644 --- a/README.md +++ b/README.md @@ -56,13 +56,14 @@ To create a new table (at the moment, it is recommended to only use one table), use the `createTable` command: ```typescript -createTable('id int, name char, sex char, job char') +createTable('id int, job char, name char, sex char') ``` `createTable` takes a single argument; This argument will create the specified columns for your database; It is vitally important that your columns match that of -your specified table row type you supply to Sibyl's root function, otherwise -you'll be unable to get data from your database, or crash your program. +your specified table row type you supply to Sibyl's root function, and that the entries +are in alphabetical order, otherwise +you'll be unable to get data from your database, have malformed data, or crash your program. ### Inserting entries into the DB diff --git a/playground/src/components/HelloWorld.vue b/playground/src/components/HelloWorld.vue index c2a643b..4cd68b1 100644 --- a/playground/src/components/HelloWorld.vue +++ b/playground/src/components/HelloWorld.vue @@ -24,7 +24,7 @@ const { createTable, Insert, Select, All } = await Sibyl(db, 'test const myResults = ref() const selection = ref() -createTable('id int, name char, sex char, job char') +createTable('id int, name char, job char, sex char') let insertions: tableRowType[] = [] for (let index = 0; index < 1000; index++) { diff --git a/src/index.ts b/src/index.ts index c1eee04..8193953 100644 --- a/src/index.ts +++ b/src/index.ts @@ -16,8 +16,20 @@ export async function Sibyl>(db: Database, table: db.run(`CREATE TABLE ${table} (${columns});`) } + function sortKeys(arr: T[]): T[] { + return arr.map(obj => { + const sortedKeys = Object.keys(obj).sort() + const sortedObj: { [key: string]: any } = {} + sortedKeys.forEach(key => { + sortedObj[key] = obj[key] + }) + return sortedObj as T + }) + } + function Insert(table: string, structs: T[]) { - const flattenedInsert = structs.map(obj => Object.values(obj)) + const sortedStructs = sortKeys(structs) + const flattenedInsert = sortedStructs.map(obj => Object.values(obj)) let insertions: string = '' for (const insert of flattenedInsert) { let row: T | string[] = [] @@ -106,5 +118,6 @@ export async function Sibyl>(db: Database, table: objectToWhereClause, buildSelectQuery, convertToObjects, + sortKeys, } } diff --git a/src/tests/all.test.ts b/src/tests/all.test.ts index fa84ca8..e96e30d 100644 --- a/src/tests/all.test.ts +++ b/src/tests/all.test.ts @@ -4,8 +4,8 @@ import { Sibyl } from '../index' interface TableRow { id: number - name: string location: string + name: string } describe('all tests', () => { @@ -19,7 +19,7 @@ describe('all tests', () => { const db = new SQL.Database() const { createTable, Insert, All } = await Sibyl(db, 'testingDB') - createTable('id int, name char, location char') + createTable('id int, location char, name char') const insertion = Insert(DBName, [ { id: 1, diff --git a/src/tests/insert.test.ts b/src/tests/insert.test.ts index 5c54f77..29e13f8 100644 --- a/src/tests/insert.test.ts +++ b/src/tests/insert.test.ts @@ -4,8 +4,8 @@ import { Sibyl } from '../index' interface TableRow { id: number - name: string location: string + name: string } describe('insert tests', () => { @@ -50,6 +50,30 @@ describe('insert tests', () => { }, ]) + expect(actual).toStrictEqual('INSERT INTO test VALUES (1,"Brighton","Craig"); INSERT INTO test VALUES (2,"Cornwall","Bob");') + }) + it('catches incorrect insert keys being the wrong way around and fixes itself', async () => { + const SQL = await sql({ + locateFile: () => { + return 'playground/public/sql-wasm.wasm' + }, + }) + const db = new SQL.Database() + const { Insert } = await Sibyl(db, 'testing-DB') + + const actual = Insert('test', [ + { + id: 1, + name: 'Craig', + location: 'Brighton', + }, + { + location: 'Cornwall', + id: 2, + name: 'Bob', + }, + ]) + expect(actual).toStrictEqual('INSERT INTO test VALUES (1,"Brighton","Craig"); INSERT INTO test VALUES (2,"Cornwall","Bob");') }) }) diff --git a/src/tests/sortKeys.test.ts b/src/tests/sortKeys.test.ts new file mode 100644 index 0000000..d1226f4 --- /dev/null +++ b/src/tests/sortKeys.test.ts @@ -0,0 +1,50 @@ +import { describe, expect, it } from 'vitest' +import sql from 'sql.js' +import { Sibyl } from '../index' + +interface TableRow { + id: number + name: string + location: string +} + +describe('sortKeys tests', () => { + it('Formats a given set of array of objects alphabetically by their keys', async () => { + const DBName = 'testingDB' + const SQL = await sql({ + locateFile: () => { + return 'playground/public/sql-wasm.wasm' + }, + }) + const db = new SQL.Database() + const { sortKeys } = await Sibyl(db, DBName) + + const unsortedArray = [ + { + name: 'Craig', + id: 1, + location: 'Brighton' + }, + { + location: 'Cornwall', + name: 'Bob', + id: 2, + } + ] + const actual = sortKeys(unsortedArray) + + const expectation = [ + { + id: 1, + location: 'Brighton', + name: 'Craig', + }, + { + id: 2, + location: 'Cornwall', + name: 'Bob', + } + ] + expect(actual).toStrictEqual(expectation) + }) +})