@pgpm/utils 0.4.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 +22 -0
- package/Makefile +6 -0
- package/README.md +5 -0
- package/__tests__/__snapshots__/utils.test.ts.snap +9 -0
- package/__tests__/utils.test.ts +46 -0
- package/deploy/schemas/utils/procedures/ensure_singleton.sql +29 -0
- package/deploy/schemas/utils/procedures/mask_pad.sql +33 -0
- package/deploy/schemas/utils/procedures/throw.sql +26 -0
- package/deploy/schemas/utils/schema.sql +14 -0
- package/jest.config.js +15 -0
- package/launchql-utils.control +8 -0
- package/launchql.plan +8 -0
- package/package.json +28 -0
- package/revert/schemas/utils/procedures/ensure_singleton.sql +7 -0
- package/revert/schemas/utils/procedures/mask_pad.sql +7 -0
- package/revert/schemas/utils/procedures/throw.sql +7 -0
- package/revert/schemas/utils/schema.sql +7 -0
- package/sql/launchql-utils--0.4.6.sql +64 -0
- package/verify/schemas/utils/procedures/ensure_singleton.sql +7 -0
- package/verify/schemas/utils/procedures/mask_pad.sql +7 -0
- package/verify/schemas/utils/procedures/throw.sql +7 -0
- package/verify/schemas/utils/schema.sql +7 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Dan Lynch <pyramation@gmail.com>
|
|
4
|
+
Copyright (c) 2025 Interweb, Inc.
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|
package/Makefile
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { getConnections, PgTestClient } from 'pgsql-test';
|
|
2
|
+
|
|
3
|
+
let pg: PgTestClient;
|
|
4
|
+
let teardown: () => Promise<void>;
|
|
5
|
+
|
|
6
|
+
beforeAll(async () => {
|
|
7
|
+
({ pg, teardown } = await getConnections());
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
afterAll(async () => {
|
|
11
|
+
await teardown();
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it('more', async () => {
|
|
15
|
+
const { mask_pad } = await pg.one(
|
|
16
|
+
`SELECT utils.mask_pad($1, $2) AS mask_pad`,
|
|
17
|
+
['101', 20]
|
|
18
|
+
);
|
|
19
|
+
expect(mask_pad).toMatchSnapshot();
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it('less', async () => {
|
|
23
|
+
const { mask_pad } = await pg.one(
|
|
24
|
+
`SELECT utils.mask_pad($1, $2) AS mask_pad`,
|
|
25
|
+
['101', 2]
|
|
26
|
+
);
|
|
27
|
+
expect(mask_pad).toMatchSnapshot();
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
describe('bitmask', () => {
|
|
31
|
+
it('more', async () => {
|
|
32
|
+
const { bitmask_pad } = await pg.one(
|
|
33
|
+
`SELECT utils.bitmask_pad($1::varbit, $2) AS bitmask_pad`,
|
|
34
|
+
['101', 20]
|
|
35
|
+
);
|
|
36
|
+
expect(bitmask_pad).toMatchSnapshot();
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('less', async () => {
|
|
40
|
+
const { bitmask_pad } = await pg.one(
|
|
41
|
+
`SELECT utils.bitmask_pad($1::varbit, $2) AS bitmask_pad`,
|
|
42
|
+
['101', 2]
|
|
43
|
+
);
|
|
44
|
+
expect(bitmask_pad).toMatchSnapshot();
|
|
45
|
+
});
|
|
46
|
+
});
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
-- Deploy schemas/utils/procedures/ensure_singleton to pg
|
|
2
|
+
|
|
3
|
+
-- requires: schemas/utils/schema
|
|
4
|
+
|
|
5
|
+
BEGIN;
|
|
6
|
+
|
|
7
|
+
CREATE FUNCTION utils.ensure_singleton()
|
|
8
|
+
RETURNS TRIGGER
|
|
9
|
+
AS $$
|
|
10
|
+
DECLARE
|
|
11
|
+
sql text;
|
|
12
|
+
num int;
|
|
13
|
+
BEGIN
|
|
14
|
+
|
|
15
|
+
sql = format('SELECT count(1) FROM %1I.%2I;', TG_TABLE_SCHEMA, TG_TABLE_NAME);
|
|
16
|
+
|
|
17
|
+
EXECUTE sql
|
|
18
|
+
INTO num;
|
|
19
|
+
|
|
20
|
+
IF (num > 0) THEN
|
|
21
|
+
RAISE EXCEPTION 'SINGLETON_TABLE';
|
|
22
|
+
END IF;
|
|
23
|
+
|
|
24
|
+
RETURN NEW;
|
|
25
|
+
END;
|
|
26
|
+
$$
|
|
27
|
+
LANGUAGE 'plpgsql';
|
|
28
|
+
|
|
29
|
+
COMMIT;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
-- Deploy schemas/utils/procedures/mask_pad to pg
|
|
2
|
+
-- requires: schemas/utils/schema
|
|
3
|
+
BEGIN;
|
|
4
|
+
|
|
5
|
+
CREATE FUNCTION utils.mask_pad (bitstr text, bitlen int, pad text DEFAULT '0')
|
|
6
|
+
RETURNS text
|
|
7
|
+
AS $$
|
|
8
|
+
SELECT
|
|
9
|
+
(
|
|
10
|
+
CASE WHEN length(bitstr) > bitlen THEN
|
|
11
|
+
substring(bitstr FROM (length(bitstr) - (bitlen - 1))
|
|
12
|
+
FOR bitlen)
|
|
13
|
+
ELSE
|
|
14
|
+
lpad(bitstr, bitlen, pad)
|
|
15
|
+
END)
|
|
16
|
+
$$
|
|
17
|
+
LANGUAGE 'sql';
|
|
18
|
+
|
|
19
|
+
CREATE FUNCTION utils.bitmask_pad (bitstr bit varying, bitlen int, pad text DEFAULT '0')
|
|
20
|
+
RETURNS bit varying
|
|
21
|
+
AS $$
|
|
22
|
+
SELECT
|
|
23
|
+
(
|
|
24
|
+
CASE WHEN length(bitstr) > bitlen THEN
|
|
25
|
+
substring(bitstr::text FROM (length(bitstr) - (bitlen - 1))
|
|
26
|
+
FOR bitlen)
|
|
27
|
+
ELSE
|
|
28
|
+
lpad(bitstr::text, bitlen, pad)
|
|
29
|
+
END)::varbit;
|
|
30
|
+
$$
|
|
31
|
+
LANGUAGE 'sql';
|
|
32
|
+
COMMIT;
|
|
33
|
+
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
-- Deploy schemas/utils/procedures/throw to pg
|
|
2
|
+
|
|
3
|
+
-- requires: schemas/utils/schema
|
|
4
|
+
|
|
5
|
+
BEGIN;
|
|
6
|
+
|
|
7
|
+
CREATE FUNCTION utils.throw()
|
|
8
|
+
RETURNS TRIGGER
|
|
9
|
+
AS $$
|
|
10
|
+
BEGIN
|
|
11
|
+
|
|
12
|
+
IF (TG_NARGS = 1) THEN
|
|
13
|
+
RAISE EXCEPTION '% (%)', TG_ARGV[0], TG_TABLE_NAME;
|
|
14
|
+
END IF;
|
|
15
|
+
|
|
16
|
+
IF (TG_NARGS > 1) THEN
|
|
17
|
+
RAISE EXCEPTION '% (%, %)', TG_ARGV[0], TG_TABLE_NAME, TG_ARGV[1];
|
|
18
|
+
END IF;
|
|
19
|
+
|
|
20
|
+
RAISE EXCEPTION 'THROWN_ERROR (%)', TG_TABLE_NAME;
|
|
21
|
+
|
|
22
|
+
END;
|
|
23
|
+
$$
|
|
24
|
+
LANGUAGE 'plpgsql';
|
|
25
|
+
|
|
26
|
+
COMMIT;
|
package/jest.config.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/** @type {import('ts-jest').JestConfigWithTsJest} */
|
|
2
|
+
module.exports = {
|
|
3
|
+
preset: 'ts-jest',
|
|
4
|
+
testEnvironment: 'node',
|
|
5
|
+
|
|
6
|
+
// Match both __tests__ and colocated test files
|
|
7
|
+
testMatch: ['**/?(*.)+(test|spec).{ts,tsx,js,jsx}'],
|
|
8
|
+
|
|
9
|
+
// Ignore build artifacts and type declarations
|
|
10
|
+
testPathIgnorePatterns: ['/dist/', '\\.d\\.ts$'],
|
|
11
|
+
modulePathIgnorePatterns: ['<rootDir>/dist/'],
|
|
12
|
+
watchPathIgnorePatterns: ['/dist/'],
|
|
13
|
+
|
|
14
|
+
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
|
|
15
|
+
};
|
package/launchql.plan
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
%syntax-version=1.0.0
|
|
2
|
+
%project=launchql-utils
|
|
3
|
+
%uri=launchql-utils
|
|
4
|
+
|
|
5
|
+
schemas/utils/schema 2017-08-11T08:11:51Z skitch <skitch@5b0c196eeb62> # add schemas/utils/schema
|
|
6
|
+
schemas/utils/procedures/mask_pad [schemas/utils/schema] 2017-08-11T08:11:51Z skitch <skitch@5b0c196eeb62> # add schemas/utils/procedures/mask_pad
|
|
7
|
+
schemas/utils/procedures/throw [schemas/utils/schema] 2017-08-11T08:11:51Z skitch <skitch@5b0c196eeb62> # add schemas/utils/procedures/throw
|
|
8
|
+
schemas/utils/procedures/ensure_singleton [schemas/utils/schema] 2021-04-25T11:50:15Z Dan Lynch <dlynch@Dans-MBP-3> # add schemas/utils/procedures/ensure_singleton
|
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pgpm/utils",
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"description": "General utility functions for PostgreSQL extensions",
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"access": "public"
|
|
7
|
+
},
|
|
8
|
+
"scripts": {
|
|
9
|
+
"bundle": "lql package",
|
|
10
|
+
"test": "jest",
|
|
11
|
+
"test:watch": "jest --watch"
|
|
12
|
+
},
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"@pgpm/verify": "0.4.0"
|
|
15
|
+
},
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"@launchql/cli": "^4.9.0"
|
|
18
|
+
},
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "https://github.com/launchql/extensions"
|
|
22
|
+
},
|
|
23
|
+
"homepage": "https://github.com/launchql/extensions",
|
|
24
|
+
"bugs": {
|
|
25
|
+
"url": "https://github.com/launchql/extensions/issues"
|
|
26
|
+
},
|
|
27
|
+
"gitHead": "cc9f52a335caa6e21ee7751b04b77c84ce6cb809"
|
|
28
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
\echo Use "CREATE EXTENSION launchql-utils" to load this file. \quit
|
|
2
|
+
CREATE SCHEMA utils;
|
|
3
|
+
|
|
4
|
+
GRANT USAGE ON SCHEMA utils TO PUBLIC;
|
|
5
|
+
|
|
6
|
+
ALTER DEFAULT PRIVILEGES IN SCHEMA utils
|
|
7
|
+
GRANT EXECUTE ON FUNCTIONS TO PUBLIC;
|
|
8
|
+
|
|
9
|
+
CREATE FUNCTION utils.mask_pad(bitstr text, bitlen int, pad text DEFAULT '0') RETURNS text AS $EOFCODE$
|
|
10
|
+
SELECT
|
|
11
|
+
(
|
|
12
|
+
CASE WHEN length(bitstr) > bitlen THEN
|
|
13
|
+
substring(bitstr FROM (length(bitstr) - (bitlen - 1))
|
|
14
|
+
FOR bitlen)
|
|
15
|
+
ELSE
|
|
16
|
+
lpad(bitstr, bitlen, pad)
|
|
17
|
+
END)
|
|
18
|
+
$EOFCODE$ LANGUAGE sql;
|
|
19
|
+
|
|
20
|
+
CREATE FUNCTION utils.bitmask_pad(bitstr pg_catalog.varbit, bitlen int, pad text DEFAULT '0') RETURNS pg_catalog.varbit AS $EOFCODE$
|
|
21
|
+
SELECT
|
|
22
|
+
(
|
|
23
|
+
CASE WHEN length(bitstr) > bitlen THEN
|
|
24
|
+
substring(bitstr::text FROM (length(bitstr) - (bitlen - 1))
|
|
25
|
+
FOR bitlen)
|
|
26
|
+
ELSE
|
|
27
|
+
lpad(bitstr::text, bitlen, pad)
|
|
28
|
+
END)::varbit;
|
|
29
|
+
$EOFCODE$ LANGUAGE sql;
|
|
30
|
+
|
|
31
|
+
CREATE FUNCTION utils.throw() RETURNS trigger AS $EOFCODE$
|
|
32
|
+
BEGIN
|
|
33
|
+
|
|
34
|
+
IF (TG_NARGS = 1) THEN
|
|
35
|
+
RAISE EXCEPTION '% (%)', TG_ARGV[0], TG_TABLE_NAME;
|
|
36
|
+
END IF;
|
|
37
|
+
|
|
38
|
+
IF (TG_NARGS > 1) THEN
|
|
39
|
+
RAISE EXCEPTION '% (%, %)', TG_ARGV[0], TG_TABLE_NAME, TG_ARGV[1];
|
|
40
|
+
END IF;
|
|
41
|
+
|
|
42
|
+
RAISE EXCEPTION 'THROWN_ERROR (%)', TG_TABLE_NAME;
|
|
43
|
+
|
|
44
|
+
END;
|
|
45
|
+
$EOFCODE$ LANGUAGE plpgsql;
|
|
46
|
+
|
|
47
|
+
CREATE FUNCTION utils.ensure_singleton() RETURNS trigger AS $EOFCODE$
|
|
48
|
+
DECLARE
|
|
49
|
+
sql text;
|
|
50
|
+
num int;
|
|
51
|
+
BEGIN
|
|
52
|
+
|
|
53
|
+
sql = format('SELECT count(1) FROM %1I.%2I;', TG_TABLE_SCHEMA, TG_TABLE_NAME);
|
|
54
|
+
|
|
55
|
+
EXECUTE sql
|
|
56
|
+
INTO num;
|
|
57
|
+
|
|
58
|
+
IF (num > 0) THEN
|
|
59
|
+
RAISE EXCEPTION 'SINGLETON_TABLE';
|
|
60
|
+
END IF;
|
|
61
|
+
|
|
62
|
+
RETURN NEW;
|
|
63
|
+
END;
|
|
64
|
+
$EOFCODE$ LANGUAGE plpgsql;
|