@keshavsoft/tallyextract 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +95 -0
- package/index.d.ts +7 -0
- package/index.js +1 -0
- package/package.json +30 -0
- package/src/core/configStore.js +10 -0
- package/src/helpers/getLatestVersion.js +15 -0
- package/src/scripts/generateSamples.js +57 -0
- package/src/scripts/samples/aggregate_count.js +5 -0
- package/src/scripts/samples/mutate_deleteWithChecks.js +5 -0
- package/src/scripts/samples/mutate_insertAsIs.js +5 -0
- package/src/scripts/samples/mutate_insertFlat.js +5 -0
- package/src/scripts/samples/mutate_insertGenPk.js +5 -0
- package/src/scripts/samples/mutate_insertWithChecks.js +5 -0
- package/src/scripts/samples/mutate_updateWithChecks.js +5 -0
- package/src/scripts/samples/query_filterByColumns.js +5 -0
- package/src/scripts/samples/query_filterByPk.js +5 -0
- package/src/scripts/samples/query_findAll.js +5 -0
- package/src/scripts/samples/query_findByPk.js +5 -0
- package/src/scripts/updateIndex.js +16 -0
- package/src/utils/pathBuilder.js +3 -0
- package/src/v1/Import/Templates/inventory.json +121 -0
- package/src/v1/Import/Templates/ledgers.json +28 -0
- package/src/v1/Import/Templates/main.json +224 -0
- package/src/v1/Import/Templates/template.json +224 -0
- package/src/v1/Import/ledgers.json +8 -0
- package/src/v1/Import/staticVariables.json +12 -0
- package/src/v1/api/index.js +3 -0
- package/src/v1/api/ledger.js +6 -0
- package/src/v1/api/stockItems.js +6 -0
- package/src/v1/cli.js +11 -0
- package/src/v1/config/getSchema.js +20 -0
- package/src/v1/core/sendToTally.js +21 -0
- package/src/v1/engine/index.js +5 -0
- package/src/v1/engine/mutate/delete/deleteBase.js +26 -0
- package/src/v1/engine/mutate/delete/deleteWithChecks.js +14 -0
- package/src/v1/engine/mutate/index.js +7 -0
- package/src/v1/engine/mutate/insert/insertAsIs.js +7 -0
- package/src/v1/engine/mutate/insert/insertBase.js +19 -0
- package/src/v1/engine/mutate/insert/insertFlat.js +24 -0
- package/src/v1/engine/mutate/insert/insertGenPk copy.js +39 -0
- package/src/v1/engine/mutate/insert/insertGenPk.js +41 -0
- package/src/v1/engine/mutate/insert/insertWithChecks.js +52 -0
- package/src/v1/engine/mutate/update/updateBase.js +26 -0
- package/src/v1/engine/mutate/update/updateWithChecks.js +18 -0
- package/src/v1/engine/query/aggregate/count.js +13 -0
- package/src/v1/engine/query/aggregate/index.js +1 -0
- package/src/v1/engine/query/filterByColumns.js +18 -0
- package/src/v1/engine/query/filterByPk.js +18 -0
- package/src/v1/engine/query/findAll.js +14 -0
- package/src/v1/engine/query/findByPk.js +18 -0
- package/src/v1/engine/query/index.js +8 -0
- package/src/v1/helpers/buildMutate.js +28 -0
- package/src/v1/helpers/buildQuery.js +27 -0
- package/src/v1/helpers/file/read.js +7 -0
- package/src/v1/helpers/file/write.js +5 -0
- package/src/v1/helpers/pkHelper.js +19 -0
- package/src/v1/helpers/validate.js +27 -0
- package/src/v1/helpers/validateHelper.js +27 -0
- package/src/v1/helpers/validators/default.js +5 -0
- package/src/v1/helpers/validators/enum.js +7 -0
- package/src/v1/helpers/validators/length.js +11 -0
- package/src/v1/helpers/validators/minMax.js +11 -0
- package/src/v1/helpers/validators/pattern.js +8 -0
- package/src/v1/helpers/validators/required.js +5 -0
- package/src/v1/helpers/validators/type.js +15 -0
- package/src/v1/helpers/validators/unique.js +8 -0
- package/src/v1/index.js +1 -0
- package/src/v1/schema/schema.json +133 -0
- package/src/v1/utils/readJson.js +6 -0
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import insertBase from "./insertBase.js";
|
|
2
|
+
|
|
3
|
+
import { getConfig } from "../../../../core/configStore.js";
|
|
4
|
+
import { buildDataPath } from "../../../../utils/pathBuilder.js";
|
|
5
|
+
import { readData } from "../../../helpers/file/read.js";
|
|
6
|
+
|
|
7
|
+
import { getSchema } from "../../../config/getSchema.js";
|
|
8
|
+
import { getPrimaryKey, attachPrimaryKey } from "../../../helpers/pkHelper.js";
|
|
9
|
+
|
|
10
|
+
const insertGenPk = ({ table, record }) => {
|
|
11
|
+
let schema;
|
|
12
|
+
try {
|
|
13
|
+
schema = getSchema(table);
|
|
14
|
+
} catch (err) {
|
|
15
|
+
console.error(err.message);
|
|
16
|
+
return; // or throw based on your design
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
// const schema = getSchema(table);
|
|
20
|
+
const pk = getPrimaryKey(schema.columns);
|
|
21
|
+
|
|
22
|
+
// ✅ if pk already exists, skip generation
|
|
23
|
+
if (record[pk] !== undefined) {
|
|
24
|
+
return insertBase({ table, record });
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const config = getConfig();
|
|
28
|
+
const path = buildDataPath(config, table);
|
|
29
|
+
const data = readData(path);
|
|
30
|
+
|
|
31
|
+
const newRecord = attachPrimaryKey(record, pk, data);
|
|
32
|
+
|
|
33
|
+
return insertBase({
|
|
34
|
+
table,
|
|
35
|
+
record: newRecord
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export default insertGenPk;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import insertBase from "./insertBase.js";
|
|
2
|
+
import { getConfig } from "../../../../core/configStore.js";
|
|
3
|
+
import { buildDataPath } from "../../../../utils/pathBuilder.js";
|
|
4
|
+
import { readData } from "../../../helpers/file/read.js";
|
|
5
|
+
import { getSchema } from "../../../config/getSchema.js";
|
|
6
|
+
import { getPrimaryKey, attachPrimaryKey } from "../../../helpers/pkHelper.js";
|
|
7
|
+
|
|
8
|
+
const validateInput = ({ table, record }) => {
|
|
9
|
+
if (!table) throw new Error("table is required");
|
|
10
|
+
if (!record || typeof record !== "object") throw new Error("record must be object");
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const safeGetSchema = (table) => {
|
|
14
|
+
const schema = getSchema(table);
|
|
15
|
+
if (!schema?.columns) throw new Error(`Invalid schema for "${table}"`);
|
|
16
|
+
return schema;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const getData = (table) => {
|
|
20
|
+
const config = getConfig();
|
|
21
|
+
const path = buildDataPath(config, table);
|
|
22
|
+
return readData(path);
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const insertGenPk = ({ table, record }) => {
|
|
26
|
+
validateInput({ table, record });
|
|
27
|
+
|
|
28
|
+
const schema = safeGetSchema(table);
|
|
29
|
+
const pk = getPrimaryKey(schema.columns);
|
|
30
|
+
|
|
31
|
+
if (record[pk] !== undefined) {
|
|
32
|
+
return insertBase({ table, record });
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const data = getData(table);
|
|
36
|
+
const newRecord = attachPrimaryKey(record, pk, data);
|
|
37
|
+
|
|
38
|
+
return insertBase({ table, record: newRecord });
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export default insertGenPk;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import insertBase from "./insertBase.js";
|
|
2
|
+
import validate from "../../../helpers/validate.js";
|
|
3
|
+
|
|
4
|
+
import { getSchema } from "../../../config/getSchema.js";
|
|
5
|
+
import { getPrimaryKey, attachPrimaryKey } from "../../../helpers/pkHelper.js";
|
|
6
|
+
|
|
7
|
+
import { getConfig } from "../../../../core/configStore.js";
|
|
8
|
+
import { buildDataPath } from "../../../../utils/pathBuilder.js";
|
|
9
|
+
import { readData } from "../../../helpers/file/read.js";
|
|
10
|
+
|
|
11
|
+
const insertWithChecks1 = ({ table, record }) => {
|
|
12
|
+
const schema = getSchema(table);
|
|
13
|
+
|
|
14
|
+
// 🔥 PK handling
|
|
15
|
+
const pk = getPrimaryKey(schema.columns);
|
|
16
|
+
|
|
17
|
+
if (record[pk] === undefined) {
|
|
18
|
+
const config = getConfig();
|
|
19
|
+
const path = buildDataPath(config, table);
|
|
20
|
+
const data = readData(path);
|
|
21
|
+
|
|
22
|
+
record = attachPrimaryKey(record, pk, data);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// 🔥 validate AFTER pk
|
|
26
|
+
validate(schema, record);
|
|
27
|
+
|
|
28
|
+
return insertBase({ table, record });
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const insertWithChecks = ({ table, record }) => {
|
|
32
|
+
try {
|
|
33
|
+
const schema = getSchema(table);
|
|
34
|
+
const pk = getPrimaryKey(schema.columns);
|
|
35
|
+
|
|
36
|
+
if (record[pk] === undefined) {
|
|
37
|
+
const config = getConfig();
|
|
38
|
+
const path = buildDataPath(config, table);
|
|
39
|
+
const data = readData(path);
|
|
40
|
+
record = attachPrimaryKey(record, pk, data);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
validate(schema, record);
|
|
44
|
+
|
|
45
|
+
return insertBase({ table, record });
|
|
46
|
+
|
|
47
|
+
} catch (err) {
|
|
48
|
+
throw new Error(`Insert failed [${table}]: ${err.message}`);
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export default insertWithChecks;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { getConfig } from "../../../../core/configStore.js";
|
|
2
|
+
import { buildDataPath } from "../../../../utils/pathBuilder.js";
|
|
3
|
+
import { readData } from "../../../helpers/file/read.js";
|
|
4
|
+
import { writeData } from "../../../helpers/file/write.js";
|
|
5
|
+
|
|
6
|
+
const updateBase = ({ table, pk, updates }) => {
|
|
7
|
+
const config = getConfig();
|
|
8
|
+
const path = buildDataPath(config, table);
|
|
9
|
+
|
|
10
|
+
const data = readData(path);
|
|
11
|
+
|
|
12
|
+
const index = data.findIndex(r => r.pk === pk);
|
|
13
|
+
if (index === -1) {
|
|
14
|
+
throw new Error(`Record with pk ${pk} not found`);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const existing = data[index];
|
|
18
|
+
const updated = { ...existing, ...updates, pk: existing.pk }; // 🔥 keep pk safe
|
|
19
|
+
|
|
20
|
+
data[index] = updated;
|
|
21
|
+
writeData(path, data);
|
|
22
|
+
|
|
23
|
+
return updated;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export default updateBase;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import updateBase from "./updateBase.js";
|
|
2
|
+
import validate from "../../../helpers/validate.js";
|
|
3
|
+
import { getSchema } from "../../../config/getSchema.js";
|
|
4
|
+
|
|
5
|
+
const updateWithChecks = ({ table, pk, record }) => {
|
|
6
|
+
if (!pk) throw new Error("pk is required");
|
|
7
|
+
|
|
8
|
+
const schema = getSchema(table);
|
|
9
|
+
|
|
10
|
+
// merge for validation
|
|
11
|
+
const merged = { ...record, pk };
|
|
12
|
+
|
|
13
|
+
validate(schema, merged); // 🔥 reuse same validator
|
|
14
|
+
|
|
15
|
+
return updateBase({ table, pk, updates: record });
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export default updateWithChecks;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { getConfig } from "../../../../core/configStore.js";
|
|
2
|
+
import { buildDataPath } from "../../../../utils/pathBuilder.js";
|
|
3
|
+
|
|
4
|
+
import { readData } from "../../../helpers/file/read.js";
|
|
5
|
+
|
|
6
|
+
export default ({ table }) => {
|
|
7
|
+
const cfg = getConfig();
|
|
8
|
+
const path = buildDataPath(cfg, table);
|
|
9
|
+
|
|
10
|
+
const data = readData(path) || [];
|
|
11
|
+
|
|
12
|
+
return Array.isArray(data) ? data.length : 0;
|
|
13
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as count } from "./count.js";
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { getConfig } from "../../../core/configStore.js";
|
|
2
|
+
import { buildDataPath } from "../../../utils/pathBuilder.js";
|
|
3
|
+
import { readData } from "../../helpers/file/read.js";
|
|
4
|
+
|
|
5
|
+
const filterByColumns = ({ table, where = {} }) => {
|
|
6
|
+
const cfg = getConfig();
|
|
7
|
+
const path = buildDataPath(cfg, table);
|
|
8
|
+
|
|
9
|
+
const data = readData(path) || [];
|
|
10
|
+
|
|
11
|
+
return data.filter(row =>
|
|
12
|
+
Object.entries(where).every(([key, val]) =>
|
|
13
|
+
row[key] === val
|
|
14
|
+
)
|
|
15
|
+
);
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export default filterByColumns;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { getConfig } from "../../../core/configStore.js";
|
|
2
|
+
import { buildDataPath } from "../../../utils/pathBuilder.js";
|
|
3
|
+
import { getPrimaryKey } from "../../helpers/pkHelper.js";
|
|
4
|
+
import { getSchema } from "../../config/getSchema.js";
|
|
5
|
+
import { readData } from "../../helpers/file/read.js";
|
|
6
|
+
|
|
7
|
+
const filterByPk = ({ table, id }) => {
|
|
8
|
+
const cfg = getConfig();
|
|
9
|
+
const schema = getSchema(table);
|
|
10
|
+
const pk = getPrimaryKey(schema.columns);
|
|
11
|
+
|
|
12
|
+
const path = buildDataPath(cfg, table);
|
|
13
|
+
const data = readData(path);
|
|
14
|
+
|
|
15
|
+
return data.filter(row => Number(row[pk]) === Number(id)) || [];
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export default filterByPk;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { getConfig } from "../../../core/configStore.js";
|
|
2
|
+
import { buildDataPath } from "../../../utils/pathBuilder.js";
|
|
3
|
+
import { readData } from "../../helpers/file/read.js";
|
|
4
|
+
|
|
5
|
+
const findAll = ({ table }) => {
|
|
6
|
+
const config = getConfig();
|
|
7
|
+
const path = buildDataPath(config, table);
|
|
8
|
+
|
|
9
|
+
const data = readData(path);
|
|
10
|
+
|
|
11
|
+
return data;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export default findAll;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { getConfig } from "../../../core/configStore.js";
|
|
2
|
+
import { buildDataPath } from "../../../utils/pathBuilder.js";
|
|
3
|
+
import { getPrimaryKey } from "../../helpers/pkHelper.js";
|
|
4
|
+
import { getSchema } from "../../config/getSchema.js";
|
|
5
|
+
import { readData } from "../../helpers/file/read.js";
|
|
6
|
+
|
|
7
|
+
const findByPk = ({ table, id }) => {
|
|
8
|
+
const cfg = getConfig();
|
|
9
|
+
const schema = getSchema(table);
|
|
10
|
+
const pk = getPrimaryKey(schema.columns);
|
|
11
|
+
|
|
12
|
+
const path = buildDataPath(cfg, table);
|
|
13
|
+
const data = readData(path);
|
|
14
|
+
|
|
15
|
+
return data.find(row => Number(row[pk]) === Number(id)) || null;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export default findByPk;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import * as aggregate from "./aggregate/index.js";
|
|
2
|
+
|
|
3
|
+
export { default as findAll } from "./findAll.js";
|
|
4
|
+
export { default as findByPk } from "./findByPk.js";
|
|
5
|
+
export { default as filterByPk } from "./filterByPk.js";
|
|
6
|
+
export { default as filterByColumns } from "./filterByColumns.js";
|
|
7
|
+
|
|
8
|
+
export { aggregate };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
const insertAsIs = (mutate, tableName) => (record) =>
|
|
2
|
+
mutate.insertAsIs({ table: tableName, record });
|
|
3
|
+
|
|
4
|
+
const insertFlat = (mutate, tableName) => (record) =>
|
|
5
|
+
mutate.insertFlat({ table: tableName, record });
|
|
6
|
+
|
|
7
|
+
const insertGenPk = (mutate, tableName) => (record) =>
|
|
8
|
+
mutate.insertGenPk({ table: tableName, record });
|
|
9
|
+
|
|
10
|
+
const insertWithChecks = (mutate, tableName) => (record) =>
|
|
11
|
+
mutate.insertWithChecks({ table: tableName, record });
|
|
12
|
+
|
|
13
|
+
const deleteWithChecks = (mutate, tableName) => (pk) =>
|
|
14
|
+
mutate.deleteWithChecks({ table: tableName, pk });
|
|
15
|
+
|
|
16
|
+
const updateWithChecks = (mutate, tableName) => (pk, record) =>
|
|
17
|
+
mutate.updateWithChecks({ table: tableName, pk, record });
|
|
18
|
+
|
|
19
|
+
const buildMutate = (mutate, tableName) => ({
|
|
20
|
+
insertAsIs: insertAsIs(mutate, tableName),
|
|
21
|
+
insertFlat: insertFlat(mutate, tableName),
|
|
22
|
+
insertGenPk: insertGenPk(mutate, tableName),
|
|
23
|
+
insertWithChecks: insertWithChecks(mutate, tableName),
|
|
24
|
+
deleteWithChecks: deleteWithChecks(mutate, tableName),
|
|
25
|
+
updateWithChecks: updateWithChecks(mutate, tableName)
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
export default buildMutate;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import * as aggregate from "../engine/query/aggregate/index.js";
|
|
2
|
+
|
|
3
|
+
const findAll = (query, tableName) => () =>
|
|
4
|
+
query.findAll({ table: tableName });
|
|
5
|
+
|
|
6
|
+
const findByPk = (query, tableName) => (id) =>
|
|
7
|
+
query.findByPk({ table: tableName, id });
|
|
8
|
+
|
|
9
|
+
const filterByPk = (query, tableName) => (id) =>
|
|
10
|
+
query.filterByPk({ table: tableName, id });
|
|
11
|
+
|
|
12
|
+
const filterByColumns = (query, tableName) => (where) =>
|
|
13
|
+
query.filterByColumns({ table: tableName, where });
|
|
14
|
+
|
|
15
|
+
const buildQuery = (query, tableName) => ({
|
|
16
|
+
findAll: findAll(query, tableName),
|
|
17
|
+
findByPk: findByPk(query, tableName),
|
|
18
|
+
filterByPk: filterByPk(query, tableName),
|
|
19
|
+
filterByColumns: filterByColumns(query, tableName),
|
|
20
|
+
|
|
21
|
+
aggregate: {
|
|
22
|
+
count: () =>
|
|
23
|
+
query.aggregate.count({ table: tableName })
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
export default buildQuery;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export const getPrimaryKey = (columns) => {
|
|
2
|
+
const pkColumn = columns.find(c => c.primary);
|
|
3
|
+
if (!pkColumn) throw new Error("Primary key not defined");
|
|
4
|
+
return pkColumn.field;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
export const attachPrimaryKey = (record, pk, data) => {
|
|
8
|
+
let maxId = 0;
|
|
9
|
+
|
|
10
|
+
for (const row of data) {
|
|
11
|
+
const val = Number(row[pk]) || 0;
|
|
12
|
+
if (val > maxId) maxId = val;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return {
|
|
16
|
+
...record,
|
|
17
|
+
[pk]: maxId + 1
|
|
18
|
+
};
|
|
19
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import required from "./validators/required.js";
|
|
2
|
+
import type from "./validators/type.js";
|
|
3
|
+
import minMax from "./validators/minMax.js";
|
|
4
|
+
import length from "./validators/length.js";
|
|
5
|
+
import enumCheck from "./validators/enum.js";
|
|
6
|
+
import pattern from "./validators/pattern.js";
|
|
7
|
+
import setDefault from "./validators/default.js";
|
|
8
|
+
|
|
9
|
+
const validators = [required, type, minMax, length, enumCheck, pattern];
|
|
10
|
+
|
|
11
|
+
const validate = (schema, record) => {
|
|
12
|
+
if (!schema?.columns) throw new Error("Invalid schema");
|
|
13
|
+
|
|
14
|
+
for (const col of schema.columns) {
|
|
15
|
+
if (!col.isConsider) continue;
|
|
16
|
+
|
|
17
|
+
setDefault(col, record); // 🔥 mutate first
|
|
18
|
+
|
|
19
|
+
const value = record[col.field];
|
|
20
|
+
|
|
21
|
+
for (const fn of validators) {
|
|
22
|
+
fn(col, value, record);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export default validate;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export const validateRecord = (record, columns, data) => {
|
|
2
|
+
columns.forEach(col => {
|
|
3
|
+
const value = record[col.field];
|
|
4
|
+
|
|
5
|
+
if (col.required && (value === undefined || value === null || value === "")) {
|
|
6
|
+
throw new Error(`${col.field} is required`);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
if (value === undefined) return;
|
|
10
|
+
|
|
11
|
+
if (col.unique && data.some(r => r[col.field] === value)) {
|
|
12
|
+
throw new Error(`${col.field} must be unique`);
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export const validateFilterKeys = (filter, columns) => {
|
|
18
|
+
const validColumns = columns.map(col => col.field);
|
|
19
|
+
|
|
20
|
+
const invalidKeys = Object.keys(filter).filter(
|
|
21
|
+
key => !validColumns.includes(key)
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
if (invalidKeys.length > 0) {
|
|
25
|
+
throw new Error(`Invalid columns: ${invalidKeys.join(", ")}`);
|
|
26
|
+
};
|
|
27
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export default (col, value) => {
|
|
2
|
+
if (value == null || typeof value !== "string") return;
|
|
3
|
+
|
|
4
|
+
if (col.minLength != null && value.length < col.minLength) {
|
|
5
|
+
throw new Error(`${col.field} min length ${col.minLength}`);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
if (col.maxLength != null && value.length > col.maxLength) {
|
|
9
|
+
throw new Error(`${col.field} max length ${col.maxLength}`);
|
|
10
|
+
}
|
|
11
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export default (col, value) => {
|
|
2
|
+
if (value == null) return;
|
|
3
|
+
|
|
4
|
+
if (col.min != null && value < col.min) {
|
|
5
|
+
throw new Error(`${col.field} must be >= ${col.min}`);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
if (col.max != null && value > col.max) {
|
|
9
|
+
throw new Error(`${col.field} must be <= ${col.max}`);
|
|
10
|
+
}
|
|
11
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export default (col, value) => {
|
|
2
|
+
if (value == null) return;
|
|
3
|
+
|
|
4
|
+
if (col.type === "text" && typeof value !== "string") {
|
|
5
|
+
throw new Error(`${col.field} must be string`);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
if (col.type === "number" && typeof value !== "number") {
|
|
9
|
+
throw new Error(`${col.field} must be number`);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
if (col.type === "boolean" && typeof value !== "boolean") {
|
|
13
|
+
throw new Error(`${col.field} must be boolean`);
|
|
14
|
+
}
|
|
15
|
+
};
|
package/src/v1/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./api/index.js";
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
{
|
|
2
|
+
"query": {
|
|
3
|
+
"findAll": {
|
|
4
|
+
"args": [],
|
|
5
|
+
"desc": "Retrieve all rows from the table",
|
|
6
|
+
"example": [
|
|
7
|
+
"",
|
|
8
|
+
"const tableData = table.query.findAll();",
|
|
9
|
+
"",
|
|
10
|
+
"console.log('Table Data:', tableData);"
|
|
11
|
+
]
|
|
12
|
+
},
|
|
13
|
+
"findByPk": {
|
|
14
|
+
"args": [
|
|
15
|
+
"pk"
|
|
16
|
+
],
|
|
17
|
+
"desc": "Retrieve a single row by primary key",
|
|
18
|
+
"example": [
|
|
19
|
+
"const table = kschema.table('LedgerNames');",
|
|
20
|
+
"const row = table.query.findByPk(1);",
|
|
21
|
+
"console.log('Row by PK:', row);"
|
|
22
|
+
]
|
|
23
|
+
},
|
|
24
|
+
"filterByPk": {
|
|
25
|
+
"args": [
|
|
26
|
+
"pk"
|
|
27
|
+
],
|
|
28
|
+
"desc": "Filter rows matching primary key",
|
|
29
|
+
"example": [
|
|
30
|
+
"const table = kschema.table('LedgerNames');",
|
|
31
|
+
"const rows = table.query.filterByPk(1);",
|
|
32
|
+
"console.log('Filtered Rows (PK):', rows);"
|
|
33
|
+
]
|
|
34
|
+
},
|
|
35
|
+
"filterByColumns": {
|
|
36
|
+
"args": [
|
|
37
|
+
"where"
|
|
38
|
+
],
|
|
39
|
+
"desc": "Filter rows using column conditions",
|
|
40
|
+
"example": [
|
|
41
|
+
"const table = kschema.table('LedgerNames');",
|
|
42
|
+
"const rows = table.query.filterByColumns({ name: 'Cash' });",
|
|
43
|
+
"console.log('Filtered Rows (Columns):', rows);"
|
|
44
|
+
]
|
|
45
|
+
},
|
|
46
|
+
"aggregate": {
|
|
47
|
+
"count": {
|
|
48
|
+
"args": [
|
|
49
|
+
"column?"
|
|
50
|
+
],
|
|
51
|
+
"desc": "Count rows or values of a column",
|
|
52
|
+
"example": [
|
|
53
|
+
"const table = kschema.table('LedgerNames');",
|
|
54
|
+
"const total = table.query.aggregate.count();",
|
|
55
|
+
"console.log('Total Rows Count:', total);"
|
|
56
|
+
]
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
"mutate": {
|
|
61
|
+
"insertAsIs": {
|
|
62
|
+
"args": [
|
|
63
|
+
"record"
|
|
64
|
+
],
|
|
65
|
+
"desc": "Insert record without modification",
|
|
66
|
+
"example": [
|
|
67
|
+
"const insertedRow = table.mutate.insertAsIs({ LedgerName: 'KeshavSoft' });",
|
|
68
|
+
"",
|
|
69
|
+
"console.log('Inserted record as insertAsIs : ', insertedRow);"
|
|
70
|
+
]
|
|
71
|
+
},
|
|
72
|
+
"insertFlat": {
|
|
73
|
+
"args": [
|
|
74
|
+
"record"
|
|
75
|
+
],
|
|
76
|
+
"desc": "Insert flattened record",
|
|
77
|
+
"example": [
|
|
78
|
+
"const table = kschema.table('LedgerNames');",
|
|
79
|
+
"table.mutate.insertFlat({ name: 'Bank' });",
|
|
80
|
+
"console.log('Inserted flat record');"
|
|
81
|
+
]
|
|
82
|
+
},
|
|
83
|
+
"insertGenPk": {
|
|
84
|
+
"args": [
|
|
85
|
+
"record"
|
|
86
|
+
],
|
|
87
|
+
"desc": "Insert record with auto-generated primary key",
|
|
88
|
+
"example": [
|
|
89
|
+
"const insertedRow = table.mutate.insertGenPk({ LedgerName: 'KeshavSoft' });",
|
|
90
|
+
"",
|
|
91
|
+
"console.log('Inserted record as insertGenPk : ', insertedRow);"
|
|
92
|
+
]
|
|
93
|
+
},
|
|
94
|
+
"insertWithChecks": {
|
|
95
|
+
"args": [
|
|
96
|
+
"record"
|
|
97
|
+
],
|
|
98
|
+
"desc": "Insert record with validation checks",
|
|
99
|
+
"example": [
|
|
100
|
+
"try {",
|
|
101
|
+
"\tconst fromInserted = table.mutate.insertWithChecks({ LedgerName: 'Keshav' });",
|
|
102
|
+
"",
|
|
103
|
+
"\tconsole.log('fromInserted : ', fromInserted);",
|
|
104
|
+
"} catch (err) {",
|
|
105
|
+
"\tconsole.log(err.message);",
|
|
106
|
+
"};"
|
|
107
|
+
]
|
|
108
|
+
},
|
|
109
|
+
"deleteWithChecks": {
|
|
110
|
+
"args": [
|
|
111
|
+
"pk"
|
|
112
|
+
],
|
|
113
|
+
"desc": "Delete record by primary key with validation",
|
|
114
|
+
"example": [
|
|
115
|
+
"const table = kschema.table('LedgerNames');",
|
|
116
|
+
"table.mutate.deleteWithChecks(1);",
|
|
117
|
+
"console.log('Deleted record with checks');"
|
|
118
|
+
]
|
|
119
|
+
},
|
|
120
|
+
"updateWithChecks": {
|
|
121
|
+
"args": [
|
|
122
|
+
"pk",
|
|
123
|
+
"record"
|
|
124
|
+
],
|
|
125
|
+
"desc": "Update record by primary key with validation",
|
|
126
|
+
"example": [
|
|
127
|
+
"const table = kschema.table('LedgerNames');",
|
|
128
|
+
"table.mutate.updateWithChecks(1, { name: 'Updated Ledger' });",
|
|
129
|
+
"console.log('Updated record with checks');"
|
|
130
|
+
]
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|