json-fs-query 1.1.0 → 2.1.0
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 +674 -674
- package/README.md +326 -98
- package/browser/jsonFsQuery.js +12819 -10934
- package/browser/jsonFsQuery.min.js +1 -1
- package/dist/data/introspect/create.d.ts +1 -1
- package/dist/data/introspect/create.js +35 -4
- package/dist/data/introspect/create.min.js +1 -1
- package/dist/data/introspect/definitions/field.d.ts +6 -2
- package/dist/data/introspect/definitions/field.js +7 -1
- package/dist/data/introspect/definitions/field.min.js +1 -1
- package/dist/data/introspect/index.d.ts +6 -3
- package/dist/data/introspect/index.js +10 -19
- package/dist/data/introspect/index.min.js +1 -1
- package/dist/data/introspect/policies/foreignKeyExists.js +28 -28
- package/dist/data/introspect/policies/hasDependents.js +53 -53
- package/dist/data/introspect/policies/keyUnique.js +34 -34
- package/dist/data/introspect/policies/typeCheck.d.ts +9 -1
- package/dist/data/introspect/policies/typeCheck.js +20 -2
- package/dist/data/introspect/policies/typeCheck.min.js +1 -1
- package/dist/data/queries/createRecord.d.ts +11 -0
- package/dist/data/queries/createRecord.js +58 -0
- package/dist/data/queries/createRecord.min.js +1 -0
- package/dist/data/queries/deleteEntity.js +100 -100
- package/dist/data/queries/describeRecord.d.ts +9 -0
- package/dist/data/queries/describeRecord.js +29 -0
- package/dist/data/queries/describeRecord.min.js +1 -0
- package/dist/data/queries/dropRecord.d.ts +9 -0
- package/dist/data/queries/dropRecord.js +61 -0
- package/dist/data/queries/dropRecord.min.js +1 -0
- package/dist/data/queries/insertEntity.js +122 -117
- package/dist/data/queries/insertEntity.min.js +1 -1
- package/dist/data/queries/parser.d.ts +1 -1
- package/dist/data/queries/parser.js +7 -15
- package/dist/data/queries/parser.min.js +1 -1
- package/dist/data/queries/query.d.ts +6 -0
- package/dist/data/queries/query.js +15 -6
- package/dist/data/queries/query.min.js +1 -1
- package/dist/data/queries/queryStatus.d.ts +15 -0
- package/dist/data/queries/queryStatus.js +52 -0
- package/dist/data/queries/queryStatus.min.js +1 -0
- package/dist/data/queries/quote.d.ts +11 -0
- package/dist/data/queries/quote.js +17 -0
- package/dist/data/queries/quote.min.js +1 -0
- package/dist/data/queries/showRecords.d.ts +6 -0
- package/dist/data/queries/showRecords.js +21 -0
- package/dist/data/queries/showRecords.min.js +1 -0
- package/dist/data/queries/sortBy.js +40 -27
- package/dist/data/queries/sortBy.min.js +1 -1
- package/dist/data/queries/updateEntity.js +245 -193
- package/dist/data/queries/updateEntity.min.js +1 -1
- package/dist/data/queries/where.d.ts +7 -7
- package/dist/data/queries/where.js +85 -67
- package/dist/data/queries/where.min.js +1 -1
- package/dist/data/utilities/compareAs.d.ts +19 -19
- package/dist/data/utilities/compareAs.js +57 -57
- package/dist/data/utilities/config.d.ts +15 -0
- package/dist/data/utilities/config.js +31 -1
- package/dist/data/utilities/config.min.js +1 -1
- package/dist/data/utilities/databaseLock.d.ts +16 -0
- package/dist/data/utilities/databaseLock.js +63 -0
- package/dist/data/utilities/databaseLock.min.js +1 -0
- package/dist/data/utilities/fieldType.d.ts +15 -15
- package/dist/data/utilities/fieldType.js +22 -22
- package/dist/data/utilities/getConditionIndexedList.js +47 -47
- package/dist/data/utilities/matchIndexedJoins.js +62 -62
- package/dist/data/utilities/mergeJoin.js +47 -47
- package/dist/data/utilities/parsers/findConditions.d.ts +19 -15
- package/dist/data/utilities/parsers/findConditions.js +34 -8
- package/dist/data/utilities/parsers/findConditions.min.js +1 -1
- package/dist/data/utilities/parsers/findInsertValues.js +3 -2
- package/dist/data/utilities/parsers/findInsertValues.min.js +1 -1
- package/dist/data/utilities/parsers/findJoinEntity.js +3 -1
- package/dist/data/utilities/parsers/findJoinEntity.min.js +1 -1
- package/dist/data/utilities/parsers/findSelects.js +6 -3
- package/dist/data/utilities/parsers/findSelects.min.js +1 -1
- package/dist/data/utilities/parsers/findSortClauses.js +5 -2
- package/dist/data/utilities/parsers/findSortClauses.min.js +1 -1
- package/dist/data/utilities/parsers/findUpdateValues.js +3 -2
- package/dist/data/utilities/parsers/findUpdateValues.min.js +1 -1
- package/dist/data/utilities/parsers/maskStrings.d.ts +39 -0
- package/dist/data/utilities/parsers/maskStrings.js +127 -0
- package/dist/data/utilities/parsers/maskStrings.min.js +1 -0
- package/dist/data/utilities/parsers/parseCreate.d.ts +20 -0
- package/dist/data/utilities/parsers/parseCreate.js +332 -0
- package/dist/data/utilities/parsers/parseCreate.min.js +1 -0
- package/dist/data/utilities/parsers/parseLiteralValue.d.ts +18 -1
- package/dist/data/utilities/parsers/parseLiteralValue.js +34 -5
- package/dist/data/utilities/parsers/parseLiteralValue.min.js +1 -1
- package/dist/data/utilities/parsers/parseQuery.d.ts +3 -0
- package/dist/data/utilities/parsers/parseQuery.js +28 -2
- package/dist/data/utilities/parsers/parseQuery.min.js +1 -1
- package/dist/data/utilities/parsers/parseSchemaStatement.d.ts +13 -0
- package/dist/data/utilities/parsers/parseSchemaStatement.js +82 -0
- package/dist/data/utilities/parsers/parseSchemaStatement.min.js +1 -0
- package/dist/data/utilities/parsers/scanQuery.d.ts +52 -0
- package/dist/data/utilities/parsers/scanQuery.js +158 -0
- package/dist/data/utilities/parsers/scanQuery.min.js +1 -0
- package/dist/data/utilities/parsers/splitQueries.d.ts +6 -0
- package/dist/data/utilities/parsers/splitQueries.js +20 -2
- package/dist/data/utilities/parsers/splitQueries.min.js +1 -1
- package/dist/data/utilities/parsers/startsQuery.d.ts +5 -0
- package/dist/data/utilities/parsers/startsQuery.js +10 -0
- package/dist/data/utilities/parsers/startsQuery.min.js +1 -0
- package/dist/data/utilities/recordExists.d.ts +6 -0
- package/dist/data/utilities/recordExists.js +18 -0
- package/dist/data/utilities/recordExists.min.js +1 -0
- package/dist/data/utilities/removeFromIndex.d.ts +14 -14
- package/dist/data/utilities/removeFromIndex.js +45 -45
- package/dist/data/utilities/runQuery.js +135 -112
- package/dist/data/utilities/runQuery.min.js +1 -1
- package/dist/data/utilities/updateIndex.d.ts +17 -17
- package/dist/data/utilities/updateIndex.js +57 -57
- package/dist/data/utilities/useJoinClause.js +56 -56
- package/dist/index.d.ts +2 -0
- package/dist/index.js +14 -0
- package/dist/index.min.js +1 -1
- package/package.json +68 -60
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.parseQuery=void 0;var _siFunciona=_interopRequireDefault(require("si-funciona")),_findCommand=require("./findCommand"),_findInsertValues=require("./findInsertValues"),_findUpdateValues=require("./findUpdateValues"),_findJoinEntity=require("./findJoinEntity"),_findSelects=require("./findSelects"),_findConditions=require("./findConditions"),_findJoinClauses=require("./findJoinClauses"),_findMergeJoins=require("./findMergeJoins"),_findSortClauses=require("./findSortClauses"),_findGroupBy=require("./findGroupBy"),_findLimit=require("./findLimit"),_findOffset=require("./findOffset");function _interopRequireDefault(
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.parseQuery=void 0;var _siFunciona=_interopRequireDefault(require("si-funciona")),_findCommand=require("./findCommand"),_findInsertValues=require("./findInsertValues"),_findUpdateValues=require("./findUpdateValues"),_findJoinEntity=require("./findJoinEntity"),_findSelects=require("./findSelects"),_findConditions=require("./findConditions"),_findJoinClauses=require("./findJoinClauses"),_findMergeJoins=require("./findMergeJoins"),_findSortClauses=require("./findSortClauses"),_findGroupBy=require("./findGroupBy"),_findLimit=require("./findLimit"),_findOffset=require("./findOffset"),_maskStrings=require("./maskStrings"),_parseCreate=require("./parseCreate"),_parseSchemaStatement=require("./parseSchemaStatement"),_startsQuery=require("./startsQuery");function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}const parseQuery=e=>{const i={command:void 0,entity:void 0,joinEntity:void 0,selectClauses:[],insertValues:[],updateValues:{},conditions:[],joinClauses:[],mergeJoins:[],sortClauses:[],groupBy:void 0,limit:void 0,offset:void 0,create:void 0,ifExists:void 0},{curry:n,pipe:r}=_siFunciona.default;if(e=(0,_maskStrings.maskArrays)((0,_maskStrings.maskStrings)(e)),_parseCreate.createMatch.test(e)){const{entity:n,create:r}=(0,_parseCreate.parseCreate)(e);return Object.assign(i,{command:"create",entity:n,create:r})}if(_parseSchemaStatement.schemaStatementMatch.test(e))return Object.assign(i,(0,_parseSchemaStatement.parseSchemaStatement)(e));if(e=r(n(_findCommand.findCommand)(i),n(_findInsertValues.findInsertValues)(i),n(_findUpdateValues.findUpdateValues)(i),n(_findJoinEntity.findJoinEntity)(i),n(_findSelects.findSelects)(i),n(_findConditions.findConditions)(i),n(_findJoinClauses.findJoinClauses)(i),n(_findMergeJoins.findMergeJoins)(i),n(_findSortClauses.findSortClauses)(i),n(_findGroupBy.findGroupBy)(i),n(_findLimit.findLimit)(i),n(_findOffset.findOffset)(i))(e)){if(_startsQuery.startsQueryMatch.test(e))throw new Error(`Missing ; before: ${(0,_maskStrings.unmaskStrings)(e.trim(),"'")}`);throw new Error(`Unrecognized query value: ${(0,_maskStrings.unmaskStrings)(e,"'")}`)}return i};exports.parseQuery=parseQuery;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export type ParsedSchemaStatement = {
|
|
2
|
+
command: 'drop' | 'describe' | 'show';
|
|
3
|
+
entity: string;
|
|
4
|
+
ifExists: boolean;
|
|
5
|
+
};
|
|
6
|
+
export declare const schemaStatementMatch: RegExp;
|
|
7
|
+
/**
|
|
8
|
+
* Parse `drop [if exists] <name>`, `describe <name>` or `show records` (strings and arrays already masked). `show`
|
|
9
|
+
* resolves to the registry, so its entity is `__RECORDS`.
|
|
10
|
+
* @param query
|
|
11
|
+
*/
|
|
12
|
+
export declare const parseSchemaStatement: (query: string) => ParsedSchemaStatement;
|
|
13
|
+
//# sourceMappingURL=parseSchemaStatement.d.ts.map
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.schemaStatementMatch = exports.parseSchemaStatement = void 0;
|
|
7
|
+
var _maskStrings = require("./maskStrings");
|
|
8
|
+
var _startsQuery = require("./startsQuery");
|
|
9
|
+
const schemaStatementMatch = exports.schemaStatementMatch = /^\s*(drop|describe|show)(?![a-z0-9_-])/i;
|
|
10
|
+
const dropMatch = /^\s*drop\s+(if\s+exists\s+)?([a-z0-9_-]+)/i;
|
|
11
|
+
const describeMatch = /^\s*describe\s+([a-z0-9_-]+)/i;
|
|
12
|
+
const showMatch = /^\s*show\s+records(?![a-z0-9_-])/i;
|
|
13
|
+
/**
|
|
14
|
+
* Throw for anything left after a statement: "Missing ; before" when it is another query.
|
|
15
|
+
* @param rest
|
|
16
|
+
*/
|
|
17
|
+
const assertNothingAfter = rest => {
|
|
18
|
+
const after = rest.trim();
|
|
19
|
+
if (!after) {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
const shown = (0, _maskStrings.unmaskStrings)(after, '\'');
|
|
23
|
+
if (_startsQuery.startsQueryMatch.test(after)) {
|
|
24
|
+
throw new Error(`Missing ; before: ${shown}`);
|
|
25
|
+
}
|
|
26
|
+
throw new Error(`Unrecognized query value: ${shown}`);
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Throw for the registry's own name, which the schema statements don't act on.
|
|
30
|
+
* @param entity
|
|
31
|
+
*/
|
|
32
|
+
const assertNotRegistry = entity => {
|
|
33
|
+
if (entity === '__RECORDS') {
|
|
34
|
+
throw new Error('__RECORDS is reserved for the list of records (use: show records)');
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* Parse `drop [if exists] <name>`, `describe <name>` or `show records` (strings and arrays already masked). `show`
|
|
39
|
+
* resolves to the registry, so its entity is `__RECORDS`.
|
|
40
|
+
* @param query
|
|
41
|
+
*/
|
|
42
|
+
const parseSchemaStatement = query => {
|
|
43
|
+
const command = query.match(schemaStatementMatch)[1].toLowerCase();
|
|
44
|
+
if (command === 'drop') {
|
|
45
|
+
const found = query.match(dropMatch);
|
|
46
|
+
// `drop if exists` with no name would otherwise read `if` as the name
|
|
47
|
+
if (!found || /^\s*drop\s+if\s+exists\s*$/i.test(query)) {
|
|
48
|
+
throw new Error('Expected: drop [if exists] <name>');
|
|
49
|
+
}
|
|
50
|
+
assertNotRegistry(found[2]);
|
|
51
|
+
assertNothingAfter(query.slice(found[0].length));
|
|
52
|
+
return {
|
|
53
|
+
command: 'drop',
|
|
54
|
+
entity: found[2],
|
|
55
|
+
ifExists: Boolean(found[1])
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
if (command === 'describe') {
|
|
59
|
+
const found = query.match(describeMatch);
|
|
60
|
+
if (!found) {
|
|
61
|
+
throw new Error('Expected: describe <name>');
|
|
62
|
+
}
|
|
63
|
+
assertNotRegistry(found[1]);
|
|
64
|
+
assertNothingAfter(query.slice(found[0].length));
|
|
65
|
+
return {
|
|
66
|
+
command: 'describe',
|
|
67
|
+
entity: found[1],
|
|
68
|
+
ifExists: false
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
const found = query.match(showMatch);
|
|
72
|
+
if (!found) {
|
|
73
|
+
throw new Error('Expected: show records (the only thing that can be shown so far)');
|
|
74
|
+
}
|
|
75
|
+
assertNothingAfter(query.slice(found[0].length));
|
|
76
|
+
return {
|
|
77
|
+
command: 'show',
|
|
78
|
+
entity: '__RECORDS',
|
|
79
|
+
ifExists: false
|
|
80
|
+
};
|
|
81
|
+
};
|
|
82
|
+
exports.parseSchemaStatement = parseSchemaStatement;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.schemaStatementMatch=exports.parseSchemaStatement=void 0;var _maskStrings=require("./maskStrings"),_startsQuery=require("./startsQuery");const schemaStatementMatch=exports.schemaStatementMatch=/^\s*(drop|describe|show)(?![a-z0-9_-])/i,dropMatch=/^\s*drop\s+(if\s+exists\s+)?([a-z0-9_-]+)/i,describeMatch=/^\s*describe\s+([a-z0-9_-]+)/i,showMatch=/^\s*show\s+records(?![a-z0-9_-])/i,assertNothingAfter=e=>{const t=e.trim();if(!t)return;const s=(0,_maskStrings.unmaskStrings)(t,"'");if(_startsQuery.startsQueryMatch.test(t))throw new Error(`Missing ; before: ${s}`);throw new Error(`Unrecognized query value: ${s}`)},assertNotRegistry=e=>{if("__RECORDS"===e)throw new Error("__RECORDS is reserved for the list of records (use: show records)")},parseSchemaStatement=e=>{const t=e.match(schemaStatementMatch)[1].toLowerCase();if("drop"===t){const t=e.match(dropMatch);if(!t||/^\s*drop\s+if\s+exists\s*$/i.test(e))throw new Error("Expected: drop [if exists] <name>");return assertNotRegistry(t[2]),assertNothingAfter(e.slice(t[0].length)),{command:"drop",entity:t[2],ifExists:Boolean(t[1])}}if("describe"===t){const t=e.match(describeMatch);if(!t)throw new Error("Expected: describe <name>");return assertNotRegistry(t[1]),assertNothingAfter(e.slice(t[0].length)),{command:"describe",entity:t[1],ifExists:!1}}const s=e.match(showMatch);if(!s)throw new Error("Expected: show records (the only thing that can be shown so far)");return assertNothingAfter(e.slice(s[0].length)),{command:"show",entity:"__RECORDS",ifExists:!1}};exports.parseSchemaStatement=parseSchemaStatement;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
export type QuoteChar = '\'' | '"';
|
|
2
|
+
export type BracketChar = '[' | '(';
|
|
3
|
+
export type ScannedString = {
|
|
4
|
+
start: number;
|
|
5
|
+
end: number;
|
|
6
|
+
quote: QuoteChar;
|
|
7
|
+
value: string;
|
|
8
|
+
};
|
|
9
|
+
export type ScannedComment = {
|
|
10
|
+
start: number;
|
|
11
|
+
end: number;
|
|
12
|
+
kind: 'line' | 'block';
|
|
13
|
+
};
|
|
14
|
+
export type ScanState = {
|
|
15
|
+
quote: QuoteChar | undefined;
|
|
16
|
+
escape: boolean;
|
|
17
|
+
blockComment: boolean;
|
|
18
|
+
brackets: BracketChar[];
|
|
19
|
+
strayCloser: {
|
|
20
|
+
char: string;
|
|
21
|
+
index: number;
|
|
22
|
+
} | undefined;
|
|
23
|
+
};
|
|
24
|
+
export type ScannedQuery = {
|
|
25
|
+
strings: ScannedString[];
|
|
26
|
+
comments: ScannedComment[];
|
|
27
|
+
semicolons: number[];
|
|
28
|
+
end: ScanState;
|
|
29
|
+
closed: boolean;
|
|
30
|
+
terminated: boolean;
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Walk a query once, left to right, finding its strings, comments and the `;` between statements.
|
|
34
|
+
*
|
|
35
|
+
* Strings use `'` or `"` and close only with the quote they opened with. Inside a string a backslash escapes the
|
|
36
|
+
* next character: `\n` and `\t` are a newline and a tab, any other `\x` is `x` (so `\'`, `\"` and `\\` are a quote
|
|
37
|
+
* and a backslash). Outside strings a backslash has no meaning. Comments run from `--` to the end of the line, or
|
|
38
|
+
* from `/*` to `*\/`; anything inside a comment is ignored, and comment markers inside a string are just text. A `--`
|
|
39
|
+
* starts a comment only at the start of the text or after whitespace or a `;`, since names can contain `-`
|
|
40
|
+
* (`my--table` is a name). A `;` inside brackets does not end a query.
|
|
41
|
+
*
|
|
42
|
+
* Never throws: whatever is left open is reported in `end`, for a prompt which asks for more input. Use
|
|
43
|
+
* assertClosed to turn it into an error.
|
|
44
|
+
* @param text
|
|
45
|
+
*/
|
|
46
|
+
export declare const scanQuery: (text?: string) => ScannedQuery;
|
|
47
|
+
/**
|
|
48
|
+
* Throw a clear error for anything scanQuery found left open or unmatched: a string, a block comment or a bracket.
|
|
49
|
+
* @param scanned
|
|
50
|
+
*/
|
|
51
|
+
export declare const assertClosed: (scanned: ScannedQuery) => ScannedQuery;
|
|
52
|
+
//# sourceMappingURL=scanQuery.d.ts.map
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.scanQuery = exports.assertClosed = void 0;
|
|
7
|
+
const escapes = {
|
|
8
|
+
n: '\n',
|
|
9
|
+
t: '\t'
|
|
10
|
+
};
|
|
11
|
+
const closers = {
|
|
12
|
+
']': '[',
|
|
13
|
+
')': '('
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Walk a query once, left to right, finding its strings, comments and the `;` between statements.
|
|
17
|
+
*
|
|
18
|
+
* Strings use `'` or `"` and close only with the quote they opened with. Inside a string a backslash escapes the
|
|
19
|
+
* next character: `\n` and `\t` are a newline and a tab, any other `\x` is `x` (so `\'`, `\"` and `\\` are a quote
|
|
20
|
+
* and a backslash). Outside strings a backslash has no meaning. Comments run from `--` to the end of the line, or
|
|
21
|
+
* from `/*` to `*\/`; anything inside a comment is ignored, and comment markers inside a string are just text. A `--`
|
|
22
|
+
* starts a comment only at the start of the text or after whitespace or a `;`, since names can contain `-`
|
|
23
|
+
* (`my--table` is a name). A `;` inside brackets does not end a query.
|
|
24
|
+
*
|
|
25
|
+
* Never throws: whatever is left open is reported in `end`, for a prompt which asks for more input. Use
|
|
26
|
+
* assertClosed to turn it into an error.
|
|
27
|
+
* @param text
|
|
28
|
+
*/
|
|
29
|
+
const scanQuery = (text = '') => {
|
|
30
|
+
const strings = [];
|
|
31
|
+
const comments = [];
|
|
32
|
+
const semicolons = [];
|
|
33
|
+
const brackets = [];
|
|
34
|
+
let strayCloser;
|
|
35
|
+
let openQuote;
|
|
36
|
+
let escape = false;
|
|
37
|
+
let blockComment = false;
|
|
38
|
+
let lastCode = -1;
|
|
39
|
+
let index = 0;
|
|
40
|
+
while (index < text.length) {
|
|
41
|
+
const char = text[index];
|
|
42
|
+
const next = text[index + 1];
|
|
43
|
+
if (char === '\'' || char === '"') {
|
|
44
|
+
const start = index;
|
|
45
|
+
let value = '';
|
|
46
|
+
index++;
|
|
47
|
+
while (index < text.length && text[index] !== char) {
|
|
48
|
+
if (text[index] === '\\') {
|
|
49
|
+
if (index + 1 === text.length) {
|
|
50
|
+
escape = true;
|
|
51
|
+
index++;
|
|
52
|
+
break;
|
|
53
|
+
}
|
|
54
|
+
value += escapes[text[index + 1]] ?? text[index + 1];
|
|
55
|
+
index += 2;
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
value += text[index];
|
|
59
|
+
index++;
|
|
60
|
+
}
|
|
61
|
+
if (index < text.length) {
|
|
62
|
+
index++;
|
|
63
|
+
} else {
|
|
64
|
+
openQuote = char;
|
|
65
|
+
}
|
|
66
|
+
strings.push({
|
|
67
|
+
start,
|
|
68
|
+
end: index,
|
|
69
|
+
quote: char,
|
|
70
|
+
value
|
|
71
|
+
});
|
|
72
|
+
lastCode = start;
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
if (char === '-' && next === '-' && (index === 0 || /[\s;]/.test(text[index - 1]))) {
|
|
76
|
+
const newline = text.indexOf('\n', index + 2);
|
|
77
|
+
const end = newline === -1 ? text.length : newline + 1;
|
|
78
|
+
comments.push({
|
|
79
|
+
start: index,
|
|
80
|
+
end,
|
|
81
|
+
kind: 'line'
|
|
82
|
+
});
|
|
83
|
+
index = end;
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
if (char === '/' && next === '*') {
|
|
87
|
+
const close = text.indexOf('*/', index + 2);
|
|
88
|
+
const end = close === -1 ? text.length : close + 2;
|
|
89
|
+
blockComment = close === -1;
|
|
90
|
+
comments.push({
|
|
91
|
+
start: index,
|
|
92
|
+
end,
|
|
93
|
+
kind: 'block'
|
|
94
|
+
});
|
|
95
|
+
index = end;
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
if (char === '[' || char === '(') {
|
|
99
|
+
brackets.push(char);
|
|
100
|
+
} else if (closers[char]) {
|
|
101
|
+
if (brackets[brackets.length - 1] === closers[char]) {
|
|
102
|
+
brackets.pop();
|
|
103
|
+
} else if (!strayCloser) {
|
|
104
|
+
strayCloser = {
|
|
105
|
+
char,
|
|
106
|
+
index
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
} else if (char === ';' && brackets.length === 0) {
|
|
110
|
+
semicolons.push(index);
|
|
111
|
+
}
|
|
112
|
+
if (!/\s/.test(char)) {
|
|
113
|
+
lastCode = index;
|
|
114
|
+
}
|
|
115
|
+
index++;
|
|
116
|
+
}
|
|
117
|
+
const end = {
|
|
118
|
+
quote: openQuote,
|
|
119
|
+
escape,
|
|
120
|
+
blockComment,
|
|
121
|
+
brackets,
|
|
122
|
+
strayCloser
|
|
123
|
+
};
|
|
124
|
+
const closed = !end.quote && !end.blockComment && brackets.length === 0;
|
|
125
|
+
return {
|
|
126
|
+
strings,
|
|
127
|
+
comments,
|
|
128
|
+
semicolons,
|
|
129
|
+
end,
|
|
130
|
+
closed,
|
|
131
|
+
terminated: closed && semicolons.length > 0 && semicolons[semicolons.length - 1] === lastCode
|
|
132
|
+
};
|
|
133
|
+
};
|
|
134
|
+
/**
|
|
135
|
+
* Throw a clear error for anything scanQuery found left open or unmatched: a string, a block comment or a bracket.
|
|
136
|
+
* @param scanned
|
|
137
|
+
*/
|
|
138
|
+
exports.scanQuery = scanQuery;
|
|
139
|
+
const assertClosed = scanned => {
|
|
140
|
+
const {
|
|
141
|
+
end
|
|
142
|
+
} = scanned;
|
|
143
|
+
if (end.quote) {
|
|
144
|
+
const open = scanned.strings[scanned.strings.length - 1];
|
|
145
|
+
throw new Error(`Unclosed string: the ${end.quote} at position ${open.start} is never closed`);
|
|
146
|
+
}
|
|
147
|
+
if (end.blockComment) {
|
|
148
|
+
throw new Error(`Unclosed comment: the /* at position ${scanned.comments[scanned.comments.length - 1].start} is never closed`);
|
|
149
|
+
}
|
|
150
|
+
if (end.strayCloser) {
|
|
151
|
+
throw new Error(`Unexpected ${end.strayCloser.char} at position ${end.strayCloser.index}: there is nothing to close`);
|
|
152
|
+
}
|
|
153
|
+
if (end.brackets.length > 0) {
|
|
154
|
+
throw new Error(`Unclosed bracket: ${end.brackets.join(' ')} never closed`);
|
|
155
|
+
}
|
|
156
|
+
return scanned;
|
|
157
|
+
};
|
|
158
|
+
exports.assertClosed = assertClosed;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.scanQuery=exports.assertClosed=void 0;const escapes={n:"\n",t:"\t"},closers={"]":"[",")":"("},scanQuery=(e="")=>{const s=[],t=[],n=[],o=[];let r,l,c=!1,i=!1,a=-1,h=0;for(;h<e.length;){const d=e[h],p=e[h+1];if("'"!==d&&'"'!==d)if("-"!==d||"-"!==p||0!==h&&!/[\s;]/.test(e[h-1]))if("/"!==d||"*"!==p)"["===d||"("===d?o.push(d):closers[d]?o[o.length-1]===closers[d]?o.pop():r||(r={char:d,index:h}):";"===d&&0===o.length&&n.push(h),/\s/.test(d)||(a=h),h++;else{const s=e.indexOf("*/",h+2),n=-1===s?e.length:s+2;i=-1===s,t.push({start:h,end:n,kind:"block"}),h=n}else{const s=e.indexOf("\n",h+2),n=-1===s?e.length:s+1;t.push({start:h,end:n,kind:"line"}),h=n}else{const t=h;let n="";for(h++;h<e.length&&e[h]!==d;)if("\\"!==e[h])n+=e[h],h++;else{if(h+1===e.length){c=!0,h++;break}n+=escapes[e[h+1]]??e[h+1],h+=2}h<e.length?h++:l=d,s.push({start:t,end:h,quote:d,value:n}),a=t}}const d={quote:l,escape:c,blockComment:i,brackets:o,strayCloser:r},p=!d.quote&&!d.blockComment&&0===o.length;return{strings:s,comments:t,semicolons:n,end:d,closed:p,terminated:p&&n.length>0&&n[n.length-1]===a}};exports.scanQuery=scanQuery;const assertClosed=e=>{const{end:s}=e;if(s.quote){const t=e.strings[e.strings.length-1];throw new Error(`Unclosed string: the ${s.quote} at position ${t.start} is never closed`)}if(s.blockComment)throw new Error(`Unclosed comment: the /* at position ${e.comments[e.comments.length-1].start} is never closed`);if(s.strayCloser)throw new Error(`Unexpected ${s.strayCloser.char} at position ${s.strayCloser.index}: there is nothing to close`);if(s.brackets.length>0)throw new Error(`Unclosed bracket: ${s.brackets.join(" ")} never closed`);return e};exports.assertClosed=assertClosed;
|
|
@@ -1,2 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Split a string of queries into one query each, on the `;` between them. A `;` inside a string or a comment does
|
|
3
|
+
* not count, comments are removed, and the `;` after the last query is optional.
|
|
4
|
+
* @param queries
|
|
5
|
+
* @throws {Error} For an unclosed string, block comment or bracket.
|
|
6
|
+
*/
|
|
1
7
|
export declare const splitQueries: (queries?: string) => string[];
|
|
2
8
|
//# sourceMappingURL=splitQueries.d.ts.map
|
|
@@ -6,6 +6,24 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
exports.splitQueries = void 0;
|
|
7
7
|
require("core-js/modules/esnext.iterator.constructor.js");
|
|
8
8
|
require("core-js/modules/esnext.iterator.filter.js");
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
require("core-js/modules/esnext.iterator.reduce.js");
|
|
10
|
+
var _scanQuery = require("./scanQuery");
|
|
11
|
+
/**
|
|
12
|
+
* Split a string of queries into one query each, on the `;` between them. A `;` inside a string or a comment does
|
|
13
|
+
* not count, comments are removed, and the `;` after the last query is optional.
|
|
14
|
+
* @param queries
|
|
15
|
+
* @throws {Error} For an unclosed string, block comment or bracket.
|
|
16
|
+
*/
|
|
17
|
+
const splitQueries = (queries = '') => {
|
|
18
|
+
const {
|
|
19
|
+
comments,
|
|
20
|
+
semicolons
|
|
21
|
+
} = (0, _scanQuery.assertClosed)((0, _scanQuery.scanQuery)(queries));
|
|
22
|
+
// Blank each comment out with spaces, so the positions of the ; stay the same and the words either side stay apart
|
|
23
|
+
const uncommented = comments.reduce((text, {
|
|
24
|
+
start,
|
|
25
|
+
end
|
|
26
|
+
}) => `${text.slice(0, start)}${' '.repeat(end - start)}${text.slice(end)}`, queries);
|
|
27
|
+
return [...semicolons, uncommented.length].map((end, index) => uncommented.slice(index === 0 ? 0 : semicolons[index - 1] + 1, end).trim()).filter(query => query.length > 0);
|
|
28
|
+
};
|
|
11
29
|
exports.splitQueries = splitQueries;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.splitQueries=void 0,require("core-js/modules/esnext.iterator.constructor.js"),require("core-js/modules/esnext.iterator.filter.js");const
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.splitQueries=void 0,require("core-js/modules/esnext.iterator.constructor.js"),require("core-js/modules/esnext.iterator.filter.js"),require("core-js/modules/esnext.iterator.reduce.js");var _scanQuery=require("./scanQuery");const splitQueries=(e="")=>{const{comments:r,semicolons:s}=(0,_scanQuery.assertClosed)((0,_scanQuery.scanQuery)(e)),t=r.reduce(((e,{start:r,end:s})=>`${e.slice(0,r)}${" ".repeat(s-r)}${e.slice(s)}`),e);return[...s,t.length].map(((e,r)=>t.slice(0===r?0:s[r-1]+1,e).trim())).filter((e=>e.length>0))};exports.splitQueries=splitQueries;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.startsQueryMatch = void 0;
|
|
7
|
+
/**
|
|
8
|
+
* Matches text that starts with a command word, for the "Missing ; before" error when a query runs on into the next.
|
|
9
|
+
*/
|
|
10
|
+
const startsQueryMatch = exports.startsQueryMatch = /^\s*(create|delete|describe|drop|insert|read|show|update)(?![a-z0-9_-])/i;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.startsQueryMatch=void 0;const startsQueryMatch=exports.startsQueryMatch=/^\s*(create|delete|describe|drop|insert|read|show|update)(?![a-z0-9_-])/i;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.recordExists = void 0;
|
|
7
|
+
var _config = require("./config");
|
|
8
|
+
/**
|
|
9
|
+
* Whether a record (its `<name>.json` file) exists in the database. Node only, like the schema statements that use it.
|
|
10
|
+
* @param entity
|
|
11
|
+
*/
|
|
12
|
+
const recordExists = entity => {
|
|
13
|
+
const {
|
|
14
|
+
existsSync
|
|
15
|
+
} = require('fs');
|
|
16
|
+
return existsSync(`${(0, _config.getSetting)('databasePath', 'database/')}${entity}.json`);
|
|
17
|
+
};
|
|
18
|
+
exports.recordExists = recordExists;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.recordExists=void 0;var _config=require("./config");const recordExists=e=>{const{existsSync:s}=require("fs");return s(`${(0,_config.getSetting)("databasePath","database/")}${e}.json`)};exports.recordExists=recordExists;
|
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import { keyDefinition } from '../introspect/definitions/key';
|
|
2
|
-
import { recordPath } from '../introspect/definitions/record';
|
|
3
|
-
import { indexEntry } from './updateIndex';
|
|
4
|
-
/**
|
|
5
|
-
* Remove a single entry file from the index that backs one of a record's keys, dropping a value
|
|
6
|
-
* entirely once its last file is removed. A key with no lookup, or an index file that doesn't
|
|
7
|
-
* exist yet, is left untouched.
|
|
8
|
-
* @param path
|
|
9
|
-
* @param key
|
|
10
|
-
* @param value
|
|
11
|
-
* @param fileName
|
|
12
|
-
* @param type - The declared type of the indexed value (see keyValueType), so '3' and 3 are one entry.
|
|
13
|
-
*/
|
|
14
|
-
export declare const removeFromIndex: (path?: recordPath, key?: keyDefinition, value?: any, fileName?: string, type?: string) => Promise<indexEntry[] | null>;
|
|
1
|
+
import { keyDefinition } from '../introspect/definitions/key';
|
|
2
|
+
import { recordPath } from '../introspect/definitions/record';
|
|
3
|
+
import { indexEntry } from './updateIndex';
|
|
4
|
+
/**
|
|
5
|
+
* Remove a single entry file from the index that backs one of a record's keys, dropping a value
|
|
6
|
+
* entirely once its last file is removed. A key with no lookup, or an index file that doesn't
|
|
7
|
+
* exist yet, is left untouched.
|
|
8
|
+
* @param path
|
|
9
|
+
* @param key
|
|
10
|
+
* @param value
|
|
11
|
+
* @param fileName
|
|
12
|
+
* @param type - The declared type of the indexed value (see keyValueType), so '3' and 3 are one entry.
|
|
13
|
+
*/
|
|
14
|
+
export declare const removeFromIndex: (path?: recordPath, key?: keyDefinition, value?: any, fileName?: string, type?: string) => Promise<indexEntry[] | null>;
|
|
15
15
|
//# sourceMappingURL=removeFromIndex.d.ts.map
|
|
@@ -1,46 +1,46 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.removeFromIndex = void 0;
|
|
7
|
-
require("core-js/modules/es.json.stringify.js");
|
|
8
|
-
require("core-js/modules/esnext.iterator.constructor.js");
|
|
9
|
-
require("core-js/modules/esnext.iterator.filter.js");
|
|
10
|
-
require("core-js/modules/esnext.iterator.map.js");
|
|
11
|
-
var _config = require("./config");
|
|
12
|
-
var _compareAs = require("./compareAs");
|
|
13
|
-
const {
|
|
14
|
-
readFile,
|
|
15
|
-
writeFile
|
|
16
|
-
} = require('fs/promises');
|
|
17
|
-
/**
|
|
18
|
-
* Remove a single entry file from the index that backs one of a record's keys, dropping a value
|
|
19
|
-
* entirely once its last file is removed. A key with no lookup, or an index file that doesn't
|
|
20
|
-
* exist yet, is left untouched.
|
|
21
|
-
* @param path
|
|
22
|
-
* @param key
|
|
23
|
-
* @param value
|
|
24
|
-
* @param fileName
|
|
25
|
-
* @param type - The declared type of the indexed value (see keyValueType), so '3' and 3 are one entry.
|
|
26
|
-
*/
|
|
27
|
-
const removeFromIndex = async (path = '', key = null, value = undefined, fileName = '', type = undefined) => {
|
|
28
|
-
if (!key || !key.lookup) {
|
|
29
|
-
return null;
|
|
30
|
-
}
|
|
31
|
-
const databasePath = (0, _config.getSetting)('databasePath', 'database/');
|
|
32
|
-
const indexPath = `${databasePath}__indexes/${path}/${key.lookup}`;
|
|
33
|
-
let index = [];
|
|
34
|
-
try {
|
|
35
|
-
index = JSON.parse(await readFile(indexPath, 'utf8'));
|
|
36
|
-
} catch (err) {
|
|
37
|
-
return null;
|
|
38
|
-
}
|
|
39
|
-
index = index.map(entry => (0, _compareAs.sameValue)(type, entry.value, value) ? {
|
|
40
|
-
value: entry.value,
|
|
41
|
-
record: entry.record.filter(record => record !== fileName)
|
|
42
|
-
} : entry).filter(entry => entry.record.length);
|
|
43
|
-
await writeFile(indexPath, JSON.stringify(index, null, 2));
|
|
44
|
-
return index;
|
|
45
|
-
};
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.removeFromIndex = void 0;
|
|
7
|
+
require("core-js/modules/es.json.stringify.js");
|
|
8
|
+
require("core-js/modules/esnext.iterator.constructor.js");
|
|
9
|
+
require("core-js/modules/esnext.iterator.filter.js");
|
|
10
|
+
require("core-js/modules/esnext.iterator.map.js");
|
|
11
|
+
var _config = require("./config");
|
|
12
|
+
var _compareAs = require("./compareAs");
|
|
13
|
+
const {
|
|
14
|
+
readFile,
|
|
15
|
+
writeFile
|
|
16
|
+
} = require('fs/promises');
|
|
17
|
+
/**
|
|
18
|
+
* Remove a single entry file from the index that backs one of a record's keys, dropping a value
|
|
19
|
+
* entirely once its last file is removed. A key with no lookup, or an index file that doesn't
|
|
20
|
+
* exist yet, is left untouched.
|
|
21
|
+
* @param path
|
|
22
|
+
* @param key
|
|
23
|
+
* @param value
|
|
24
|
+
* @param fileName
|
|
25
|
+
* @param type - The declared type of the indexed value (see keyValueType), so '3' and 3 are one entry.
|
|
26
|
+
*/
|
|
27
|
+
const removeFromIndex = async (path = '', key = null, value = undefined, fileName = '', type = undefined) => {
|
|
28
|
+
if (!key || !key.lookup) {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
const databasePath = (0, _config.getSetting)('databasePath', 'database/');
|
|
32
|
+
const indexPath = `${databasePath}__indexes/${path}/${key.lookup}`;
|
|
33
|
+
let index = [];
|
|
34
|
+
try {
|
|
35
|
+
index = JSON.parse(await readFile(indexPath, 'utf8'));
|
|
36
|
+
} catch (err) {
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
index = index.map(entry => (0, _compareAs.sameValue)(type, entry.value, value) ? {
|
|
40
|
+
value: entry.value,
|
|
41
|
+
record: entry.record.filter(record => record !== fileName)
|
|
42
|
+
} : entry).filter(entry => entry.record.length);
|
|
43
|
+
await writeFile(indexPath, JSON.stringify(index, null, 2));
|
|
44
|
+
return index;
|
|
45
|
+
};
|
|
46
46
|
exports.removeFromIndex = removeFromIndex;
|