Merge pull request #53 from thevahidal/bug/index-field

issue with indexed fields while creating table fixed
This commit is contained in:
Vahid Al
2022-11-01 12:25:19 +03:30
committed by GitHub
4 changed files with 23 additions and 7 deletions

View File

@@ -8,3 +8,4 @@ RATE_LIMIT_ENABLED=False
RATE_LIMIT_WINDOW_MS=1000
RATE_LIMIT_MAX_REQUESTS=10
DB=foobar.db

View File

@@ -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": {

View File

@@ -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 }
@@ -57,7 +58,7 @@ const createTable = async (req, res) => {
column += ` ON UPDATE ${foreignKey.onUpdate}`;
}
if (index) {
column += ` INDEX ${index}`;
indices.push(name);
}
return column;
@@ -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] = {
@@ -105,7 +120,7 @@ const createTable = async (req, res) => {
res.status(201).json({
message: 'Table created',
data: {
name,
name: tableName,
schema: generatedSchema,
},
});

View File

@@ -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. "
},