Skip to content

Commit

Permalink
Merge pull request #53 from thevahidal/bug/index-field
Browse files Browse the repository at this point in the history
issue with indexed fields while creating table fixed
  • Loading branch information
thevahidal authored Nov 1, 2022
2 parents b74e30d + a6a47e5 commit 5079b6a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
1 change: 1 addition & 0 deletions core/.env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ RATE_LIMIT_ENABLED=False
RATE_LIMIT_WINDOW_MS=1000
RATE_LIMIT_MAX_REQUESTS=10

DB=foobar.db
2 changes: 1 addition & 1 deletion core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "soul-cli",
"version": "0.0.5",
"version": "0.0.6",
"description": "A SQLite RESTful server",
"main": "src/server.js",
"bin": {
Expand Down
25 changes: 20 additions & 5 deletions core/src/controllers/tables.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ const createTable = async (req, res) => {
}
*/
const {
name,
name: tableName,
schema,
autoAddCreatedAt = true,
autoAddUpdatedAt = true,
} = req.body;

let indices = [];
let schemaString = schema
// support name, type, default, not null, unique, primary key, foreign key, index
// e.g. { name: 'id', type: 'INTEGER', primaryKey: true }
Expand Down Expand Up @@ -57,7 +58,7 @@ const createTable = async (req, res) => {
column += ` ON UPDATE ${foreignKey.onUpdate}`;
}
if (index) {
column += ` INDEX ${index}`;
indices.push(name);
}

return column;
Expand Down Expand Up @@ -88,11 +89,25 @@ const createTable = async (req, res) => {
`;
}

const query = `CREATE TABLE ${name} (${schemaString})`;
let indicesString = indices
.map((field) => {
return `
CREATE INDEX ${tableName}_${field}_index
ON ${tableName} (${field})
`;
})
.join(';');

const query = `CREATE TABLE ${tableName} (${schemaString})`;

try {
db.prepare(query).run();

const generatedSchema = db.prepare(`PRAGMA table_info(${name})`).all();
if (indicesString) {
db.prepare(indicesString).run();
}

const generatedSchema = db.prepare(`PRAGMA table_info(${tableName})`).all();

/*
#swagger.responses[201] = {
Expand All @@ -105,7 +120,7 @@ const createTable = async (req, res) => {
res.status(201).json({
message: 'Table created',
data: {
name,
name: tableName,
schema: generatedSchema,
},
});
Expand Down
2 changes: 1 addition & 1 deletion core/src/swagger/swagger.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"swagger": "2.0",
"info": {
"version": "0.0.1",
"version": "0.0.6",
"title": "Soul API",
"description": "API Documentation for <b>Soul</b>, a simple SQLite RESTful server. "
},
Expand Down

0 comments on commit 5079b6a

Please sign in to comment.