UNPKG

@sidekick-coder/db

Version:

Cli Tool to manipulate data from diferent sources

459 lines (447 loc) 12.4 kB
'use strict'; var sift = require('sift'); var valibot = require('valibot'); var path = require('path'); var fs = require('fs'); require('url'); var yaml = require('yaml'); var qs = require('qs'); var inquirer = require('@inquirer/prompts'); var lodashEs = require('lodash-es'); function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; } function _interopNamespace(e) { if (e && e.__esModule) return e; var n = Object.create(null); if (e) { Object.keys(e).forEach(function (k) { if (k !== 'default') { var d = Object.getOwnPropertyDescriptor(e, k); Object.defineProperty(n, k, d.get ? d : { enumerable: true, get: function () { return e[k]; } }); } }); } n.default = e; return Object.freeze(n); } var sift__default = /*#__PURE__*/_interopDefault(sift); var valibot__namespace = /*#__PURE__*/_interopNamespace(valibot); var path__default = /*#__PURE__*/_interopDefault(path); var fs__default = /*#__PURE__*/_interopDefault(fs); var qs__default = /*#__PURE__*/_interopDefault(qs); var inquirer__namespace = /*#__PURE__*/_interopNamespace(inquirer); // src/core/provider/queryArray.ts // src/utils/tryCatch.ts async function tryCatch(tryer) { try { const result = await tryer(); return [result, null]; } catch (error) { return [null, error]; } } tryCatch.sync = function(tryer) { try { const result = tryer(); return [result, null]; } catch (error) { return [null, error]; } }; var parse = yaml.parse; var stringify = yaml.stringify; var YAML = { parse, stringify }; function readFileSync(path4) { const [content, error] = tryCatch.sync(() => fs__default.default.readFileSync(path4)); if (error) { return null; } return new Uint8Array(content); } readFileSync.text = function(filepath, defaultValue = "") { const content = readFileSync(filepath); if (!content) { return defaultValue; } return new TextDecoder().decode(content); }; readFileSync.json = function(path4, options) { const content = readFileSync.text(path4); if (!content) { return (options == null ? void 0 : options.default) || null; } const [json, error] = tryCatch.sync(() => JSON.parse(content, options == null ? void 0 : options.reviver)); return error ? (options == null ? void 0 : options.default) || null : json; }; readFileSync.yaml = function(path4, options) { const content = readFileSync.text(path4); if (!content) { return (options == null ? void 0 : options.default) || null; } const [yml, error] = tryCatch.sync(() => YAML.parse(content, options == null ? void 0 : options.parseOptions)); return error ? (options == null ? void 0 : options.default) || null : yml; }; var filesystem = { readSync: readFileSync}; function createReviver(folder) { return (_, value) => { if (typeof value == "string" && value.startsWith("./")) { return path.resolve(path.dirname(folder), value); } return value; }; } var schema = valibot__namespace.optional( valibot__namespace.pipe( valibot__namespace.any(), valibot__namespace.transform((value) => { if (typeof value == "object") { return value; } if (/\.yml$/.test(value)) { const file = value.replace(/^@/, ""); const folder = path.dirname(file); return filesystem.readSync.yaml(value.replace(/^@/, ""), { reviver: createReviver(folder) }); } if (typeof value == "string" && value.includes("=")) { const result = qs__default.default.parse(value, { allowEmptyArrays: true }); return result; } if (typeof value == "string" && value.startsWith("{")) { return JSON.parse(value); } if (typeof value == "string" && value.startsWith("[")) { return JSON.parse(value); } return value; }), valibot__namespace.record(valibot__namespace.string(), valibot__namespace.any()) ) ); function createPathNode() { return { resolve: (...args) => path__default.default.resolve(...args), join: (...args) => path__default.default.join(...args), dirname: (args) => path__default.default.dirname(args), basename: (args) => path__default.default.basename(args) }; } // src/core/validator/valibot.ts var stringList = valibot__namespace.pipe( valibot__namespace.any(), valibot__namespace.transform((value) => { if (typeof value === "string") { return value.split(","); } if (Array.isArray(value)) { return value; } }), valibot__namespace.array(valibot__namespace.string()) ); function array2(s) { return valibot__namespace.pipe( v2.union([v2.array(s), s]), valibot__namespace.transform((value) => Array.isArray(value) ? value : [value]), valibot__namespace.array(s) ); } function path3(dirname2, path4 = createPathNode()) { return valibot__namespace.pipe( valibot__namespace.string(), valibot__namespace.transform((value) => path4.resolve(dirname2, value)) ); } function uint8() { return valibot__namespace.pipe( valibot__namespace.any(), valibot__namespace.check((value) => value instanceof Uint8Array), valibot__namespace.transform((value) => value) ); } var prompts = { password: (options) => valibot__namespace.optionalAsync(valibot__namespace.string(), () => { return inquirer__namespace.password({ message: "Enter password", ...options }); }) }; var extras = { array: array2, vars: schema, stringList, path: path3, uint8, number: valibot__namespace.pipe( valibot__namespace.any(), valibot__namespace.transform(Number), valibot__namespace.check((n) => !isNaN(n)), valibot__namespace.number() ) }; var vWithExtras = { ...valibot__namespace, extras, prompts }; var v2 = vWithExtras; // src/core/database/where.ts function parseCondition(condition) { if (condition.value === "$true") { return { or: [], and: [ { field: condition.field, operator: condition.operator, value: true } ] }; } if (condition.value === "$false") { return { or: [], and: [ { field: condition.field, operator: condition.operator, value: false } ] }; } if (condition.value === "$exists") { return { or: [], and: [ { field: condition.field, operator: "exists", value: true } ] }; } if (typeof condition.value == "string" && condition.value.startsWith("$in")) { const values = condition.value.slice(4, -1).split(","); return { or: [], and: [ { field: condition.field, operator: "in", value: values } ] }; } return { and: [condition], or: [] }; } function transformWhere(where) { const { and, or, ...rest } = where; const result = { and: [], or: [] }; if ((rest == null ? void 0 : rest.field) && (rest == null ? void 0 : rest.operator)) { return { field: rest.field, operator: rest.operator, value: rest.value }; } for (const [key, value] of Object.entries(rest)) { const { and: and2, or: or2 } = parseCondition({ field: (value == null ? void 0 : value.field) || key, operator: (value == null ? void 0 : value.operator) || "eq", value: (value == null ? void 0 : value.value) || value }); if (and2) { result.and.push(...and2); } if (or2) { result.or.push(...or2); } } if (and == null ? void 0 : and.length) { and.forEach((w) => { result.and.push(transformWhere(w)); }); } if (or == null ? void 0 : or.length) { or.forEach((w) => { result.or.push(transformWhere(w)); }); } if (!result.or.length) { delete result.or; } if (!result.and.length) { delete result.and; } return result; } v2.pipe(v2.any(), v2.transform(transformWhere)); var operatorMap = { eq: "$eq", ne: "$ne", gt: "$gt", gte: "$gte", lt: "$lt", lte: "$lte", in: "$in", exists: "$exists" }; function parseCondition2(condition) { const { field, operator, value } = condition; if (!field || !operator) { console.error("Invalid condition:", condition); return {}; } const siftOperator = operatorMap[operator]; if (!siftOperator) { throw new Error(`Unsupported operator: ${operator}`); } return { [field]: { [siftOperator]: value } }; } function parseGroup(group) { const { and, or, ...rest } = group; const parsed = {}; if ((rest == null ? void 0 : rest.field) && (rest == null ? void 0 : rest.operator)) { return parseCondition2(rest); } if (and == null ? void 0 : and.length) { parsed.$and = and.map(parseGroup); } if (or == null ? void 0 : or.length) { parsed.$or = or.map(parseGroup); } return parsed; } function parseWhere(payload) { var _a, _b; if (!payload) return {}; const transformed = transformWhere(payload); const parsed = parseGroup(transformed); if (!((_a = parsed.$and) == null ? void 0 : _a.length)) { delete parsed.$and; } if (!((_b = parsed.$or) == null ? void 0 : _b.length)) { delete parsed.$or; } return parsed; } function query(data, options) { var _a; const { where, include, exclude } = options; const limit = options.limit; const offset = options.offset || 0; const siftQuery = parseWhere(where); let items = data.filter(sift__default.default(siftQuery)); if (include == null ? void 0 : include.length) { items = items.map((item) => lodashEs.pick(item, options.include)); } if ((exclude == null ? void 0 : exclude.length) && !(include == null ? void 0 : include.length)) { items = items.map((item) => lodashEs.omit(item, exclude)); } if ((_a = options.sortBy) == null ? void 0 : _a.length) { const sort = options.sortBy.map((f, i) => ({ field: f, order: options.sortDesc && options.sortDesc[i] ? "desc" : "asc" })); items = lodashEs.orderBy( items, sort.map((s) => s.field), sort.map((s) => s.order) ); } items = items.slice(offset, limit ? offset + limit : void 0); return items; } function count(data, options) { const items = query(data, { where: options.where }); return items.length; } // src/providers/folder/list.ts async function list(payload) { const { filesystem: filesystem2, root, options, parser } = payload; const resolve3 = (...args) => filesystem2.path.resolve(root, ...args); const where = (options == null ? void 0 : options.where) || {}; const exclude = (options == null ? void 0 : options.exclude) || []; const include = (options == null ? void 0 : options.include) || []; const limit = options == null ? void 0 : options.limit; const page = (options == null ? void 0 : options.page) || 1; const sortBy = options == null ? void 0 : options.sortBy; const sortDesc = options == null ? void 0 : options.sortDesc; let files = filesystem2.readdirSync(root); const excludeDirs = [".db"]; files = files.filter((file) => !excludeDirs.includes(file)); const result = []; for (const id of files) { const filename = resolve3(id, `index.${parser.ext}`); const folder = resolve3(id); if (!filesystem2.existsSync(filename)) { throw new Error(`File "${filename}" not found`); } const raw = filesystem2.readSync.text(resolve3(id, `index.${parser.ext}`)); const item = { id, folder, raw }; Object.assign(item, parser.parse(raw)); result.push(item); } const items = query(result, { where, exclude, include, limit, offset: page > 1 ? (page - 1) * limit : 0, sortBy, sortDesc }); const meta = { total: count(result, { where }), limit, total_pages: limit ? Math.ceil(result.length / limit) : 1 }; return { meta, data: items }; } // src/providers/folder/destroy.ts async function destroy(payload) { const { filesystem: filesystem2, root, parser, options } = payload; const { data: items } = await list({ filesystem: filesystem2, root, parser, options: { where: options.where, limit: options.limit } }); for (const item of items) { filesystem2.removeSync(item.folder); } return { count: items.length }; } exports.destroy = destroy;