@pgpm/stamps 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 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
@@ -0,0 +1,6 @@
1
+ EXTENSION = launchql-stamps
2
+ DATA = sql/launchql-stamps--0.4.6.sql
3
+
4
+ PG_CONFIG = pg_config
5
+ PGXS := $(shell $(PG_CONFIG) --pgxs)
6
+ include $(PGXS)
package/README.md ADDED
@@ -0,0 +1,5 @@
1
+ # @pgpm/stamps
2
+
3
+ Timestamp utilities and audit trail functions for PostgreSQL.
4
+
5
+ Provides created_at, updated_at, and other timestamp management functions for tracking data changes and audit trails.
@@ -0,0 +1,54 @@
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
+ await pg.any(`
10
+ CREATE TABLE public.test_stamps (
11
+ id serial PRIMARY KEY,
12
+ name text,
13
+ created_at timestamptz,
14
+ updated_at timestamptz,
15
+ created_by uuid,
16
+ updated_by uuid
17
+ );
18
+
19
+ CREATE TRIGGER set_stamps
20
+ BEFORE INSERT OR UPDATE ON public.test_stamps
21
+ FOR EACH ROW
22
+ EXECUTE FUNCTION stamps.timestamps();
23
+
24
+ CREATE TRIGGER set_peoplestamps
25
+ BEFORE INSERT OR UPDATE ON public.test_stamps
26
+ FOR EACH ROW
27
+ EXECUTE FUNCTION stamps.peoplestamps();
28
+ `);
29
+ });
30
+
31
+ afterAll(async () => {
32
+ try {
33
+ await teardown();
34
+ } catch (e) {
35
+ console.error('Teardown failed:', e);
36
+ }
37
+ });
38
+
39
+ beforeEach(() => pg.beforeEach());
40
+ afterEach(() => pg.afterEach());
41
+
42
+ it('applies timestamps and peoplestamps', async () => {
43
+ await pg.setContext({ 'jwt.claims.user_id': '00000000-0000-0000-0000-000000000001' });
44
+
45
+ const insertRes = await pg.one(
46
+ `INSERT INTO public.test_stamps (name) VALUES ($1) RETURNING *`,
47
+ ['Alice']
48
+ );
49
+
50
+ expect(insertRes.created_at).toBeTruthy();
51
+ expect(insertRes.updated_at).toBeTruthy();
52
+ expect(insertRes.created_by).toBe('00000000-0000-0000-0000-000000000001');
53
+ expect(insertRes.updated_by).toBe('00000000-0000-0000-0000-000000000001');
54
+ });
@@ -0,0 +1,35 @@
1
+ -- Deploy schemas/stamps/procedures/utils to pg
2
+
3
+ -- requires: schemas/stamps/schema
4
+
5
+ BEGIN;
6
+
7
+ CREATE FUNCTION stamps.peoplestamps()
8
+ RETURNS TRIGGER AS $$
9
+ BEGIN
10
+ IF TG_OP = 'INSERT' THEN
11
+ NEW.created_by = jwt_public.current_user_id();
12
+ NEW.updated_by = jwt_public.current_user_id();
13
+ ELSIF TG_OP = 'UPDATE' THEN
14
+ NEW.created_by = OLD.created_by;
15
+ NEW.updated_by = jwt_public.current_user_id();
16
+ END IF;
17
+ RETURN NEW;
18
+ END;
19
+ $$ LANGUAGE 'plpgsql';
20
+
21
+ CREATE FUNCTION stamps.timestamps()
22
+ RETURNS TRIGGER AS $$
23
+ BEGIN
24
+ IF TG_OP = 'INSERT' THEN
25
+ NEW.created_at = NOW();
26
+ NEW.updated_at = NOW();
27
+ ELSIF TG_OP = 'UPDATE' THEN
28
+ NEW.created_at = OLD.created_at;
29
+ NEW.updated_at = NOW();
30
+ END IF;
31
+ RETURN NEW;
32
+ END;
33
+ $$ LANGUAGE 'plpgsql';
34
+
35
+ COMMIT;
@@ -0,0 +1,15 @@
1
+ -- Deploy schemas/stamps/schema to pg
2
+
3
+
4
+ BEGIN;
5
+
6
+ CREATE SCHEMA stamps;
7
+
8
+ GRANT USAGE ON SCHEMA stamps
9
+ TO authenticated, anonymous;
10
+
11
+ ALTER DEFAULT PRIVILEGES IN SCHEMA stamps
12
+ GRANT EXECUTE ON FUNCTIONS
13
+ TO authenticated;
14
+
15
+ 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
+ };
@@ -0,0 +1,8 @@
1
+ # launchql-stamps extension
2
+ comment = 'launchql-stamps extension'
3
+ default_version = '0.4.6'
4
+ module_pathname = '$libdir/launchql-stamps'
5
+ requires = 'plpgsql,launchql-jwt-claims,launchql-verify'
6
+ relocatable = false
7
+ superuser = false
8
+
package/launchql.plan ADDED
@@ -0,0 +1,6 @@
1
+ %syntax-version=1.0.0
2
+ %project=launchql-stamps
3
+ %uri=launchql-stamps
4
+
5
+ schemas/stamps/schema 2020-12-18T04:55:26Z Dan Lynch <dlynch@Dans-MBP-3> # add schemas/stamps/schema
6
+ schemas/stamps/procedures/utils [schemas/stamps/schema] 2020-12-18T04:57:09Z Dan Lynch <dlynch@Dans-MBP-3> # add schemas/stamps/procedures/utils
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@pgpm/stamps",
3
+ "version": "0.4.0",
4
+ "description": "Timestamp utilities and audit trail functions for PostgreSQL",
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/jwt-claims": "0.4.0",
15
+ "@pgpm/verify": "0.4.0"
16
+ },
17
+ "devDependencies": {
18
+ "@launchql/cli": "^4.9.0"
19
+ },
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "https://github.com/launchql/extensions"
23
+ },
24
+ "homepage": "https://github.com/launchql/extensions",
25
+ "bugs": {
26
+ "url": "https://github.com/launchql/extensions/issues"
27
+ },
28
+ "gitHead": "cc9f52a335caa6e21ee7751b04b77c84ce6cb809"
29
+ }
@@ -0,0 +1,8 @@
1
+ -- Revert schemas/stamps/procedures/utils from pg
2
+
3
+ BEGIN;
4
+
5
+ DROP FUNCTION stamps.peoplestamps();
6
+ DROP FUNCTION stamps.timestamps();
7
+
8
+ COMMIT;
@@ -0,0 +1,7 @@
1
+ -- Revert schemas/stamps/schema from pg
2
+
3
+ BEGIN;
4
+
5
+ DROP SCHEMA stamps;
6
+
7
+ COMMIT;
@@ -0,0 +1,33 @@
1
+ \echo Use "CREATE EXTENSION launchql-stamps" to load this file. \quit
2
+ CREATE SCHEMA stamps;
3
+
4
+ GRANT USAGE ON SCHEMA stamps TO authenticated, anonymous;
5
+
6
+ ALTER DEFAULT PRIVILEGES IN SCHEMA stamps
7
+ GRANT EXECUTE ON FUNCTIONS TO authenticated;
8
+
9
+ CREATE FUNCTION stamps.peoplestamps() RETURNS trigger AS $EOFCODE$
10
+ BEGIN
11
+ IF TG_OP = 'INSERT' THEN
12
+ NEW.created_by = jwt_public.current_user_id();
13
+ NEW.updated_by = jwt_public.current_user_id();
14
+ ELSIF TG_OP = 'UPDATE' THEN
15
+ NEW.created_by = OLD.created_by;
16
+ NEW.updated_by = jwt_public.current_user_id();
17
+ END IF;
18
+ RETURN NEW;
19
+ END;
20
+ $EOFCODE$ LANGUAGE plpgsql;
21
+
22
+ CREATE FUNCTION stamps.timestamps() RETURNS trigger AS $EOFCODE$
23
+ BEGIN
24
+ IF TG_OP = 'INSERT' THEN
25
+ NEW.created_at = NOW();
26
+ NEW.updated_at = NOW();
27
+ ELSIF TG_OP = 'UPDATE' THEN
28
+ NEW.created_at = OLD.created_at;
29
+ NEW.updated_at = NOW();
30
+ END IF;
31
+ RETURN NEW;
32
+ END;
33
+ $EOFCODE$ LANGUAGE plpgsql;
@@ -0,0 +1,8 @@
1
+ -- Verify schemas/stamps/procedures/utils on pg
2
+
3
+ BEGIN;
4
+
5
+ SELECT verify_function ('stamps.peoplestamps');
6
+ SELECT verify_function ('stamps.timestamps');
7
+
8
+ ROLLBACK;
@@ -0,0 +1,7 @@
1
+ -- Verify schemas/stamps/schema on pg
2
+
3
+ BEGIN;
4
+
5
+ SELECT verify_schema ('stamps');
6
+
7
+ ROLLBACK;